aboutsummaryrefslogtreecommitdiffstats
path: root/src/repl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/repl.cpp')
-rw-r--r--src/repl.cpp78
1 files changed, 53 insertions, 25 deletions
diff --git a/src/repl.cpp b/src/repl.cpp
index 9709656..afa81e9 100644
--- a/src/repl.cpp
+++ b/src/repl.cpp
@@ -29,22 +29,35 @@
using namespace std;
-static bool
-readParseType(CEnv& cenv, Cursor& cursor, istream& is, const AST*& exp, const AST*& ast)
+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;
+}
+
+static const AST*
+readExpand(PEnv& penv, Cursor& cursor, istream& is, const AST*& exp)
{
try {
- exp = cenv.penv.parse(cursor, is);
+ exp = penv.parse(cursor, is);
} catch (Error e) {
cerr << e.what() << endl;
is.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Skip REPL junk
throw e;
}
- if (!exp || (exp->to_tuple() && exp->to_tuple()->empty()))
- return false;
+ if (!exp) {
+ return NULL;
+ }
- ast = cenv.penv.expand(exp); // Parse input
+ return penv.expand(exp);
+}
+static bool
+constrainUnify(CEnv& cenv, const AST* ast)
+{
Constraints c(cenv.tsubst);
resp_constrain(cenv.tenv, c, ast); // Constrain types
cenv.tsubst = unify(c);
@@ -72,14 +85,6 @@ callPrintCollect(CEnv& cenv, CFunc f, const AST* result, const AST* resultT, boo
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)
{
@@ -177,18 +182,28 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute)
const AST* ast = NULL;
try {
- // Parse and type all expressions
- Code parsed;
- while (readParseType(cenv, cursor, is, exp, ast))
- parsed.push_back(ast);
+ // Read and expand all expressions
+ Code expanded;
+ while (!is.eof()) {
+ if ((ast = readExpand(cenv.penv, cursor, is, exp)))
+ expanded.push_back(ast);
+ }
+ if (cenv.args.find("-E") != cenv.args.end()) {
+ dump(cenv, expanded);
+ return 0;
+ }
+
+ // Type constrain and unify expanded code
+ for (auto e : expanded)
+ constrainUnify(cenv, e);
if (cenv.args.find("-T") != cenv.args.end()) {
- dump(cenv, parsed);
+ dump(cenv, expanded);
return 0;
}
Code defs;
bool hasMain = false;
- const AST* retT = compile(cenv, parsed, defs, hasMain, "main");
+ const AST* retT = compile(cenv, expanded, defs, hasMain, "main");
if (cenv.args.find("-F") != cenv.args.end()) {
dump(cenv, defs);
return 0;
@@ -230,16 +245,29 @@ repl(CEnv& cenv)
Cursor cursor("(stdin)", 1, 1);
try {
- if (!readParseType(cenv, cursor, std::cin, exp, ast))
+ // Read and expand expression
+ Code expanded;
+ if ((ast = readExpand(cenv.penv, cursor, std::cin, exp)))
+ expanded.push_back(ast);
+ else
break;
+ if (cenv.args.find("-E") != cenv.args.end()) {
+ dump(cenv, expanded);
+ return 0;
+ }
+
+ // Type constrain and unify expanded code
+ for (auto e : expanded)
+ constrainUnify(cenv, e);
+ if (cenv.args.find("-T") != cenv.args.end()) {
+ dump(cenv, expanded);
+ return 0;
+ }
- 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());
+ const AST* retT = compile(cenv, expanded, defs, hasMain, replName.c_str());
if (cenv.args.find("-F") != cenv.args.end()) {
dump(cenv, defs);
continue;