-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
115 lines (95 loc) · 1.83 KB
/
function.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
111
112
113
114
115
package parsegen
import (
"errors"
"fmt"
)
var S = function{}
type code int
const (
missed code = iota
eof
zero
exit
)
type tFunc func(Iterator) code
var codes = []string{"missed", "eof", "zero", "empty"}
func (c code) String() string {
return codes[c]
}
// lvalue = rvalue ~
// function = instructions
type function struct {
typ byte
name string
terminal tFunc
funcs []*function
marked bool
}
func (f *function) call(it Iterator) code {
i := it.GP()
ret := f.terminal(it)
if it.EOF() {
return eof
}
if !f.isEmpty() && i == it.GP() {
return missed
}
return ret
}
func (f *function) isTerminal() bool {
return f.typ == 'T' //&& f.terminal != nil
}
func (f *function) isEmpty() bool {
return f.name == "empty" //&& f.terminal != nil
}
func (f *function) hasNext(current int) bool {
return current+1 < len(f.funcs)
}
func (f *function) existFunc(current int) bool {
return current < len(f.funcs)
}
func (f *function) isCycle() bool {
return f.typ == 'C'
}
func recPrint(f *function, i int) {
if f.isTerminal() {
fmt.Printf("%s [T]\n", f.name)
} else {
fmt.Printf("%s-- [%c]\n", f.name, f.typ)
}
for _, subf := range f.funcs {
fmt.Printf("\t")
for j := 0; j < i-1; j++ {
fmt.Printf("\t")
}
fmt.Printf("-")
if subf.name != f.name {
recPrint(subf, i+1)
} else {
fmt.Printf("%s\n", f.name)
}
}
}
func printTree(f *function) {
recPrint(f, 1)
}
func checkBNF(f *function) error {
if f.isTerminal() {
if f.hasNext(0) {
return errors.New("[check] terminal has child")
}
return nil
}
if f.isCycle() && f.hasNext(0) {
return errors.New("[check] cycle has more child than one")
}
for _, fn := range f.funcs {
if fn == f || fn.name == f.name {
return errors.New("[check] found recursion " + f.name)
}
if err := checkBNF(fn); err != nil {
return err
}
}
return nil
}