mirror of
https://github.com/hymkor/lispect.git
synced 2026-09-02 15:17:49 -04:00
Implement (expect) like (case)
This commit is contained in:
@@ -49,7 +49,7 @@ func (g *Global) spawn(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Nod
|
||||
|
||||
var symTimeout = gmnlisp.NewSymbol("timeout")
|
||||
|
||||
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (g *Global) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
patterns := []string{}
|
||||
timeOut := 0
|
||||
|
||||
@@ -83,3 +83,58 @@ func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node
|
||||
}
|
||||
return gmnlisp.Integer(result), nil
|
||||
}
|
||||
|
||||
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
|
||||
patterns := []string{}
|
||||
actions := []gmnlisp.Node{}
|
||||
|
||||
timeoutSec := 0
|
||||
var timeoutAct gmnlisp.Node
|
||||
|
||||
for gmnlisp.IsSome(node) {
|
||||
var err error
|
||||
var _patAndAct gmnlisp.Node
|
||||
_patAndAct, node, err = gmnlisp.Shift(node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
patAndAct, err := gmnlisp.ExpectClass[*gmnlisp.Cons](ctx, w, _patAndAct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pat := patAndAct.Car
|
||||
act := patAndAct.Cdr
|
||||
|
||||
if patInt, ok := pat.(gmnlisp.Integer); ok {
|
||||
timeoutSec = int(patInt)
|
||||
timeoutAct = act
|
||||
} else if _, ok := pat.(*gmnlisp.Cons); ok {
|
||||
for gmnlisp.IsSome(pat) {
|
||||
var pat1 gmnlisp.Node
|
||||
|
||||
pat1, pat, err = gmnlisp.Shift(pat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
actions = append(actions, act)
|
||||
patterns = append(patterns, pat1.String())
|
||||
}
|
||||
} else {
|
||||
actions = append(actions, act)
|
||||
patterns = append(patterns, pat.String())
|
||||
}
|
||||
}
|
||||
|
||||
var result int
|
||||
if timeoutSec == 0 {
|
||||
result = g.w.Expect(patterns...)
|
||||
} else {
|
||||
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeoutSec), patterns...)
|
||||
}
|
||||
if result >= 0 {
|
||||
return gmnlisp.Progn(ctx, w, actions[result])
|
||||
} else {
|
||||
return gmnlisp.Progn(ctx, w, timeoutAct)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user