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); }
/// <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); }