make XT an interface, allowing different implementations

This commit is contained in:
Helmut Merz 2023-07-28 20:01:07 +02:00
parent 0bb39f022f
commit 25333f5ab2

View file

@ -45,15 +45,42 @@ var newVoc = voc.NewVoc[fitem]
var newScalar = ptr.NewScalar[fitem]
var newPtr = ptr.NewSlice[fitem]
// new XT ...
type XT interface {
Name() string
Fct() Callable
Code() fptr
Data() fptr
}
// xitem (executable item) / XT (execution token)
type xitem struct {
name string
fct Callable
body fptr
code fptr
data fptr
}
type XT = *xitem
func (it *xitem) Name() string {
return it.name
}
func (it *xitem) Fct() Callable {
return it.fct
}
func (it *xitem) Code() fptr {
//return it.code
return it.body
}
func (it *xitem) Data() fptr {
//return it.data
return it.body
}
func Register(voc *fvoc, name string, fct Callable) *xitem {
it := xitem{
@ -64,10 +91,6 @@ func Register(voc *fvoc, name string, fct Callable) *xitem {
return &it
}
func (it *xitem) Name() string {
return it.name
}
// forgeEnv methods
func (f *forgeEnv) Code(items ...fitem) fptr {
@ -81,7 +104,7 @@ func (f *forgeEnv) Call(code fptr) {
f.ip = code.Clone()
for f.ip.Next() != nil {
xt := f.ip.Value().(XT)
xt.fct(f, xt)
xt.Fct()(f, xt)
}
f.ip = f.rs.Pop().(fptr)
}
@ -126,9 +149,9 @@ func (f *forgeEnv) Peek(d int) fitem {
// basic functions for executable items
func doDef(f *forgeEnv, xt XT) {
f.Call(xt.body.Value().(fptr))
f.Call(xt.Code().Value().(fptr))
}
func doData(f *forgeEnv, xt XT) {
f.Push(xt.body.Value())
f.Push(xt.Data().Value())
}