Esempio n. 1
0
 public ForLoopNode(AssignmentNode ass, BinaryExpressionNode be, string id)
 {
     this.Assignment = ass;
     this.Condition  = be;
     this.Statements = new List <Statement>();
     this.Identifier = id;
 }
Esempio n. 2
0
        ASTNode ASSIGN()
        {
            var node = new AssignmentNode();

            node.identifier = IDENT();
            node.lexeme     = Consume(":=", TokenType.SEPARATOR);
            node.value      = EXPR();
            return(node);
        }
Esempio n. 3
0
        public void VisitAssignment(AssignmentNode an)
        {
            SymbolType typeOfIdentifier = CheckThatIdentifierIsDeclaredAndGetEntry(an.Identifier, an.Token).Type;

            UnaryOperandNode expression = an.Expression.Visit(this);

            if (typeOfIdentifier != expression.Type)
            {
                throw new Error($"Illegal Assignment: Can not assign {expression.Type} to variable {an.Identifier}, because it is a {typeOfIdentifier}", expression.Token);
            }
            if (!this.symbolTable.ModifyEntry(an.Identifier, expression.Value))
            {
                throw new Error($"Illegal Assignment: Can not assign to variable {an.Identifier}, because it is used in For Loop", expression.Token);
            }
        }
Esempio n. 4
0
        public void VisitAssignment(AssignmentNode an)
        {
            // TODO: Also has Token
            int    temp    = this.depth;
            string spaces1 = IncreaseDepth();

            this.io.WriteLine($"{spaces1}Assignment: (");
            string spaces2 = IncreaseDepth();

            this.io.WriteLine($"{spaces2}Identifier: {an.Identifier},\n{spaces2}Token: {an.Token},\n{spaces2}Expression: (");
            an.Expression.Visit(this);
            this.io.WriteLine($"{spaces2})");
            this.io.WriteLine($"{spaces1})");
            this.depth = temp;
        }
Esempio n. 5
0
        private void Statement(BlockNode pn)
        {
            Expression expr;
            Token      identifier;

            switch (this.token.SymbolType)
            {
            case SymbolType.Variable:
                // <stmt> ::= "var" <var_ident> ":" <type> [ ":=" <expr> ]
                this.token = this.scanner.NextToken();
                identifier = this.token;
                Match(SymbolType.Identifier);
                Match(SymbolType.Colon);
                SymbolType type = Type();
                pn.AddStatement(new DeclarationNode(identifier.Value, type, identifier));
                if (this.token.SymbolType == SymbolType.Assignment)
                {
                    this.token = this.scanner.NextToken();
                    expr       = Expression();
                    pn.AddStatement(new AssignmentNode(identifier.Value, expr, identifier));
                }
                break;

            case SymbolType.Identifier:
                // <stmt> ::= <var_ident> ":=" <expr>
                identifier = this.token;
                this.token = this.scanner.NextToken();
                Match(SymbolType.Assignment);
                expr = Expression();
                pn.AddStatement(new AssignmentNode(identifier.Value, expr, identifier));
                break;

            case SymbolType.For:
                // <stmt> ::= "for" <var_ident> "in" <expr> ".." <expr> "do" <stmts> "end" "for"
                this.token = this.scanner.NextToken();
                identifier = this.token;
                string forLoopVar = identifier.Value;
                Match(SymbolType.Identifier);
                Match(SymbolType.In);
                expr = Expression();
                AssignmentNode assignment = new AssignmentNode(identifier.Value, expr, identifier);
                Match(SymbolType.Range);
                expr = Expression();
                BinaryExpressionNode condition = new BinaryExpressionNode(
                    new UnaryOperandNode(identifier.Value, SymbolType.Identifier, identifier),
                    "to",
                    new ExpressionOperandNode(expr) // BinaryExpression expects Operands
                    );
                ForLoopNode fln = new ForLoopNode(assignment, condition, forLoopVar);
                Match(SymbolType.Do);
                Statements(fln);
                Match(SymbolType.End);
                Match(SymbolType.For);
                pn.AddStatement(fln);
                break;

            case SymbolType.Read:
                // <stmt> ::= "read" <var_ident>
                this.token = this.scanner.NextToken();
                identifier = this.token;
                Match(SymbolType.Identifier);
                pn.AddStatement(new ReadNode(identifier.Value, "", identifier));
                break;

            case SymbolType.Print:
                // <stmt> ::= "print" <expr>
                this.token = this.scanner.NextToken();
                expr       = Expression();
                pn.AddStatement(new PrintNode(expr));
                break;

            case SymbolType.Assert:
                // <stmt> ::= "assert" "(" <expr> ")"
                this.token = this.scanner.NextToken();
                Match(SymbolType.LeftParenthesis);
                expr = Expression();
                Match(SymbolType.RightParenthesis);
                pn.AddStatement(new AssertNode(expr));
                break;

            default:
                throw new Error($"Statement can not start with {this.token.SymbolType}", this.token);
            }
        }