Skip to content

Commit

Permalink
a tentative approach to enabling language options (morganstanley#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
kthielen authored Feb 4, 2019
1 parent 05dfe68 commit 96139e0
Show file tree
Hide file tree
Showing 15 changed files with 2,869 additions and 2,668 deletions.
28 changes: 21 additions & 7 deletions bin/hi/evaluator.C
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void bindArguments(hobbes::cc& ctx, const Args::NameVals& args) {
}

// set up the evaluation environment for our cc
evaluator::evaluator(const Args& args) : silent(args.silent), wwwd(0) {
evaluator::evaluator(const Args& args) : silent(args.silent), wwwd(0), opts(args.opts) {
using namespace hobbes;

bindArguments(this->ctx, args.scriptNameVals);
Expand Down Expand Up @@ -124,7 +124,7 @@ void evaluator::loadModule(const std::string& mfile) {
}

void evaluator::evalExpr(const std::string& expr) {
std::pair<std::string, hobbes::ExprPtr> ed = this->ctx.readExprDefn(expr);
std::pair<std::string, hobbes::ExprPtr> ed = readExprDefn(expr);

if (!ed.first.empty()) {
this->ctx.define(ed.first, ed.second);
Expand All @@ -139,7 +139,7 @@ void evaluator::evalExpr(const std::string& expr) {

void evaluator::printUnsweetenedExpr(const std::string& expr) {
std::cout << setfgc(colors.unsweetfg)
<< hobbes::showAnnotated(this->ctx.unsweetenExpression(this->ctx.readExpr(expr)))
<< hobbes::showAnnotated(this->ctx.unsweetenExpression(readExpr(expr)))
<< std::endl;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ void printQualType(const hobbes::Constraints& cs, const hobbes::MonoTypePtr& ty)
}

void evaluator::printTypeOf(const std::string& expr, bool showHiddenTCs) {
hobbes::QualTypePtr t = this->ctx.unsweetenExpression(this->ctx.readExpr(expr))->type();
hobbes::QualTypePtr t = this->ctx.unsweetenExpression(readExpr(expr))->type();
hobbes::Constraints cs = showHiddenTCs ? t->constraints() : hobbes::expandHiddenTCs(this->ctx.typeEnv(), t->constraints());
hobbes::QualTypePtr st = hobbes::simplifyVarNames(hobbes::qualtype(cs, t->monoType()));

Expand Down Expand Up @@ -209,7 +209,7 @@ void evaluator::printAssembly(const std::string& expr, void (*f)(void*,size_t))

void evaluator::perfTestExpr(const std::string& expr) {
typedef void (*pvthunk)();
pvthunk f = this->ctx.compileFn<void()>(this->ctx.readExpr("let x = (" + expr + ") in ()"));
pvthunk f = this->ctx.compileFn<void()>(readExpr("let x = (" + expr + ") in ()"));
f();

const size_t numRuns = 1000;
Expand Down Expand Up @@ -237,12 +237,12 @@ void evaluator::breakdownEvalExpr(const std::string& expr) {
long t0;

t0 = hobbes::tick();
this->ctx.unsweetenExpression(this->ctx.readExpr(pexpr));
this->ctx.unsweetenExpression(readExpr(pexpr));
long ust = hobbes::tick() - t0;

t0 = hobbes::tick();
typedef void (*pvthunk)();
pvthunk f = this->ctx.compileFn<void()>(this->ctx.readExpr(pexpr));
pvthunk f = this->ctx.compileFn<void()>(readExpr(pexpr));
long ct = hobbes::tick() - t0;

t0 = hobbes::tick();
Expand Down Expand Up @@ -300,5 +300,19 @@ void evaluator::searchDefs(const std::string& expr_to_type) {
}
}

void evaluator::setOption(const std::string& o) {
this->opts.push_back(o);
}

hobbes::ExprPtr evaluator::readExpr(const std::string& x) {
return hobbes::translateExprWithOpts(this->opts, this->ctx.readExpr(x));
}

std::pair<std::string, hobbes::ExprPtr> evaluator::readExprDefn(const std::string& x) {
auto p = this->ctx.readExprDefn(x);
p.second = hobbes::translateExprWithOpts(this->opts, p.second);
return p;
}

}

8 changes: 8 additions & 0 deletions bin/hi/evaluator.H
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef std::vector<std::string> ModuleFiles;

struct Args {
typedef std::map<std::string, std::string> NameVals;
typedef std::vector<std::string> strs;

ModuleFiles mfiles;
std::string evalExpr;
Expand All @@ -22,6 +23,7 @@ struct Args {
bool exitAfterEval;
NameVals scriptNameVals;
bool machineREPL; // should we structure console I/O for machine-reading?
strs opts;

Args() : useDefColors(false), silent(false), replPort(-1), httpdPort(-1), exitAfterEval(false), machineREPL(false) {
}
Expand Down Expand Up @@ -54,10 +56,16 @@ public:
void searchDefs(const std::string& expr_to_type);
void resetREPLCycle();
bool satisfied(const hobbes::ConstraintPtr&);

void setOption(const std::string&);
private:
hobbes::cc ctx;
bool silent;
WWWServer* wwwd;
Args::strs opts;

hobbes::ExprPtr readExpr(const std::string&);
std::pair<std::string, hobbes::ExprPtr> readExprDefn(const std::string&);
};

}
Expand Down
14 changes: 12 additions & 2 deletions bin/hi/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void showShellHelp() {
cds.push_back(CmdDesc(":z E", "Evaluate E and show a breakdown of compilation/evaluation time"));
cds.push_back(CmdDesc(":c N", "Describe the type class named N"));
cds.push_back(CmdDesc(":i N", "Show instances and instance generators for the type class N"));
cds.push_back(CmdDesc(":o K", "Enable language option K"));
showShellHelp(cds);
}

Expand Down Expand Up @@ -305,7 +306,6 @@ void evalLine(char* x) {
return;
}

// should we save or load a file?
if (line.size() > 2) {
std::string cmd = line.substr(0, 2);

Expand All @@ -318,6 +318,9 @@ void evalLine(char* x) {
} else if (cmd == ":i") {
eval->showInstances(str::trim(line.substr(2)));
return;
} else if (cmd == ":o") {
eval->setOption(str::trim(line.substr(2)));
return;
}
}

Expand Down Expand Up @@ -551,13 +554,14 @@ using namespace hi;
void printUsage() {
std::cout << "hi : an interactive interpreter for hobbes" << std::endl
<< std::endl
<< "usage: hi [-p port] [-w port] [-e expr] [-s] [-x] [-a name=val]* [file+]" << std::endl
<< "usage: hi [-p port] [-w port] [-e expr] [-s] [-x] [-o opt] [-a name=val]* [file+]" << std::endl
<< std::endl
<< " -p : run a REPL server on <port>" << std::endl
<< " -w : run a web server on <port>" << std::endl
<< " -e : evaluate <expr>" << std::endl
<< " -s : run in 'silent' mode without normal formatting" << std::endl
<< " -x : exit after input scripts are evaluated" << std::endl
<< " -o opt : enable language option 'opt'" << std::endl
<< " -a name=val : add a name/val pair to the set of arguments passed to subsequent scripts" << std::endl
<< " files : hobbes script files to evaluate" << std::endl
<< std::endl;
Expand All @@ -582,6 +586,8 @@ Args processCommandLine(int argc, char** argv) {
m = 3;
} else if (arg == "-a") {
m = 4;
} else if (arg == "-o") {
m = 5;
} else if (arg == "-c" || arg == "--color") {
r.useDefColors = true;
} else if (arg == "-s") {
Expand Down Expand Up @@ -615,6 +621,10 @@ Args processCommandLine(int argc, char** argv) {
m = 0;
break;
}
case 5:
r.opts.push_back(arg);
m = 0;
break;
}
}
}
Expand Down
Loading

0 comments on commit 96139e0

Please sign in to comment.