public ASTNode_BinaryExpression(int LineNum, BinaryOp Opt, ASTNode_Expression LeftNode, ASTNode_Expression RightNode) : base(ASTNode.ASTType.BinaryExpression, LineNum) { BinaryOperator = Opt; Left = LeftNode; Right = RightNode; }
/// <summary> /// 执行一个表达式 /// </summary> /// <param name="Expression">表达式语法树</param> /// <returns>执行结果</returns> public object ExecExpression(ASTNode_Expression Expression) { switch (Expression.Type) { case ASTNode.ASTType.BinaryExpression: { ASTNode_BinaryExpression tBinaryExpression = (ASTNode_BinaryExpression)Expression; object tLeftResult = ExecExpression(tBinaryExpression.Left); object tRightResult = ExecExpression(tBinaryExpression.Right); return(applyBinaryOperator(tBinaryExpression.BinaryOperator, tLeftResult, tRightResult, Expression.LineNumber)); } case ASTNode.ASTType.UnaryExpression: { ASTNode_UnaryExpression tUnaryExpression = (ASTNode_UnaryExpression)Expression; object tRightResult = ExecExpression(tUnaryExpression.Right); return(applyUnaryOperator(tUnaryExpression.UnaryOperator, tRightResult, Expression.LineNumber)); } case ASTNode.ASTType.DigitLiteral: { ASTNode_DigitLiteral tDigitLiteral = (ASTNode_DigitLiteral)Expression; return(tDigitLiteral.Value); } case ASTNode.ASTType.CallExpression: { ASTNode_CallExpression tCallExpression = (ASTNode_CallExpression)Expression; return(applyCallOperator( tCallExpression.Identifier, tCallExpression.IdentifierLower, tCallExpression.ArgList, Expression.LineNumber )); } case ASTNode.ASTType.SymbolExpression: { ASTNode_SymbolExpression tSymbolExpression = (ASTNode_SymbolExpression)Expression; return(fetchValueOfIdentifier( tSymbolExpression.Identifier, tSymbolExpression.IdentifierLower, Expression.LineNumber )); } case ASTNode.ASTType.TupleExpression: { ASTNode_TupleExpression tTupleExpression = (ASTNode_TupleExpression)Expression; return(calcuTupleValue(tTupleExpression, Expression.LineNumber)); } default: throw new RuntimeException(Expression.LineNumber, "internal error."); } }
/// <summary> /// 解析二元表达式 /// /// 当优先级为0时退化到一元表达式 /// </summary> /// <param name="Lex">词法分析器</param> /// <param name="Priority">优先级</param> /// <returns>解析结果</returns> private static ASTNode_Expression ParseBinaryExpression(Lexer Lex, int Priority) { // 退化 if (Priority == 0) { return(ParseUnaryExpression(Lex)); } // 递归解析左侧的表达式 ASTNode_Expression tRet = ParseBinaryExpression(Lex, Priority - 1); // 检查是否为二元运算符 if (Lex.CurrentToken >= Lexer.Token.Plus && (int)Lex.CurrentToken < (int)Lexer.Token.Plus + BinaryOperatorPriorityTable.Length) { int tPriority = BinaryOperatorPriorityTable[Lex.CurrentToken - Lexer.Token.Plus]; if (tPriority > Priority) // 优先级不符,返回 { return(tRet); } else { Priority = tPriority; } } else { return(tRet); } // 循环解析右侧的表达式 while (true) { // 检查下一个算符的优先级 Lexer.Token tOpt = Lex.CurrentToken; if (!(tOpt >= Lexer.Token.Plus && (int)tOpt < (int)Lexer.Token.Plus + BinaryOperatorPriorityTable.Length && BinaryOperatorPriorityTable[Lex.CurrentToken - Lexer.Token.Plus] == Priority)) { break; } // 吃掉运算符 Lex.Next(); // 获取算符右侧 ASTNode_Expression tRight = ParseBinaryExpression(Lex, Priority - 1); // 组合成二元AST树 tRet = new ASTNode_BinaryExpression(Lex.Line, BinaryOp.Plus + (tOpt - Lexer.Token.Plus), tRet, tRight); } return(tRet); }
/// <summary> /// 解析括号表达式 /// /// 括号表达式不可为空,当仅有一个元素时表达为单值,当有多个元素时表达为向量 /// </summary> /// <param name="Lex">词法分析器</param> /// <returns>解析结果</returns> private static ASTNode_Expression ParseBracketExpression(Lexer Lex) { // 先读取第一个表达式 ASTNode_Expression tFirstExpr = ParseExpression(Lex); // 检查后续元素 if (Lex.CurrentToken != Lexer.Token.RightBracket) { ASTNode_TupleExpression tList = new ASTNode_TupleExpression(Lex.Line); tList.Args.Add(tFirstExpr); while (true) { MatchToken(Lex, Lexer.Token.Comma); // ',' tList.Args.Add(ParseExpression(Lex)); if (Lex.CurrentToken == Lexer.Token.Comma) // peek ',' { continue; } if (Lex.CurrentToken == Lexer.Token.RightBracket) // peek ')' { break; } else { throw new SyntaxException(Lex.Position, Lex.Line, Lex.Row, String.Format("unexpected token {0}.", Lex.FormatCurrentToken())); } } return(tList); } else { return(tFirstExpr); } }
public ASTNode_UnaryExpression(int LineNum, UnaryOp Opt, ASTNode_Expression RightNode) : base(ASTNode.ASTType.UnaryExpression, LineNum) { UnaryOperator = Opt; Right = RightNode; }
public ASTNode_Assignment(int LineNum, string Id, ASTNode_Expression Expr) : base(ASTNode.ASTType.Assignment, LineNum) { Identifier = Id; Expression = Expr; }