Ejemplo n.º 1
0
 public ForNode(Var counter, VisitableNode from, VisitableNode to, List <VisitableNode> statements)
 {
     this.counter    = counter;
     this.from       = from;
     this.to         = to;
     this.statements = statements;
 }
Ejemplo n.º 2
0
        public Print Print()
        {
            /*
             * Print: PRINT Expr
             */
            VisitableNode n = Expr();

            return(new Print(n));
        }
Ejemplo n.º 3
0
        public Assert AssertionStatement()
        {
            /*
             * AssertionStatement: ASSERT LPAREN expr RPAREN
             */
            Eat(TokenValues.ASSERT);
            Eat(TokenValues.LPAREN);
            VisitableNode n = Expr();

            Eat(TokenValues.RPAREN);
            return(new Assert(n));
        }
Ejemplo n.º 4
0
        public VisitableNode Statement()
        {
            /* For now only assignment statements and empties
             * Statement : AssignmentStatement
             | VariableDeclaration
             | Print
             | Empty
             | AssertionStatement
             | ReadStatement
             | ForStatement
             */
            VisitableNode result = null;

            if (this.currentToken.GetTokenValueType() == TokenValues.ID)
            {
                result = AssignmentStatement();
            }
            else if (this.currentToken.GetTokenValueType() == TokenValues.VAR)
            {
                Eat(TokenValues.VAR);
                result = VariableDeclaration();
            }
            else if (this.currentToken.GetTokenValueType() == TokenValues.PRINT)
            {
                Eat(TokenValues.PRINT);
                result = Print();
            }
            else if (this.currentToken.GetTokenValueType() == TokenValues.ASSERT)
            {
                result = AssertionStatement();
            }
            else if (this.currentToken.GetTokenValueType() == TokenValues.READ)
            {
                result = ReadStatement();
            }
            else if (this.currentToken.GetTokenValueType() == TokenValues.FOR)
            {
                result = ForStatement();
            }
            else
            {
                result = Empty();
            }

            return(result);
        }
Ejemplo n.º 5
0
        public dynamic Expr()
        {
            VisitableNode result = Term();

            HashSet <TokenValues> values = new HashSet <TokenValues>();

            values.Add(TokenValues.PLUS);
            values.Add(TokenValues.MINUS);
            values.Add(TokenValues.EQUAL);
            values.Add(TokenValues.LESSTHAN);

            /*
             *  expr    : Term ((PLUS|MINUS|EQUAL|LESSTHAN) Term) *
             *  Term    : Factor ((MUL|DIV) Factor) *
             *  Factor  : INTEGER | LPAREN expr RPAREN | Variable | STRING | TRUE | FALSE
             */
            while (values.Contains(this.currentToken.GetTokenValueType()))
            {
                TokenValues type = this.currentToken.GetTokenValueType();
                Token       curr = this.currentToken;
                switch (type)
                {
                case TokenValues.MINUS:
                    Eat(TokenValues.MINUS);
                    break;

                case TokenValues.PLUS:
                    Eat(TokenValues.PLUS);
                    break;

                case TokenValues.EQUAL:
                    Eat(TokenValues.EQUAL);
                    break;

                case TokenValues.LESSTHAN:
                    Eat(TokenValues.LESSTHAN);
                    break;

                default:
                    throw new InterpreterException("syntax error");
                }
                result = new BinOp(result, curr, this.Term());
            }
            return(result);
        }
Ejemplo n.º 6
0
        public ForNode ForStatement()
        {
            /*
             * ForStatement: FOR Var IN Expr DDOT Expr DO Statements END FOR
             */
            Eat(TokenValues.FOR);
            Var v = Variable();

            Eat(TokenValues.IN);
            VisitableNode from = Expr();

            Eat(TokenValues.DDOT);
            VisitableNode to = Expr();

            Eat(TokenValues.DO);
            List <VisitableNode> s = Statements();

            Eat(TokenValues.END);
            Eat(TokenValues.FOR);
            return(new ForNode(v, from, to, s));
        }
Ejemplo n.º 7
0
        public List <VisitableNode> Statements()
        {
            /* Statements : Statement SEMI
             *             | Statement SEMI Statements
             */
            VisitableNode node = Statement();

            List <VisitableNode> results = new List <VisitableNode>();

            results.Add(node);

            while (this.currentToken.GetTokenValueType() == TokenValues.SEMI)
            {
                Eat(TokenValues.SEMI);
                results.Add(Statement());
            }
            if (this.currentToken.GetTokenValueType() == TokenValues.ID)
            {
                throw new InterpreterException("Syntax error");
            }
            return(results);
        }
Ejemplo n.º 8
0
 public void addChild(VisitableNode c)
 {
     this.children.Add(c);
 }