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
153
154
155
156
157
158
159
|
// -*-go-*-
//
// Reverse Polish Notation Calculator
// Copyright (c) 2010 J.A. Roberts Tunney
// MIT License
//
// To compile:
//
// ragel -Z -T0 -o rpn.go rpn.rl
// go build -o rpn rpn.go
// ./rpn
//
// To show a diagram of your state machine:
//
// ragel -V -Z -p -o rpn.dot rpn.rl
// xdot -Tpng -o rpn.png rpn.dot
//
package main
import (
"errors"
"fmt"
"os"
"strconv"
)
type stack struct {
items []int
count int
}
func (s *stack) pop() int {
s.count--
v := s.items[s.count]
return v
}
func (s *stack) push(v int) {
s.items[s.count] = v
s.count++
}
func abs(v int) int {
if v < 0 {
v = -v
}
return v
}
%% machine rpn;
%% write data;
func rpn(data string) (res int, err error) {
// p, pe, eof := 0, len(data), len(data)
cs, p, pe := 0, 0, len(data)
mark := 0
st := &stack{items: make([]int, 128), count: 0}
%%{
action mark { mark = p }
action push { x, _ := strconv.Atoi(data[mark:p]); st.push(x) }
action add { y, x := st.pop(), st.pop(); st.push(x + y) }
action sub { y, x := st.pop(), st.pop(); st.push(x - y) }
action mul { y, x := st.pop(), st.pop(); st.push(x * y) }
action div { y, x := st.pop(), st.pop(); st.push(x / y) }
action abs { st.push(abs(st.pop())) }
action abba { st.push(666) }
stuff = digit+ >mark %push
| '+' @add
| '-' @sub
| '*' @mul
| '/' @div
| 'abs' %abs
| 'add' %add
| 'abba' %abba
;
main := ( space | stuff space )* ;
write init;
write exec;
}%%
if cs < rpn_first_final {
if p == pe {
return 0, errors.New("unexpected eof")
} else {
return 0, errors.New(fmt.Sprintf("error at position %d", p))
}
}
if st.count == 0 {
return 0, errors.New("rpn stack empty on result")
}
return st.pop(), nil
}
//////////////////////////////////////////////////////////////////////
type rpnTest struct {
s string
v int
}
var rpnTests = []rpnTest{
rpnTest{"666\n", 666},
rpnTest{"666 111\n", 111},
rpnTest{"4 3 add\n", 7},
rpnTest{"4 3 +\n", 7},
rpnTest{"4 3 -\n", 1},
rpnTest{"4 3 *\n", 12},
rpnTest{"6 2 /\n", 3},
rpnTest{"0 3 -\n", -3},
rpnTest{"0 3 - abs\n", 3},
rpnTest{" 2 2 + 3 - \n", 1},
rpnTest{"10 7 3 2 * - +\n", 11},
rpnTest{"abba abba add\n", 1332},
}
type rpnFailTest struct {
s string
e string
}
var rpnFailTests = []rpnFailTest{
rpnFailTest{"\n", "rpn stack empty on result"},
}
func main() {
rc := 0
for _, test := range rpnTests {
res, err := rpn(test.s)
if err != nil {
fmt.Fprintf(os.Stderr, "FAIL rpn(%#v) %s\n", test.s, err)
rc = 1
} else if res != test.v {
fmt.Fprintf(os.Stderr, "FAIL rpn(%#v) -> %#v != %#v\n",
test.s, res, test.v)
rc = 1
}
}
for _, test := range rpnFailTests {
res, err := rpn(test.s)
if err == nil {
fmt.Fprintf(os.Stderr, "FAIL rpn(%#v) -> %#v should fail: %#v\n",
test.s, res, test.e)
} else if err.Error() != test.e {
fmt.Fprintf(os.Stderr, "FAIL rpn(%#v) %#v should be %#v\n",
test.s, err.Error(), test.e)
}
}
os.Exit(rc)
}
|