Beispiel #1
0
        private void DeclareMember(Method member)
        {
            var name = member.Name;

            CheckName(name, member);
            var modifier = member.Modifier;

            name = RenameModifier(modifier, name);
            var mtd = new ScriptMethod(Class, member.Parameters, member.Statements, Context);

            if (member.IsStatic)
            {
                Class.StaticMembers.Add(name, mtd);
            }
            else
            {
                Class.InstanceMembers.Add(name, mtd);
            }
        }
Beispiel #2
0
        private void DeclareMember(Indexer member)
        {
            CheckIndexer(member, member.IsStatic);
            var paras = member.Parameters;

            if (paras == null || paras.Count == 0)
            {
                throw new RuntimeException(member.LinePragma,
                                           ExceptionResource.ParemetersExpected);
            }
            FunctionHelper.CheckParameters(paras);
            var modifier = member.Modifier;
            var g        = member.Getter;
            var s        = member.Setter;

            CheckModifier(modifier, g, s);
            ClassMethod gmtd = null, smtd = null;
            Modifier    gmod = 0, smod = 0;

            if (g == null && s == null)
            {
                throw new RuntimeException(member.LinePragma,
                                           ExceptionResource.AccessorStatementsExpected);
            }
            else if (g != null && s != null)
            {
                gmod = g.Modifier ?? member.Modifier;
                smod = s.Modifier ?? member.Modifier;
                if (g.Statements == null && s.Statements == null)
                {
                    throw new RuntimeException(member.LinePragma,
                                               ExceptionResource.AccessorStatementsExpected);
                }
                else if (g.Statements != null && s.Statements != null)
                {
                    gmtd = new ScriptMethod(Class,
                                            paras,
                                            g.Statements, Context);
                    Parameter paraValue = paras.FirstOrDefault(p => p.Name == "value");
                    if (paraValue != null)
                    {
                        throw new RuntimeException(paraValue.LinePragma,
                                                   string.Format(ExceptionResource.AutoParameterNameExist, "value"));
                    }
                    var list = paras.ToList();
                    list.Insert(0, new Parameter(s.LinePragma, "value"));
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(list),
                                            s.Statements, Context);
                }
                else
                {
                    throw new RuntimeException((g.Statements == null ? g : s).LinePragma,
                                               ExceptionResource.AccessorStatementsExpected);
                }
            }
            else if (g != null)
            {
                gmod = g.Modifier ?? member.Modifier;
                if (g.Statements == null)
                {
                    throw new RuntimeException(g.LinePragma,
                                               ExceptionResource.AccessorStatementsExpected);
                }
                else
                {
                    gmtd = new ScriptMethod(Class,
                                            paras,
                                            g.Statements, Context);
                }
            }
            else
            {
                smod = s.Modifier ?? member.Modifier;
                if (s.Statements == null)
                {
                    throw new RuntimeException(s.LinePragma,
                                               ExceptionResource.AccessorStatementsExpected);
                }
                else
                {
                    Parameter paraValue = paras.FirstOrDefault(p => p.Name == "value");
                    if (paraValue != null)
                    {
                        throw new RuntimeException(paraValue.LinePragma,
                                                   string.Format(ExceptionResource.AutoParameterNameExist, "value"));
                    }
                    var list = paras.ToList();
                    list.Insert(0, new Parameter(s.LinePragma, "value"));
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(list),
                                            s.Statements, Context);
                }
            }
            if (gmtd != null)
            {
                var idxget = ClassHelper.RenameIndexerGetter;
                idxget = RenameModifier(gmod, idxget);
                if (member.IsStatic)
                {
                    Class.StaticMembers.Add(idxget, new IndexerGetter(gmtd));
                }
                else
                {
                    Class.InstanceMembers.Add(idxget, new IndexerGetter(gmtd));
                }
            }
            if (smtd != null)
            {
                var idxset = ClassHelper.RenameIndexerSetter;
                idxset = RenameModifier(smod, idxset);
                if (member.IsStatic)
                {
                    Class.StaticMembers.Add(idxset, new IndexerSetter(smtd));
                }
                else
                {
                    Class.InstanceMembers.Add(idxset, new IndexerSetter(smtd));
                }
            }
        }
