Use string values instead of string pointers in Expression. (#591)

This commit is contained in:
Geoff Romer
2021-06-24 11:01:13 -07:00
committed by GitHub
parent 9bdabda851
commit 85cfb3b930
4 changed files with 32 additions and 35 deletions
@@ -624,10 +624,10 @@ void StepLvalue() {
// { {x :: C, E, F} :: S, H}
// -> { {E(x) :: C, E, F} :: S, H}
std::optional<Address> pointer =
CurrentEnv(state).Get(*(exp->GetVariable().name));
CurrentEnv(state).Get(exp->GetVariable().name);
if (!pointer) {
std::cerr << exp->line_num << ": could not find `"
<< *(exp->GetVariable().name) << "`" << std::endl;
<< exp->GetVariable().name << "`" << std::endl;
exit(-1);
}
const Value* v = Value::MakePtrVal(*pointer);
@@ -721,10 +721,10 @@ void StepExp() {
case ExpressionKind::Variable: {
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
std::optional<Address> pointer =
CurrentEnv(state).Get(*(exp->GetVariable().name));
CurrentEnv(state).Get(exp->GetVariable().name);
if (!pointer) {
std::cerr << exp->line_num << ": could not find `"
<< *(exp->GetVariable().name) << "`" << std::endl;
<< exp->GetVariable().name << "`" << std::endl;
exit(-1);
}
const Value* pointee = state->heap.Read(*pointer, exp->line_num);
@@ -1082,7 +1082,7 @@ void HandleValue() {
// -> { { &v.f :: C, E, F} :: S, H }
const Value* str = act->results[0];
Address a = GetMember(ValToPtr(str, exp->line_num),
*exp->GetFieldAccess().field, exp->line_num);
exp->GetFieldAccess().field, exp->line_num);
frame->todo.Pop(2);
frame->todo.Push(MakeValAct(Value::MakePtrVal(a)));
break;
@@ -1135,7 +1135,7 @@ void HandleValue() {
const Expression* exp = act->u.exp;
switch (exp->tag()) {
case ExpressionKind::PatternVariable: {
auto v = Value::MakeVarPatVal(*exp->GetPatternVariable().name,
auto v = Value::MakeVarPatVal(exp->GetPatternVariable().name,
act->results[0]);
frame->todo.Pop(2);
frame->todo.Push(MakeValAct(v));
@@ -1194,7 +1194,7 @@ void HandleValue() {
// { { v :: [].f :: C, E, F} :: S, H}
// -> { { v_f :: C, E, F} : S, H}
auto a = GetMember(ValToPtr(act->results[0], exp->line_num),
*exp->GetFieldAccess().field, exp->line_num);
exp->GetFieldAccess().field, exp->line_num);
const Value* element = state->heap.Read(a, exp->line_num);
frame->todo.Pop(2);
frame->todo.Push(MakeValAct(element));
+16 -17
View File
@@ -157,10 +157,9 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
} else if (expected) {
ExpectType(e->line_num, "pattern variable", t, expected);
}
auto new_e =
Expression::MakeVarPat(e->line_num, *e->GetPatternVariable().name,
ReifyType(t, e->line_num));
types.Set(*e->GetPatternVariable().name, t);
auto new_e = Expression::MakeVarPat(
e->line_num, e->GetPatternVariable().name, ReifyType(t, e->line_num));
types.Set(e->GetPatternVariable().name, t);
return TCResult(new_e, t, types);
}
case ExpressionKind::Index: {
@@ -241,30 +240,30 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
case ValKind::StructTV:
// Search for a field
for (auto& field : *t->GetStructType().fields) {
if (*e->GetFieldAccess().field == field.first) {
if (e->GetFieldAccess().field == field.first) {
const Expression* new_e = Expression::MakeGetField(
e->line_num, res.exp, *e->GetFieldAccess().field);
e->line_num, res.exp, e->GetFieldAccess().field);
return TCResult(new_e, field.second, res.types);
}
}
// Search for a method
for (auto& method : *t->GetStructType().methods) {
if (*e->GetFieldAccess().field == method.first) {
if (e->GetFieldAccess().field == method.first) {
const Expression* new_e = Expression::MakeGetField(
e->line_num, res.exp, *e->GetFieldAccess().field);
e->line_num, res.exp, e->GetFieldAccess().field);
return TCResult(new_e, method.second, res.types);
}
}
std::cerr << e->line_num << ": compilation error, struct "
<< *t->GetStructType().name
<< " does not have a field named "
<< *e->GetFieldAccess().field << std::endl;
<< e->GetFieldAccess().field << std::endl;
exit(-1);
case ValKind::TupleV:
for (const TupleElement& field : *t->GetTuple().elements) {
if (*e->GetFieldAccess().field == field.name) {
if (e->GetFieldAccess().field == field.name) {
auto new_e = Expression::MakeGetField(e->line_num, res.exp,
*e->GetFieldAccess().field);
e->GetFieldAccess().field);
return TCResult(new_e,
state->heap.Read(field.address, e->line_num),
res.types);
@@ -273,14 +272,14 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
std::cerr << e->line_num << ": compilation error, struct "
<< *t->GetStructType().name
<< " does not have a field named "
<< *e->GetFieldAccess().field << std::endl;
<< e->GetFieldAccess().field << std::endl;
exit(-1);
case ValKind::ChoiceTV:
for (auto vt = t->GetChoiceType().alternatives->begin();
vt != t->GetChoiceType().alternatives->end(); ++vt) {
if (*e->GetFieldAccess().field == vt->first) {
if (e->GetFieldAccess().field == vt->first) {
const Expression* new_e = Expression::MakeGetField(
e->line_num, res.exp, *e->GetFieldAccess().field);
e->line_num, res.exp, e->GetFieldAccess().field);
auto fun_ty = Value::MakeFunTypeVal(vt->second, t);
return TCResult(new_e, fun_ty, res.types);
}
@@ -288,7 +287,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
std::cerr << e->line_num << ": compilation error, struct "
<< *t->GetStructType().name
<< " does not have a field named "
<< *e->GetFieldAccess().field << std::endl;
<< e->GetFieldAccess().field << std::endl;
exit(-1);
default:
@@ -301,12 +300,12 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
}
}
case ExpressionKind::Variable: {
std::optional<const Value*> type = types.Get(*(e->GetVariable().name));
std::optional<const Value*> type = types.Get(e->GetVariable().name);
if (type) {
return TCResult(e, *type, types);
} else {
std::cerr << e->line_num << ": could not find `"
<< *(e->GetVariable().name) << "`" << std::endl;
<< e->GetVariable().name << "`" << std::endl;
exit(-1);
}
}