aboutsummaryrefslogtreecommitdiffstats
path: root/src/repl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/repl.cpp')
-rw-r--r--src/repl.cpp70
1 files changed, 34 insertions, 36 deletions
diff --git a/src/repl.cpp b/src/repl.cpp
index b70618e..c461526 100644
--- a/src/repl.cpp
+++ b/src/repl.cpp
@@ -72,6 +72,14 @@ callPrintCollect(CEnv& cenv, CFunc f, const AST* result, const AST* resultT, boo
Object::pool.collect(Object::pool.roots());
}
+static inline int
+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;
+}
+
/// Compile and evaluate code from @a is
int
eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute)
@@ -79,51 +87,41 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute)
const AST* exp = NULL;
const AST* ast = NULL;
- typedef list<const AST*> Parsed;
- Parsed parsed;
-
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()) {
- for (Parsed::const_iterator i = parsed.begin(); i != parsed.end(); ++i)
- pprint(cout, *i, &cenv, (cenv.args.find("-a") != cenv.args.end()));
- return 0;
- }
+ if (cenv.args.find("-T") != cenv.args.end())
+ return dump(cenv, parsed);
// Simplify all expressions
Code simplified;
- for (Parsed::const_iterator i = parsed.begin(); i != parsed.end(); ++i) {
- const AST* l = resp_simplify(cenv, *i);
- if (l)
- simplified.push_back(l);
- }
-
- if (cenv.args.find("-R") != cenv.args.end()) {
- for (Code::const_iterator i = simplified.begin(); i != simplified.end(); ++i)
- pprint(cout, *i, &cenv, (cenv.args.find("-a") != cenv.args.end()));
- return 0;
- }
-
- CVal val = NULL;
- CFunc f = NULL;
-
+ 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 (Parsed::const_iterator i = simplified.begin(); i != simplified.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, (cenv.args.find("-a") != cenv.args.end()));
- return 0;
- }
-
+ 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);
+
// Compile top-level (lifted) functions
+ CVal val = NULL;
+ CFunc f = NULL;
Code exprs;
for (Code::const_iterator i = lifted.begin(); i != lifted.end(); ++i) {
const ATuple* call = (*i)->to_tuple();