/* 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, bool& hasMain, const char* mainName) { // Convert to CPS (currently not used in the compilation process) if (cenv.args.find("-C") != cenv.args.end()) { Code cps; for (Code::const_iterator i = parsed.begin(); i != parsed.end(); ++i) { const AST* exp = resp_cps(cenv, *i, cenv.penv.sym("display")); if (exp) { cps.push_back(exp); } } h return dump(cenv, cps); } struct Pass { const RespPass* pass; ///< Pass function const char* option; ///< Command line option to stop and dump here }; const Pass passes[] = { { resp_simplify, "-R" }, { resp_lift, "-L" }, { resp_depoly, "-D" }, { NULL, NULL } }; // List of stages of code after each pass std::list stages; stages.push_back(parsed); // Apply each pass in turn and append to stages for (const Pass* p = passes; p->pass; ++p) { const Code& input = stages.back(); Code output; for (Code::const_iterator i = input.begin(); i != input.end(); ++i) { const AST* exp = (*p->pass)(cenv, output, *i); if (exp) { output.push_back(exp); } } stages.push_back(output); if (cenv.args.find(p->option) != cenv.args.end()) { return dump(cenv, output); } } // Flatten expressions const AST* retT = NULL; Code exprs; for (Code::const_iterator i = stages.back().begin(); i != stages.back().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); } } } // Flatten top-level code into main function if (!exprs.empty()) { const ASymbol* main = cenv.penv.sym(mainName); 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)); } hasMain = !exprs.empty(); 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; bool hasMain = false; const AST* retT = compile(cenv, parsed, defs, hasMain, "main"); 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); Object::pool.addRoot(*i); } if (cenv.args.find("-S") != cenv.args.end()) { cenv.engine()->writeModule(cenv, cenv.out); return 0; } // Call main and print result if (hasMain) 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; while (1) { cenv.out << "() "; cenv.out.flush(); Cursor cursor("(stdin)", 1, 1); try { if (!readParseType(cenv, cursor, std::cin, exp, ast)) break; Code parsed; parsed.push_back(ast); Code defs; bool hasMain; const std::string replName = cenv.penv.gensymstr("_repl"); const AST* retT = compile(cenv, parsed, defs, hasMain, replName.c_str()); if (cenv.args.find("-F") != cenv.args.end()) { dump(cenv, defs); continue; } // 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); continue; } // Call main and print result if (hasMain) { callPrintCollect(cenv, cenv.engine()->getFn(cenv, replName), ast, retT, true); } } catch (Error& e) { cenv.err << e.what() << endl; } } return 0; }