Add a hacky implememntation of global constants

This commit is contained in:
Nikita Lisitsa 2026-03-30 20:02:41 +03:00
parent 86216e8cf3
commit ac6eacd80b
4 changed files with 26 additions and 3 deletions

View file

@ -9,6 +9,7 @@ namespace pslang::ast
{
struct variable_base;
struct variable_declaration;
struct function_definition;
struct foreign_function_declaration;
@ -16,6 +17,7 @@ namespace pslang::ast
{
std::string name;
ast::location location;
variable_declaration * constant_node = nullptr;
variable_base * variable_node = nullptr;
function_definition * function_node = nullptr;
foreign_function_declaration * foreign_function_node = nullptr;

View file

@ -22,6 +22,7 @@ namespace pslang::ast
std::unordered_map<std::string, foreign_function_declaration *> foreign_functions;
std::unordered_map<std::string, struct_definition *> structs;
std::unordered_map<std::string, variable_base *> variables;
std::unordered_map<std::string, variable_declaration *> constants;
bool contains_transitive(std::string const & name) const
{
@ -29,6 +30,7 @@ namespace pslang::ast
|| (functions.count(name) > 0)
|| (foreign_functions.count(name) > 0)
|| (structs.count(name) > 0)
|| (constants.count(name) > 0)
;
}
@ -46,6 +48,7 @@ namespace pslang::ast
|| (foreign_functions.count(name) > 0)
|| (structs.count(name) > 0)
|| (variables.count(name) > 0)
|| (constants.count(name) > 0)
;
}
@ -180,6 +183,12 @@ namespace pslang::ast
return;
}
if (auto jt = it->constants.find(identifier.name); jt != it->constants.end())
{
identifier.constant_node = jt->second;
return;
}
if (!crossed_function_scope)
{
if (auto jt = it->variables.find(identifier.name); jt != it->variables.end())
@ -286,7 +295,10 @@ namespace pslang::ast
apply(*variable_declaration.type);
apply(*variable_declaration.initializer);
scopes.back().variables[variable_declaration.name] = &variable_declaration;
if (variable_declaration.category == value_category::compile_time)
scopes.back().constants[variable_declaration.name] = &variable_declaration;
else
scopes.back().variables[variable_declaration.name] = &variable_declaration;
}
void apply(if_chain const & if_chain)

View file

@ -246,7 +246,11 @@ namespace pslang::ast
void apply(identifier & node)
{
if (node.variable_node)
if (node.constant_node)
{
node.inferred_type = node.constant_node->inferred_type;
}
else if (node.variable_node)
{
node.inferred_type = node.variable_node->inferred_type;
}

View file

@ -90,7 +90,12 @@ namespace pslang::ir
node_ref apply(ast::identifier const & node)
{
if (node.variable_node)
if (node.constant_node)
{
// Temporary hack: replace reading constants with their ininitialization code
return apply(*node.constant_node->initializer);
}
else if (node.variable_node)
{
return lcontext.variables.at(node.variable_node);
}