forge: Exec() basically working
This commit is contained in:
parent
503990f180
commit
91322dae3a
4 changed files with 87 additions and 7 deletions
|
@ -9,13 +9,14 @@ type sptr[V any] struct {
|
|||
offset int
|
||||
}
|
||||
|
||||
func newSliceSeq[V any]() *sliceseq[V] {
|
||||
return &sliceseq[V]{}
|
||||
func newSliceSeq[V any](s ...V) *sliceseq[V] {
|
||||
seq := sliceseq[V](s)
|
||||
return &seq
|
||||
}
|
||||
|
||||
func NewSlice[V any]() *sptr[V] {
|
||||
func NewSlice[V any](s ...V) *sptr[V] {
|
||||
return &sptr[V]{
|
||||
seq: newSliceSeq[V](),
|
||||
seq: newSliceSeq[V](s...),
|
||||
offset: -1,
|
||||
}
|
||||
}
|
||||
|
|
23
forge/builtins/builtins.go
Normal file
23
forge/builtins/builtins.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package builtins
|
||||
|
||||
import "git.sr.ht/~cco/go-scopes/forge"
|
||||
|
||||
type FE = *forge.ForgeEnv
|
||||
type XT = *forge.Item
|
||||
|
||||
type builtins struct {
|
||||
Add, Lit forge.XT
|
||||
}
|
||||
|
||||
func Setup(f FE) *builtins {
|
||||
r := func(name string, fct forge.Callable) XT {
|
||||
return forge.Register(f.Voc(), name, fct)
|
||||
}
|
||||
return &builtins{
|
||||
Lit: r("literal", literal),
|
||||
}
|
||||
}
|
||||
|
||||
func literal(f FE, it XT) {
|
||||
f.Push(f.IP().Next().Value())
|
||||
}
|
|
@ -16,7 +16,7 @@ type Ptr = ptr.Ptr[DataItem]
|
|||
type Stack = stack.Stack[DataItem]
|
||||
type Voc = voc.Vocabulary[XT]
|
||||
|
||||
type Callable func(*ForgeEnv, *Item)
|
||||
type Callable func(*ForgeEnv, XT)
|
||||
|
||||
type Item struct {
|
||||
name string
|
||||
|
@ -58,9 +58,60 @@ func newFE(voc *Voc) *ForgeEnv {
|
|||
}
|
||||
}
|
||||
|
||||
// basic functions and methods
|
||||
|
||||
func Register(voc *Voc, name string, fct Callable) *Item {
|
||||
it := Item{
|
||||
name: name,
|
||||
fct: fct,
|
||||
}
|
||||
voc.Register(name, &it)
|
||||
return &it
|
||||
}
|
||||
|
||||
func (it *Item) IsImmediate() bool {
|
||||
return it.immediate
|
||||
}
|
||||
|
||||
func (it *Item) Immediate() {
|
||||
it.immediate = true
|
||||
}
|
||||
|
||||
func (it *Item) Body() Ptr {
|
||||
if it.body == nil {
|
||||
it.body = ptr.NewSlice[DataItem]()
|
||||
}
|
||||
return it.body
|
||||
}
|
||||
|
||||
func (it *Item) Name() string {
|
||||
return it.name
|
||||
}
|
||||
|
||||
// ForgeEnv methods
|
||||
|
||||
func (f *ForgeEnv) Exec() {
|
||||
func (f *ForgeEnv) Voc() *Voc {
|
||||
return f.voc
|
||||
}
|
||||
|
||||
func (f *ForgeEnv) IP() Ptr {
|
||||
return f.ip
|
||||
}
|
||||
|
||||
func (f *ForgeEnv) Exec(items ...DataItem) {
|
||||
f.ip = ptr.NewSlice[DataItem](items...)
|
||||
for f.ip.Next() != nil {
|
||||
it := f.ip.Value().(XT)
|
||||
it.fct(f, it)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *ForgeEnv) Push(it DataItem) {
|
||||
f.ds.Push(it)
|
||||
}
|
||||
|
||||
func (f *ForgeEnv) Pop() DataItem {
|
||||
return f.ds.Pop()
|
||||
}
|
||||
|
||||
// dummy stuff for testing
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"git.sr.ht/~cco/go-scopes/common/testing"
|
||||
"git.sr.ht/~cco/go-scopes/forge"
|
||||
"git.sr.ht/~cco/go-scopes/forge/builtins"
|
||||
)
|
||||
|
||||
func TestForge(tb *tbase.T) {
|
||||
|
@ -14,5 +15,9 @@ func TestForge(tb *tbase.T) {
|
|||
|
||||
func ExecTest(t *testing.T) {
|
||||
fe := forge.NewFE()
|
||||
fe.Exec()
|
||||
b := builtins.Setup(fe)
|
||||
fe.Exec(b.Lit, 4)
|
||||
t.AssertEqual(fe.Pop(), 4)
|
||||
// fe.Execute(forge.Code{b.Lit, 3, b.Lit, 2, b.Add})
|
||||
// t.AssertEqual(fe.Pop(), 5)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue