public ParanExpr(Expr v) { Value = v; }
public MathExpr(Expr lexpr, Symbol o, Expr rexpr) { LeftExpr = lexpr; Op = o; RightExpr = rexpr; }
public ElseIfBlock(Expr lexpr, Symbol o, Expr rexpr) { LeftExpr = lexpr; Op = o; RightExpr = rexpr; }
public Return(Expr e) { Expr = e; }
public Assign(string i, Expr v) { Ident = i; Value = v; }
public static Expr Parse(TokenList _tokens) { Expr? ret = null; Token t = _tokens.GetToken(); if (_tokens.PeekToken().TokenName == Lexer.Tokens.LeftParan) { string ident = ""; if (t.TokenName == Lexer.Tokens.Ident) { ident = t.TokenValue.ToString(); } _tokens.Pos++; if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan) { ret = new CallExpr(ident, new List <Expr>()); } else { ret = new CallExpr(ident, Call.ParseArgs(_tokens)); } } else if (t.TokenName == Lexer.Tokens.IntLiteral) { string originalStr = t.TokenValue.ToString().BinaryStrToStr() .Replace(ITokens.IPartial.INT_LITERAL, ""); // remove int-literals to parse to number IntLiteral i = new IntLiteral( Convert.ToInt32( originalStr ) ); ret = i; } else if (t.TokenName == Lexer.Tokens.StringLiteral) { StringLiteral s = new StringLiteral(t.TokenValue.ToString()); ret = s; } else if (t.TokenName == Lexer.Tokens.Ident) { string ident = t.TokenValue.ToString(); Ident i = new Ident(ident); ret = i; } else if (t.TokenName == Lexer.Tokens.LeftParan) { Expr e = Expr.Parse(_tokens); if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan) { _tokens.Pos++; } ParanExpr p = new ParanExpr(e); if (_tokens.PeekToken().TokenName == Lexer.Tokens.Add) { _tokens.Pos++; Expr expr = Expr.Parse(_tokens); ret = new MathExpr(p, Symbol.add, expr); } else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Sub) { _tokens.Pos++; Expr expr = Expr.Parse(_tokens); ret = new MathExpr(p, Symbol.sub, expr); } else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Mul) { _tokens.Pos++; Expr expr = Expr.Parse(_tokens); ret = new MathExpr(p, Symbol.mul, expr); } else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Div) { _tokens.Pos++; Expr expr = Expr.Parse(_tokens); ret = new MathExpr(p, Symbol.div, expr); } else { ret = p; } } if (_tokens.PeekToken().TokenName == Lexer.Tokens.Add || _tokens.PeekToken().TokenName == Lexer.Tokens.Sub || _tokens.PeekToken().TokenName == Lexer.Tokens.Mul || _tokens.PeekToken().TokenName == Lexer.Tokens.Div) { Expr? lexpr = ret; Symbol op = 0; if (_tokens.PeekToken().TokenName == Lexer.Tokens.Add) { op = Symbol.add; } else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Sub) { op = Symbol.sub; } else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Mul) { op = Symbol.mul; } else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Div) { op = Symbol.div; } _tokens.Pos++; Expr rexpr = Expr.Parse(_tokens); ret = new MathExpr( lexpr ?? throw new Exception($"Left expression of mathematical expression is missing: '{op}'->'{rexpr}'"), op, rexpr); } if (ret == null) { throw new Exception($"Caught unexpected expression -> '{t.TokenName}':'{t.TokenValue}' at position '{_tokens.Pos}', total-length: '{_tokens.Tokens.Count}'"); } return(ret); }