Join Nostr
2026-05-17 23:48:52 UTC

:seven: on Nostr: okay, okay, okay, it works I can’t believe it fucking works I didn’t even set out ...

okay, okay, okay, it works I can’t believe it fucking works I didn’t even set out to do this this is a side effect of having a forth-like repl for playing with the vm directly but… genserver.

I’ve implemented a genserver with the VM and it works.

```
{ 0 1 | ; arity 0, 1 capture counter body

load_captured 0 store 0 ; @init: state := captured 0

recv_peek 20 ; @loop: empty mailbox → @park
store 1 ; msg: local 1
load 1
is_tagged_tuple 16 4 :call ; not :call/4 → @retry
pop
recv_remove
load 1 tuple_ref 1 ; from-pid
:reply
load 1 tuple_ref 2 ; ref
load 0 1 + ; new-state = state + 1
dup store 0 ; state := new-state
tuple 3 ; {:reply ref new-state}
send pop ; deliver to from-pid
jmp -20 ; : @loop

recv_advance -21 ; @retry: skip msg → @loop
recv_wait -22 ; @park: block: @loop
}

spawn ; spawn the counter, push its pid
make_ref store 1 ; ref: local 1
dup ; keep pid for later
:call self load 1 :inc
tuple 4 ; {:call self ref :inc}
send pop ; deliver

recv_peek 5 ; @wait: empty: @park
store 2 ; reply: local 2
load 2 tuple_ref 2 ; extract n
recv_remove
jmp 1 ; : @done
recv_wait -7 ; @park: block: @wait
; @done: stack = [counter-pid, n]
```

and the equivalent ish code:

```
fn counter state do
receive do
{:call from ref :inc} -> do
new-state = state + 1
send from {:reply ref new-state}
counter new-state
end
end
end

fn main do
pid = spawn (fn -> counter 0)
ref = make-ref
send pid {:call (self) ref :inc}
receive do
{:reply ^ref n} -> n
end
end
```

GUYS. GUYS. YOU FUCKING GUYS I HAVE SOMETHING.