Пример #1
0
        public override void EnterFieldDeclaration([NotNull] NovaParser.FieldDeclarationContext context)
        {
            NovaParser.MemberDeclarationContext parent = (NovaParser.MemberDeclarationContext)context.parent;
            ModifiersEnum modifiers = ParserUtils.ParseModifier(parent.modifier().classModifier().GetText());

            VariableDeclaratorContext declarator = context.variableDeclarator();

            string type = context.typeType().GetText();
            string name = declarator.variableDeclaratorId().GetText();

            Field field = new Field(Class, Class.PopFieldId(), modifiers, new Variable(name, type, context.variableDeclarator()));

            ExpressionNode value = new ExpressionNode(field);

            var initializer = declarator.variableInitializer();

            if (initializer != null)
            {
                ExpressionContext expressionContext = initializer.expression();

                ExpressionListener listener = new ExpressionListener(field); // same here

                foreach (var expression in expressionContext.GetRuleContexts <ParserRuleContext>())
                {
                    expression.EnterRule(listener);
                }

                value = listener.GetResult();
            }

            field.Value = value;

            Class.Fields.Add(field.Name, field);
        }
Пример #2
0
        public override void EnterLocalVariableDeclaration([NotNull] NovaParser.LocalVariableDeclarationContext context)
        {
            VariableDeclaratorContext declarator = context.variableDeclarator();

            string type = context.typeType().GetChild(0).GetText();
            string name = declarator.variableDeclaratorId().GetText();

            DeclarationStatement statement = new DeclarationStatement(Parent, context);

            Variable variable = new Variable(name, type, context.variableDeclarator());

            ExpressionNode value = new ExpressionNode(statement);

            VariableInitializerContext initializer = declarator.variableInitializer();

            if (initializer != null)
            {
                ExpressionContext  expressionContext = initializer.expression();
                ExpressionListener listener          = new ExpressionListener(statement);

                expressionContext.EnterRule(listener);

                value = listener.GetResult();
            }

            statement.Variable = variable;
            statement.Value    = value;

            Result.Add(statement);
        }
Пример #3
0
        public override void EnterFieldAccessor([NotNull] FieldAccessorContext context)
        {
            VariableNameExpression expr = new VariableNameExpression(Result, context);

            expr.Name = context.IDENTIFIER().GetText();

            ExpressionListener expressionListener = new ExpressionListener(expr);

            context.expression().EnterRule(expressionListener);
            expr.ParentAccessor = expressionListener.Result.Get <AccessorExpression>(0);

            this.Result.Insert(expr);
        }
Пример #4
0
        public override void EnterReturnStatement([NotNull] ReturnStatementContext context)
        {
            ReturnStatement returnStatement = new ReturnStatement(Parent, context);

            if (context.expression() != null)
            {
                ExpressionListener listener = new ExpressionListener(returnStatement);

                context.expression().EnterRule(listener);

                returnStatement.Value = listener.GetResult();
            }
            Result.Add(returnStatement);
        }
Пример #5
0
        public override void EnterStatementExpression([NotNull] StatementExpressionContext context)
        {
            ExpressionStatement statement = new ExpressionStatement(Parent, context);

            ExpressionListener listener = new ExpressionListener(statement);

            context.expression().EnterRule(listener);

            ExpressionNode value = listener.GetResult();

            statement.Expression = value;

            Result.Add(statement);
        }
Пример #6
0
        public override void EnterWhileStatement([NotNull] WhileStatementContext context)
        {
            WhileStatement whileStatement = new WhileStatement(Parent, context);

            ExpressionListener expressionListener = new ExpressionListener(whileStatement);

            context.parExpression().expression().EnterRule(expressionListener);
            whileStatement.Condition = expressionListener.GetResult();

            StatementListener statementListener = new StatementListener(whileStatement);

            context.statement().EnterRule(statementListener);
            whileStatement.Statements = statementListener.Result;

            Result.Add(whileStatement);
        }
Пример #7
0
        public override void EnterMethodAccessor([NotNull] MethodAccessorContext context)
        {
            MethodCallExpression expr = new MethodCallExpression(Result, context);

            expr.Name = context.methodCall().IDENTIFIER().GetText();

            ExpressionListener expressionListener = new ExpressionListener(expr);

            context.expression().EnterRule(expressionListener);

            expr.ParentAccessor = expressionListener.Result.Get <AccessorExpression>(0);

            expr.Parameters = GetMethodCallParameters(expr, context, context.methodCall().expressionList());

            this.Result.Insert(expr);
        }
Пример #8
0
        /*
         * Assignation
         * [Type] [Identifier] = [ExpressionNode]
         */
        public override void EnterAssignationStatement([NotNull] AssignationStatementContext context)
        {
            AssignationStatement statement = new AssignationStatement(Parent, context);

            ExpressionListener listener = new ExpressionListener(statement);

            context.right.EnterRule(listener);
            ExpressionNode value = listener.GetResult();

            statement.Value = value;

            listener = new ExpressionListener(statement);
            context.left.EnterRule(listener);

            statement.Target = listener.GetResult().Get <VariableNameExpression>(0);

            statement.Operator = '=';

            Result.Add(statement);
        }
Пример #9
0
        private List <ExpressionNode> GetMethodCallParameters(IChild parent, ParserRuleContext context, ExpressionListContext expressionListContext)
        {
            List <ExpressionNode> results = new List <ExpressionNode>();

            if (expressionListContext != null)
            {
                var parameters = expressionListContext.GetRuleContexts <ParserRuleContext>();

                foreach (var parameter in parameters)
                {
                    ExpressionListener listener = new ExpressionListener(parent);

                    parameter.EnterRule(listener);

                    ExpressionNode result = listener.GetResult();
                    results.Add(result);
                }
            }

            return(results);
        }
Пример #10
0
        public override void EnterIfStatement([NotNull] IfStatementContext context)
        {
            IfStatement statement = new IfStatement(Parent, context);

            ExpressionListener listener = new ExpressionListener(statement);

            context.parExpression().expression().EnterRule(listener);
            statement.IfCondition = listener.GetResult();

            StatementListener statementListener = new StatementListener(statement);

            context.ifSt.EnterRule(statementListener);
            statement.IfStatements = statementListener.GetResult();

            if (context.elseSt != null)
            {
                statementListener = new StatementListener(statement);
                context.elseSt.EnterRule(statementListener);
                statement.ElseStatements = statementListener.GetResult();
            }

            Result.Add(statement);
        }
Пример #11
0
        public override void EnterForStatement([NotNull] ForStatementContext context)
        {
            ForStatement statement = new ForStatement(Parent, context);

            StatementListener statementListener = new StatementListener(statement);

            context.forControl().forInit.EnterRule(statementListener);
            statement.Init = statementListener.GetResult().First();

            statementListener = new StatementListener(statement);
            context.statement().EnterRule(statementListener);
            statement.Statements = statementListener.GetResult();

            ExpressionListener expressionListener = new ExpressionListener(statement);

            context.forControl().forCond.EnterRule(expressionListener);
            statement.Condition = expressionListener.GetResult();

            statementListener = new StatementListener(statement);
            context.forControl().forUpdate.EnterRule(statementListener);
            statement.Update = statementListener.GetResult().First();

            Result.Add(statement);
        }