forge/rep: Compile() basically working

This commit is contained in:
Helmut Merz 2023-07-31 20:02:05 +02:00
parent 373c9484c8
commit 434fba79d8
2 changed files with 11 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package rep
import ( import (
"fmt" "fmt"
"strings"
"git.sr.ht/~cco/go-scopes/forge" "git.sr.ht/~cco/go-scopes/forge"
) )
@ -29,12 +30,17 @@ func (ci code) Compile(f forge.FE) forge.FPtr {
} else { } else {
fmt.Println(err) fmt.Println(err)
} }
case code:
c.Append(i.Compile(f))
} }
} }
return c.Reset() return c.Reset()
} }
func compStr(f forge.FE, s string) (forge.FItem, error) { func compStr(f forge.FE, s string) (forge.FItem, error) {
if strings.HasPrefix(s, "'") {
return forge.FItem(strings.Trim(s, "'")), nil
}
if v, ok := f.Voc().Lookup(s); ok { if v, ok := f.Voc().Lookup(s); ok {
return forge.FItem(v), nil return forge.FItem(v), nil
} }

View file

@ -43,4 +43,8 @@ func RepTest(t *testing.T) {
c := src.Compile(fe) c := src.Compile(fe)
fe.Call(c) fe.Call(c)
t.AssertEqual(fe.Pop(), 6) t.AssertEqual(fe.Pop(), 6)
src = rep.Code(rep.Code("dup", "dup", "*", "*"), "'cube'", "reg")
fe.Call(src.Compile(fe))
fe.Call(rep.Code(3, "cube").Compile(fe))
t.AssertEqual(fe.Pop(), 27)
} }