예제 #1
0
        private void EvaluateVariableDeclarationStatement(BoundVariableDeclarationStatement node)
        {
            var value = EvaluateExpression(node.Initializer);

            lastValue = value;
            Assign(node.Variable, value);
        }
예제 #2
0
파일: Evaluator.cs 프로젝트: fr3gu/minsk
        private void EvaluateVariableDeclarationStatement(BoundVariableDeclarationStatement node)
        {
            var expression       = node.Initializer;
            var initializerValue = EvaluateExpression(expression);

            _variables[node.Variable] = initializerValue;
            _lastValue = initializerValue;
        }
예제 #3
0
        private void EvaluateVariableDeclarationStatement(BoundVariableDeclarationStatement statement)
        {
            var value = EvaluateExpression(statement.Initializer);

            Assign(statement.Variable, value);

            _lastValue = value;
        }
예제 #4
0
 public override void VisitVariableDeclarationStatement(BoundVariableDeclarationStatement node)
 {
     _writer.WriteKeyword(node.Variable.IsReadOnly ? "val " : "var ");
     _writer.WriteIdentifier(node.Variable.Name);
     _writer.WritePunctuation(": ");
     _writer.WriteKeyword(node.Variable.Type.ToString());
     if (node.Expression != null)
     {
         _writer.WritePunctuation(" = ");
         node.Expression.Accept(this);
     }
     _writer.WriteLine();
 }
예제 #5
0
        private void OutputVariableDeclaration(BoundVariableDeclarationStatement node, string prefix)
        {
            builder.AddFragment(new OutputFragment(prefix, DefaultColour));
            builder.AddFragment(new OutputFragment(node.Variable.Name, VariableColour));
            builder.AddFragment(new OutputFragment(DelimeterString, DefaultColour));
            builder.AddFragment(new OutputFragment(node.Variable.ValueType.ToString(), TypeColour));

            if (node.Initialiser != null)
            {
                builder.AddFragment(new OutputFragment(" = ", StatementColour));
                Output(node.Initialiser, string.Empty);
            }
        }
예제 #6
0
파일: Lowerer.cs 프로젝트: fr3gu/minsk
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //      <body>
            //
            // ------>
            //
            // {
            //      var <var> = <lower>
            //      let upperBound = <upper>
            //      while <lower> <= upperBound
            //      {
            //          <body>
            //          <var> = <var> + 1
            //      }
            // }
            //

            var variableDeclaration = new BoundVariableDeclarationStatement(node.Variable, node.LowerBound);

            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSymbol      = new VariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration = new BoundVariableDeclarationStatement(upperBoundSymbol, node.UpperBound);

            var condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessThanOrEqualToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperBoundSymbol)
                );

            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            var whileBlock     = new BoundBlockStatement(ImmutableArray.Create(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBlock);
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                             variableDeclaration,
                                                             upperBoundDeclaration,
                                                             whileStatement));

            return(RewriteStatement(result));
        }
예제 #7
0
        protected override BoundStatement RewriteForToStatement(BoundForToStatement node)
        {
            // for <var> = <lower> to <upper> <body>
            // --- to --->
            // {
            // var <var> = <lower>
            // let upperBound = <upper>
            // while <var> <= upperBound
            //     <body>
            //     continue:
            //     <var> = <var> + 1
            // }

            var variableDeclaration
                = new BoundVariableDeclarationStatement(node.Variable, node.LowerBound);

            var upperBoundSymbol = new LocalVariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration
                = new BoundVariableDeclarationStatement(upperBoundSymbol, node.UpperBound);
            var upperBoundExpression = new BoundVariableExpression(upperBoundSymbol);

            var condition = new BoundBinaryExpression(
                new BoundVariableExpression(node.Variable),
                BoundBinaryOperator.Bind(Lexing.TokenKind.LessOrEquals, TypeSymbol.Int, TypeSymbol.Int),
                upperBoundExpression);

            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        new BoundVariableExpression(node.Variable),
                        BoundBinaryOperator.Bind(Lexing.TokenKind.Plus, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1))));

            var whileBlock =
                new BoundBlockStatement(
                    node.Body,
                    increment);

            var whileStatement
                = new BoundWhileStatement(condition, whileBlock, node.BreakLabel, node.ContinueLabel);

            var result = new BoundBlockStatement(
                variableDeclaration,
                upperBoundDeclaration,
                whileStatement);

            return(RewriteStatement(result));
        }
