private Expr parse(string input) { string in_ = eatWhitespace(input); if (peekChar(in_) == '(') { in_ = eatWhitespace(eatChar('(', in_)); Expr fun; List <Expr> args = new List <Expr>(); string fun_string; if (peekChar(in_) == '(') { fun_string = peekList(in_); fun = parse(fun_string); } else { fun_string = peekWord(in_); if (env.ContainsKey(fun_string)) { fun = new VarExpr(fun_string); } else { switch (fun_string) { case "lambda": return(parseLam(in_)); case "define": return(parseDef(in_)); case "if": // if appears to be special too return(parseIf(in_)); case "or": return(parseOr(in_)); case "and": return(parseAnd(in_)); default: { fun = new VarExpr(fun_string); break; } } } } in_ = eatWhitespace(eatWord(fun_string, in_)); while (peekChar(in_) != ')') { if (peekChar(in_) == '(') { string word = peekList(in_); args.Add(parse(word)); in_ = eatWhitespace(eatWord(word, in_)); } else { string word = peekWord(in_); args.Add(parse(word)); in_ = eatWhitespace(eatWord(word, in_)); } } return(new AppExpr(fun, args, env)); } else { if (peekChar(in_) == '"') // we have a string { return(new StrExpr(in_)); } long num; if (Int64.TryParse(input, out num)) { return(new NumExpr(num)); } else if (input == "empty") { return(new EmptyExpr()); } else if (input == "#t" || input == "true") { return(new BoolExpr(true)); } else if (input == "#f" || input == "false") { return(new BoolExpr(false)); } else { return(new VarExpr(input)); } } }
private Expr parse(string input) { string in_ = eatWhitespace(input); if (peekChar(in_) == '(') { in_ = eatWhitespace(eatChar('(', in_)); Expr fun; List<Expr> args = new List<Expr>(); string fun_string; if (peekChar(in_) == '(') { fun_string = peekList(in_); fun = parse(fun_string); } else { fun_string = peekWord(in_); if (env.ContainsKey(fun_string)) fun = new VarExpr(fun_string); else switch (fun_string) { case "lambda": return parseLam(in_); case "define": return parseDef(in_); case "if": // if appears to be special too return parseIf(in_); case "or": return parseOr(in_); case "and": return parseAnd(in_); default: { fun = new VarExpr(fun_string); break; } } } in_ = eatWhitespace(eatWord(fun_string, in_)); while (peekChar(in_) != ')') { if (peekChar(in_) == '(') { string word = peekList(in_); args.Add(parse(word)); in_ = eatWhitespace(eatWord(word, in_)); } else { string word = peekWord(in_); args.Add(parse(word)); in_ = eatWhitespace(eatWord(word, in_)); } } return new AppExpr(fun, args, env); } else { if (peekChar(in_) == '"') // we have a string return new StrExpr(in_); long num; if (Int64.TryParse(input, out num)) { return new NumExpr(num); } else if (input == "empty") return new EmptyExpr(); else if (input == "#t" || input == "true") return new BoolExpr(true); else if (input == "#f" || input == "false") return new BoolExpr(false); else return new VarExpr(input); } }