private SimpleASTNode Multiplicative(TokenReader tokens) { SimpleASTNode child1 = Primary(tokens); SimpleASTNode node = child1; while (true) { Token token = tokens.Peek(); if (token != null && (token.GetType() == TokenType.Star || token.GetType() == TokenType.Slash)) { token = tokens.Read(); SimpleASTNode child2 = Primary(tokens); if (child2 != null) { node = new SimpleASTNode(ASTNodeType.Multiplicative, token.GetText()); node.AddChild(child1); node.AddChild(child2); child1 = node; } else { throw new Exception("invalid multiplicative expression, expecting the right part."); } } else { break; } } return(node); }
public static void TextCalculator() { SimpleCalculator calculator = new SimpleCalculator(); string script = "int a = b+3"; Console.WriteLine("解析变量声明语句:" + script); SimpleLexer lexer = new SimpleLexer(); TokenReader tokens = lexer.Tokenize(script); try { SimpleASTNode node = calculator.InitDeclare(tokens); calculator.DumpAST(node, ""); } catch (Exception ex) { Console.WriteLine(ex.Message); } // script = "2 + 3*5"; //Console.WriteLine("\n计算:" + script + ",看上去一切正常"); script = "2 + 3 + 4"; calculator.Evaluate(script); }
/// <summary> /// 基础表达式 /// </summary> /// <param name="tokens"></param> /// <returns></returns> private SimpleASTNode Primary(TokenReader tokens) { SimpleASTNode node = null; Token token = tokens.Peek(); if (token.GetType() == TokenType.IntLiteral) { token = tokens.Read(); node = new SimpleASTNode(ASTNodeType.IntLiteral, token.GetText()); } else if (token.GetType() == TokenType.Identifier) { token = tokens.Read(); node = new SimpleASTNode(ASTNodeType.Identifier, token.GetText()); } else if (token.GetType() == TokenType.LeftParen) { tokens.Read(); node = Additive2(tokens); if (node != null) { token = tokens.Peek(); if (token != null && token.GetType() == TokenType.RightParen) { tokens.Read(); } } else { throw new Exception("expecting an additive expression inside parenthesis"); } } return(node); }
/// <summary> /// 解析加法表达式 /// </summary> /// <param name="tokens"></param> /// <returns></returns> private SimpleASTNode Additive(TokenReader tokens) { SimpleASTNode child1 = Multiplicative(tokens); SimpleASTNode node = child1; Token token = tokens.Peek(); if (child1 != null && token != null) { if (token.GetType() == TokenType.Plus || token.GetType() == TokenType.Minus) { token = tokens.Read(); SimpleASTNode child2 = Additive(tokens); if (child2 != null) { node = new SimpleASTNode(ASTNodeType.Additive, token.GetText()); node.AddChild(child1); node.AddChild(child2); } else { throw new Exception("invalid additive expression, expecting the right part."); } } } return(node); }
private SimpleASTNode Additive2(TokenReader tokens) { SimpleASTNode child1 = Multiplicative(tokens); SimpleASTNode node = child1; if (child1 != null) { while (true) { Token token = tokens.Peek(); if (token != null && (token.GetType() == TokenType.Plus || token.GetType() == TokenType.Minus)) { token = tokens.Read(); SimpleASTNode child2 = Multiplicative(tokens); node = new SimpleASTNode(ASTNodeType.Additive, token.GetText()); node.AddChild(child1); node.AddChild(child2); child1 = node; } else { break; } } } return(node); }
/// <summary> /// 根节点 /// </summary> /// <param name="tokens"></param> /// <returns></returns> private SimpleASTNode Prog(TokenReader tokens) { SimpleASTNode node = new SimpleASTNode(ASTNodeType.Program, "Caculator"); SimpleASTNode child = Additive2(tokens); if (child != null) { node.AddChild(child); } return(node); }
/// <summary> /// 声明变量 /// int a /// int b = 2 * 3 /// </summary> /// <param name="tokens"></param> /// <returns></returns> private SimpleASTNode IntDeclare(TokenReader tokens) { SimpleASTNode node = null; Token token = tokens.Peek(); if (token != null && token.GetType() == TokenType.Int) { token = tokens.Read(); if (tokens.Peek().GetType() == TokenType.Identifier) { token = tokens.Read(); node = new SimpleASTNode(ASTNodeType.IntDeclaration, token.GetText()); token = tokens.Peek(); if (token != null && token.GetType() == TokenType.Assignment) { tokens.Read(); SimpleASTNode child = Additive(tokens); if (child == null) { throw new Exception("invalid variable iniialization, expecting an expression."); } else { node.AddChild(child); } } } else { throw new Exception("Variable name expected."); } } if (node != null) { token = tokens.Peek(); if (token != null && token.GetType() == TokenType.SemiColon) { tokens.Read(); } else { throw new Exception("invalid statement, expecting semicolon"); } } return(node); }
private SimpleASTNode AssignmentStatement(TokenReader tokens) { SimpleASTNode node = null; Token token = tokens.Peek(); if (token != null && token.GetType() == TokenType.Identifier) { token = tokens.Read(); node = new SimpleASTNode(ASTNodeType.AssignmentStmt, token.GetText()); token = tokens.Peek(); if (token != null && token.GetType() == TokenType.Assignment) { tokens.Read(); SimpleASTNode child = Additive(tokens); if (child == null) { throw new Exception("invalid assignment statement, expecting an expression"); } else { node.AddChild(child); token = tokens.Peek(); if (token != null && token.GetType() == TokenType.SemiColon) { tokens.Read(); } else { throw new Exception("invalid statement, expecting semicolon"); } } } else { tokens.UnRead(); node = null; } } return(node); }
public void AddChild(SimpleASTNode child) { children.Add(child); child.parent = this; }