dForth

Forth-like interpreter in go
Log | Files | Refs

commit a810762abd78929e5af31822e8061131526e45aa
parent 315c71ea3d9eb9c3e8b50892939fb04f3b583f84
Author: Pavel Renev <an2qzavok@gmail.com>
Date:   Mon, 18 Oct 2021 09:06:17 +0000

more builtins, memCompile()

Diffstat:
MdForth.go | 20++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/dForth.go b/dForth.go @@ -50,6 +50,7 @@ func NewForth() *Forth { NewWord("_call", forth.biCall), NewWord("_lit", forth.biLit), NewWord("_jump", forth.biJump), + NewWord("_ret", forth.biRet), NewWord(".", forth.biDot), NewWord("@", forth.biFetch), NewWord("!", forth.biStore), @@ -60,6 +61,7 @@ func NewForth() *Forth { _ = forth.memStore(dtpt, dictstart) _ = forth.memStore(lwpt, 0) _ = forth.memStore(pcpt, dictstart) + forth.memCompile(0, 2, dictstart, 3) return forth } @@ -83,6 +85,14 @@ func (forth *Forth) memStore(pt, val int32) error { return nil } +func (forth *Forth) memCompile(codes ...int32) { + dt, _ := forth.memFetch(dtpt) + for i, code := range codes { + _ = forth.memStore(int32(int(dt) + i * 4), code) + } + forth.memStore(dtpt, int32(int(dt) + len(codes) * 4)) +} + func (forth *Forth) Push(pt, val int32) { tos, err := forth.memFetch(pt) if err != nil { @@ -174,7 +184,7 @@ func (forth *Forth) biRead() error { } func (forth *Forth) biCall() error { - /* do we need call? + /* do we need _call? * can we just treat opcode as call * if it's >= dictstart ? */ @@ -182,12 +192,18 @@ func (forth *Forth) biCall() error { if err != nil { return err } - forth.Push(rspt, pc + 1) + forth.Push(rspt, pc + 4) jmp := forth.Pop(stpt) err = forth.memStore(pcpt, jmp) return err } +func (forth *Forth) biRet() error { + jmp := forth.Pop(rspt) + err := forth.memStore(pcpt, jmp) + return err +} + func (forth *Forth) biLit() error { pc, err := forth.memFetch(pcpt) if err != nil {