/
pratt.nim
186 lines (138 loc) · 3.68 KB
/
pratt.nim
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import std/[strformat, strutils, unittest, algorithm, options]
type
SKind = enum
skAtom
skCons
S = object
ch: char
case kind*: SKind
of skCons:
sub: seq[S]
else:
discard
TokenKind = enum
tkAtom
tkOp
tkEof
Token = object
case kind*: TokenKind:
of tkAtom, tkOp:
ch: char
else:
discard
Lexer = object
tokens: seq[Token]
proc `$`(s: S): string =
case s.kind:
of skAtom:
result = $s.ch
of skCons:
result = "(" & $s.ch
for sub in s.sub:
result &= " " & $sub
result &= ")"
proc initLexer(str: string): Lexer =
for ch in str:
case ch:
of IdentChars:
result.tokens.add Token(kind: tkAtom, ch: ch)
of Whitespace:
discard
else:
result.tokens.add Token(kind: tkOp, ch: ch)
reverse(result.tokens)
proc next(lex: var Lexer): Token =
if lex.tokens.len > 0:
lex.tokens.pop
else:
Token(kind: tkEof)
proc peek(lex: var Lexer): Token =
if lex.tokens.len > 0:
lex.tokens[^1]
else:
Token(kind: tkEof)
proc prefix_binding_power(op: char): int =
case op:
of '+', '-': result = 9
else: assert false, $op
proc postfix_binding_power(op: char): Option[int] =
case op:
of '!': some(11)
of '[': some(11)
else: none(int)
proc infix_binding_power(op: char): Option[(int, int)] =
case op:
of '=': some (2, 1)
of '?': some (4, 3)
of '+', '-': some (5, 6)
of '*', '/': some (7, 8)
of '.': some (14, 13)
else: none (int, int)
proc exprBp(lexer: var Lexer, min_bp: int): S =
let tok = lexer.next()
var lhs = case tok.kind:
of tkAtom:
S(kind: skAtom, ch: tok.ch)
of tkOp:
if tok.ch == '(':
let lhs = exprBp(lexer, 0)
assert lexer.next().ch == ')'
lhs
else:
let r_bp = prefix_binding_power(tok.ch);
let rhs = expr_bp(lexer, r_bp);
S(kind: skCons, ch: tok.ch, sub: @[rhs])
else:
assert false
while true:
var op: char
case lexer.peek():
of Eof():
break
of Op(ch: @opCh):
op = opCh
else: raiseImplementError($lexer.peek())
if Some(@l_bp) ?= postfix_binding_power(op):
if l_bp < min_bp:
break
discard lexer.next()
if op == '[':
let rhs = expr_bp(lexer, 0)
discard lexer.next()
lhs = S(kind: skCons, ch: op, sub: @[lhs, rhs])
else:
lhs = S(kind: skCons, ch: op, sub: @[lhs])
continue
if Some((@l_bp, @r_bp)) ?= infix_binding_power(op):
if l_bp < min_bp:
break
discard lexer.next()
if op == '?':
let mhs = expr_bp(lexer, 0);
discard lexer.next()
let rhs = expr_bp(lexer, r_bp);
lhs = S(kind: skCons, ch: op, sub: @[lhs, mhs, rhs])
else:
let rhs = expr_bp(lexer, r_bp);
lhs = S(kind: skCons, ch: op, sub: @[lhs, rhs])
continue
break
return lhs
proc expr(str: string): S =
var lexer = initLexer(str)
return exprBp(lexer, 0)
import hpprint
when isMainModule:
check $expr("1") == "1"
check $expr("1 + 2 * 3") == "(+ 1 (* 2 3))"
check $expr("a + b * c * d + e") == "(+ (+ a (* (* b c) d)) e)"
check $expr("f . g . h") == "(. f (. g h))"
check $expr("1 + 2 + f . g . h * 3 * 4") == "(+ (+ 1 2) (* (* (. f (. g h)) 3) 4))"
check $expr("--1 * 2") == "(* (- (- 1)) 2)"
check $expr("--f . g") == "(- (- (. f g)))"
check $expr("-9!") == "(- (! 9))"
check $expr("f . g !") == "(! (. f g))"
check $expr("(((0)))") == "0"
check $expr("x[0][1]") == "([ ([ x 0) 1)"
check $expr("a ? b : c ? d : e") == "(? a b (? c d e))"
check $expr("a = 0 ? b : c = d") == "(= a (= (? 0 b c) d))"