@@ -4,6 +4,8 @@ package asm
4
4
5
5
import (
6
6
"fmt"
7
+
8
+ "github.com/ohler55/ojg/jp"
7
9
)
8
10
9
11
func init () {
@@ -20,17 +22,54 @@ is returned.`,
20
22
21
23
func cond (root map [string ]any , at any , args ... any ) any {
22
24
for _ , arg := range args {
23
- v := evalArg (root , at , arg )
24
- list , ok := v .([]any )
25
+ list , ok := arg .([]any )
25
26
if ! ok {
26
- panic (fmt .Errorf ("cond expects array arguments, not a %T" , v ))
27
+ panic (fmt .Errorf ("cond expects array arguments, not a %T" , arg ))
27
28
}
28
29
if len (list ) != 2 {
29
30
panic (fmt .Errorf ("cond array arguments must have two elements, not a %d" , len (list )))
30
31
}
31
- if b , _ := evalArg (root , at , list [0 ]).(bool ); b {
32
- return list [1 ]
32
+ if bv , _ := evalValue (root , at , list [0 ]).(bool ); bv {
33
+ return evalValue ( root , at , list [1 ])
33
34
}
34
35
}
35
36
return nil
36
37
}
38
+
39
+ func evalValue (root map [string ]any , at any , value any ) (result any ) {
40
+ top:
41
+ switch tv := value .(type ) {
42
+ case * Fn :
43
+ result = tv .Eval (root , at , tv .Args ... )
44
+ case []any :
45
+ if 0 < len (tv ) {
46
+ if name , _ := tv [0 ].(string ); 0 < len (name ) {
47
+ if af := NewFn (name ); af != nil {
48
+ af .Args = tv [1 :]
49
+ af .compile ()
50
+ value = af
51
+ goto top
52
+ }
53
+ }
54
+ }
55
+ case jp.Expr :
56
+ if 0 < len (tv ) {
57
+ if _ , ok := tv [0 ].(jp.At ); ok {
58
+ result = tv .First (at )
59
+ } else {
60
+ result = tv .First (root )
61
+ }
62
+ }
63
+ case string :
64
+ if 0 < len (tv ) && (tv [0 ] == '$' || tv [0 ] == '@' ) {
65
+ if x , err := jp .Parse ([]byte (tv )); err == nil {
66
+ value = x
67
+ goto top
68
+ }
69
+ }
70
+ result = tv
71
+ default :
72
+ result = value
73
+ }
74
+ return
75
+ }
0 commit comments