From ac6eacd80b0cad0e01106a9bd4c4876ee174eccb Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 30 Mar 2026 20:02:41 +0300 Subject: [PATCH] Add a hacky implememntation of global constants --- libs/ast/include/pslang/ast/identifier.hpp | 2 ++ libs/ast/source/resolve_identifiers.cpp | 14 +++++++++++++- libs/ast/source/type_check.cpp | 6 +++++- libs/ir/source/compiler.cpp | 7 ++++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/libs/ast/include/pslang/ast/identifier.hpp b/libs/ast/include/pslang/ast/identifier.hpp index 8cd3e7d..c8334f6 100644 --- a/libs/ast/include/pslang/ast/identifier.hpp +++ b/libs/ast/include/pslang/ast/identifier.hpp @@ -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; diff --git a/libs/ast/source/resolve_identifiers.cpp b/libs/ast/source/resolve_identifiers.cpp index a33d402..4e96102 100644 --- a/libs/ast/source/resolve_identifiers.cpp +++ b/libs/ast/source/resolve_identifiers.cpp @@ -22,6 +22,7 @@ namespace pslang::ast std::unordered_map foreign_functions; std::unordered_map structs; std::unordered_map variables; + std::unordered_map 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) diff --git a/libs/ast/source/type_check.cpp b/libs/ast/source/type_check.cpp index 038a721..f3ed367 100644 --- a/libs/ast/source/type_check.cpp +++ b/libs/ast/source/type_check.cpp @@ -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; } diff --git a/libs/ir/source/compiler.cpp b/libs/ir/source/compiler.cpp index 250871f..6daf306 100644 --- a/libs/ir/source/compiler.cpp +++ b/libs/ir/source/compiler.cpp @@ -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); }