Split interpreter into smaller modules (#662)

- Move Heap and Frame/Scope to their own headers.
- Move some functions to more appropriate headers (e.g. CopyValue -> value.h).
- Define a separate Bazel rule for each header/cpp pair.
- Modify PrintValue to not print the frames of a ContinuationValue. This was necessary to break a dependency cycle between PrintValue, PrintFrame, and Action::Print.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
Geoff Romer
2021-07-19 13:51:35 -07:00
committed by GitHub
co-authored by Jon Meow
parent def98d1182
commit d5124256a2
11 changed files with 347 additions and 235 deletions
+63 -7
View File
@@ -8,7 +8,6 @@
#include <iostream>
#include "common/check.h"
#include "executable_semantics/interpreter/interpreter.h"
namespace Carbon {
@@ -434,16 +433,73 @@ auto PrintValue(const Value* val, std::ostream& out) -> void {
out << "choice " << val->GetChoiceType().name;
break;
case ValKind::ContinuationValue:
out << "continuation[[";
for (Frame* frame : val->GetContinuationValue().stack) {
PrintFrame(frame, out);
out << " :: ";
}
out << "]]";
out << "continuation";
// TODO: Find a way to print useful information about the continuation
// without creating a dependency cycle.
break;
}
}
auto CopyVal(const Value* val, int line_num) -> const Value* {
switch (val->tag()) {
case ValKind::TupleValue: {
std::vector<TupleElement> elements;
for (const TupleElement& element : val->GetTupleValue().elements) {
elements.push_back(
{.name = element.name, .value = CopyVal(element.value, line_num)});
}
return Value::MakeTupleValue(std::move(elements));
}
case ValKind::AlternativeValue: {
const Value* arg = CopyVal(val->GetAlternativeValue().argument, line_num);
return Value::MakeAlternativeValue(val->GetAlternativeValue().alt_name,
val->GetAlternativeValue().choice_name,
arg);
}
case ValKind::StructValue: {
const Value* inits = CopyVal(val->GetStructValue().inits, line_num);
return Value::MakeStructValue(val->GetStructValue().type, inits);
}
case ValKind::IntValue:
return Value::MakeIntValue(val->GetIntValue());
case ValKind::BoolValue:
return Value::MakeBoolValue(val->GetBoolValue());
case ValKind::FunctionValue:
return Value::MakeFunctionValue(val->GetFunctionValue().name,
val->GetFunctionValue().param,
val->GetFunctionValue().body);
case ValKind::PointerValue:
return Value::MakePointerValue(val->GetPointerValue());
case ValKind::ContinuationValue:
// Copying a continuation is "shallow".
return val;
case ValKind::FunctionType:
return Value::MakeFunctionType(
CopyVal(val->GetFunctionType().param, line_num),
CopyVal(val->GetFunctionType().ret, line_num));
case ValKind::PointerType:
return Value::MakePointerType(
CopyVal(val->GetPointerType().type, line_num));
case ValKind::IntType:
return Value::MakeIntType();
case ValKind::BoolType:
return Value::MakeBoolType();
case ValKind::TypeType:
return Value::MakeTypeType();
case ValKind::AutoType:
return Value::MakeAutoType();
case ValKind::ContinuationType:
return Value::MakeContinuationType();
case ValKind::StructType:
case ValKind::ChoiceType:
case ValKind::BindingPlaceholderValue:
case ValKind::AlternativeConstructorValue:
// TODO: These should be copied so that they don't get destructed.
return val;
}
}
auto TypeEqual(const Value* t1, const Value* t2) -> bool {
if (t1->tag() != t2->tag()) {
return false;