static void Main(string[] args) { // Abstract Syntax Tree (AST) for 5*10 Exp e = new BinaryExp(new NumericConstant(5), new NumericConstant(10), OPERATOR.MUL); // // Evaluate the Expression // // Console.WriteLine(e.Evaluate(null)); // AST for -(10 + (30 + 50 ) ) e = new UnaryExp( new BinaryExp(new NumericConstant(10), new BinaryExp(new NumericConstant(30), new NumericConstant(50), OPERATOR.PLUS), OPERATOR.PLUS), OPERATOR.MINUS); // // Evaluate the Expression // Console.WriteLine(e.Evaluate(null)); // // Pause for a key stroke // Console.Read(); }
/// <summary> /// /// </summary> public Exp Factor() { TOKEN l_token; Exp RetValue = null; if (Current_Token == TOKEN.TOK_DOUBLE) { RetValue = new NumericConstant(GetNumber()); Current_Token = GetToken(); } else if (Current_Token == TOKEN.TOK_OPAREN) { Current_Token = GetToken(); RetValue = Expr(); // Recurse if (Current_Token != TOKEN.TOK_CPAREN) { Console.WriteLine("Missing Closing Parenthesis\n"); throw new Exception(); } Current_Token = GetToken(); } else if (Current_Token == TOKEN.TOK_PLUS || Current_Token == TOKEN.TOK_SUB) { l_token = Current_Token; Current_Token = GetToken(); RetValue = Factor(); RetValue = new UnaryExp(RetValue, l_token == TOKEN.TOK_PLUS ? OPERATOR.PLUS : OPERATOR.MINUS); } else { Console.WriteLine("Illegal Token"); throw new Exception(); } return(RetValue); }