rename env and ct_env (#419)

* change env to types, ct_env to values in type checker, change env to values in interpreter

* fix bazeliskrc, change to use latest (#420)
This commit is contained in:
Jeremy G. Siek
2021-03-30 13:41:24 -04:00
committed by GitHub
parent c7b93c4477
commit 3d02412f2f
4 changed files with 168 additions and 163 deletions
@@ -154,8 +154,8 @@ void KillObject(Address address) {
}
}
void PrintEnv(Env env, std::ostream& out) {
for (const auto& [name, value] : env) {
void PrintEnv(Env values, std::ostream& out) {
for (const auto& [name, value] : values) {
out << name << ": ";
PrintValue(state->heap[value], out);
out << ", ";
@@ -196,7 +196,7 @@ void PrintHeap(const std::vector<const Value*>& heap, std::ostream& out) {
auto CurrentEnv(State* state) -> Env {
Frame* frame = state->stack.Top();
return frame->scopes.Top()->env;
return frame->scopes.Top()->values;
}
void PrintState(std::ostream& out) {
@@ -206,7 +206,7 @@ void PrintState(std::ostream& out) {
out << std::endl << "heap: ";
PrintHeap(state->heap, out);
if (!state->stack.IsEmpty() && !state->stack.Top()->scopes.IsEmpty()) {
out << std::endl << "env: ";
out << std::endl << "values: ";
PrintEnv(CurrentEnv(state), out);
}
out << std::endl << "}" << std::endl;
@@ -328,8 +328,8 @@ auto StructDeclaration::InitGlobals(Env& globals) const -> void {
}
auto FunctionDeclaration::InitGlobals(Env& globals) const -> void {
Env env;
auto pt = InterpExp(env, definition->param_pattern);
Env values;
auto pt = InterpExp(values, definition->param_pattern);
auto f = MakeFunVal(definition->name, pt, definition->body);
Address a = AllocateValue(f);
globals.Set(definition->name, a);
@@ -353,15 +353,15 @@ void CallFunction(int line_num, std::vector<const Value*> operas,
case ValKind::FunV: {
// Bind arguments to parameters
std::list<std::string> params;
std::optional<Env> env_with_matches = PatternMatch(
std::optional<Env> matches = PatternMatch(
operas[0]->u.fun.param, operas[1], globals, &params, line_num);
if (!env_with_matches) {
if (!matches) {
std::cerr << "internal error in call_function, pattern match failed"
<< std::endl;
exit(-1);
}
// Create the new frame and push it on the stack
auto* scope = new Scope(*env_with_matches, params);
auto* scope = new Scope(*matches, params);
auto* frame = new Frame(*operas[0]->u.fun.name, Stack(scope),
Stack(MakeStmtAct(operas[0]->u.fun.body)));
state->stack.Push(frame);
@@ -393,7 +393,7 @@ void CallFunction(int line_num, std::vector<const Value*> operas,
void KillScope(int line_num, Scope* scope) {
for (const auto& l : scope->locals) {
std::optional<Address> a = scope->env.Get(l);
std::optional<Address> a = scope->values.Get(l);
if (!a) {
std::cerr << "internal error in KillScope" << std::endl;
exit(-1);
@@ -427,15 +427,15 @@ void CreateTuple(Frame* frame, Action* act, Expression* /*exp*/) {
//
// The names of the pattern variables are added to the vars parameter.
// Returns nullopt if the value doesn't match the pattern.
auto PatternMatch(const Value* p, const Value* v, Env env,
auto PatternMatch(const Value* p, const Value* v, Env values,
std::list<std::string>* vars, int line_num)
-> std::optional<Env> {
switch (p->tag) {
case ValKind::VarPatV: {
Address a = AllocateValue(CopyVal(v, line_num));
vars->push_back(*p->u.var_pat.name);
env.Set(*p->u.var_pat.name, a);
return env;
values.Set(*p->u.var_pat.name, a);
return values;
}
case ValKind::TupleV:
switch (v->tag) {
@@ -453,14 +453,15 @@ auto PatternMatch(const Value* p, const Value* v, Env env,
std::cerr << std::endl;
exit(-1);
}
std::optional<Env> env_with_matches = PatternMatch(
state->heap[elt.second], state->heap[*a], env, vars, line_num);
if (!env_with_matches) {
std::optional<Env> matches =
PatternMatch(state->heap[elt.second], state->heap[*a], values,
vars, line_num);
if (!matches) {
return std::nullopt;
}
env = *env_with_matches;
values = *matches;
} // for
return env;
return values;
}
default:
std::cerr
@@ -476,13 +477,13 @@ auto PatternMatch(const Value* p, const Value* v, Env env,
*p->u.alt.alt_name != *v->u.alt.alt_name) {
return std::nullopt;
}
std::optional<Env> env_with_matches =
PatternMatch(state->heap[p->u.alt.argument],
state->heap[v->u.alt.argument], env, vars, line_num);
if (!env_with_matches) {
std::optional<Env> matches = PatternMatch(
state->heap[p->u.alt.argument], state->heap[v->u.alt.argument],
values, vars, line_num);
if (!matches) {
return std::nullopt;
}
return *env_with_matches;
return *matches;
}
default:
std::cerr
@@ -495,20 +496,20 @@ auto PatternMatch(const Value* p, const Value* v, Env env,
case ValKind::FunctionTV:
switch (v->tag) {
case ValKind::FunctionTV: {
std::optional<Env> env_with_matches = PatternMatch(
p->u.fun_type.param, v->u.fun_type.param, env, vars, line_num);
if (!env_with_matches) {
std::optional<Env> matches = PatternMatch(
p->u.fun_type.param, v->u.fun_type.param, values, vars, line_num);
if (!matches) {
return std::nullopt;
}
return PatternMatch(p->u.fun_type.ret, v->u.fun_type.ret,
*env_with_matches, vars, line_num);
return PatternMatch(p->u.fun_type.ret, v->u.fun_type.ret, *matches,
vars, line_num);
}
default:
return std::nullopt;
}
default:
if (ValueEqual(p, v, line_num)) {
return env;
return values;
} else {
return std::nullopt;
}
@@ -925,8 +926,8 @@ void StepStmt() {
// Store the continuation's address in the frame.
continuation_frame->continuation = continuation_address;
// Bind the continuation object to the continuation variable
frame->scopes.Top()->env.Set(*stmt->u.continuation.continuation_variable,
continuation_address);
frame->scopes.Top()->values.Set(
*stmt->u.continuation.continuation_variable, continuation_address);
// Pop the continuation statement.
frame->todo.Pop();
break;
@@ -1249,17 +1250,17 @@ void HandleValue() {
const Value* v = act->results[0];
const Value* p = act->results[1];
std::optional<Env> env_with_matches =
PatternMatch(p, v, frame->scopes.Top()->env,
std::optional<Env> matches =
PatternMatch(p, v, frame->scopes.Top()->values,
&frame->scopes.Top()->locals, stmt->line_num);
if (!env_with_matches) {
if (!matches) {
std::cerr
<< stmt->line_num
<< ": internal error in variable definition, match failed"
<< std::endl;
exit(-1);
}
frame->scopes.Top()->env = *env_with_matches;
frame->scopes.Top()->values = *matches;
frame->todo.Pop(2);
}
break;
@@ -1341,12 +1342,12 @@ void HandleValue() {
} else { // try to match
auto v = act->results[0];
auto pat = act->results[clause_num + 1];
auto env = CurrentEnv(state);
auto values = CurrentEnv(state);
std::list<std::string> vars;
std::optional<Env> env_with_matches =
PatternMatch(pat, v, env, &vars, stmt->line_num);
if (env_with_matches) { // we have a match, start the body
auto* new_scope = new Scope(*env_with_matches, vars);
std::optional<Env> matches =
PatternMatch(pat, v, values, &vars, stmt->line_num);
if (matches) { // we have a match, start the body
auto* new_scope = new Scope(*matches, vars);
frame->scopes.Push(new_scope);
Statement* body_block = MakeBlock(stmt->line_num, c->second);
Action* body_act = MakeStmtAct(body_block);
@@ -1486,9 +1487,9 @@ auto InterpProgram(std::list<Declaration>* fs) -> int {
}
// Interpret an expression at compile-time.
auto InterpExp(Env env, Expression* e) -> const Value* {
auto InterpExp(Env values, Expression* e) -> const Value* {
auto todo = Stack(MakeExpAct(e));
auto* scope = new Scope(env, std::list<std::string>());
auto* scope = new Scope(values, std::list<std::string>());
auto* frame = new Frame("InterpExp", Stack(scope), todo);
state->stack = Stack(frame);