/* 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 "resp.hpp" using namespace std; static bool readParseType(CEnv& cenv, Cursor& cursor, istream& is, const AST*& exp, const AST*& ast) { try { exp = read_expression(cenv.penv, cursor, is); } catch (Error e) { 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 //cout << "(CONSTRAINTS " << endl << c << ")" << endl; const Subst subst = unify(c); // Solve type constraints for (Subst::const_iterator i = subst.begin(); i != subst.end(); ++i) { if (!cenv.tsubst.contains(i->first)) { // Substitution's LHS is a new variable cenv.tsubst.push_back(*i); // Add substitution to global type substitution Object::pool.addRoot(i->first); Object::pool.addRoot(i->second); } } //cout << "(SUBST " << endl << subst << ")" << endl; //cout << "(TSUBST " << endl << cenv.tsubst << ")" << endl; cenv.tsubst = Subst::compose(cenv.tsubst, subst); // FIXME: breaks polymorphism + repl 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 AType* resultT, bool execute) { if (execute) cenv.out << cenv.engine()->call(cenv, f, resultT); // Print type (if applicable) if (resultT->head()->str() != "Nothing") cenv.out << " : " << resultT << endl; Object::pool.collect(Object::pool.roots()); } /// 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; typedef list Parsed; Parsed parsed; try { while (readParseType(cenv, cursor, is, exp, ast)) parsed.push_back(ast); if (cenv.args.find("-T") != cenv.args.end()) { for (Parsed::const_iterator i = parsed.begin(); i != parsed.end(); ++i) pprint(cout, *i, &cenv, true); return 0; } CVal val = NULL; CFunc f = NULL; // Lift all expressions Code lifted; for (Parsed::const_iterator i = parsed.begin(); i != parsed.end(); ++i) { const AST* l = resp_lift(cenv, lifted, *i); if (l) lifted.push_back(l); } if (cenv.args.find("-L") != cenv.args.end()) { for (Code::const_iterator i = lifted.begin(); i != lifted.end(); ++i) pprint(cout, *i, &cenv, true); return 0; } // Compile top-level (lifted) functions Code exprs; for (Code::const_iterator i = lifted.begin(); i != lifted.end(); ++i) { const ATuple* call = (*i)->to_tuple(); if (call && is_form(call, "def") && is_form(call->list_ref(2), "fn")) { val = resp_compile(cenv, call); } else { assert(*i); const ATuple* tup = (*i)->to_tuple(); if (!tup || (tup->tup_len() > 0)) exprs.push_back(*i); } } const AType* type = cenv.type(exprs.back()); const AType* fnT = tup(cursor, cenv.tenv.Fn, new AType(cursor), type, 0); // Create function for top-level of program f = cenv.engine()->startFn(cenv, "main", new ATuple(cursor), fnT); // Compile expressions (other than function definitions) into it for (Code::const_iterator i = exprs.begin(); i != exprs.end(); ++i) val = resp_compile(cenv, *i); // Finish compilation cenv.engine()->finishFn(cenv, f, val); if (cenv.args.find("-S") != cenv.args.end()) { cenv.engine()->writeModule(cenv, cenv.out); return 0; } // Call and print result callPrintCollect(cenv, f, ast, type, execute); } catch (Error& e) { cenv.err << e.what() << endl; return 1; } return 0; } /// Read Eval Print Loop int repl(CEnv& cenv) { 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)"); try { if (!readParseType(cenv, cursor, std::cin, exp, ast)) break; Code lifted; ast = resp_lift(cenv, lifted, ast); const AType* type = cenv.type(ast); const AType* fnT = tup(cursor, cenv.tenv.Fn, new AType(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, f, resp_compile(cenv, ast)); callPrintCollect(cenv, f, ast, type, true); if (cenv.args.find("-d") != cenv.args.end()) cenv.engine()->writeModule(cenv, cenv.out); } catch (Error& e) { cenv.out << e.msg << endl; cenv.engine()->eraseFn(cenv, f); } } catch (Error& e) { cenv.err << e.what() << endl; } } return 0; }