Skip to content

6 3 collections math and filesystem

TransCompilator edited this page Nov 26, 2026 · 1 revision

6.3 Collections, Math and Filesystem

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/6-3-collections-math-and-filesystem

Collections, Math and Filesystem

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

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.

1. Collections: Lists and Maps

Dynamic Lists (std/list.eng)

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_push encounters items's len equals items's cap, it doubles the capacity (subject to minimum bounds of 8 or 16 elements), invokes resize_memory, and updates the pointer and capacity std/list.eng:11-20.
  • Bounds Safety: list_get, list_set, list_first, and list_last verify 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, and list_max_i64 provide type-safe folds over whole numbers and decimals std/list.eng:79-141.

Hash Maps (std/map.eng)

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 states byte array tracking slot occupation (0 = empty, 1 = occupied, 2 = deleted) std/map.eng:7-8.
  • Hashing:
    • hash key mixes bits using integer multiplication and bitwise shifts std/hash.eng:10-14.
    • hash text key implements 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 map allocates 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"
Loading

Sources: std/list.eng:1-160, std/map.eng:1-143


2. Mathematical Helpers (std/math.eng)

std/math.eng provides zero-dependency arithmetic functions operating on whole numbers (i64) and decimals (f64) std/math.eng:1-59.

  • Absolute Values: abs_i64 and abs_f64 check 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, and clamp_i64 enforce numeric bounds std/math.eng:16-30,56-59.
  • Square Root: sqrt computes decimal square roots via 24 iterations of the Babylonian method (Hero's method), rejecting negative inputs std/math.eng:32-42.
  • Exponentiation: pow_i64 calculates 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


3. Filesystem and Linux Syscalls (std/fs.eng)

std/fs.eng wraps low-level Linux system invocation primitives (ask Linux kernel) to construct a sandboxed file API std/fs.eng:1-124.

Syscall Mapping

  • Open (sys_openat - 257): open path with flags uses ask Linux kernel 257 with directory descriptor -100 (AT_FDCWD), the path data pointer, configuration flags, and mode 420 (0644) std/fs.eng:7-8.
  • Read (sys_read - 0): file_read_bytes invokes kernel syscall 0 with the file descriptor, data buffer, and byte limit std/fs.eng:25-26.
  • Write (sys_write - 1): file_write_bytes and write_str_to_file invoke kernel syscall 1 std/fs.eng:28-29,39-44.
  • Close (sys_close - 3): file_close invokes kernel syscall 3 and invalidates the file descriptor to -1 std/fs.eng:34-37.
  • Seek (sys_lseek - 8): file_seek invokes kernel syscall 8 for offset positioning std/fs.eng:31-32.
  • Unlink (sys_unlinkat - 263): delete_file invokes kernel syscall 263 relative to AT_FDCWD std/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"
Loading

Sources: std/fs.eng:1-125


4. Parity Testing and Verification

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.

Parity Validation Pipeline

  1. Source Loading: The test runner resolves import or use directives, locating modules under std/*.eng crates/tc-semantics/tests/phase4_stdlib_parity.rs:24-76.
  2. Compilation: Source files are parsed and lowered to Core-7 IR via compile_english_to_ir crates/tc-semantics/tests/phase4_stdlib_parity.rs:151-154.
  3. Dual Execution:
  4. 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


Clone this wiki locally