forge: work in progress: code and data definitions

This commit is contained in:
Helmut Merz 2023-07-29 18:18:33 +02:00
parent 1f27700334
commit 465cfe7aa9
3 changed files with 13 additions and 8 deletions

View file

@ -30,7 +30,7 @@ func setup(f FE) *builtins {
Dup: r("dup", func(f FE, _ XT) { f.Push(f.Peek(0)) }),
Get: r("@", func(f FE, _ XT) { f.Push(f.Pop().(forge.FPtr).Value()) }),
Here: r("here", func(f FE, _ XT) { f.Push(f.Here()) }),
Lit: r("literal", func(f FE, _ XT) { f.Literal() }),
Lit: r("literal", func(f FE, _ XT) { f.Push(f.Literal()) }),
Mult: r("*", func(f FE, _ XT) { f.Push(f.Pop().(int) * f.Pop().(int)) }),
New: r("new", func(f FE, _ XT) { f.New() }),
Put: r("!", func(f FE, _ XT) { f.Pop().(forge.FPtr).Set(f.Pop()) }),

View file

@ -159,10 +159,6 @@ func (f *forgeEnv) Exec(items ...fitem) {
f.Call(f.Code(items...))
}
func (f *forgeEnv) Literal() {
f.Push(f.ip.Next().Value())
}
// definition methods, may become obsolete
func (f *forgeEnv) Def(name string, items ...fitem) XT {
@ -182,14 +178,20 @@ func (f *forgeEnv) New() {
f.cp = newPtr()
}
func (f *forgeEnv) Compile(it fitem) {
f.cp.Append(it)
func (f *forgeEnv) Compile(its ...fitem) {
for _, it := range its {
f.cp.Append(it)
}
}
func (f *forgeEnv) Here() fptr {
return f.cp.Clone()
}
func (f *forgeEnv) Literal() fitem {
return f.ip.Next().Value()
}
func (f *forgeEnv) Voc() *fvoc {
return f.voc
}

View file

@ -32,11 +32,14 @@ func ExecTest(t *testing.T) {
func CoreTest(t *testing.T) {
//sq := fe.Def("square", b.Dup, b.Mult)
//fe.Comp(b.Dup, b.Mult); fe.Exec(b.Lit, "square", b.def)
fe.Exec(b.New, b.Here)
fe.Exec(b.Lit, b.Dup, b.Comp, b.Lit, b.Mult, b.Comp, b.Lit, "square", b.Reg)
sq := fe.Pop()
//seven := fe.Const("seven", 7)
//fe.Exec(b.Lit, 7, b.Lit, "seven", b.const)
fe.Exec(b.New, b.Here)
fe.Exec(b.Lit, b.Lit, b.Comp, b.Lit, 7, b.Comp, b.Lit, "x", b.Reg)
fe.Exec(b.Lit, b.Lit, b.Comp, b.Lit, 7, b.Comp, b.Lit, "seven", b.Reg)
seven := fe.Pop()
fe.Exec(b.Lit, 3, seven, sq, b.Add)
t.AssertEqual(fe.Pop(), 52)