public ExprAST ParseBinOpsRHS(int ExprPrec, ExprAST LHS) { while (true) { int opPrec = GetOpPrecedence(); if (opPrec < ExprPrec) { return(LHS); } char BinOp = lexer.CurrentToken.value[0]; lexer.NextToken(); var RHS = ParsePrimary(); if (RHS == null) { return(null); } int NextPrec = GetOpPrecedence(); if (opPrec < NextPrec) { RHS = ParseBinOpsRHS(opPrec + 1, RHS); if (RHS == null) { return(null); } } LHS = new BinaryOpExprAST(BinOp, LHS, RHS); } }
AST ParseReturn() { Token retTok = tokens.current; ExprAST expAST = ParseExpr(); return(new RetCmdAST(retTok, expAST)); }
public IfExpAST(ExprAST condition, ExprAST then, ExprAST @else) { this.Condition = condition; this.Then = then; this.Else = @else; this.NodeType = ExprType.IfExpr; }
public IfExpAST(ExprAST condition, ExprAST then, ExprAST @else) { this.Condition = condition; this.Then = then; this.Else = @else; this.NodeType = ExprType.IfExpr; }
ExprAST ParseExpr(int precedenceOffset = 0) // scan the whole line and parses the expression, curToken is just before the expression { Token firsttoken = tokens.Next(); List <AST> bodyASTs = new List <AST>(); List <Token> bodyToksList = new List <Token>(); while (!tokens.IsType(TokenType.EndLine) && !tokens.IsType(TokenType.EndFile)) { bodyToksList.Add(tokens.current); tokens.Next(); } Token[] bodyToks = bodyToksList.ToArray(); int[] order = new int[bodyToks.Length]; order = ParseExprScanToks(bodyToks, order, precedenceOffset); int[] astIdx = new int[bodyToks.Length]; Array.Fill(astIdx, -1); int idx = IndexOfHighestPrecedence(bodyToks, order); if (idx < 0) // only a single item { if (bodyToks[0].tokenType == TokenType.IntNum || bodyToks[0].tokenType == TokenType.FloatNum || bodyToks[0].tokenType == TokenType.DoubleNum) { bodyASTs.Add(new NumAST(bodyToks[0], bodyToks[0].value, bodyToks[0].tokenType)); } else if (bodyToks[0].tokenType == TokenType.Identifier) { bodyASTs.Add(new IdentifierAST(bodyToks[0], bodyToks[0].value)); } } while (idx > 0) { AST left, right; if (astIdx[idx - 1] >= 0) { left = bodyASTs[astIdx[idx - 1]]; } else { left = new NumAST(bodyToks[idx - 1], bodyToks[idx - 1].value, bodyToks[idx - 1].tokenType); } if (astIdx[idx + 1] >= 0) { right = bodyASTs[astIdx[idx + 1]]; } else { right = new NumAST(bodyToks[idx + 1], bodyToks[idx + 1].value, bodyToks[idx + 1].tokenType); } astIdx[idx] = astIdx[idx + 1] = astIdx[idx - 1] = bodyASTs.Count; order[idx] = order[idx + 1] = order[idx - 1] = -1; bodyASTs.Add(new BinOpAST(bodyToks[idx], left, right)); idx = IndexOfHighestPrecedence(bodyToks, order); } ExprAST expr = new ExprAST(firsttoken, bodyASTs.ToArray()); return(expr); }
public ExprAST Visit(ExprAST node) { if (!dontVisit) { return(visitor.Visit(node)); } return(node); }
public virtual ExprAST Visit(ExprAST node) { if (node != null) { return node.Accept(this); } return null; }
public ForExprAST(string varName, ExprAST start, ExprAST end, ExprAST step, ExprAST body) { this.VarName = varName; this.Start = start; this.End = end; this.Step = step; this.Body = body; this.NodeType = ExprType.ForExpr; }
public virtual ExprAST Visit(ExprAST node) { if (node != null) { return(node.Accept(this)); } return(null); }
public ForExprAST(string varName, ExprAST start, ExprAST end, ExprAST step, ExprAST body) { this.VarName = varName; this.Start = start; this.End = end; this.Step = step; this.Body = body; this.NodeType = ExprType.ForExpr; }
public object DoForExpr(ExprAST xpr, object options = null) { PositionOutput(xpr.token.indent); ConsoleEx.WriteLine("{0}(Expr)", ConsoleColor.DarkBlue); foreach (var x in xpr.body) { x.Accept(this, options); } return(options); }
public object DoForExpr(ExprAST exprAST, object options = null) { ObjExpr objExpr = null; foreach (var expr in exprAST.body) { objExpr = (ObjExpr)expr.Accept(this, objExpr); } return(objExpr); }
public void ASTComparsion() { var left = new ExprAST(Token.Plus); var right = new ExprAST(Token.Plus); Assert.Equal(left, right); var left1 = new RootAST(new List <RootAST>(new RootAST[] { new ExprAST(Token.Plus), new LoopAST(new List <RootAST>(new RootAST[] { new ExprAST(Token.Plus) })) })); var right1 = new RootAST(new List <RootAST>(new RootAST[] { new ExprAST(Token.Plus), new LoopAST(new List <RootAST>(new RootAST[] { new ExprAST(Token.Plus) })) })); Assert.Equal(left1, right1); Assert.NotEqual(left, left1); }
//式の解析 public static ExprAST CreateSikiAST(TokenStream tokenst) { tokenst.SetCheckPoint(); ExprAST exprAST = CreateKouAST(tokenst); if (exprAST == null) { tokenst.Rollback(); return(null); } while (tokenst.NowIndex < tokenst.Size) { string opstr = tokenst.Get().Str; if (opstr == "+" || opstr == "-") { tokenst.Next(); ExprAST baseAST2 = CreateKouAST(tokenst); if (baseAST2 == null) { tokenst.Rollback(); return(null); } if (opstr == "+") { exprAST = new BinaryExprAST(BinaryExprAST.Op.Add, exprAST, baseAST2); } else { exprAST = new BinaryExprAST(BinaryExprAST.Op.Sub, exprAST, baseAST2); } } else { break; } } return(exprAST); }
public void ExitRule(ExprAST argument) { string ruleName = this.descentStack.Pop(); this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Exit" + ruleName), this.listener, argument)); this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Enter" + ruleName), this.listener, argument)); }
public BinaryExprAST(Op op, ExprAST left, ExprAST right) { this.op = op; this.left = left; this.right = right; }
public LambdaAST(ExprAST exprAST) { this.exprAST = exprAST; }
public VariableDeclarationAST(VariableTable vtable, string name, ExprAST baseAST) { this.name = name; this.exprAST = baseAST; this.vtable = vtable; }
public MinusAST(ExprAST exprAST) { this.exprAST = exprAST; }
public virtual ExprAST Visit(ExprAST node) { return(node?.Accept(this)); }
protected internal virtual ExprAST VisitExtension(ExprAST node) { return(node.VisitChildren(this)); }
BlockAST Statments(int startIndent = 0) { List <AST> stmts = new List <AST>(); Token firstTok = tokens.current; do { string found = GetStmntMatch(tokens); switch (found) { case "Command": { AST stmtAST = null; switch (tokens.curTokenType) { case TokenType.Return: Token retTok = tokens.current; ExprAST expAST = ParseExpr(); stmtAST = new RetCmdAST(retTok, expAST); break; default: ShowError("Unknown command", tokens.current); break; } if (stmtAST != null) { stmts.Add(stmtAST); } break; } case "Main": { TypeAST typeAST = new TypeAST(tokens.current); Token mainTok = tokens.Next(); ArgAST[] argsAST = ParseArgs(); symbols.AddParent(typeAST, mainTok.value, argsAST); BlockAST stmtAST = Statments(startIndent + 1); symbols.RemParent(); AST functAST = new FuncDefAST(mainTok, typeAST, argsAST, stmtAST); stmts.Add(functAST); break; } case "Function": { TypeAST typeAST = new TypeAST(tokens.current); Token instTok = tokens.Next(); ArgAST[] argsAST = ParseArgs(); symbols.AddParent(typeAST, instTok.value, argsAST); BlockAST stmtAST = Statments(startIndent + 1); symbols.RemParent(); AST functAST = new FuncDefAST(instTok, typeAST, argsAST, stmtAST); stmts.Add(functAST); break; } case "ObjectDef": { // TokenType.Identifier, TokenType.Colon TypeAST typeAST = new TypeAST(tokens.current); tokens.Next(); symbols.AddParent(typeAST, "", null); tokens.Next(); BlockAST stmtAST = Statments(startIndent + 1); symbols.RemParent(); ObjDefAST odAST = new ObjDefAST(tokens.current, stmtAST); stmts.Add(odAST); break; } case "SimpleObjectInst": { // TokenType.AllTypes, TokenType.Identifier TypeAST typeAST = new TypeAST(tokens.current); tokens.Next(); symbols.Add(typeAST, tokens.current.value, null); ObjInstAST odAST = new ObjInstAST(tokens.current, typeAST, null); stmts.Add(odAST); break; } case "ObjectInst": { TypeAST typeAST = new TypeAST(tokens.current); tokens.Next(); symbols.Add(typeAST, tokens.current.value, null); ObjInstAST odAST = new ObjInstAST(tokens.current, typeAST, null); stmts.Add(odAST); break; } case "SimpleObjectInstAssigned": { TypeAST typeAST = new TypeAST(tokens.current); Token instTok = tokens.Next(); symbols.Add(typeAST, instTok.value, null); tokens.Next(); NumAST numAST = new NumAST(tokens.current, tokens.current.value, tokens.curTokenType); ObjInstAST odAST = new ObjInstAST(instTok, typeAST, numAST); stmts.Add(odAST); break; } case "ObjectInstAssigned": { TypeAST typeAST = new TypeAST(tokens.current); Token instTok = tokens.Next(); symbols.Add(typeAST, instTok.value, null); tokens.Next(); NumAST numAST = new NumAST(tokens.current, tokens.current.value, tokens.curTokenType); ObjInstAST odAST = new ObjInstAST(instTok, typeAST, numAST); stmts.Add(odAST); break; } default: ShowError("Unable to parse statement", tokens.current); break; } } while (tokens.Remaining() > 0); return(new BlockAST(firstTok, stmts.ToArray())); }
// binoprhs // ::= ('+' primary)* private ExprAST ParseBinOpRHS(int exprPrec, ExprAST lhs) { // If this is a binop, find its precedence. while (true) { int tokPrec = this.scanner.GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (tokPrec < exprPrec) { return lhs; } // Okay, we know this is a binop. int binOp = this.scanner.CurrentToken; this.scanner.GetNextToken(); // eat binop // Parse the primary expression after the binary operator. ExprAST rhs = this.ParsePrimary(); if (rhs == null) { return null; } // If BinOp binds less tightly with RHS than the operator after RHS, let // the pending operator take RHS as its LHS. int nextPrec = this.scanner.GetTokPrecedence(); if (tokPrec < nextPrec) { rhs = this.ParseBinOpRHS(tokPrec + 1, rhs); if (rhs == null) { return null; } } // Merge LHS/RHS. lhs = new BinaryExprAST((char)binOp, lhs, rhs); } }
protected internal virtual ExprAST VisitExtension(ExprAST node) { return node.VisitChildren(this); }
public FunctionAST(PrototypeAST proto, ExprAST body) { this.Proto = proto; this.Body = body; this.NodeType = ExprType.FunctionExpr; }
public RetCmdAST(Token token, ExprAST expression) { this.token = token; this.expression = expression; }
public FunctionAST(PrototypeAST proto, ExprAST body) { this.Proto = proto; this.Body = body; this.NodeType = ExprType.FunctionExpr; }
public ASTContext(MethodInfo methodInfo, object instance, ExprAST argument) { this.MethodInfo = methodInfo; this.Instance = instance; this.Argument = argument; }