aboutsummaryrefslogtreecommitdiffstats
path: root/src/repl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/repl.cpp')
-rw-r--r--src/repl.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/repl.cpp b/src/repl.cpp
index 8fe43f5..216bbb2 100644
--- a/src/repl.cpp
+++ b/src/repl.cpp
@@ -118,16 +118,15 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute)
lifted.push_back(exp);
if (cenv.args.find("-L") != cenv.args.end())
return dump(cenv, lifted);
-
- // Compile top-level (lifted) functions
- CVal val = NULL;
- CFunc f = NULL;
- Code exprs;
+
+ // Flatten expressions
+ Code flattened; // Type and function definitions
+ Code exprs; // Other top-level expressions (main code)
for (Code::const_iterator i = lifted.begin(); i != lifted.end(); ++i) {
const ATuple* call = (*i)->to_tuple();
- if (call && ( (is_form(call, "def-type"))
- || (is_form(call, "def") && is_form(call->frrst(), "fn")))) {
- val = resp_compile(cenv, call);
+ if (call && (is_form(*i, "def-type")
+ || (is_form(*i, "def") && is_form(call->frrst(), "fn")))) {
+ resp_flatten(cenv, flattened, call);
} else {
const ATuple* tup = (*i)->to_tuple();
if (!tup || !tup->empty()) {
@@ -135,18 +134,26 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute)
}
}
}
+ if (cenv.args.find("-F") != cenv.args.end())
+ return dump(cenv, flattened);
+
+ // Compile type and function definitions
+ for (Code::const_iterator i = flattened.begin(); i != flattened.end(); ++i) {
+ resp_compile(cenv, *i);
+ }
+ // Compile other top-level expressions into "main" function
if (!exprs.empty()) {
const AST* type = cenv.type(exprs.back());
const ATuple* fnT = tup(cursor, cenv.tenv.Fn, new ATuple(cursor), type, 0);
- // Create function for program containing all expressions except definitions
- f = cenv.engine()->startFn(cenv, "main", new ATuple(cursor), fnT);
+ CFunc f = cenv.engine()->startFn(cenv, "main", new ATuple(cursor), fnT);
+ CVal val = NULL;
for (Code::const_iterator i = exprs.begin(); i != exprs.end(); ++i)
val = resp_compile(cenv, *i);
cenv.engine()->finishFn(cenv, f, val, type);
- // Call and print result
+ // Call main and print result
if (cenv.args.find("-S") == cenv.args.end())
callPrintCollect(cenv, f, ast, type, execute);
}