diff options
Diffstat (limited to 'src/repl.cpp')
-rw-r--r-- | src/repl.cpp | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/src/repl.cpp b/src/repl.cpp index 1169b3d..2848a53 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -83,45 +83,55 @@ dump(CEnv& cenv, const Code& code) const AST* compile(CEnv& cenv, const Code& parsed, Code& defs, bool& hasMain, const char* mainName) { - const AST* exp = NULL; - - // Simplify all expressions - Code simplified; - 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); + // 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); + } - // Lift all expressions - Code lifted; - 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); - - // Depoly all expressions - Code concrete; - for (Code::const_iterator i = lifted.begin(); i != lifted.end(); ++i) - if ((exp = resp_depoly(cenv, concrete, *i))) - concrete.push_back(exp); - if (cenv.args.find("-D") != cenv.args.end()) { - return dump(cenv, concrete); + 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<Code> 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 = concrete.begin(); i != concrete.end(); ++i) { + 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")))) { |