aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-16 10:09:47 +0000
committer_ov4 <ov4@ov4.org>2026-10-16 10:09:47 +0000
commite2c65706e61be09ddb7eeb28cd1957acd4317bcf (patch)
tree15447a03d67d395a722c5be3f0b05bc9f197197a /include
parent45c9d37eb27d204a6202b33cb37e890af0171357 (diff)
feat(constants): implement literal constants with lexical scope
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'include')
-rw-r--r--include/cps/AST.h10
-rw-r--r--include/cps/CodeGen.h11
-rw-r--r--include/cps/FunctionGen.h2
-rw-r--r--include/cps/Lexer.h1
-rw-r--r--include/cps/Parser.h1
-rw-r--r--include/cps/TypeSystem.h2
6 files changed, 26 insertions, 1 deletions
diff --git a/include/cps/AST.h b/include/cps/AST.h
index 87c9341..1afc359 100644
--- a/include/cps/AST.h
+++ b/include/cps/AST.h
@@ -105,6 +105,16 @@ public:
class ThrowStmtAST : public StmtAST {};
+class ConstantStmtAST : public StmtAST {
+ std::string Name;
+ std::unique_ptr<ExprAST> Value;
+public:
+ ConstantStmtAST(std::string Name, std::unique_ptr<ExprAST> Value)
+ : Name(std::move(Name)), Value(std::move(Value)) {}
+ const std::string &getName() const { return Name; }
+ ExprAST *getValue() const { return Value.get(); }
+};
+
class DeclareStmtAST : public StmtAST {
std::string Name;
std::string Type;
diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h
index 4259d63..2ba5e79 100644
--- a/include/cps/CodeGen.h
+++ b/include/cps/CodeGen.h
@@ -13,6 +13,7 @@
#include <map>
#include <memory>
+#include <optional>
#include <string>
namespace cps {
@@ -27,6 +28,16 @@ class CodeGen {
std::unique_ptr<llvm::Module> TheModule;
std::unique_ptr<llvm::IRBuilder<>> Builder;
std::map<std::string, SymbolInfo> Symbols;
+ struct ConstantScope {
+ unsigned ID;
+ std::map<std::string, std::optional<SymbolInfo>> Saved;
+ };
+ std::vector<ConstantScope> ConstantScopes = {{0, {}}};
+ unsigned NextScopeID = 1;
+ void emitScopedBlock(const std::vector<std::unique_ptr<StmtAST>> &Body);
+ bool prepareSymbolDeclaration(const std::string &Name, bool IsConstant = false);
+ bool requireWritable(const SymbolInfo &Info, const std::string &Name);
+ void emitConstantStmt(ConstantStmtAST *Stmt);
std::unique_ptr<TypeSystem> Types;
std::unique_ptr<ArrayHandler> Arrays;
diff --git a/include/cps/FunctionGen.h b/include/cps/FunctionGen.h
index a7cead5..80c91ea 100644
--- a/include/cps/FunctionGen.h
+++ b/include/cps/FunctionGen.h
@@ -81,7 +81,7 @@ public:
llvm::Function *emitPrototype(PrototypeAST *Proto);
- llvm::Function *emitFunctionDef(FunctionDefAST *FuncAST, const std::function<void(StmtAST*)> &StmtEmitter);
+ llvm::Function *emitFunctionDef(FunctionDefAST *FuncAST, const std::function<void(StmtAST*)> &StmtEmitter, unsigned ScopeID);
llvm::Value *emitCallExpr(CallExprAST *Call, const std::vector<llvm::Value*> &Args);
diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h
index 7f3d5b5..efedec8 100644
--- a/include/cps/Lexer.h
+++ b/include/cps/Lexer.h
@@ -14,6 +14,7 @@ enum Token {
tok_input = -4,
tok_output = -5,
+ tok_constant = -43,
tok_case = -40,
tok_endcase = -41,
tok_otherwise = -42,
diff --git a/include/cps/Parser.h b/include/cps/Parser.h
index 921480d..0079c8b 100644
--- a/include/cps/Parser.h
+++ b/include/cps/Parser.h
@@ -57,6 +57,7 @@ class Parser {
std::unique_ptr<StmtAST> ParseForStmt();
std::unique_ptr<StmtAST> ParseDeclare();
+ std::unique_ptr<StmtAST> ParseConstant();
std::unique_ptr<StmtAST> ParseAssignStmt();
bool ParseDesignatorSuffix(std::vector<DesignatorAccess> &Out);
diff --git a/include/cps/TypeSystem.h b/include/cps/TypeSystem.h
index a07c75f..b615ed8 100644
--- a/include/cps/TypeSystem.h
+++ b/include/cps/TypeSystem.h
@@ -106,6 +106,8 @@ struct SymbolInfo {
llvm::Value *Storage = nullptr;
std::string TypeName;
bool IsArray = false;
+ llvm::Constant *ConstantValue = nullptr; // Non-null for CONSTANT; no storage.
+ unsigned ScopeID = 0;
};
class TypeSystem {