// 执行call操作 private object applyCallOperator(string Identifier, string IdentifierLower, ASTNode_ArgList Args, int LineNumber) { object pNativeFunc = fetchValueOfIdentifier(Identifier, IdentifierLower, LineNumber); if (pNativeFunc is NativeCallHandler == false) { throw new RuntimeException(LineNumber, String.Format("\"{0}\" is not callable.", Identifier)); } NativeCallHandler pHandler = (NativeCallHandler)pNativeFunc; object[] pArgs = null; if (Args.Args.Count > 0) { pArgs = new object[Args.Args.Count]; for (int i = 0; i < Args.Args.Count; ++i) { // 计算参数 pArgs[i] = ExecExpression(Args.Args[i]); } } // 调用函数 return(pHandler(this, pArgs, LineNumber)); }
/// <summary> /// 解析一个ArgList /// </summary> /// <param name="Lex">词法分析器</param> /// <returns>解析结果</returns> private static ASTNode_ArgList ParseArgList(Lexer Lex) { ASTNode_ArgList tRet = new ASTNode_ArgList(); if (Lex.CurrentToken != Lexer.Token.RightBracket) // 非空arglist { while (true) { tRet.Args.Add(ParseExpression(Lex)); if (TryMatchToken(Lex, Lexer.Token.Comma)) // ',' { continue; } else 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(tRet); }
/// <summary> /// 解析原子表达式 /// </summary> /// <param name="Lex">词法分析器</param> /// <returns>解析结果</returns> private static ASTNode_Expression ParseAtomExpression(Lexer Lex) { ASTNode_Expression tRet; if (Lex.CurrentToken == Lexer.Token.DigitLiteral) // digit_literal { tRet = new ASTNode_DigitLiteral(Lex.Line, Lex.CurrentDigit); Lex.Next(); return(tRet); } else if (Lex.CurrentToken == Lexer.Token.Identifier) // symbol or call_expression { string tIdentifier = MatchIdentifier(Lex); // 检查下一个符号 if (TryMatchToken(Lex, Lexer.Token.LeftBracket)) // '(' -- call_expression { ASTNode_ArgList tArgList = ParseArgList(Lex); MatchToken(Lex, Lexer.Token.RightBracket); // ')' return(new ASTNode_CallExpression(Lex.Line, tIdentifier, tArgList)); } else // symbol { return(new ASTNode_SymbolExpression(Lex.Line, tIdentifier)); } } else if (TryMatchToken(Lex, Lexer.Token.LeftBracket)) // '(' -- bracket_expression { tRet = ParseBracketExpression(Lex); MatchToken(Lex, Lexer.Token.RightBracket); // ')' return(tRet); } else { throw new SyntaxException(Lex.Position, Lex.Line, Lex.Row, String.Format("unexpected token {0}.", Lex.FormatCurrentToken())); } }
public ASTNode_CallExpression(int LineNum, string Id, ASTNode_ArgList AL) : base(ASTNode.ASTType.Call, LineNum) { Identifier = Id; ArgList = AL; }