-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_interpreter_test.go
152 lines (128 loc) · 2.75 KB
/
example_interpreter_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package prattle_test
import (
"fmt"
"strconv"
"unicode"
"github.com/askeladdk/prattle"
)
const (
ksemicolon = 1 + iota
kassign
kplus
kident
knumber
)
func testScan(s *prattle.Scanner) int {
s.ExpectAny(unicode.IsSpace)
s.Skip()
switch {
case s.Done():
return 0
case s.ExpectOne(unicode.IsLetter):
s.ExpectAny(unicode.IsLetter)
return kident
case s.ExpectOne(unicode.IsDigit):
s.ExpectAny(unicode.IsDigit)
return knumber
case s.Expect('='):
return kassign
case s.Expect('+'):
return kplus
case s.Expect(';'):
return ksemicolon
}
s.Advance()
return -1
}
type testDriver struct {
stack []prattle.Token
idents map[string]int
}
func (d *testDriver) pop() (v prattle.Token) {
if n := len(d.stack); n > 0 {
v, d.stack = d.stack[n-1], d.stack[:n-1]
}
return v
}
func (d *testDriver) push(v prattle.Token) {
d.stack = append(d.stack, v)
}
func (d *testDriver) Precedence(kind int) int {
return kind
}
func (d *testDriver) tonumber(t prattle.Token) int {
if t.Kind == kident {
return d.idents[t.Text]
}
num, _ := strconv.Atoi(t.Text)
return num
}
func (d *testDriver) plus(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(d.Precedence(t.Kind)); err != nil {
return err
}
right := d.tonumber(d.pop())
left := d.tonumber(d.pop())
d.push(prattle.Token{
Kind: knumber,
Text: strconv.Itoa(left + right),
})
return nil
}
func (d *testDriver) assign(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(d.Precedence(kassign)); err != nil {
return err
}
right := d.pop()
left := d.pop()
d.idents[left.Text] = d.tonumber(right)
return nil
}
func (d *testDriver) primitive(p *prattle.Parser, t prattle.Token) error {
d.push(t)
return nil
}
func (d *testDriver) Prefix(kind int) prattle.ParseFunc {
switch kind {
default:
return nil
case kident, knumber:
return d.primitive
}
}
func (d *testDriver) Infix(kind int) prattle.ParseFunc {
switch kind {
default:
return nil
case kplus:
return d.plus
case kassign:
return d.assign
}
}
func (d *testDriver) ParseError(t prattle.Token) error {
return fmt.Errorf("%s: unexpected '%s'", t.Position, t.Text)
}
// This example demonstrates parsing a simple programming language that consists of a sequence of statements.
func Example_interpreter() {
c := testDriver{
idents: make(map[string]int),
}
source := "a = 1;\nb = 2;\nc = a+b+b+a;\n"
s := prattle.Scanner{Scan: testScan}
p := prattle.Parser{Driver: &c}
p.Init(s.InitWithString(source))
// Parse expressions separated by semicolons.
for p.Peek().Kind != 0 {
if err := p.Parse(ksemicolon); err != nil {
fmt.Println(err)
return
} else if !p.Expect(ksemicolon) {
fmt.Println("expected semicolon")
return
}
}
fmt.Printf("c = %d\n", c.idents["c"])
// Output:
// c = 6
}