コード例 #1
0
        // возвращает ExpressionStatement
        public override object VisitExpressionStatement([NotNull] DoshikParser.ExpressionStatementContext context)
        {
            _compilationContext.SetParsingAntlrContext(context);

            var statement = new ExpressionStatement(_currentNode);

            statement.Expression = ExpressionCreationVisitor.Apply(_compilationContext, statement, context.expression());

            return(statement);
        }
コード例 #2
0
        // Возвращает ExpressionTree
        public override object VisitVariableInitializer([NotNull] DoshikParser.VariableInitializerContext context)
        {
            _compilationContext.SetParsingAntlrContext(context);

            var expressionCtx = context.expression();

            if (expressionCtx != null)
            {
                var expressionTree = ExpressionCreationVisitor.Apply(_compilationContext, _currentExpressionParent, expressionCtx);

                if (_declaringVariable.Type != expressionTree.RootExpression.ReturnOutputSlot.Type)
                {
                    throw _compilationContext.ThrowCompilationError("declaring variable type differs from initialization expression return type");
                }

                return(expressionTree);
            }
            else
            {
                throw _compilationContext.ThrowCompilationError("variables can only be initialized using expressions (no special initializer support yet)");
            }
        }
コード例 #3
0
        public override object VisitWhileLoopStatement([NotNull] DoshikParser.WhileLoopStatementContext context)
        {
            _compilationContext.SetParsingAntlrContext(context);

            var statement = new WhileStatement(_currentNode);

            _currentExpressionParent = statement;

            statement.Condition = ExpressionCreationVisitor.Apply(_compilationContext, _currentExpressionParent, context.condition);

            if (statement.Condition.RootExpression.ReturnOutputSlot.Type != _compilationContext.TypeLibrary.FindByKnownType(KnownType.Boolean))
            {
                throw _compilationContext.ThrowCompilationError("condition must evaluate to bool value");
            }

            _isInLoopCounter++;

            statement.BodyStatement = (Statement)Visit(context.body);

            _isInLoopCounter--;

            return(statement);
        }
コード例 #4
0
        // возвращает Statement (IfStatement)
        public override object VisitIfStatement([NotNull] DoshikParser.IfStatementContext context)
        {
            _compilationContext.SetParsingAntlrContext(context);

            var statement = new IfStatement(_currentNode);

            _currentExpressionParent = statement;

            statement.Condition = ExpressionCreationVisitor.Apply(_compilationContext, _currentExpressionParent, context.condition);

            if (statement.Condition.RootExpression.ReturnOutputSlot.Type != _compilationContext.TypeLibrary.FindByKnownType(KnownType.Boolean))
            {
                throw _compilationContext.ThrowCompilationError("condition must evaluate to bool value");
            }

            statement.TrueStatement = (Statement)Visit(context.trueBody);

            if (context.falseBody != null)
            {
                statement.FalseStatement = (Statement)Visit(context.falseBody);
            }

            return(statement);
        }