diff options
Diffstat (limited to 'include/cps')
| -rw-r--r-- | include/cps/FunctionAST.h | 28 | ||||
| -rw-r--r-- | include/cps/FunctionGen.h | 2 | ||||
| -rw-r--r-- | include/cps/Lexer.h | 7 | ||||
| -rw-r--r-- | include/cps/Parser.h | 13 | ||||
| -rw-r--r-- | include/cps/TypeAST.h | 120 |
5 files changed, 160 insertions, 10 deletions
diff --git a/include/cps/FunctionAST.h b/include/cps/FunctionAST.h index 613079c..f663867 100644 --- a/include/cps/FunctionAST.h +++ b/include/cps/FunctionAST.h @@ -1,27 +1,43 @@ #pragma once #include "cps/AST.h" +#include <cstdint> #include <vector> #include <string> #include <memory> -#include <tuple> +#include <utility> namespace cps { +enum class PassMode : uint8_t { ByVal, ByRef }; + +// One declared FUNCTION/PROCEDURE parameter. For array parameters TypeName +// is the ELEMENT type; DeclaredBounds holds literal per-dimension bounds for +// the bounded form (empty for `ARRAY OF T`, which is rank 1 with the bounds +// travelling alongside the argument). +struct ParamDecl { + std::string Name; + std::string TypeName; + PassMode Mode = PassMode::ByVal; + bool IsArray = false; + int Rank = 0; // 0 = scalar + std::vector<std::pair<int64_t, int64_t>> DeclaredBounds; +}; + class PrototypeAST { std::string Name; - std::vector<std::tuple<std::string, std::string, bool>> Args; + std::vector<ParamDecl> Params; std::string ReturnType; bool IsExternal; public: - PrototypeAST(const std::string &Name, - std::vector<std::tuple<std::string, std::string, bool>> Args, + PrototypeAST(const std::string &Name, + std::vector<ParamDecl> Params, const std::string &ReturnType, bool IsExternal = false) - : Name(Name), Args(std::move(Args)), ReturnType(ReturnType), IsExternal(IsExternal) {} + : Name(Name), Params(std::move(Params)), ReturnType(ReturnType), IsExternal(IsExternal) {} const std::string &getName() const { return Name; } - const std::vector<std::tuple<std::string, std::string, bool>> &getArgs() const { return Args; } + const std::vector<ParamDecl> &getParams() const { return Params; } const std::string &getReturnType() const { return ReturnType; } bool isExternal() const { return IsExternal; } }; diff --git a/include/cps/FunctionGen.h b/include/cps/FunctionGen.h index cce17c3..9bb6eef 100644 --- a/include/cps/FunctionGen.h +++ b/include/cps/FunctionGen.h @@ -30,7 +30,7 @@ class FunctionGen { std::map<std::string, FuncSig> Signatures; - void createArgumentAllocas(llvm::Function *F, const std::vector<std::tuple<std::string, std::string, bool>> &Args); + void createArgumentAllocas(llvm::Function *F, const std::vector<ParamDecl> &Params); public: FunctionGen(llvm::LLVMContext &C, diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h index 81fbd99..2af0260 100644 --- a/include/cps/Lexer.h +++ b/include/cps/Lexer.h @@ -78,7 +78,12 @@ enum Token { tok_read = -504, tok_write = -505, tok_append = -506, - tok_random = -507 + tok_random = -507, + + tok_type = -600, + tok_endtype = -601, + tok_set = -602, // reserved: SET types are recognised but not implemented + tok_define = -603 // reserved: DEFINE (set literals) likewise }; class Lexer { diff --git a/include/cps/Parser.h b/include/cps/Parser.h index 0ecb7f9..49f418c 100644 --- a/include/cps/Parser.h +++ b/include/cps/Parser.h @@ -1,12 +1,14 @@ #pragma once #include "cps/Lexer.h" #include "cps/AST.h" +#include "cps/TypeAST.h" +#include "cps/FunctionAST.h" +#include <cstdint> #include <vector> #include <initializer_list> #include <memory> #include <map> #include <optional> -#include <tuple> #include <string> namespace cps { @@ -15,6 +17,7 @@ class Parser { Lexer &Lex; int CurTok; unsigned NumErrors = 0; + unsigned BlockDepth = 0; // ParseBlock nesting; 1 = top level std::map<int, int> BinopPrecedence; @@ -42,12 +45,18 @@ class Parser { std::unique_ptr<StmtAST> ParseForStmt(); std::unique_ptr<StmtAST> ParseDeclare(); + std::unique_ptr<StmtAST> ParseAssignStmt(); + bool ParseDesignatorSuffix(std::vector<DesignatorAccess> &Out); + + std::unique_ptr<StmtAST> ParseTypeDecl(); + bool ParseRecordField(std::vector<RecordFieldDecl> &Out); std::unique_ptr<StmtAST> ParseFunction(); std::unique_ptr<StmtAST> ParseProcedure(); std::unique_ptr<StmtAST> ParseCallStmt(); std::unique_ptr<StmtAST> ParseReturnStmt(); - std::optional<std::vector<std::tuple<std::string, std::string, bool>>> ParsePrototypeArgs(); + std::optional<std::vector<ParamDecl>> ParsePrototypeArgs(bool AllowByRef); + bool ParseSignedIntBound(int64_t &Out); std::unique_ptr<StmtAST> ParseOpenFile(); std::unique_ptr<StmtAST> ParseReadFile(); diff --git a/include/cps/TypeAST.h b/include/cps/TypeAST.h new file mode 100644 index 0000000..d349859 --- /dev/null +++ b/include/cps/TypeAST.h @@ -0,0 +1,120 @@ +#pragma once +#include "cps/AST.h" +#include <cstdint> +#include <memory> +#include <string> +#include <utility> +#include <vector> + +namespace cps { + +// ---- User-defined type declarations (CIE 9618 §4.1) ---- + +// One field of a record TYPE body. Plain data, not a statement node, so the +// emitStmt chain can never execute a field by accident. +struct RecordFieldDecl { + std::string Name; + std::string TypeName; +}; + +// TYPE <name> = (value1, value2, ...) +class EnumTypeDeclAST : public StmtAST { + std::string Name; + std::vector<std::string> ValueNames; + +public: + EnumTypeDeclAST(const std::string &Name, std::vector<std::string> ValueNames) + : Name(Name), ValueNames(std::move(ValueNames)) {} + + const std::string &getName() const { return Name; } + const std::vector<std::string> &getValueNames() const { return ValueNames; } +}; + +// TYPE <name> = ^<pointee type> +class PointerTypeDeclAST : public StmtAST { + std::string Name; + std::string PointeeTypeName; + +public: + PointerTypeDeclAST(const std::string &Name, const std::string &PointeeTypeName) + : Name(Name), PointeeTypeName(PointeeTypeName) {} + + const std::string &getName() const { return Name; } + const std::string &getPointeeTypeName() const { return PointeeTypeName; } +}; + +// TYPE <name> +// DECLARE <field> : <type> +// ... +// ENDTYPE +class RecordTypeDeclAST : public StmtAST { + std::string Name; + std::vector<RecordFieldDecl> Fields; + +public: + RecordTypeDeclAST(const std::string &Name, std::vector<RecordFieldDecl> Fields) + : Name(Name), Fields(std::move(Fields)) {} + + const std::string &getName() const { return Name; } + const std::vector<RecordFieldDecl> &getFields() const { return Fields; } +}; + +// ---- Designators: chained accesses usable as lvalue or rvalue (§4.2) ---- + +enum class AccessKind : uint8_t { Index, Field, Deref }; + +// One postfix accessor: [indices], .Field, or ^ (dereference). +struct DesignatorAccess { + AccessKind Kind; + std::vector<std::unique_ptr<ExprAST>> Indices; // Index only + std::string FieldName; // Field only +}; + +// <base name> followed by one or more accessors, e.g. Form[i].YearGroup, p^. +// Bare names and single-index accesses keep their legacy nodes (parser +// degrade rule); a designator node always carries at least one accessor +// beyond those shapes, except as the operand of AddrOfExprAST. +class DesignatorExprAST : public ExprAST { + std::string BaseName; + std::vector<DesignatorAccess> Accesses; + int Line; + +public: + DesignatorExprAST(const std::string &BaseName, + std::vector<DesignatorAccess> Accesses, + int Line) + : BaseName(BaseName), Accesses(std::move(Accesses)), Line(Line) {} + + const std::string &getBaseName() const { return BaseName; } + const std::vector<DesignatorAccess> &getAccesses() const { return Accesses; } + int getLine() const { return Line; } +}; + +// ^<designator> — address-of. The operand is a designator by construction, +// so an lvalue is guaranteed at parse time. +class AddrOfExprAST : public ExprAST { + std::unique_ptr<DesignatorExprAST> Target; + +public: + explicit AddrOfExprAST(std::unique_ptr<DesignatorExprAST> Target) + : Target(std::move(Target)) {} + + DesignatorExprAST *getTarget() const { return Target.get(); } +}; + +// <designator> <- <expr> for targets beyond a bare name or single indexing +// (those degrade to AssignStmtAST / ArrayAssignStmtAST in the parser). +class DesignatorAssignStmtAST : public StmtAST { + std::unique_ptr<DesignatorExprAST> Target; + std::unique_ptr<ExprAST> Expr; + +public: + DesignatorAssignStmtAST(std::unique_ptr<DesignatorExprAST> Target, + std::unique_ptr<ExprAST> Expr) + : Target(std::move(Target)), Expr(std::move(Expr)) {} + + DesignatorExprAST *getTarget() const { return Target.get(); } + ExprAST *getExpr() const { return Expr.get(); } +}; + +} // namespace cps |
