-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
91 lines (80 loc) · 1.7 KB
/
parse.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
package main
import (
"fmt"
"strings"
)
type Node interface{ fmt.Stringer }
type Op struct {
Token Token
Num int
}
func (o Op) String() string {
if o.Num == 0 {
return string(o.Token)
}
return fmt.Sprintf("(%s:%d)", o.Token, o.Num)
}
type Loop []Node
func (l Loop) String() string {
nodes := make([]string, len(l))
for i, n := range l {
nodes[i] = n.String()
}
return fmt.Sprintf("[%s]", strings.Join(nodes, ", "))
}
type Program []Node
func (p Program) String() string {
nodes := make([]string, len(p))
for i, n := range p {
nodes[i] = n.String()
}
return strings.Join(nodes, ", ")
}
func ParseOp(pos int, tokens []Token) (Op, int, error) {
op := Op{Token: tokens[pos]}
for pos = pos; pos < len(tokens); pos++ {
if tokens[pos] == op.Token {
op.Num++
} else {
pos--
break
}
}
return op, pos, nil
}
func ParseNodes(pos int, tokens []Token) ([]Node, int, error) {
var nodes []Node
for pos = pos; pos < len(tokens); pos++ {
tok := tokens[pos]
switch tok {
case INVALID:
// ignore invalid tokens
case EOF:
return nodes, pos, nil
case CLOSE:
return nodes, pos, nil
case OPEN:
lnodes, lpos, err := ParseNodes(pos+1, tokens)
if err != nil {
return nil, lpos, err
}
if tok := tokens[lpos]; tok != CLOSE {
return nil, pos, fmt.Errorf("expected %s, got %s", CLOSE, tok)
}
pos = lpos
nodes = append(nodes, Loop(lnodes))
default:
onode, opos, err := ParseOp(pos, tokens)
if err != nil {
return nil, opos, err
}
pos = opos
nodes = append(nodes, onode)
}
}
return nil, 0, fmt.Errorf("missing %s", EOF)
}
func Parse(tokens []Token) (Program, error) {
nodes, _, err := ParseNodes(0, tokens)
return Program(nodes), err
}