示例#1
0
 protected dynamic Visit(AST node)
 {
     return(node switch
     {
         Program program => VisitProgram(program),
         Block block => VisitBlock(block),
         VarDecl decl => VisitDecl(decl),
         Compound compound => VisitCompound(compound),
         Assign assign => VisitAssign(assign),
         NoOp noOp => VisitNoOp(noOp),
         Var var => VisitVar(var),
         Num num => VisitNum(num),
         BinOp binOp => VisitBinOp(binOp),
         UnaryOp unaryOp => VisitUnaryOp(unaryOp),
         _ => throw new NotImplementedException()
     });
示例#2
0
        internal AST Expr()
        {
            var result = Term();

            while (currentToken.Type == Operation.Plus || currentToken.Type == Operation.Minus)
            {
                if (currentToken.Type == Operation.Plus)
                {
                    Eat(Operation.Plus);
                    result = new BinOp(result, Operation.Plus, Term());
                }
                else if (currentToken.Type == Operation.Minus)
                {
                    Eat(Operation.Minus);
                    result = new BinOp(result, Operation.Minus, Term());
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            return(result);
        }
示例#3
0
        internal AST Term()
        {
            var result = Factor();

            while (currentToken.Type == Operation.Mul || currentToken.Type == Operation.FloatDiv || currentToken.Type == Operation.IntegerDiv)
            {
                if (currentToken.Type == Operation.Mul)
                {
                    Eat(Operation.Mul);
                    result = new BinOp(result, Operation.Mul, Factor());
                }
                else if (currentToken.Type == Operation.FloatDiv)
                {
                    Eat(Operation.FloatDiv);
                    result = new BinOp(result, Operation.FloatDiv, Factor());
                }
                else if (currentToken.Type == Operation.IntegerDiv)
                {
                    Eat(Operation.IntegerDiv);
                    result = new BinOp(result, Operation.IntegerDiv, Factor());
                }
            }
            return(result);
        }