From 74b6d06711afd3efbbc47436f1491900994313f3 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 06:54:32 +0000 Subject: feat(classes): allocate objects with NEW and rebind named class variables Co-Authored-By: Claude Opus 5.5 --- include/cps/CodeGen.h | 4 ++++ include/cps/Parser.h | 1 + include/cps/TypeAST.h | 11 +++++++++++ include/cps/TypeSystem.h | 5 ++++- 4 files changed, 20 insertions(+), 1 deletion(-) (limited to 'include/cps') diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index 1813d9d..a70d191 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -25,6 +25,7 @@ class FileHandler; class MemoryHandler; class ExceptionHandler; class CastExprAST; +class NewExprAST; class CodeGen { std::unique_ptr TheContext; @@ -127,6 +128,9 @@ class CodeGen { const std::vector> &ArgExprs, int Line, std::vector &Out); + llvm::Value *symbolAddress(const SymbolInfo &Info); + void emitAssignment(const LValueInfo &Target, ExprAST *Expr, + const SymbolInfo *Symbol = nullptr); void emitDeclareStmt(DeclareStmtAST *Stmt); void emitOutputValue(llvm::Value *Val, const TypeInfo *TypeInfo, bool AppendNewline = true); diff --git a/include/cps/Parser.h b/include/cps/Parser.h index 4874d7a..9a53e95 100644 --- a/include/cps/Parser.h +++ b/include/cps/Parser.h @@ -34,6 +34,7 @@ class Parser { int GetTokPrecedence(); std::unique_ptr ParseExpression(); + std::unique_ptr ParseAssignmentRHS(); std::unique_ptr ParseUnaryImpl(); std::unique_ptr ParseUnary(); std::unique_ptr ParsePrimaryImpl(); diff --git a/include/cps/TypeAST.h b/include/cps/TypeAST.h index de5d12c..a5e40a1 100644 --- a/include/cps/TypeAST.h +++ b/include/cps/TypeAST.h @@ -9,6 +9,17 @@ namespace cps { +// Only an assignment may own this expression. +class NewExprAST : public ExprAST { + std::string ClassName; + std::vector> Args; +public: + NewExprAST(std::string ClassName, std::vector> Args, int Line) + : ExprAST(Line), ClassName(std::move(ClassName)), Args(std::move(Args)) {} + const std::string &getClassName() const { return ClassName; } + const auto &getArgs() const { return Args; } +}; + class CastExprAST : public ExprAST { std::string TypeName; std::unique_ptr Operand; diff --git a/include/cps/TypeSystem.h b/include/cps/TypeSystem.h index 8bee6b9..d358794 100644 --- a/include/cps/TypeSystem.h +++ b/include/cps/TypeSystem.h @@ -66,6 +66,7 @@ struct TypeInfo { std::string Name; llvm::Type *LLVMType = nullptr; TypeKind Kind = TypeKind::Void; + bool IsClass = false; RecordPayload Record; // Kind == Record only EnumPayload Enum; // Kind == Enum only @@ -109,6 +110,7 @@ struct SymbolInfo { bool IsArray = false; llvm::Constant *ConstantValue = nullptr; // Non-null for CONSTANT; no storage. unsigned ScopeID = 0; + bool IsReference = false; // Storage is a slot containing the current object address. }; class TypeSystem { @@ -130,7 +132,8 @@ public: // order). Pass A reserves every name so pointer pointees may forward- // reference; pass B fills definitions. On failure Err carries the // diagnostic and nothing is (further) registered. - bool declareUserType(const std::string &Name, TypeKind Kind, std::string &Err); + bool declareUserType(const std::string &Name, TypeKind Kind, std::string &Err, + bool IsClass = false); bool defineEnum(const std::string &Name, const std::vector &Values, std::string &Err); -- cgit v1.3.1-4-g156e