Skip to content

Commit

Permalink
Sketching evaluation interface in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ajreynol committed Jul 27, 2023
1 parent 4b7b0f3 commit b2d2af6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 33 deletions.
7 changes: 6 additions & 1 deletion src/compiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ Expr TypeChecker::run_getTypeInternal(Expr& hdType, std::vector<Expr>& args, std
return nullptr;
}

Expr TypeChecker::run_evaluate(Expr& hd, std::vector<Expr>& args)
Expr TypeChecker::run_evaluate(Expr& e, Ctx& ctx)
{
return nullptr;
}

Expr TypeChecker::run_evaluateProgram(Expr& hd, std::vector<Expr>& args, Ctx& ctx)
{
return nullptr;
}
Expand Down
28 changes: 17 additions & 11 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,23 @@ Compiler::Compiler(State& s) : d_state(s), d_nscopes(0), d_global(d_decl, d_init
// TODO: write error?
d_tcEnd << " return nullptr;" << std::endl;
d_tcEnd << "}" << std::endl;
d_eval << "Expr TypeChecker::run_evaluate(Expr& e, std::vector<Expr>& args)" << std::endl;
d_eval << "Expr TypeChecker::run_evaluate(Expr& e, Ctx& ctx)" << std::endl;
d_eval << "{" << std::endl;
d_eval << " std::map<ExprValue*, size_t>::iterator itr = _runId.find(e.get());" << std::endl;
d_eval << " switch(itr->second)" << std::endl;
d_eval << " {" << std::endl;
d_evalEnd << " default: break;" << std::endl;
d_evalEnd << " }" << std::endl;
// otherwise just return itself (unevaluated)
d_evalEnd << " std::vector<Expr> eargs;" << std::endl;
d_evalEnd << " eargs.push_back(e);" << std::endl;
d_evalEnd << " eargs.insert(eargs.end(), args.begin(), args.end());" << std::endl;
d_evalEnd << " return d_state.mkExprInternal(Kind::APPLY, eargs);" << std::endl;
d_evalEnd << " return nullptr;" << std::endl;
d_evalEnd << "}" << std::endl;
d_evalp << "Expr TypeChecker::run_evaluateProgram(Expr& e, std::vector<Expr>& args, Ctx& ctx)" << std::endl;
d_evalp << "{" << std::endl;
d_evalp << " std::map<ExprValue*, size_t>::iterator itr = _runId.find(e.get());" << std::endl;
d_evalp << " switch(itr->second)" << std::endl;
d_evalp << " {" << std::endl;
d_evalpEnd << " default: break;" << std::endl;
d_evalpEnd << " }" << std::endl;
// otherwise just return itself (unevaluated)
d_evalpEnd << " std::vector<Expr> eargs;" << std::endl;
d_evalpEnd << " eargs.push_back(e);" << std::endl;
d_evalpEnd << " eargs.insert(eargs.end(), args.begin(), args.end());" << std::endl;
d_evalpEnd << " return d_state.mkExprInternal(Kind::APPLY, eargs);" << std::endl;
d_evalpEnd << "}" << std::endl;
}

