From a29cb23c5517fd4f8f5524fdf38b67ff5fe4a902 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 15:02:01 +0000 Subject: feat(parser): parse EXTERN "C" declarations and INCLUDE_OBJ Co-Authored-By: Claude Opus 5.5 --- include/cps/AST.h | 8 ++++++++ include/cps/FunctionAST.h | 13 +++++++++++++ include/cps/Lexer.h | 3 +++ include/cps/Parser.h | 1 + 4 files changed, 25 insertions(+) (limited to 'include') diff --git a/include/cps/AST.h b/include/cps/AST.h index 8c8c3be..2b78aaa 100644 --- a/include/cps/AST.h +++ b/include/cps/AST.h @@ -107,6 +107,14 @@ public: const std::string &getPath() const { return Path; } }; +// INCLUDE_OBJ "path" adds an object file (or the built-in "__libc") to the link. +class IncludeObjStmtAST : public StmtAST { + std::string Path; +public: + explicit IncludeObjStmtAST(std::string Path) : Path(std::move(Path)) {} + const std::string &getPath() const { return Path; } +}; + class GlobalBlockAST : public StmtAST { std::vector> Body; public: diff --git a/include/cps/FunctionAST.h b/include/cps/FunctionAST.h index 9995a4c..716f004 100644 --- a/include/cps/FunctionAST.h +++ b/include/cps/FunctionAST.h @@ -33,6 +33,7 @@ class PrototypeAST { bool IsOverload; std::string OwnerClass; bool IsPublic; + bool IsVariadic = false; public: PrototypeAST(const std::string &Name, @@ -47,6 +48,8 @@ public: const std::string &getReturnType() const { return ReturnType; } bool isOverload() const { return IsOverload; } bool isExternal() const { return IsExternal; } + bool isVariadic() const { return IsVariadic; } + void setVariadic(bool V) { IsVariadic = V; } const std::string &getOwnerClass() const { return OwnerClass; } bool isPublic() const { return IsPublic; } std::string getLinkName() const { @@ -68,6 +71,16 @@ public: const std::vector> &getBody() const { return Body; } }; +// EXTERN "C" EXTERN_FUNCTION/EXTERN_PROCEDURE: a body-less C prototype whose +// name is the exact C symbol. +class ExternDeclAST : public StmtAST { + std::unique_ptr Proto; + +public: + explicit ExternDeclAST(std::unique_ptr Proto) : Proto(std::move(Proto)) {} + PrototypeAST *getProto() const { return Proto.get(); } +}; + class CallExprAST : public ExprAST { std::string Callee; std::vector> Args; diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h index 40f168e..e2f5fdc 100644 --- a/include/cps/Lexer.h +++ b/include/cps/Lexer.h @@ -103,6 +103,9 @@ enum Token { tok_new = -613, tok_delete = -614, tok_include = -650, tok_global = -651, tok_endglobal = -652, + tok_include_obj = -653, tok_extern = -654, + tok_extern_function = -655, tok_extern_procedure = -656, + tok_ellipsis = -657, // ... tok_try = -700, tok_catch = -701, diff --git a/include/cps/Parser.h b/include/cps/Parser.h index d9e8358..f877ae6 100644 --- a/include/cps/Parser.h +++ b/include/cps/Parser.h @@ -75,6 +75,7 @@ class Parser { std::unique_ptr ParseFunction(const std::string &OwnerClass = {}, bool IsPublic = true, bool IsOverload = false); std::unique_ptr ParseProcedure(const std::string &OwnerClass = {}, bool IsPublic = true, bool IsOverload = false); + std::unique_ptr ParseExternDecl(); std::unique_ptr ParseCallStmt(); std::unique_ptr ParseReturnStmt(); std::optional> ParsePrototypeArgs(bool AllowByRef); -- cgit v1.3.1-4-g156e