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 /lib/Parser | |
| 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 'lib/Parser')
| -rw-r--r-- | lib/Parser/Parser.cc | 15 | ||||
| -rw-r--r-- | lib/Parser/ParserFile.cc | 92 |
2 files changed, 106 insertions, 1 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 77df9bb..e67dda7 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -59,7 +59,7 @@ std::unique_ptr<ExprAST> Parser::ParseIdentifierExpr() { getNextToken(); auto Args = ParseExprList(')', true); if (!Args) return nullptr; - return std::make_unique<CallExprAST>(IdName, std::move(*Args)); + return std::make_unique<CallExprAST>(IdName, std::move(*Args), Line); } if (CurTok == '[') { @@ -213,6 +213,7 @@ void Parser::syncToStatementStart() { case tok_function: case tok_procedure: case tok_call: case tok_return: case tok_else: case tok_endif: case tok_endwhile: case tok_until: case tok_next: case tok_endfunction: case tok_endprocedure: + case tok_openfile: case tok_readfile: case tok_writefile: case tok_closefile: return; default: getNextToken(); @@ -537,6 +538,18 @@ std::unique_ptr<StmtAST> Parser::ParseStatementImpl() { else if (CurTok == tok_return) { return ParseReturnStmt(); } + else if (CurTok == tok_openfile) { + return ParseOpenFile(); + } + else if (CurTok == tok_readfile) { + return ParseReadFile(); + } + else if (CurTok == tok_writefile) { + return ParseWriteFile(); + } + else if (CurTok == tok_closefile) { + return ParseCloseFile(); + } fprintf(stderr, "Error: Unexpected token at line %d when expecting a statement\n", Lex.getLine()); getNextToken(); // guarantee progress so error recovery cannot loop forever diff --git a/lib/Parser/ParserFile.cc b/lib/Parser/ParserFile.cc new file mode 100644 index 0000000..6fd368c --- /dev/null +++ b/lib/Parser/ParserFile.cc @@ -0,0 +1,92 @@ +#include "cps/Parser.h" +#include "cps/FileAST.h" +#include <cstdio> + +using namespace cps; + +// OPENFILE <expr> FOR READ|WRITE|APPEND +std::unique_ptr<StmtAST> Parser::ParseOpenFile() { + int Line = Lex.getLine(); + getNextToken(); // eat OPENFILE + + auto FileName = ParseExpression(); + if (!FileName) return nullptr; + + if (CurTok != tok_for) { + fprintf(stderr, "Error: Expected FOR after OPENFILE filename at line %d\n", Lex.getLine()); + return nullptr; + } + getNextToken(); // eat FOR + + FileMode Mode; + switch (CurTok) { + case tok_read: Mode = FileMode::Read; break; + case tok_write: Mode = FileMode::Write; break; + case tok_append: Mode = FileMode::Append; break; + case tok_random: + fprintf(stderr, "Error: File mode RANDOM is not implemented (text files only) at line %d\n", + Lex.getLine()); + return nullptr; + default: + fprintf(stderr, "Error: Expected file mode READ, WRITE or APPEND at line %d\n", Lex.getLine()); + return nullptr; + } + getNextToken(); // eat the mode keyword + + return std::make_unique<OpenFileStmtAST>(std::move(FileName), Mode, Line); +} + +// READFILE <expr>, <identifier> +std::unique_ptr<StmtAST> Parser::ParseReadFile() { + int Line = Lex.getLine(); + getNextToken(); // eat READFILE + + auto FileName = ParseExpression(); + if (!FileName) return nullptr; + + if (CurTok != ',') { + fprintf(stderr, "Error: Expected ',' after READFILE filename at line %d\n", Lex.getLine()); + return nullptr; + } + getNextToken(); // eat ',' + + if (CurTok != tok_identifier) { + fprintf(stderr, "Error: Expected variable name after ',' in READFILE at line %d\n", Lex.getLine()); + return nullptr; + } + std::string VarName = Lex.IdentifierStr; + getNextToken(); // eat the variable name + + return std::make_unique<ReadFileStmtAST>(std::move(FileName), VarName, Line); +} + +// WRITEFILE <expr>, <expr> +std::unique_ptr<StmtAST> Parser::ParseWriteFile() { + int Line = Lex.getLine(); + getNextToken(); // eat WRITEFILE + + auto FileName = ParseExpression(); + if (!FileName) return nullptr; + + if (CurTok != ',') { + fprintf(stderr, "Error: Expected ',' after WRITEFILE filename at line %d\n", Lex.getLine()); + return nullptr; + } + getNextToken(); // eat ',' + + auto Data = ParseExpression(); + if (!Data) return nullptr; + + return std::make_unique<WriteFileStmtAST>(std::move(FileName), std::move(Data), Line); +} + +// CLOSEFILE <expr> +std::unique_ptr<StmtAST> Parser::ParseCloseFile() { + int Line = Lex.getLine(); + getNextToken(); // eat CLOSEFILE + + auto FileName = ParseExpression(); + if (!FileName) return nullptr; + + return std::make_unique<CloseFileStmtAST>(std::move(FileName), Line); +} |