Beispiel #3
0
        private void DeclareMember(Property member)
        {
            var name = member.Name;

            CheckName(name, member);
            var modifier = member.Modifier;
            var g        = member.Getter;
            var s        = member.Setter;

            CheckModifier(modifier, g, s);
            ClassMethod gmtd = null, smtd = null;
            Modifier    gmod = 0, smod = 0;

            if (g == null && s == null)
            {
                throw new RuntimeException(member.LinePragma,
                                           ExceptionResource.AccessorStatementsExpected);
            }
            else if (g != null && s != null)
            {
                gmod = g.Modifier ?? member.Modifier;
                smod = s.Modifier ?? member.Modifier;
                if (g.Statements == null && s.Statements == null)
                {
                    var id = "#autoproperty_" + member.Name;
                    // CheckName(id, member);
                    if (member.IsStatic)
                    {
                        Class.StaticFields.Add(id, ScriptNull.Instance);
                    }
                    else
                    {
                        Class.FieldInitializers.Add(id, null);
                    }
                    gmtd = new ScriptMethod(Class,
                                            new ParameterCollection(),
                                            new StatementCollection(
                                                new ReturnStatement(g.LinePragma,
                                                                    new MemberReferenceExpression(g.LinePragma,
                                                                                                  member.IsStatic ?
                                                                                                  new VariableReferenceExpression(g.LinePragma, Class.Name) as Expression :
                                                                                                  new ThisReferenceExpression(g.LinePragma),
                                                                                                  id))),
                                            Context);
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(new Parameter(s.LinePragma, "value")),
                                            new StatementCollection(
                                                new ExpressionStatement(s.LinePragma,
                                                                        new AssignExpression(s.LinePragma,
                                                                                             new MemberReferenceExpression(s.LinePragma,
                                                                                                                           member.IsStatic ?
                                                                                                                           new VariableReferenceExpression(s.LinePragma, Class.Name) as Expression :
                                                                                                                           new ThisReferenceExpression(s.LinePragma),
                                                                                                                           id),
                                                                                             new VariableReferenceExpression(s.LinePragma, "value")))),
                                            Context);
                }
                else if (g.Statements != null && s.Statements != null)
                {
                    gmtd = new ScriptMethod(Class,
                                            new ParameterCollection(),
                                            g.Statements, Context);
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(new Parameter(s.LinePragma, "value")),
                                            s.Statements, Context);
                }
                else
                {
                    throw new RuntimeException((g.Statements == null ? g : s).LinePragma,
                                               ExceptionResource.AccessorStatementsExpected);
                }
            }
            else if (g != null)
            {
                gmod = g.Modifier ?? member.Modifier;
                if (g.Statements == null)
                {
                    throw new RuntimeException(g.LinePragma,
                                               ExceptionResource.AutoPropertyMustHasGetSet);
                }
                else
                {
                    gmtd = new ScriptMethod(Class,
                                            new ParameterCollection(), g.Statements,
                                            Context);
                }
            }
            else
            {
                smod = s.Modifier ?? member.Modifier;
                if (s.Statements == null)
                {
                    throw new RuntimeException(s.LinePragma,
                                               ExceptionResource.AutoPropertyMustHasGetSet);
                }
                else
                {
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(new Parameter(s.LinePragma, "value")), s.Statements,
                                            Context);
                }
            }
            if (gmtd != null)
            {
                var propget = ClassHelper.RenamePropertyGetter(name);
                propget = RenameModifier(gmod, propget);
                if (member.IsStatic)
                {
                    Class.StaticMembers.Add(propget, new PropertyGetter(gmtd));
                }
                else
                {
                    Class.InstanceMembers.Add(propget, new PropertyGetter(gmtd));
                }
            }
            if (smtd != null)
            {
                var propset = ClassHelper.RenamePropertySetter(name);
                propset = RenameModifier(smod, propset);
                if (member.IsStatic)
                {
                    Class.StaticMembers.Add(propset, new PropertySetter(smtd));
                }
                else
                {
                    Class.InstanceMembers.Add(propset, new PropertySetter(smtd));
                }
            }
        }