summaryrefslogtreecommitdiff
path: root/vere/C_INSIGHTS.md
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-06 22:16:33 +0700
committerpolwex <polwex@sortug.com>2025-10-06 22:16:33 +0700
commitda03400d5857aab4c809c34f0416d1c09c4fed12 (patch)
treee44d94e3ceeafc108e1290a1de2e5c9c12012d48 /vere/C_INSIGHTS.md
parent64b132efc5ad870677ac974334b30fdbc4afafd3 (diff)
pretty good pretty good
Diffstat (limited to 'vere/C_INSIGHTS.md')
-rw-r--r--vere/C_INSIGHTS.md51
1 files changed, 51 insertions, 0 deletions
diff --git a/vere/C_INSIGHTS.md b/vere/C_INSIGHTS.md
new file mode 100644
index 0000000..016ee83
--- /dev/null
+++ b/vere/C_INSIGHTS.md
@@ -0,0 +1,51 @@
+## on nock.c
+CLAUDE:
+The key is that call_count is static (shared across ALL function calls), while my_call is local (each function call gets its own copy on the stack).
+
+ Here's what happens without my_call:
+
+ 1. Call #0 ENTERS → logs "ENTER call #0", call_count becomes 1
+ 2. Call #1 ENTERS (recursive) → logs "ENTER call #1", call_count becomes 2
+ 3. Call #2 ENTERS (recursive) → logs "ENTER call #2", call_count becomes 3
+ 4. Call #2 EXITS → logs "EXIT call #2" using (call_count-1 = 2) ✓ Correct!
+ 5. Call #1 EXITS → logs "EXIT call #1" using (call_count-1 = 2) ✗ Wrong! Should be 1
+ 6. Call #0 EXITS → logs "EXIT call #0" using (call_count-1 = 2) ✗ Wrong! Should be 0
+
+ The problem: by the time call #0 exits, call_count has already been incremented by all the recursive calls.
+
+ With my_call (a local variable):
+
+ 1. Call #0: my_call=0 (saved on stack), call_count++
+ 2. Call #1: my_call=1 (saved on stack), call_count++
+ 3. Call #2: my_call=2 (saved on stack), call_count++
+ 4. Call #2 EXITS → uses its own my_call=2 ✓
+ 5. Call #1 EXITS → uses its own my_call=1 ✓
+ 6. Call #0 EXITS → uses its own my_call=0 ✓
+
+ Each recursive call preserves its own my_call value on the stack, so the EXIT log always matches the ENTER log!
+
+ This is a classic recursion pattern in C: use static variables for things you want to share across calls (like a counter), but capture their values in local variables if you need them to survive recursion.
+
+
+ ## after first round on vere
+ CLAUDE:
+ Current Logging Features
+
+ The C Nock interpreter now logs:
+ - Call number: Unique ID for each Nock evaluation (call #0, #1, etc.)
+ - Recursion depth: Indented to show call hierarchy
+ - Opcode: The Nock rule being executed (0-12) or CELL for distribution
+ - Subject type: Whether the subject (bus) is a cell or atom
+ - Mug: Hash of the subject/result for tracking identical nouns
+ - Return value type: Cell or atom with its mug
+
+ Example Pattern from the Log
+
+ >>> ENTER call #8 depth=5 opcode=8 bus=cell[mug=0x5cc2e3ff]
+ >>> ENTER call #9 depth=6 opcode=8 bus=cell[mug=0x528e9b65]
+ <<< EXIT call #9 depth=6 returns=cell[mug=0x6d2bc01f]
+ >>> ENTER call #10 depth=6 opcode=8 bus=cell[mug=0x479b42af]
+ <<< EXIT call #10 depth=6 returns=cell[mug=0x6d2bc01f]
+ <<< EXIT call #8 depth=5 returns=cell[mug=0x6d2bc01f]
+
+ This shows: opcode 8 (Nock Eight = extend subject) calling itself recursively twice, both returning the same noun (mug 0x6d2bc01f).