From b2455d8dba18a52b1cdde8934492062a220a25f6 Mon Sep 17 00:00:00 2001 From: "Jeremy G. Siek" Date: Thu, 18 Mar 2021 22:19:31 -0400 Subject: [PATCH] renames ExecutionEnvironment, fixes #394 (#395) --- executable_semantics/ast/declaration.h | 38 ++++++++++--------- .../interpreter/typecheck.cpp | 32 ++++++++-------- executable_semantics/interpreter/typecheck.h | 2 +- .../syntax/syntax_helpers.cpp | 6 +-- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index 2e240a569be3..48934045cf86 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -21,17 +21,21 @@ using Address = unsigned int; using TypeEnv = Dictionary; using Env = Dictionary; -/// TODO:explain this. Also name it if necessary. Consult with jsiek. -using ExecutionEnvironment = std::pair; +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 Declaration(Model d) : box(std::make_shared>(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`, where `Content` - /// satisfies the Declaration concept. + // A base class that erases the type of a `Boxed`, 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 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; diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index d0161565cc8c..1dea406d2c80 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -658,8 +658,8 @@ auto VariableDeclaration::TypeChecked(TypeEnv env, Env ct_env) const return *this; } -auto TopLevel(std::list* fs) -> std::pair { - ExecutionEnvironment tops; +auto TopLevel(std::list* fs) -> TypeCheckContext { + TypeCheckContext tops; bool found_main = false; for (auto const& d : *fs) { @@ -677,37 +677,37 @@ auto TopLevel(std::list* fs) -> std::pair { 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 diff --git a/executable_semantics/interpreter/typecheck.h b/executable_semantics/interpreter/typecheck.h index 7aed729fcfdb..419d2b46aa7a 100644 --- a/executable_semantics/interpreter/typecheck.h +++ b/executable_semantics/interpreter/typecheck.h @@ -45,7 +45,7 @@ auto TypeCheckStmt(Statement*, TypeEnv, Env, Value*) -> TCStatement; auto TypeCheckFunDef(struct FunctionDefinition*, TypeEnv) -> struct FunctionDefinition*; -auto TopLevel(std::list* fs) -> std::pair; +auto TopLevel(std::list* fs) -> TypeCheckContext; void PrintErrorString(const std::string& s); diff --git a/executable_semantics/syntax/syntax_helpers.cpp b/executable_semantics/syntax/syntax_helpers.cpp index eb9600600e0d..713cf0673efa 100644 --- a/executable_semantics/syntax/syntax_helpers.cpp +++ b/executable_semantics/syntax/syntax_helpers.cpp @@ -21,9 +21,9 @@ void ExecProgram(std::list* fs) { std::cout << "********** type checking **********" << std::endl; } state = new State(); // Compile-time state. - std::pair 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 new_decls; for (const auto& decl : *fs) { new_decls.push_back(decl.TypeChecked(top, ct_top));