예제 #8
0
        private object EvaluateVariableDeclaration(BoundVariableDeclarationStatement statement)
        {
            object exprVal = null;

            if (statement.Initialiser != null)
            {
                if (statement.Initialiser.Type == NodeType.FunctionPointer)
                {
                    exprVal = EvaluateFunctionPointer((BoundFunctionPointer)statement.Initialiser, statement.Variable.Name);
                }
                else
                {
                    exprVal = EvaluateExpression(statement.Initialiser);
                }
            }

            Assign(statement.Variable, exprVal);
            return(exprVal);
        }
예제 #9
0
        protected override BoundStatement RewriteVariableDeclaration(BoundVariableDeclarationStatement statement)
        {
            if (statement.Initialiser == null)
            {
                return(statement);
            }

            BoundExpression initialiser = RewriteExpression(statement.Initialiser);

            if (initialiser.ValueType != statement.Variable.ValueType && TypeConversionSymbol.TryFind(initialiser.ValueType, statement.Variable.ValueType, out var symbol))
            {
                initialiser = new BoundInternalTypeConversion(symbol, initialiser);
            }

            if (initialiser == statement.Initialiser)
            {
                return(statement);
            }

            return(new BoundVariableDeclarationStatement(statement.Variable, initialiser));
        }
예제 #10
0
 private static void WriteBoundVariableDeclarationStatement(this IndentedTextWriter writer, BoundVariableDeclarationStatement node)
 {
     writer.WriteBlueKeyword("var");
     writer.WriteSpace();
     writer.WriteVariable(node.Variable.Name);
     writer.WriteSpace();
     writer.ColorWrite("=");
     writer.WriteSpace();
     writer.WriteBoundNode(node.Expression);
 }
예제 #11
0
 public virtual void VisitVariableDeclarationStatement(BoundVariableDeclarationStatement node) =>
 this.DefaultVisit(node);
예제 #12
0
 protected virtual BoundStatement RewriteVariableDeclaration(BoundVariableDeclarationStatement statement)
 {
     return(statement);
 }
예제 #13
0
    protected override BoundExpression RewriteForExpression(BoundForExpression node)
    {
        /*
         * convert from for to while
         *
         * for (x <- l to u) expr
         *
         * var x = l
         * while(x < u) {
         *     expr
         *     continue:
         *     x = x + 1
         * }
         */

        var lowerBound = RewriteExpression(node.LowerBound);
        var upperBound = RewriteExpression(node.UpperBound);
        var body       = RewriteExpression(node.Body);

        var declareX = new BoundVariableDeclarationStatement(
            node.Syntax,
            node.Variable,
            lowerBound
            );

        var variableExpression = ValueExpression(node.Syntax, node.Variable);
        var condition          = new BoundBinaryExpression(
            node.Syntax,
            variableExpression,
            BoundBinaryOperator.BindOrThrow(SyntaxKind.LessThanToken, Type.Int, Type.Int),
            upperBound
            );
        var continueLabelStatement = new BoundLabelStatement(node.Syntax, node.ContinueLabel);
        var incrementX             = new BoundExpressionStatement(
            node.Syntax,
            new BoundAssignmentExpression(
                node.Syntax,
                variableExpression,
                new BoundBinaryExpression(
                    node.Syntax,
                    variableExpression,
                    BoundBinaryOperator.BindOrThrow(SyntaxKind.PlusToken, Type.Int, Type.Int),
                    new BoundLiteralExpression(node.Syntax, 1)
                    )
                )
            );
        var whileBody = new BoundBlockExpression(
            node.Syntax,
            ImmutableArray.Create <BoundStatement>(
                new BoundExpressionStatement(body.Syntax, body),
                continueLabelStatement,
                incrementX
                ),
            new BoundUnitExpression(node.Syntax)
            );

        var newBlock = new BoundBlockExpression(
            node.Syntax,
            ImmutableArray.Create <BoundStatement>(declareX),
            new BoundWhileExpression(
                node.Syntax,
                condition,
                whileBody,
                node.BreakLabel,
                new BoundLabel("continue")
                )
            );

        return(RewriteExpression(newBlock));
    }