-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ml
266 lines (224 loc) · 6.77 KB
/
parser.ml
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
type t = {
pos : int;
stream : char list
}
type error = {
location : int;
desc : string
}
type sign =
| Plus
| Minus
type ast =
Const of int
| Add of ast * ast
| Sub of ast * ast
| Mul of ast * ast
| Div of ast * ast
| Mod of ast * ast
| Exp of ast * ast
let init (s:string) : t = {
pos = 0;
stream = s
|> String.to_seq
|> List.of_seq
|> List.filter (fun c -> c <> ' ')
}
let str_of_chars (cs: char list) : string =
cs |> List.to_seq |> String.of_seq
type 'a parser = {
run: t -> (('a * t), error) result
}
let return (a: 'a) : 'b parser = {
run = fun input -> Ok(a, input)
}
let ch (c:char) : char parser = {
run = fun input ->
match input.stream with
| [] -> Error { location = input.pos; desc = "eof" }
| x :: xs -> if x = c
then Ok (c, { pos = input.pos + 1; stream = xs })
else Error {
location = input.pos;
desc = Printf.sprintf
"@ %d: got '%c', expected '%c'"
input.pos c x
}
}
let alpha : char parser = {
run = fun input ->
match input.stream with
| [] -> Error { location = input.pos; desc = "eof" }
| x :: xs -> match x with
| 'a'..'z' | 'A'..'Z' ->
Ok (x, { pos = input.pos + 1; stream = xs })
| _ -> Error {
location = input.pos;
desc = Printf.sprintf
"@ %d: got '%c', expected 'alpha'"
input.pos x
}
}
let digit : char parser = {
run = fun input ->
match input.stream with
| [] -> Error { location = input.pos; desc = "eof" }
| x :: xs -> match x with
| '0'..'9' -> Ok (x, { pos = input.pos + 1; stream = xs })
| _ -> Error {
location = input.pos;
desc = Printf.sprintf
"@ %d: got '%c', expected 'digit'"
input.pos x
}
}
let ( <|> ) (a: 'a parser) (b: 'b parser) : 'c parser = {
run = fun input ->
match a.run input with
| Ok (c, input) -> Ok (c, input)
| Error _ -> b.run input
}
let ( *> ) (a: 'a parser) (b: 'b parser) : 'c parser = {
run = fun input ->
match a.run input with
| Error e -> Error e
| Ok (c, input) -> b.run input
}
let ( <* ) (a: 'a parser) (b: 'b parser) : 'c parser = {
run = fun input ->
match a.run input with
| Error e -> Error e
| Ok (c, input) -> match b.run input with
| Error e -> Error e
| Ok (_, input) -> Ok (c, input)
}
let ( >> ) (x:'a parser) (f:'a -> 'b) : ('b parser) = {
run = fun input ->
match x.run(input) with
| Error e -> Error e
| Ok (a, input) -> Ok (f a, input)
}
let ( >>= ) (x:'a parser) (f:'a -> 'b parser) : ('b parser) = {
run = fun input ->
match x.run(input) with
| Error e -> Error e
| Ok (a, input) -> (f a).run(input)
}
let ( let* ) = ( >>= )
let ( >>. ) (a:'a parser) (b:'b parser) : ('a * 'b) parser =
a >>= fun a_res -> b >> fun b_res -> a_res, b_res
let andthen = ( >>. )
let ( <*> ) (f: ('a->'b) parser) (a:'a parser) : 'b parser =
f >>. a >> fun (f,x) -> f x
let apply = ( <*> )
let fail (msg:string) : 'a parser = {
run = fun input -> Error {
location = input.pos;
desc = "no match"
}
}
let choice (ps: 'a parser list) : 'a parser =
List.fold_left (<|>) (fail "no match in choice") ps
let many (p: 'a parser) : ('a list parser) = {
run = fun input ->
let rec aux acc input : 'a list * t =
match p.run input with
| Error _ -> List.rev acc, input
| Ok (r, input) -> aux (r :: acc) input
in
Ok(aux [] input)
}
let many1 (p: 'a parser) : ('a list parser) = many p >>= fun r -> {
run = fun input ->
if List.is_empty r
then Error {
location = input.pos;
desc = "expected alteast one match"
}
else Ok(r, input)
}
let opt (p:'a parser) =
(p >> fun x -> Some x) <|>
return None
let whitespace : (string parser) =
many (
ch ' '
<|> ch '\n'
<|> ch '\t'
<|> ch '\r'
) >> str_of_chars
let const x _ = x
let add_sign sign d = match sign with
| Some Minus -> d * -1
| _ -> d
let number : int parser =
opt (
ch '-'
>> const Minus <|> (
ch '+'
>> const Plus
)
)
>>. (
many1 digit
>> str_of_chars
)
>> fun (sign, digits) -> (
int_of_string digits |> add_sign sign
)
let parse_const = number >> fun i -> Const i
let fix f =
let rec p = lazy (f r)
and r = { run = fun input ->
(Lazy.force p).run input }
in
r
let rec ( ^ ) x y =
if y <= 0 then 1
else x * ( ^ ) x (y - 1)
let parse_expr = fix @@ fun parse_expr ->
let term =
let factor =
(parse_const >>= return) <|>
(ch '(' *> parse_expr <* ch ')')
in
let unary =
(ch '+' *> factor) <|>
(ch '-' *> factor
>>= fun right -> return (Mul(Const (-1), right))
) in let add = ch '+' in
let sub = ch '-' in
let modulus = ch '%' in
let mul = ch '*' in
let div = ch '/' in
let exp = ch '^' in
let rec aux left =
(add *> parse_expr >>= fun right -> return (Add(left, right))) <|>
(sub *> parse_expr >>= fun right -> return (Sub(left, right))) <|>
(modulus *> factor >>= fun right -> aux (Mod(left, right))) <|>
(mul *> factor >>= fun right -> aux (Mul(left, right))) <|>
(div *> factor >>= fun right -> aux (Div(left, right))) <|>
(exp *> factor >>= fun right -> aux (Exp(left, right))) <|>
return left
in
factor <|> unary >>= aux
in
term
let rec evaluate (a:ast) : int =
match a with
| Const i -> i
| Add (x,y) -> evaluate x + evaluate y
| Sub (x,y) -> evaluate x - evaluate y
| Mod (x,y) -> (evaluate x) mod (evaluate y)
| Mul (x,y) -> evaluate x * evaluate y
| Div (x,y) -> evaluate x / evaluate y
| Exp (x,y) -> evaluate x ^ evaluate y
let rec show (a:ast) : string =
match a with
| Const i -> (string_of_int i)
| Add (x,y) -> Printf.sprintf "(%s + %s)" (show x) (show y)
| Sub (x,y) -> Printf.sprintf "(%s - %s)" (show x) (show y)
| Mod (x,y) -> Printf.sprintf "(%s %% %s)" (show x) (show y)
| Mul (x,y) -> Printf.sprintf "(%s * %s)" (show x) (show y)
| Div (x,y) -> Printf.sprintf "(%s / %s)" (show x) (show y)
| Exp (x,y) -> Printf.sprintf "(%s ^ %s)" (show x) (show y)