mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
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:
@@ -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_
|
||||
|
||||
Reference in New Issue
Block a user