public object ExecuteFile(string filePath, ExecEnvironment ee) { string code = File.ReadAllText(filePath); var p = new Parser(); var path = EugineVM.GetDirectoryName(filePath); var source = Path.GetFileName(filePath); ee["~path"] = new SString(path); ee["~source"] = new SString(source); var s = p.Parse(code, path, source); return(SExpression.Cast(s).Evaluate(ee).Get <object>()); }
private SExpr ParseNext(List <SToken> tokens) { var token = tokens.Pop(); if (token == null) { return(null); } if (token.TType == SToken.TokenType.RPAREN || token.TType == SToken.TokenType.RBRACK) { throw new VMException("unexpected character", token); } if (token.TType == SToken.TokenType.LPAREN || token.TType == SToken.TokenType.LBRACK) { var ending = token.TType == SToken.TokenType.LPAREN ? SToken.TokenType.RPAREN : SToken.TokenType.RBRACK; var comp = new SExprComp(); while (tokens.First().TType != ending) { comp.Atomics.Add(ParseNext(tokens)); } if (tokens.Count == 0 || tokens[0].TType != ending) { throw new VMException("unexpected character", token); } tokens.Pop(); if (token.TType == SToken.TokenType.LBRACK && comp.Atomics.Count >= 2) { var tmp = comp.Atomics[0]; comp.Atomics[0] = comp.Atomics[1]; comp.Atomics[1] = tmp; } if (comp.Atomics.Count == 2 && (comp.Atomics[0] as SExprAtomic)?.Token.TValue.ToString() == "~include") { var path = (comp.Atomics[1] as SExprAtomic); if (path != null && path.Token.TType == SToken.TokenType.STRING) { var codePath = basePath + (string)path.Token.TValue; var codeFolder = EugineVM.GetDirectoryName(codePath); var codeSource = Path.GetFileName(codePath); try { var p = new Parser(); SExprComp inc = p.Parse(File.ReadAllText(codePath), codeFolder, codeSource); comp.Atomics = new List <SExpr>(); comp.Atomics.Add(inc); } catch (Exception ex) { throw new VMException("error when reading " + codePath + ", " + ex.Message, comp.Atomics[0] as SExprAtomic); } } else { throw new VMException("it must be a static string", comp.Atomics[0] as SExprAtomic); } } return(comp); } return(new SExprAtomic(token)); }
static void Main(string[] args) { var vm = new EugineVM(); vm.ExecuteFile("./tests/test.lisp", vm.DefaultEnvironment); }