Refactor Expression accessor/mutator style (#883)

This commit is contained in:
Jon Meow
2021-10-13 16:56:27 -07:00
committed by GitHub
parent c2140c6cb9
commit 55ecb62ce5
7 changed files with 177 additions and 172 deletions
@@ -23,11 +23,10 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
source_loc, "format_str",
arena->New<ExpressionPattern>(
arena->New<StringTypeLiteral>(source_loc))))};
auto print_return =
arena->New<Return>(source_loc,
arena->New<IntrinsicExpression>(
IntrinsicExpression::IntrinsicKind::Print),
false);
auto print_return = arena->New<Return>(
source_loc,
arena->New<IntrinsicExpression>(IntrinsicExpression::Intrinsic::Print),
false);
auto print = arena->New<FunctionDeclaration>(arena->New<FunctionDefinition>(
source_loc, "Print", std::vector<GenericBinding>(),
arena->New<TuplePattern>(source_loc, print_fields),
@@ -406,7 +406,7 @@ auto Interpreter::StepLvalue() -> Transition {
// { {x :: C, E, F} :: S, H}
// -> { {E(x) :: C, E, F} :: S, H}
Address pointer = GetFromEnv(exp->source_loc(),
cast<IdentifierExpression>(*exp).Name());
cast<IdentifierExpression>(*exp).name());
Nonnull<const Value*> v = arena->New<PointerValue>(pointer);
return Done{v};
}
@@ -415,13 +415,13 @@ auto Interpreter::StepLvalue() -> Transition {
// { {e.f :: C, E, F} :: S, H}
// -> { e :: [].f :: C, E, F} :: S, H}
return Spawn{arena->New<LValAction>(
cast<FieldAccessExpression>(*exp).Aggregate())};
&cast<FieldAccessExpression>(*exp).aggregate())};
} else {
// { v :: [].f :: C, E, F} :: S, H}
// -> { { &v.f :: C, E, F} :: S, H }
Address aggregate = cast<PointerValue>(*act->results()[0]).Val();
Address field = aggregate.SubobjectAddress(
cast<FieldAccessExpression>(*exp).Field());
cast<FieldAccessExpression>(*exp).field());
return Done{arena->New<PointerValue>(field)};
}
}
@@ -430,11 +430,11 @@ auto Interpreter::StepLvalue() -> Transition {
// { {e[i] :: C, E, F} :: S, H}
// -> { e :: [][i] :: C, E, F} :: S, H}
return Spawn{
arena->New<LValAction>(cast<IndexExpression>(*exp).Aggregate())};
arena->New<LValAction>(&cast<IndexExpression>(*exp).aggregate())};
} else if (act->pos() == 1) {
return Spawn{
arena->New<ExpressionAction>(cast<IndexExpression>(*exp).Offset())};
return Spawn{arena->New<ExpressionAction>(
&cast<IndexExpression>(*exp).offset())};
} else {
// { v :: [][i] :: C, E, F} :: S, H}
// -> { { &v[i] :: C, E, F} :: S, H }
@@ -452,9 +452,8 @@ auto Interpreter::StepLvalue() -> Transition {
// H}
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
// H}
Nonnull<const Expression*> elt =
cast<TupleLiteral>(*exp).fields()[act->pos()].expression();
return Spawn{arena->New<LValAction>(elt)};
return Spawn{arena->New<LValAction>(
&cast<TupleLiteral>(*exp).fields()[act->pos()].expression())};
} else {
return Done{CreateTuple(act, exp)};
}
@@ -491,10 +490,10 @@ auto Interpreter::StepExp() -> Transition {
// { { e[i] :: C, E, F} :: S, H}
// -> { { e :: [][i] :: C, E, F} :: S, H}
return Spawn{arena->New<ExpressionAction>(
cast<IndexExpression>(*exp).Aggregate())};
&cast<IndexExpression>(*exp).aggregate())};
} else if (act->pos() == 1) {
return Spawn{
arena->New<ExpressionAction>(cast<IndexExpression>(*exp).Offset())};
return Spawn{arena->New<ExpressionAction>(
&cast<IndexExpression>(*exp).offset())};
} else {
// { { v :: [][i] :: C, E, F} :: S, H}
// -> { { v_i :: C, E, F} : S, H}
@@ -520,9 +519,8 @@ auto Interpreter::StepExp() -> Transition {
// H}
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
// H}
Nonnull<const Expression*> elt =
cast<TupleLiteral>(*exp).fields()[act->pos()].expression();
return Spawn{arena->New<ExpressionAction>(elt)};
return Spawn{arena->New<ExpressionAction>(
&cast<TupleLiteral>(*exp).fields()[act->pos()].expression())};
} else {
return Done{CreateTuple(act, exp)};
}
@@ -530,9 +528,8 @@ auto Interpreter::StepExp() -> Transition {
case Expression::Kind::StructLiteral: {
const auto& literal = cast<StructLiteral>(*exp);
if (act->pos() < static_cast<int>(literal.fields().size())) {
Nonnull<const Expression*> elt =
literal.fields()[act->pos()].expression();
return Spawn{arena->New<ExpressionAction>(elt)};
return Spawn{arena->New<ExpressionAction>(
&literal.fields()[act->pos()].expression())};
} else {
return Done{CreateStruct(literal.fields(), act->results())};
}
@@ -541,7 +538,7 @@ auto Interpreter::StepExp() -> Transition {
const auto& struct_type = cast<StructTypeLiteral>(*exp);
if (act->pos() < static_cast<int>(struct_type.fields().size())) {
return Spawn{arena->New<ExpressionAction>(
struct_type.fields()[act->pos()].expression())};
&struct_type.fields()[act->pos()].expression())};
} else {
VarValues fields;
for (size_t i = 0; i < struct_type.fields().size(); ++i) {
@@ -555,40 +552,40 @@ auto Interpreter::StepExp() -> Transition {
if (act->pos() == 0) {
// { { e.f :: C, E, F} :: S, H}
// -> { { e :: [].f :: C, E, F} :: S, H}
return Spawn{arena->New<ExpressionAction>(access.Aggregate())};
return Spawn{arena->New<ExpressionAction>(&access.aggregate())};
} else {
// { { v :: [].f :: C, E, F} :: S, H}
// -> { { v_f :: C, E, F} : S, H}
return Done{act->results()[0]->GetField(
arena, FieldPath(access.Field()), exp->source_loc())};
arena, FieldPath(access.field()), exp->source_loc())};
}
}
case Expression::Kind::IdentifierExpression: {
CHECK(act->pos() == 0);
const auto& ident = cast<IdentifierExpression>(*exp);
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
Address pointer = GetFromEnv(exp->source_loc(), ident.Name());
Address pointer = GetFromEnv(exp->source_loc(), ident.name());
return Done{heap.Read(pointer, exp->source_loc())};
}
case Expression::Kind::IntLiteral:
CHECK(act->pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
return Done{arena->New<IntValue>(cast<IntLiteral>(*exp).Val())};
return Done{arena->New<IntValue>(cast<IntLiteral>(*exp).value())};
case Expression::Kind::BoolLiteral:
CHECK(act->pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
return Done{arena->New<BoolValue>(cast<BoolLiteral>(*exp).Val())};
return Done{arena->New<BoolValue>(cast<BoolLiteral>(*exp).value())};
case Expression::Kind::PrimitiveOperatorExpression: {
const auto& op = cast<PrimitiveOperatorExpression>(*exp);
if (act->pos() != static_cast<int>(op.Arguments().size())) {
if (act->pos() != static_cast<int>(op.arguments().size())) {
// { {v :: op(vs,[],e,es) :: C, E, F} :: S, H}
// -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H}
Nonnull<const Expression*> arg = op.Arguments()[act->pos()];
Nonnull<const Expression*> arg = op.arguments()[act->pos()];
return Spawn{arena->New<ExpressionAction>(arg)};
} else {
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}
return Done{EvalPrim(op.Op(), act->results(), exp->source_loc())};
return Done{EvalPrim(op.op(), act->results(), exp->source_loc())};
}
}
case Expression::Kind::CallExpression:
@@ -596,12 +593,12 @@ auto Interpreter::StepExp() -> Transition {
// { {e1(e2) :: C, E, F} :: S, H}
// -> { {e1 :: [](e2) :: C, E, F} :: S, H}
return Spawn{arena->New<ExpressionAction>(
cast<CallExpression>(*exp).Function())};
&cast<CallExpression>(*exp).function())};
} else if (act->pos() == 1) {
// { { v :: [](e) :: C, E, F} :: S, H}
// -> { { e :: v([]) :: C, E, F} :: S, H}
return Spawn{arena->New<ExpressionAction>(
cast<CallExpression>(*exp).Argument())};
&cast<CallExpression>(*exp).argument())};
} else if (act->pos() == 2) {
// { { v2 :: v1([]) :: C, E, F} :: S, H}
// -> { {C',E',F'} :: {C, E, F} :: S, H}
@@ -637,8 +634,8 @@ auto Interpreter::StepExp() -> Transition {
case Expression::Kind::IntrinsicExpression:
CHECK(act->pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
switch (cast<IntrinsicExpression>(*exp).Intrinsic()) {
case IntrinsicExpression::IntrinsicKind::Print:
switch (cast<IntrinsicExpression>(*exp).intrinsic()) {
case IntrinsicExpression::Intrinsic::Print:
Address pointer = GetFromEnv(exp->source_loc(), "format_str");
Nonnull<const Value*> pointee = heap.Read(pointer, exp->source_loc());
CHECK(pointee->kind() == Value::Kind::StringValue);
@@ -662,12 +659,12 @@ auto Interpreter::StepExp() -> Transition {
case Expression::Kind::FunctionTypeLiteral: {
if (act->pos() == 0) {
return Spawn{arena->New<ExpressionAction>(
cast<FunctionTypeLiteral>(*exp).Parameter())};
&cast<FunctionTypeLiteral>(*exp).parameter())};
} else if (act->pos() == 1) {
// { { pt :: fn [] -> e :: C, E, F} :: S, H}
// -> { { e :: fn pt -> []) :: C, E, F} :: S, H}
return Spawn{arena->New<ExpressionAction>(
cast<FunctionTypeLiteral>(*exp).ReturnType())};
&cast<FunctionTypeLiteral>(*exp).return_type())};
} else {
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
// -> { fn pt -> rt :: {C, E, F} :: S, H}
@@ -683,7 +680,7 @@ auto Interpreter::StepExp() -> Transition {
case Expression::Kind::StringLiteral:
CHECK(act->pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
return Done{arena->New<StringValue>(cast<StringLiteral>(*exp).Val())};
return Done{arena->New<StringValue>(cast<StringLiteral>(*exp).value())};
case Expression::Kind::StringTypeLiteral: {
CHECK(act->pos() == 0);
return Done{arena->New<StringType>()};
@@ -719,8 +716,8 @@ auto Interpreter::StepPattern() -> Transition {
// H}
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
// H}
Nonnull<const Pattern*> elt = tuple.Fields()[act->pos()].pattern;
return Spawn{arena->New<PatternAction>(elt)};
return Spawn{
arena->New<PatternAction>(tuple.Fields()[act->pos()].pattern)};
} else {
std::vector<TupleElement> elements;
for (size_t i = 0; i < tuple.Fields().size(); ++i) {
@@ -435,12 +435,12 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
switch (e->kind()) {
case Expression::Kind::IndexExpression: {
auto& index = cast<IndexExpression>(*e);
auto res = TypeCheckExp(index.Aggregate(), types, values);
Nonnull<const Value*> aggregate_type = index.Aggregate()->static_type();
auto res = TypeCheckExp(&index.aggregate(), types, values);
Nonnull<const Value*> aggregate_type = index.aggregate().static_type();
switch (aggregate_type->kind()) {
case Value::Kind::TupleValue: {
auto i =
cast<IntValue>(*interpreter.InterpExp(values, index.Offset()))
cast<IntValue>(*interpreter.InterpExp(values, &index.offset()))
.Val();
std::string f = std::to_string(i);
std::optional<Nonnull<const Value*>> field_t =
@@ -461,11 +461,11 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
std::vector<TupleElement> arg_types;
auto new_types = types;
for (auto& arg : cast<TupleLiteral>(*e).fields()) {
auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
auto arg_res = TypeCheckExp(&arg.expression(), new_types, values);
new_types = arg_res.types;
new_args.push_back(FieldInitializer(arg.name(), arg.expression()));
new_args.push_back(FieldInitializer(arg.name(), &arg.expression()));
arg_types.push_back(
{.name = arg.name(), .value = arg.expression()->static_type()});
{.name = arg.name(), .value = arg.expression().static_type()});
}
SetStaticType(e, arena->New<TupleValue>(std::move(arg_types)));
return TCResult(new_types);
@@ -475,10 +475,10 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
VarValues arg_types;
auto new_types = types;
for (auto& arg : cast<StructLiteral>(*e).fields()) {
auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
auto arg_res = TypeCheckExp(&arg.expression(), new_types, values);
new_types = arg_res.types;
new_args.push_back(FieldInitializer(arg.name(), arg.expression()));
arg_types.push_back({arg.name(), arg.expression()->static_type()});
new_args.push_back(FieldInitializer(arg.name(), &arg.expression()));
arg_types.push_back({arg.name(), arg.expression().static_type()});
}
SetStaticType(e, arena->New<StructType>(std::move(arg_types)));
return TCResult(new_types);
@@ -488,11 +488,11 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
std::vector<FieldInitializer> new_args;
auto new_types = types;
for (auto& arg : struct_type.fields()) {
auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
auto arg_res = TypeCheckExp(&arg.expression(), new_types, values);
new_types = arg_res.types;
ExpectIsConcreteType(arg.expression()->source_loc(),
interpreter.InterpExp(values, arg.expression()));
new_args.push_back(FieldInitializer(arg.name(), arg.expression()));
ExpectIsConcreteType(arg.expression().source_loc(),
interpreter.InterpExp(values, &arg.expression()));
new_args.push_back(FieldInitializer(arg.name(), &arg.expression()));
}
if (struct_type.fields().empty()) {
// `{}` is the type of `{}`, just as `()` is the type of `()`.
@@ -507,57 +507,57 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
}
case Expression::Kind::FieldAccessExpression: {
auto& access = cast<FieldAccessExpression>(*e);
auto res = TypeCheckExp(access.Aggregate(), types, values);
Nonnull<const Value*> aggregate_type = access.Aggregate()->static_type();
auto res = TypeCheckExp(&access.aggregate(), types, values);
Nonnull<const Value*> aggregate_type = access.aggregate().static_type();
switch (aggregate_type->kind()) {
case Value::Kind::StructType: {
const auto& struct_type = cast<StructType>(*aggregate_type);
for (const auto& [field_name, field_type] : struct_type.fields()) {
if (access.Field() == field_name) {
if (access.field() == field_name) {
SetStaticType(&access, field_type);
return TCResult(res.types);
}
}
FATAL_COMPILATION_ERROR(access.source_loc())
<< "struct " << struct_type << " does not have a field named "
<< access.Field();
<< access.field();
}
case Value::Kind::NominalClassType: {
const auto& t_class = cast<NominalClassType>(*aggregate_type);
// Search for a field
for (auto& field : t_class.Fields()) {
if (access.Field() == field.first) {
if (access.field() == field.first) {
SetStaticType(&access, field.second);
return TCResult(res.types);
}
}
// Search for a method
for (auto& method : t_class.Methods()) {
if (access.Field() == method.first) {
if (access.field() == method.first) {
SetStaticType(&access, method.second);
return TCResult(res.types);
}
}
FATAL_COMPILATION_ERROR(e->source_loc())
<< "class " << t_class.Name() << " does not have a field named "
<< access.Field();
<< access.field();
}
case Value::Kind::TupleValue: {
const auto& tup = cast<TupleValue>(*aggregate_type);
for (const TupleElement& field : tup.Elements()) {
if (access.Field() == field.name) {
if (access.field() == field.name) {
SetStaticType(&access, field.value);
return TCResult(res.types);
}
}
FATAL_COMPILATION_ERROR(e->source_loc())
<< "tuple " << tup << " does not have a field named "
<< access.Field();
<< access.field();
}
case Value::Kind::ChoiceType: {
const auto& choice = cast<ChoiceType>(*aggregate_type);
for (const auto& vt : choice.Alternatives()) {
if (access.Field() == vt.first) {
if (access.field() == vt.first) {
SetStaticType(&access, arena->New<FunctionType>(
std::vector<GenericBinding>(),
vt.second, aggregate_type));
@@ -566,7 +566,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
}
FATAL_COMPILATION_ERROR(e->source_loc())
<< "choice " << choice.Name() << " does not have a field named "
<< access.Field();
<< access.field();
}
default:
FATAL_COMPILATION_ERROR(e->source_loc())
@@ -576,13 +576,13 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
}
case Expression::Kind::IdentifierExpression: {
auto& ident = cast<IdentifierExpression>(*e);
std::optional<Nonnull<const Value*>> type = types.Get(ident.Name());
std::optional<Nonnull<const Value*>> type = types.Get(ident.name());
if (type) {
SetStaticType(&ident, *type);
return TCResult(types);
} else {
FATAL_COMPILATION_ERROR(e->source_loc())
<< "could not find `" << ident.Name() << "`";
<< "could not find `" << ident.name() << "`";
}
}
case Expression::Kind::IntLiteral:
@@ -596,13 +596,13 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
std::vector<Nonnull<Expression*>> es;
std::vector<Nonnull<const Value*>> ts;
auto new_types = types;
for (Nonnull<Expression*> argument : op.Arguments()) {
for (Nonnull<Expression*> argument : op.arguments()) {
auto res = TypeCheckExp(argument, types, values);
new_types = res.types;
es.push_back(argument);
ts.push_back(argument->static_type());
}
switch (op.Op()) {
switch (op.op()) {
case Operator::Neg:
ExpectExactType(e->source_loc(), "negation", arena->New<IntType>(),
ts[0]);
@@ -664,18 +664,18 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
}
case Expression::Kind::CallExpression: {
auto& call = cast<CallExpression>(*e);
auto fun_res = TypeCheckExp(call.Function(), types, values);
switch (call.Function()->static_type()->kind()) {
auto fun_res = TypeCheckExp(&call.function(), types, values);
switch (call.function().static_type()->kind()) {
case Value::Kind::FunctionType: {
const auto& fun_t =
cast<FunctionType>(*call.Function()->static_type());
auto arg_res = TypeCheckExp(call.Argument(), fun_res.types, values);
cast<FunctionType>(*call.function().static_type());
auto arg_res = TypeCheckExp(&call.argument(), fun_res.types, values);
auto parameter_type = fun_t.Param();
auto return_type = fun_t.Ret();
if (!fun_t.Deduced().empty()) {
auto deduced_args = ArgumentDeduction(
e->source_loc(), TypeEnv(arena), parameter_type,
call.Argument()->static_type());
call.argument().static_type());
for (auto& deduced_param : fun_t.Deduced()) {
// TODO: change the following to a CHECK once the real checking
// has been added to the type checking of function signatures.
@@ -689,7 +689,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
return_type = Substitute(deduced_args, return_type);
} else {
ExpectType(e->source_loc(), "call", parameter_type,
call.Argument()->static_type());
call.argument().static_type());
}
SetStaticType(&call, return_type);
return TCResult(arg_res.types);
@@ -704,10 +704,10 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
}
case Expression::Kind::FunctionTypeLiteral: {
auto& fn = cast<FunctionTypeLiteral>(*e);
ExpectIsConcreteType(fn.Parameter()->source_loc(),
interpreter.InterpExp(values, fn.Parameter()));
ExpectIsConcreteType(fn.ReturnType()->source_loc(),
interpreter.InterpExp(values, fn.ReturnType()));
ExpectIsConcreteType(fn.parameter().source_loc(),
interpreter.InterpExp(values, &fn.parameter()));
ExpectIsConcreteType(fn.return_type().source_loc(),
interpreter.InterpExp(values, &fn.return_type()));
SetStaticType(&fn, arena->New<TypeType>());
return TCResult(types);
}
@@ -715,8 +715,8 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
SetStaticType(e, arena->New<StringType>());
return TCResult(types);
case Expression::Kind::IntrinsicExpression:
switch (cast<IntrinsicExpression>(*e).Intrinsic()) {
case IntrinsicExpression::IntrinsicKind::Print:
switch (cast<IntrinsicExpression>(*e).intrinsic()) {
case IntrinsicExpression::Intrinsic::Print:
SetStaticType(e, TupleValue::Empty());
return TCResult(types);
}