From 344206d57e834b2394cbe2ec0df50b04a8b3c171 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 15:45:40 +0000 Subject: feat: implement CIE 9.1 text file handling (OPENFILE/READFILE/WRITEFILE/CLOSEFILE/EOF) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lib/CodeGen/RuntimeCheck.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/CodeGen/RuntimeCheck.cc') diff --git a/lib/CodeGen/RuntimeCheck.cc b/lib/CodeGen/RuntimeCheck.cc index d51d78a..c2ced87 100644 --- a/lib/CodeGen/RuntimeCheck.cc +++ b/lib/CodeGen/RuntimeCheck.cc @@ -27,8 +27,12 @@ void RuntimeCheck::setupExternalFunctions() { } void RuntimeCheck::emitErrorAndExit(Value *Condition, Value *Msg, int Line) { + emitErrorAndExit(Condition, Msg, {ConstantInt::get(TheContext, APInt(32, Line))}); +} + +void RuntimeCheck::emitErrorAndExit(Value *Condition, Value *Msg, ArrayRef FmtArgs) { Function *TheFunction = Builder.GetInsertBlock()->getParent(); - + BasicBlock *FailBB = BasicBlock::Create(TheContext, "check_fail", TheFunction); BasicBlock *ContBB = BasicBlock::Create(TheContext, "check_cont", TheFunction); @@ -37,7 +41,7 @@ void RuntimeCheck::emitErrorAndExit(Value *Condition, Value *Msg, int Line) { Builder.SetInsertPoint(FailBB); std::vector PrintArgs; PrintArgs.push_back(Msg); - PrintArgs.push_back(ConstantInt::get(TheContext, APInt(32, Line))); + PrintArgs.insert(PrintArgs.end(), FmtArgs.begin(), FmtArgs.end()); Builder.CreateCall(PrintfFunc, PrintArgs); Builder.CreateCall(ExitFunc, ConstantInt::get(TheContext, APInt(32, 1))); -- cgit v1.3.1-4-g156e