Global variables (#378)

* global variables

* implemented type checking of global variable, added test case

* added a comment

* improvements based on Dave's suggestions

* improvements based on Jon's suggestions

* added test cases about global variable ordering
This commit is contained in:
Jeremy G. Siek
2021-03-15 16:06:27 -04:00
committed by GitHub
parent 41786eae25
commit ced98ef021
22 changed files with 822 additions and 3 deletions
+31
View File
@@ -38,10 +38,18 @@ class Declaration {
public: // Declaration concept API, in addition to ValueSemantic.
void Print() const { box->Print(); }
auto Name() const -> std::string { return box->Name(); }
// Signals a type error if the declaration is not well typed,
// otherwise returns this declaration with annotated types.
//
// - Parameter env: types of run-time names.
// - Paraemter ct_env: values of compile-time names.
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration {
return box->TypeChecked(env, ct_env);
}
// 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);
}
@@ -130,6 +138,29 @@ struct ChoiceDeclaration {
auto TopLevel(ExecutionEnvironment&) const -> void;
};
// Global variable definition implements the Declaration concept.
class VariableDeclaration {
public:
VariableDeclaration(int sourceLocation, std::string name, Expression* type,
Expression* initializer)
: sourceLocation(sourceLocation),
name(name),
type(type),
initializer(initializer) {}
void Print() const;
auto Name() const -> std::string;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
void InitGlobals(Env& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
private:
int sourceLocation;
std::string name;
Expression* type;
Expression* initializer;
};
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_