Repository navigation
6 3 collections math and filesystem
Source: https://deepwiki.com/PonvasanthPon/TransCompilator/6-3-collections-math-and-filesystem
Relevant source files
The following files were used as context for generating this wiki page:
This section covers the standard library modules providing dynamic data structures, arithmetic extensions, and sandboxed operating system interfaces in the TransCompilator (tc_r1) architecture:
-
std/list.eng: Growable generic sequences with sorting and aggregation primitives std/list.eng:1-160. -
std/map.eng: Integer and text hash maps with stable insertion-order mechanics and open-addressing std/map.eng:1-143. -
std/math.eng: Everyday numeric helpers, iterative square roots, and exponentiation std/math.eng:1-59. -
std/fs.eng: Linux file descriptor wrappers and high-level file persistence built on direct kernel system calls std/fs.eng:1-124.
The List of T record defines a growable sequence holding a raw pointer to memory (data), a current length (len), and an allocated capacity (cap) std/list.eng:5-5.
-
Resizing: When
list_pushencountersitems's len equals items's cap, it doubles the capacity (subject to minimum bounds of 8 or 16 elements), invokesresize_memory, and updates the pointer and capacity std/list.eng:11-20. -
Bounds Safety:
list_get,list_set,list_first, andlist_lastverify index boundaries and terminate execution with explicit error strings if violated std/list.eng:29-49. -
Aggregations: Specialized routines like
list_sum_i64,list_avg_i64,list_min_i64, andlist_max_i64provide type-safe folds over whole numbers and decimals std/list.eng:79-141.
std/map.eng implements IntMap of V (whole number keys) and StrMap of V (pointer to TcString keys) std/map.eng:7-8.
-
Storage Layout: Both maps maintain parallel arrays for keys, values, and a
statesbyte array tracking slot occupation (0 = empty, 1 = occupied, 2 = deleted) std/map.eng:7-8. -
Hashing:
-
hash keymixes bits using integer multiplication and bitwise shifts std/hash.eng:10-14. -
hash text keyimplements the FNV-1a hash algorithm over string byte buffers std/map.eng:16-24.
-
-
Probe and Rehash: Linear probing resolves collisions. When
table's len + 1 >= table's cap,rehash int mapallocates a larger table and re-inserts active entries std/map.eng:35-69.
graph TD
subcode["Natural Language & Code Entity Space"]
EngList[""List of T Record"" -->|push/pop| ListImpl[""std/list.eng""]
EngMap[""IntMap / StrMap Record"" -->|put/get/remove| MapImpl[""std/map.eng""]
ListImpl --> Resize[""resize_memory (std/alloc.eng)""]
MapImpl --> Hash[""hash key / hash text key"" --> Probe[""Linear Probe & States Array""]]
Sources: "std/list.eng:1-50, std/map.eng:7-95"
Sources: std/list.eng:1-160, std/map.eng:1-143
std/math.eng provides zero-dependency arithmetic functions operating on whole numbers (i64) and decimals (f64) std/math.eng:1-59.
-
Absolute Values:
abs_i64andabs_f64check for negative signs and return additive inverses or identity values std/math.eng:7-14. -
Extrema and Clamping:
min_i64,max_i64,min_f64,max_f64, andclamp_i64enforce numeric bounds std/math.eng:16-30,56-59. -
Square Root:
sqrtcomputes decimal square roots via 24 iterations of the Babylonian method (Hero's method), rejecting negative inputs std/math.eng:32-42. -
Exponentiation:
pow_i64calculates whole-number powers using binary exponentiation (exponentiation by squaring) in$O(\log n)$ steps std/math.eng:44-54.
Sources: std/math.eng:1-60
std/fs.eng wraps low-level Linux system invocation primitives (ask Linux kernel) to construct a sandboxed file API std/fs.eng:1-124.
-
Open (
sys_openat- 257):open path with flagsusesask Linux kernel 257with directory descriptor-100(AT_FDCWD), the path data pointer, configuration flags, and mode420(0644) std/fs.eng:7-8. -
Read (
sys_read- 0):file_read_bytesinvokes kernel syscall0with the file descriptor, data buffer, and byte limit std/fs.eng:25-26. -
Write (
sys_write- 1):file_write_bytesandwrite_str_to_fileinvoke kernel syscall1std/fs.eng:28-29,39-44. -
Close (
sys_close- 3):file_closeinvokes kernel syscall3and invalidates the file descriptor to-1std/fs.eng:34-37. -
Seek (
sys_lseek- 8):file_seekinvokes kernel syscall8for offset positioning std/fs.eng:31-32. -
Unlink (
sys_unlinkat- 263):delete_fileinvokes kernel syscall263relative toAT_FDCWDstd/fs.eng:121-124.
graph TD
subcode["Filesystem Control Flow & Kernel Traps"]
API[""read_file_to_str / file_open_read"" --> FS[""std/fs.eng (File Record)""]
FS --> OpenSys[""ask Linux kernel 257 (openat)""]
FS --> ReadSys[""ask Linux kernel 0 (read)""]
FS --> CloseSys[""ask Linux kernel 3 (close)""]
OpenSys --> Kernel[""Linux Kernel Sandbox""]
ReadSys --> Kernel
CloseSys --> Kernel
Sources: "std/fs.eng:7-98"
Sources: std/fs.eng:1-125
Standard library modules are validated against regression and parity test suites (phase4_stdlib_parity.rs and phase4_5_natural_english_stdlib.rs) crates/tc-semantics/tests/phase4_stdlib_parity.rs:1-15, crates/tc-semantics/tests/phase4_5_natural_english_stdlib.rs:1-15.
-
Source Loading: The test runner resolves
importorusedirectives, locating modules understd/*.engcrates/tc-semantics/tests/phase4_stdlib_parity.rs:24-76. -
Compilation: Source files are parsed and lowered to Core-7 IR via
compile_english_to_ircrates/tc-semantics/tests/phase4_stdlib_parity.rs:151-154. -
Dual Execution:
- The IR module is run in the reference interpreter (
interpret_in_dir) crates/tc-semantics/tests/phase4_stdlib_parity.rs:160-166. - The IR module is compiled to a static ELF64 binary via
tc_codegen_x86::compile_to_elfand executed natively in an isolated temporary sandbox crates/tc-semantics/tests/phase4_stdlib_parity.rs:167-171.
- The IR module is run in the reference interpreter (
- Assertion: Exit codes, stdout/stderr streams, and generated sandbox files are compared byte-for-byte between the interpreter and the native executable crates/tc-semantics/tests/phase4_stdlib_parity.rs:174-197.
Sources: crates/tc-semantics/tests/phase4_stdlib_parity.rs:1-197, crates/tc-semantics/tests/phase4_5_natural_english_stdlib.rs:1-147
The TransCompilator Project