diff options
| author | _ov4 <ov4@ov4.org> | 2026-07-26 15:45:40 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-07-26 15:45:40 +0000 |
| commit | 344206d57e834b2394cbe2ec0df50b04a8b3c171 (patch) | |
| tree | 0eb282bc9878bf990496a5d840ca8492b3cfc508 /include/cps/FileAST.h | |
| parent | d1c47a328785ece9a8ffd9bbca3138818b8203b3 (diff) | |
feat: implement CIE 9.1 text file handling (OPENFILE/READFILE/WRITEFILE/CLOSEFILE/EOF)
Front end: OPENFILE/READFILE/WRITEFILE/CLOSEFILE plus reserved mode
keywords READ/WRITE/APPEND (RANDOM reserved with a dedicated
not-implemented error); statement nodes in new FileAST.h, parsing in new
ParserFile.cc; EOF joins the Builtins table as the 12th builtin and
CallExprAST now carries a line number for its runtime diagnostics.
Runtime: new FileHandler (StringHandler-shaped leaf) lazily synthesizes
internal __cps_file_* helper functions and a 16-slot open-file table
keyed by filename content (strcmp — STRING '=' is pointer identity).
EOF is an fgetc/ungetc peek; READFILE grows a malloc/realloc line buffer
and strips a trailing CR; fopen uses text modes so mingw-w64 line
translation works. File-free programs get byte-identical IR. All misuse
(not open, wrong mode, reopen, fopen failure, read past EOF, table full)
dies with the existing "[Fatal] line N:" pattern via a new
RuntimeCheck::emitErrorAndExit overload taking runtime printf args.
Also fixes a pre-existing ArithmeticHandler hole surfaced by adversarial
review: mixed-width integer operands (i1 BOOLEAN / i8 CHAR vs i64) now
widen via ZExt instead of emitting invalid IR, and remaining type
mismatches get a clean diagnostic; -EOF(f) and -IS_NUM(x) compile.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'include/cps/FileAST.h')
| -rw-r--r-- | include/cps/FileAST.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/include/cps/FileAST.h b/include/cps/FileAST.h new file mode 100644 index 0000000..04bd06a --- /dev/null +++ b/include/cps/FileAST.h @@ -0,0 +1,63 @@ +#pragma once +#include "cps/AST.h" +#include "cps/FileMode.h" +#include <memory> +#include <string> + +namespace cps { + +class OpenFileStmtAST : public StmtAST { + std::unique_ptr<ExprAST> FileName; + FileMode Mode; + int Line; + +public: + OpenFileStmtAST(std::unique_ptr<ExprAST> FileName, FileMode Mode, int Line) + : FileName(std::move(FileName)), Mode(Mode), Line(Line) {} + + ExprAST *getFileName() const { return FileName.get(); } + FileMode getMode() const { return Mode; } + int getLine() const { return Line; } +}; + +class ReadFileStmtAST : public StmtAST { + std::unique_ptr<ExprAST> FileName; + std::string VarName; + int Line; + +public: + ReadFileStmtAST(std::unique_ptr<ExprAST> FileName, const std::string &VarName, int Line) + : FileName(std::move(FileName)), VarName(VarName), Line(Line) {} + + ExprAST *getFileName() const { return FileName.get(); } + const std::string &getVarName() const { return VarName; } + int getLine() const { return Line; } +}; + +class WriteFileStmtAST : public StmtAST { + std::unique_ptr<ExprAST> FileName; + std::unique_ptr<ExprAST> Data; + int Line; + +public: + WriteFileStmtAST(std::unique_ptr<ExprAST> FileName, std::unique_ptr<ExprAST> Data, int Line) + : FileName(std::move(FileName)), Data(std::move(Data)), Line(Line) {} + + ExprAST *getFileName() const { return FileName.get(); } + ExprAST *getData() const { return Data.get(); } + int getLine() const { return Line; } +}; + +class CloseFileStmtAST : public StmtAST { + std::unique_ptr<ExprAST> FileName; + int Line; + +public: + CloseFileStmtAST(std::unique_ptr<ExprAST> FileName, int Line) + : FileName(std::move(FileName)), Line(Line) {} + + ExprAST *getFileName() const { return FileName.get(); } + int getLine() const { return Line; } +}; + +} // namespace cps |