Compiler::~Compiler(){}
Expand Down Expand Up @@ -533,6 +537,8 @@ std::string Compiler::toString()
ss << d_tcEnd.str() << std::endl;
ss << d_eval.str();
ss << d_evalEnd.str() << std::endl;
ss << d_evalp.str();
ss << d_evalpEnd.str() << std::endl;
ss << "}" << std::endl;
return ss.str();
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class Compiler
*/
std::stringstream d_eval;
std::stringstream d_evalEnd;
/**
* Code to be called for evaluating programs, returns the case
*/
std::stringstream d_evalp;
std::stringstream d_evalpEnd;
/** Identifier counts */
CompilerScope d_global;
/**
Expand Down
9 changes: 7 additions & 2 deletions src/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ void ExprValue::computeFlags()
else
{
visit.pop_back();
if (cur->getKind()==Kind::APPLY &&
children[0]->getKind()==Kind::PROGRAM_CONST)
Kind ck = cur->getKind();
if (ck==Kind::APPLY && children[0]->getKind()==Kind::PROGRAM_CONST)
{
cur->setFlag(Flag::IS_EVAL, true);
}
else if (ck==Kind::REQUIRES_TYPE)
{
// requires type may evaluate
cur->setFlag(Flag::IS_EVAL, true);
}
for (Expr& c : children)
{
if (c->getFlag(Flag::IS_NON_GROUND))
Expand Down
2 changes: 1 addition & 1 deletion src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ void State::defineProgram(const Expr& v, const Expr& prog)
}
}

Expr State::evaluate(const std::vector<Expr>& children, Ctx& newCtx)
Expr State::evaluateProgram(const std::vector<Expr>& children, Ctx& newCtx)
{
Expr hd = children[0];
std::map<Expr, Expr>::iterator it = d_programs.find(hd);
Expand Down
2 changes: 1 addition & 1 deletion src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class State
/** Define program */
void defineProgram(const Expr& v, const Expr& prog);
/** Maybe evaluate */
Expr evaluate(const std::vector<Expr>& children, Ctx& newCtx);
Expr evaluateProgram(const std::vector<Expr>& children, Ctx& newCtx);
private:
/** */
ExprInfo* getOrMkInfo(const ExprValue* e);
Expand Down
41 changes: 25 additions & 16 deletions src/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Expr TypeChecker::getTypeInternal(Expr& e, std::ostream* out)
// if compiled, run the compiled version of the type checker
if (hdType->isCompiled())
{
std::cout << "RUN type check " << hdType << std::endl;
return run_getTypeInternal(hdType, ctypes, out);
}
Ctx ctx;
Expand Down Expand Up @@ -350,7 +351,8 @@ Expr TypeChecker::evaluate(Expr& e, Ctx& ctx)
while (!visit.empty())
{
cur = visit.back();
// unevaluatable terms stay the same
// the term will stay the same if it is not evaluatable and either it
// is ground, or the context is empty.
if (!cur->isEvaluatable() && (cur->isGround() || cctx.empty()))
{
visited[cur] = cur;
Expand All @@ -373,6 +375,13 @@ Expr TypeChecker::evaluate(Expr& e, Ctx& ctx)
// TODO: error, variable not filled?
//std::cout << "WARNING: unfilled variable " << cur << std::endl;
}
// if it is compiled, we run its evaluation here
if (cur->isCompiled())
{
visited[cur] = run_evaluate(cur, cctx);
continue;
}

std::vector<Expr>& children = cur->d_children;
it = visited.find(cur);
if (it == visited.end())
Expand Down Expand Up @@ -428,28 +437,28 @@ Expr TypeChecker::evaluate(Expr& e, Ctx& ctx)
// maybe evaluate the program?
if (cchildren[0]->getKind()==Kind::PROGRAM_CONST)
{
ctxs.emplace_back();
if (cchildren[0]->isCompiled())
{
std::vector<Expr> pargs(cchildren.begin()+1, cchildren.end());
evaluated = run_evaluate(cchildren[0], pargs);
evaluated = run_evaluateProgram(cchildren[0], pargs, ctxs.back());
}
else
{
ctxs.emplace_back();
// see if we evaluate
evaluated = d_state.evaluate(cchildren, ctxs.back());
if (ctxs.back().empty())
{
// if there is no context, we don't have to push a scope
ctxs.pop_back();
}
else
{
// otherwise push an evaluation scope
newContext = true;
visits.emplace_back(std::vector<Expr>{evaluated});
visiteds.emplace_back();
}
evaluated = d_state.evaluateProgram(cchildren, ctxs.back());
}
if (ctxs.back().empty())
{
// if there is no context, we don't have to push a scope
ctxs.pop_back();
}
else
{
// otherwise push an evaluation scope
newContext = true;
visits.emplace_back(std::vector<Expr>{evaluated});
visiteds.emplace_back();
}
}
break;
Expand Down
4 changes: 3 additions & 1 deletion src/type_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class TypeChecker
/** Return its type */
Expr getTypeInternal(Expr& e, std::ostream* out);
/** Compiled version */
Expr run_evaluate(Expr& hd, std::vector<Expr>& args);
Expr run_evaluate(Expr& e, Ctx& ctx);
/** Compiled version */
Expr run_evaluateProgram(Expr& hd, std::vector<Expr>& args, Ctx& ctx);
/** The state */
State& d_state;
/** The builtin literal kinds */
Expand Down

0 comments on commit b2d2af6

Please sign in to comment.