renames ExecutionEnvironment, fixes #394 (#395)

This commit is contained in:
Jeremy G. Siek
2021-03-18 22:19:31 -04:00
committed by GitHub
parent e440b07eb3
commit b2455d8dba
4 changed files with 40 additions and 38 deletions
+20 -18
View File
@@ -21,17 +21,21 @@ using Address = unsigned int;
using TypeEnv = Dictionary<std::string, Value*>;
using Env = Dictionary<std::string, Address>;
/// TODO:explain this. Also name it if necessary. Consult with jsiek.
using ExecutionEnvironment = std::pair<TypeEnv, Env>;
struct TypeCheckContext {
// Symbol table mapping names of runtime entities to their type.
TypeEnv types;
// Symbol table mapping names of compile time entities to their value.
Env values;
};
/// An existential AST declaration satisfying the Declaration concept.
// An existential AST declaration satisfying the Declaration concept.
class Declaration {
public: // ValueSemantic concept API.
Declaration(const Declaration& other) = default;
Declaration& operator=(const Declaration& other) = default;
/// Constructs an instance equivalent to `d`, where `Model` satisfies the
/// Declaration concept.
// Constructs an instance equivalent to `d`, where `Model` satisfies the
// Declaration concept.
template <class Model>
Declaration(Model d) : box(std::make_shared<Boxed<Model>>(d)) {}
@@ -50,13 +54,11 @@ class Declaration {
// Add an entry in the runtime global symbol table for this declaration.
void InitGlobals(Env& globals) const { return box->InitGlobals(globals); }
// Add an entry in the compile time global symbol tables for this declaration.
auto TopLevel(ExecutionEnvironment& e) const -> void {
return box->TopLevel(e);
}
auto TopLevel(TypeCheckContext& e) const -> void { return box->TopLevel(e); }
private: // types
/// A base class that erases the type of a `Boxed<Content>`, where `Content`
/// satisfies the Declaration concept.
// A base class that erases the type of a `Boxed<Content>`, where `Content`
// satisfies the Declaration concept.
struct Box {
protected:
Box() {}
@@ -70,11 +72,11 @@ class Declaration {
virtual auto Name() const -> std::string = 0;
virtual auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration = 0;
virtual auto InitGlobals(Env& globals) const -> void = 0;
virtual auto TopLevel(ExecutionEnvironment&) const -> void = 0;
virtual auto TopLevel(TypeCheckContext&) const -> void = 0;
};
/// The derived class that holds an instance of `Content` satisfying the
/// Declaration concept.
// The derived class that holds an instance of `Content` satisfying the
// Declaration concept.
template <class Content>
struct Boxed final : Box {
const Content content;
@@ -88,7 +90,7 @@ class Declaration {
auto InitGlobals(Env& globals) const -> void override {
content.InitGlobals(globals);
}
auto TopLevel(ExecutionEnvironment& e) const -> void override {
auto TopLevel(TypeCheckContext& e) const -> void override {
content.TopLevel(e);
}
};
@@ -107,7 +109,7 @@ struct FunctionDeclaration {
auto Name() const -> std::string;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
auto InitGlobals(Env& globals) const -> void;
auto TopLevel(ExecutionEnvironment&) const -> void;
auto TopLevel(TypeCheckContext&) const -> void;
};
struct StructDeclaration {
@@ -119,7 +121,7 @@ struct StructDeclaration {
auto Name() const -> std::string;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
void InitGlobals(Env& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
auto TopLevel(TypeCheckContext&) const -> void;
};
struct ChoiceDeclaration {
@@ -135,7 +137,7 @@ struct ChoiceDeclaration {
auto Name() const -> std::string;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
void InitGlobals(Env& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
auto TopLevel(TypeCheckContext&) const -> void;
};
// Global variable definition implements the Declaration concept.
@@ -152,7 +154,7 @@ class VariableDeclaration {
auto Name() const -> std::string;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
void InitGlobals(Env& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
auto TopLevel(TypeCheckContext&) const -> void;
private:
int source_location;
+16 -16
View File
@@ -658,8 +658,8 @@ auto VariableDeclaration::TypeChecked(TypeEnv env, Env ct_env) const
return *this;
}
auto TopLevel(std::list<Declaration>* fs) -> std::pair<TypeEnv, Env> {
ExecutionEnvironment tops;
auto TopLevel(std::list<Declaration>* fs) -> TypeCheckContext {
TypeCheckContext tops;
bool found_main = false;
for (auto const& d : *fs) {
@@ -677,37 +677,37 @@ auto TopLevel(std::list<Declaration>* fs) -> std::pair<TypeEnv, Env> {
return tops;
}
auto FunctionDeclaration::TopLevel(ExecutionEnvironment& tops) const -> void {
auto t = TypeOfFunDef(tops.first, tops.second, definition);
tops.first.Set(Name(), t);
auto FunctionDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
auto t = TypeOfFunDef(tops.types, tops.values, definition);
tops.types.Set(Name(), t);
}
auto StructDeclaration::TopLevel(ExecutionEnvironment& tops) const -> void {
auto st = TypeOfStructDef(&definition, tops.first, tops.second);
auto StructDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
auto st = TypeOfStructDef(&definition, tops.types, tops.values);
Address a = AllocateValue(st);
tops.second.Set(Name(), a); // Is this obsolete?
tops.values.Set(Name(), a); // Is this obsolete?
auto params = MakeTupleTypeVal(st->u.struct_type.fields);
auto fun_ty = MakeFunTypeVal(params, st);
tops.first.Set(Name(), fun_ty);
tops.types.Set(Name(), fun_ty);
}
auto ChoiceDeclaration::TopLevel(ExecutionEnvironment& tops) const -> void {
auto ChoiceDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
auto alts = new VarValues();
for (auto a : alternatives) {
auto t = ToType(line_num, InterpExp(tops.second, a.second));
auto t = ToType(line_num, InterpExp(tops.values, a.second));
alts->push_back(std::make_pair(a.first, t));
}
auto ct = MakeChoiceTypeVal(name, alts);
Address a = AllocateValue(ct);
tops.second.Set(Name(), a); // Is this obsolete?
tops.first.Set(Name(), ct);
tops.values.Set(Name(), a); // Is this obsolete?
tops.types.Set(Name(), ct);
}
// Associate the variable name with it's declared type in the
// compile-time symbol table.
auto VariableDeclaration::TopLevel(ExecutionEnvironment& tops) const -> void {
Value* declared_type = ToType(source_location, InterpExp(tops.second, type));
tops.first.Set(Name(), declared_type);
auto VariableDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
Value* declared_type = ToType(source_location, InterpExp(tops.values, type));
tops.types.Set(Name(), declared_type);
}
} // namespace Carbon
+1 -1
View File
@@ -45,7 +45,7 @@ auto TypeCheckStmt(Statement*, TypeEnv, Env, Value*) -> TCStatement;
auto TypeCheckFunDef(struct FunctionDefinition*, TypeEnv)
-> struct FunctionDefinition*;
auto TopLevel(std::list<Declaration>* fs) -> std::pair<TypeEnv, Env>;
auto TopLevel(std::list<Declaration>* fs) -> TypeCheckContext;
void PrintErrorString(const std::string& s);
@@ -21,9 +21,9 @@ void ExecProgram(std::list<Declaration>* fs) {
std::cout << "********** type checking **********" << std::endl;
}
state = new State(); // Compile-time state.
std::pair<TypeEnv, Env> p = TopLevel(fs);
TypeEnv top = p.first;
Env ct_top = p.second;
TypeCheckContext p = TopLevel(fs);
TypeEnv top = p.types;
Env ct_top = p.values;
std::list<Declaration> new_decls;
for (const auto& decl : *fs) {
new_decls.push_back(decl.TypeChecked(top, ct_top));