private string GenerateCode(string file) { StreamReader?sr; try { sr = new StreamReader(file); } catch (Exception ex) when( ex is DirectoryNotFoundException || ex is FileNotFoundException) { throw new Exception($"Run: Could not find input file -> {file}", ex); } string uncompiledCode = sr.ReadToEnd(); Log.Here().Verbose($"Uncompiled code -> \n{uncompiledCode}"); TokenList tokens = Tokenize(uncompiledCode); Parser parser; List <Stmt>?tree; try { parser = new Parser(tokens); tree = parser.Tree; } catch (Exception ex) { throw new Exception($"Could not parse code -> Parser: {tokens}", ex); } Log.Here().Verbose($"Statement Tree -> {JsonSerializer.Serialize(tree)}"); Compiler compiler = new Compiler(tree); string compiledCode = compiler.GetCode(); compiledCode += LoadImports(parser.Imports); return(compiledCode); }
public static Return Parse(TokenList _tokens) { return(new Return(Expr.Parse(_tokens))); }
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); }