-
Notifications
You must be signed in to change notification settings - Fork 0
/
callable.go
70 lines (61 loc) · 1.5 KB
/
callable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package djson
import (
"fmt"
"strings"
)
type Callable interface {
call(k string, caller Value, scanner TokenScanner, vars Context) (Value, error)
}
type CallableRegister struct {
calls map[string]Callback
typ string
}
type Callback func(caller Value, scanner TokenScanner, vars Context) (Value, error)
func NewCallableRegister(typ string) *CallableRegister {
return &CallableRegister{typ: typ, calls: map[string]Callback{
"if": ifCall,
}}
}
func (c *CallableRegister) RegisterCall(k string, ck Callback) {
if c.calls == nil {
c.calls = make(map[string]Callback)
}
c.calls[k] = ck
}
func (c *CallableRegister) call(k string, caller Value, scanner TokenScanner, vars Context) (val Value, err error) {
call, ok := c.calls[k]
if !ok {
call, ok = c.caseInsensitiveCallback(k)
}
if !ok {
err = fmt.Errorf("undefined method [%s] for %s", k, c.typ)
return
}
return call(caller, scanner, vars)
}
func (c *CallableRegister) caseInsensitiveCallback(k string) (Callback, bool) {
for ck, c := range c.calls {
if strings.EqualFold(ck, k) {
return c, true
}
}
return nil, false
}
func ifCall(val Value, scanner TokenScanner, ctx Context) (ret Value, err error) {
ctx.pushMe(val)
defer ctx.popMe()
scanner.PushEnds(TokenParenthesesClose)
defer scanner.PopEnds(TokenParenthesesClose)
expr := NewStmtExecutor(scanner, ctx)
if err = expr.Execute(For(NullValue())); err != nil {
return
}
if expr.Exited() {
Exit()
}
if ret = expr.Value(); ret.Type != ValueNull {
return
}
ret = val
return
}