/* Resp: A programming language * Copyright (C) 2008-2009 David Robillard * * Resp is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Resp is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General * Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Resp. If not, see . */ /** @file * @brief REPL and eval */ #include #include #include #include #include #include "resp.hpp" using namespace std; static bool readParseType(CEnv& cenv, Cursor& cursor, istream& is, const AST*& exp, const AST*& ast) { try { exp = cenv.penv.parse(cursor, is); } catch (Error e) { cerr << e.what() << endl; is.ignore(std::numeric_limits::max(), '\n'); // Skip REPL junk throw e; } if (!exp || (exp->to_tuple() && exp->to_tuple()->empty())) return false; ast = cenv.penv.expand(exp); // Parse input Constraints c(cenv.tsubst); resp_constrain(cenv.tenv, c, ast); // Constrain types cenv.tsubst = unify(c); //cout << "(TSUBST " << endl << cenv.tsubst << ")" << endl; Object::pool.addRoot(ast); // Make parsed expression a GC root so it is not deleted return true; } static void callPrintCollect(CEnv& cenv, CFunc f, const AST* result, const AST* resultT, bool execute) { Object::pool.collect(Object::pool.roots()); if (execute) cenv.out << cenv.engine()->call(cenv, f, resultT); // Print type (if applicable) const std::string type_str = resultT->str(); if (type_str != "Nothing") cenv.out << " : " << type_str << endl; Object::pool.collect(Object::pool.roots()); } static inline const AST* dump(CEnv& cenv, const Code& code) { for (Code::const_iterator i = code.begin(); i != code.end(); ++i) pprint(cout, *i, &cenv, (cenv.args.find("-a") != cenv.args.end())); return 0; } const AST* compile(CEnv& cenv, const Code& parsed, Code& defs, Code& exprs) { const AST* exp = NULL; // Simplify all expressions Code simplified; for (Code::const_iterator i = parsed.begin(); i != parsed.end(); ++i) if ((exp = resp_simplify(cenv, *i))) simplified.push_back(exp); if (cenv.args.find("-R") != cenv.args.end()) return dump(cenv, simplified); // Convert to CPS Code cps; for (Code::const_iterator i = simplified.begin(); i != simplified.end(); ++i) if ((exp = resp_cps(cenv, *i, cenv.penv.sym("display")))) cps.push_back(exp); if (cenv.args.find("-C") != cenv.args.end()) return dump(cenv, cps); // Lift all expressions Code lifted; for (Code::const_iterator i = simplified.begin(); i != simplified.end(); ++i) if ((exp = resp_lift(cenv, lifted, *i))) lifted.push_back(exp); if (cenv.args.find("-L") != cenv.args.end()) return dump(cenv, lifted); // Flatten expressions const AST* retT = NULL; for (Code::const_iterator i = lifted.begin(); i != lifted.end(); ++i) { const ATuple* call = (*i)->to_tuple(); if (call && (is_form(*i, "def-type") || (is_form(*i, "def") && is_form(call->frrst(), "fn")))) { resp_flatten(cenv, defs, call); } else { const ATuple* tup = (*i)->to_tuple(); if (!tup || !tup->empty()) { exprs.push_back(*i); retT = cenv.type(*i); } } } return retT; } /// Compile and evaluate code from @a is int eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute) { const AST* exp = NULL; const AST* ast = NULL; try { // Parse and type all expressions Code parsed; while (readParseType(cenv, cursor, is, exp, ast)) parsed.push_back(ast); if (cenv.args.find("-T") != cenv.args.end()) { dump(cenv, parsed); return 0; } Code defs; Code exprs; const AST* retT = compile(cenv, parsed, defs, exprs); // Flatten main code into `defs' if (!exprs.empty()) { const ASymbol* main = cenv.penv.sym("main"); List mainT(Cursor(), cenv.penv.sym("Fn"), new ATuple(Cursor()), retT, NULL); cenv.def(main, NULL, mainT, NULL); defs.push_back( tup(Cursor(), cenv.penv.sym("fn-start"), main, NULL)); const AST* mainRet = NULL; for (Code::const_iterator i = exprs.begin(); i != exprs.end(); ++i) mainRet = resp_flatten(cenv, defs, *i); defs.push_back( tup(Cursor(), cenv.penv.sym("fn-end"), main, mainRet, NULL)); } if (cenv.args.find("-F") != cenv.args.end()) { dump(cenv, defs); return 0; } // Compile flattened code for (Code::const_iterator i = defs.begin(); i != defs.end(); ++i) { resp_compile(cenv, *i); } if (cenv.args.find("-S") != cenv.args.end()) { cenv.engine()->writeModule(cenv, cenv.out); return 0; } // Call main and print result if (!exprs.empty()) callPrintCollect(cenv, cenv.engine()->getFn(cenv, "main"), ast, retT, execute); } catch (Error& e) { cenv.err << e.what() << endl; return 1; } return 0; } /// Read Eval Print Loop int repl(CEnv& cenv) { cenv.repl = true; const AST* exp = NULL; const AST* ast = NULL; const string replFnName = cenv.penv.gensymstr("_repl"); while (1) { cenv.out << "() "; cenv.out.flush(); Cursor cursor("(stdin)", 1, 1); try { if (!readParseType(cenv, cursor, std::cin, exp, ast)) break; Code lifted; ast = resp_lift(cenv, lifted, ast); const AST* type = cenv.type(ast); const ATuple* fnT = tup(cursor, cenv.tenv.Fn, new ATuple(cursor), type, 0); CFunc f = NULL; try { // Create function for this repl loop f = cenv.engine()->startFn(cenv, replFnName, new ATuple(cursor), fnT); cenv.engine()->finishFn(cenv, resp_compile(cenv, ast), type); if (cenv.args.find("-S") != cenv.args.end()) cenv.engine()->writeModule(cenv, cenv.out); else callPrintCollect(cenv, f, ast, type, true); } catch (Error& e) { cenv.out << e.msg << endl; cenv.engine()->eraseFn(cenv, f); } } catch (Error& e) { cenv.err << e.what() << endl; } } return 0; }