blob: 016ee83aa2ffa84fe2914d38f23417e85b672dfa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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).
|