Skip to content

Commit

Permalink
make heap compatible with original ws: memory is not initialized to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
voliva committed Jul 1, 2024
1 parent 9c2f82c commit ef410ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions playground.wsa
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; hello world in 2 stages
include memory
call memory_init
include io

push 6
Expand Down
8 changes: 7 additions & 1 deletion src/whitespace/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ function stepHeap(state: MachineState, instruction: HeapOp): MachineState {
`Heap op ${state.pc} failed: Can't access a negative address. addr = ${addr}`
);
}
state.stack.push(state.heap[Number(addr)] ?? 0n);
const value = state.heap[Number(addr)];
if (value === undefined) {
throw new Error(
`Heap op ${state.pc} failed: Access to uninitialized heap address ${addr}`
);
}
state.stack.push(value);
} else {
if (state.stack.length < 2) {
throw new Error(
Expand Down
7 changes: 7 additions & 0 deletions src/wsa/lib/memory.wsa
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
valueinteger _STACK_HEAD 0
valueinteger _HEAP_HEAD 4294967294

label memory_init
push 0
doub
store _STACK_HEAD
store _HEAP_HEAD
ret

; arg: size
label malloc
retrive _HEAP_HEAD
Expand Down

0 comments on commit ef410ac

Please sign in to comment.