-
Notifications
You must be signed in to change notification settings - Fork 15
/
earley_eval.go
110 lines (105 loc) · 2.44 KB
/
earley_eval.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package fmr
import (
"fmt"
"math/big"
)
// Eval returns the denotation of Node n
func (n *Node) Eval() (interface{}, error) {
if n.Value.Rb == nil || n.Value.Rb.F == nil {
if n.p == nil {
return "", nil
}
return n.OriginalText(), nil
}
return n.fmrEval(n.Value.Rb.F, n.Children)
}
func (n *Node) fmrEval(f *FMR, children []*Node) (interface{}, error) {
if f == nil {
return "", nil
}
if f.Fn == "nf.I" {
if len(f.Args) != 1 {
return "", fmt.Errorf("the length of Args of nf.I should be one")
}
s, err := n.semEval(f.Args[0], children)
if err != nil {
return "", err
}
return s, nil
}
var args []interface{}
for _, arg := range f.Args {
s, err := n.semEval(arg, children)
if err != nil {
return "", err
}
args = append(args, s)
}
if Debug {
fmt.Printf("funcs.Call(%s, %+v)\n", f.Fn, args)
}
return Call(f.Fn, args...)
}
func (n *Node) semEval(arg *Arg, nodes []*Node) (interface{}, error) {
if arg == nil {
return "", fmt.Errorf("arg is nil")
}
switch arg.Type {
case "string":
if s, ok := arg.Value.(string); ok {
return s, nil
}
return "", fmt.Errorf("arg.Value: %+v is not string", arg.Value)
case "int":
if i, ok := arg.Value.(*big.Int); ok {
return i.String(), nil
}
return "", fmt.Errorf("arg.Value: %+v is not int", arg.Value)
case "float":
if f, ok := arg.Value.(*big.Float); ok {
return f.String(), nil
}
return "", fmt.Errorf("arg.Value: %+v is not float", arg.Value)
case "func":
if fmr, ok := arg.Value.(*FMR); ok {
return n.fmrEval(fmr, nodes)
}
return "", fmt.Errorf("arg.Value: %+v is not func", arg.Value)
case "index":
i, ok := arg.Value.(int)
if !ok {
return "", fmt.Errorf("arg.Value: %+v is not index", arg.Value)
}
if i < 0 || i > len(nodes) {
return "", fmt.Errorf("i=%d not in range [0, %d]", i, len(nodes))
}
if i == 0 {
return n.NL(), nil
}
s, err := nodes[i-1].Eval()
if err != nil {
return "", err
}
return s, nil
case "context":
subnodes := []map[string]interface{}{}
for _, node := range nodes {
ni, err := node.Eval()
if err != nil {
ni = node.OriginalText()
}
subnodes = append(subnodes, map[string]interface{}{node.Term().Value: ni})
}
ret := map[string]interface{}{
"text": n.OriginalText(),
"pos": n.Pos(),
"nodes": subnodes,
}
if n.Term().Type != Terminal {
ret["type"] = n.Term().Value
}
return ret, nil
default:
return "", fmt.Errorf("arg.Type: %s invalid", arg.Type)
}
}