From e2c65706e61be09ddb7eeb28cd1957acd4317bcf Mon Sep 17 00:00:00 2001 From: _ov4 Date: Fri, 16 Oct 2026 10:09:47 +0000 Subject: feat(constants): implement literal constants with lexical scope Co-Authored-By: Claude Fable 5.1 --- include/cps/AST.h | 10 ++++++++++ include/cps/CodeGen.h | 11 +++++++++++ include/cps/FunctionGen.h | 2 +- include/cps/Lexer.h | 1 + include/cps/Parser.h | 1 + include/cps/TypeSystem.h | 2 ++ 6 files changed, 26 insertions(+), 1 deletion(-) (limited to 'include/cps') 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 Value; +public: + ConstantStmtAST(std::string Name, std::unique_ptr 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 #include +#include #include namespace cps { @@ -27,6 +28,16 @@ class CodeGen { std::unique_ptr TheModule; std::unique_ptr> Builder; std::map Symbols; + struct ConstantScope { + unsigned ID; + std::map> Saved; + }; + std::vector ConstantScopes = {{0, {}}}; + unsigned NextScopeID = 1; + void emitScopedBlock(const std::vector> &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 Types; std::unique_ptr 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 &StmtEmitter); + llvm::Function *emitFunctionDef(FunctionDefAST *FuncAST, const std::function &StmtEmitter, unsigned ScopeID); llvm::Value *emitCallExpr(CallExprAST *Call, const std::vector &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 ParseForStmt(); std::unique_ptr ParseDeclare(); + std::unique_ptr ParseConstant(); std::unique_ptr ParseAssignStmt(); bool ParseDesignatorSuffix(std::vector &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 { -- cgit v1.3.1-4-g156e