-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.ml
More file actions
467 lines (420 loc) · 15.3 KB
/
reader.ml
File metadata and controls
467 lines (420 loc) · 15.3 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
(* reader.ml
* A skeleton for the reader for the 2021-2022 course on compiler-construction
*)
#use "pc.ml";;
let rec gcd a b =
match (a, b) with
| (0, b) -> b
| (a, 0) -> a
| (a, b) -> gcd b (a mod b);;
type scm_number =
| ScmRational of (int * int)
| ScmReal of float;;
type sexpr =
| ScmVoid
| ScmNil
| ScmBoolean of bool
| ScmChar of char
| ScmString of string
| ScmSymbol of string
| ScmNumber of scm_number
| ScmVector of (sexpr list)
| ScmPair of (sexpr * sexpr);;
module type READER = sig
val nt_sexpr : sexpr PC.parser
end;; (* end of READER signature *)
module Reader : READER = struct
open PC;;
let unitify nt = pack nt (fun _ -> ());;
let rec nt_whitespace str =
const (fun ch -> ch <= ' ') str
and nt_end_of_line_or_file str =
let nt1 = unitify (char '\n') in
let nt2 = unitify nt_end_of_input in
let nt1 = disj nt1 nt2 in
nt1 str
and nt_line_comment str =
let nt1 =
pack
(caten
(pack
(caten
(char ';')
(star (const (fun ch -> (ch != (char_of_int 10) && ch != (char_of_int 4) && ch != (char_of_int 3) ) ) ) )
)
(fun (a, b) -> ()
)
)
(nt_end_of_line_or_file)
)
(fun (a, b) -> ()
) in
nt1 str
and nt_paired_comment str =
let nt1 =
pack
(caten
(char '{')
(caten
(star
(disj_list
[
(pack
(const
(fun ch -> (ch != '}' && ch != '{' && ch != '\"' && ch != '#') )
)
(fun ch -> ())
);
(pack nt_char (fun ch -> () ) );
(pack nt_string (fun s -> () ) );
nt_paired_comment
]
)
)
(char '}')
)
)
(fun (a, (b, c)) -> ()) in
nt1 str
and nt_sexpr_comment str =
let nt1 =
pack
(caten
(word "#;")
nt_sexpr
)
(fun (a, b) -> ()) in
nt1 str
and nt_comment str =
disj_list
[nt_line_comment;
nt_paired_comment;
nt_sexpr_comment] str
and nt_skip_star str =
let nt1 = disj (unitify nt_whitespace) nt_comment in
let nt1 = unitify (star nt1) in
nt1 str
and make_skipped_star (nt : 'a parser) =
let nt1 = caten nt_skip_star (caten nt nt_skip_star) in
let nt1 = pack nt1 (fun (_, (e, _)) -> e) in
nt1
and nt_int str =
(pack (caten
(maybe
(one_of
"+-"
)
)
(plus
(range '0' '9')
)
)
(fun (a, b) ->
let n = List.fold_left (fun acc curr -> acc*10 + ((int_of_char curr) - (int_of_char '0'))) 0 b in
match a with
| Some a -> if a = '-' then -n else n
| None -> n
)
) str
and nt_frac str =
(pack
(caten
nt_int
(pack
(caten
(char '/')
(plus
(range '0' '9')
)
)
(fun (a, b) ->
if b = ['0'] then raise X_no_match (* In this case, the symbol parser will take care of it.*)
else
let n = List.fold_left (fun acc curr -> acc*10 + ((int_of_char curr) - (int_of_char '0'))) 0 b in
n (* Don't need the '/'.. only the number*)
)
)
)
(fun (a, b) ->
let g = (gcd a b) in
ScmRational ((a / g), (b / g))
)) str
and nt_integer_part str = plus (range '0' '9') str
and nt_mantissa str = plus (range '0' '9') str
and nt_exponent str =
let nt1 = pack (char 'e') (fun c -> [c]) in
let nt2 = pack (char 'E') (fun c -> [c]) in
let nt3 = word "*10^" in
let nt4 = word "*10**" in
(caten (disj_list [nt1;nt2;nt3;nt4]) nt_int) str
and nt_float str =
let nt1 = pack (maybe (one_of "-+"))
(fun c -> match c with
| Some s -> [s]
| None -> ['+']) in
let nt_floatA =
pack (caten_list [
nt_integer_part;
word ".";
pack
(maybe nt_mantissa)
(fun a ->
match a with
| Some a -> a
| None -> ['0']
);
pack
(maybe nt_exponent)
(fun a ->
match a with
| Some (a,b) -> List.append ['e'] (string_to_list (string_of_int b))
| _ -> ['e';'0']
)
])
(fun l -> List.flatten l) in
let nt_floatB =
pack (caten_list [
word ".";
nt_mantissa;
pack
(maybe nt_exponent)
(fun a ->
match a with
| Some (a,b) -> List.append ['e'] (string_to_list (string_of_int b))
| _ -> ['e';'0']
)
])
(fun l -> List.flatten l) in
let nt_floatC =
pack (caten_list[
nt_integer_part;
pack nt_exponent (fun (a,b) -> List.append ['e'] (string_to_list (string_of_int b)))
])
(fun l -> List.flatten l) in
(pack (caten nt1 (disj_list [nt_floatA;nt_floatB;nt_floatC]))
(fun (a,b) -> ScmReal (float_of_string (list_to_string (List.append a b))))) str
and nt_number str =
let nt1 = nt_float in
let nt2 = nt_frac in
let nt3 = pack nt_int (fun n -> ScmRational(n, 1)) in
let nt1 = disj nt1 (disj nt2 nt3) in
let nt1 = pack nt1 (fun r -> ScmNumber r) in
let nt1 = not_followed_by nt1 nt_symbol_char in
nt1 str
and nt_boolean str = (pack
(caten (char '#') (one_of_ci "tf")) (fun (a, b) -> if (b = 't' || b = 'T') then (ScmBoolean true) else (ScmBoolean false))) str
and nt_char_simple str = (pack (range '!' '~') (fun a -> [a])) str
and make_named_char char_name ch = pack (word_ci char_name) (fun w -> [ch])
and nt_hexadecimal_char str =
let nt1 = caten (char_ci 'x') (plus nt_char_hex) in
let nt1 = pack nt1 (fun (a, li) -> [(char_of_int (hex_list_to_int li))]) in
nt1 str
and nt_char_named str =
let nt1 =
disj_list [(make_named_char "newline" '\n');
(make_named_char "page" '\012');
(make_named_char "return" '\r');
(make_named_char "space" ' ');
(make_named_char "tab" '\t');
(make_named_char "nul" '\000')
] in
nt1 str
and nt_char_hex str =
let nt1 = disj_list [(range '0' '9');(range_ci 'a' 'f')] in
nt1 str
and nt_char str =
let nt_char_prefix = caten_list [(char '#');(char '\\')] in
let nt1 = disj_list [nt_hexadecimal_char; nt_char_named; nt_char_simple ] in
(pack
(caten nt_char_prefix nt1)
(fun l ->
match l with
| ( _ , [b]) -> ScmChar b
| _ -> raise X_no_match)) str
and nt_symbol_char str = (disj_list [(range '0' '9'); (range 'a' 'z'); (range 'A' 'Z'); (char '!'); (char '$'); (char '<'); (char '>');
(char '='); (char '-'); (char '_'); (char '^'); (char ':'); (char '+'); (char '?'); (char '/'); (char '*')]) str
and nt_symbol str =
let nt1 = plus nt_symbol_char in
let nt1 = pack nt1 list_to_string in
let nt1 = pack nt1 (fun name -> ScmSymbol name) in
let nt1 = diff nt1 nt_number in
nt1 str
and nt_string_char str =
let nt_str_lit_chr = pack (only_if (range (char_of_int 0) '}')
(fun c -> c != '\\' && c != '\"' && c != '~'))
(fun c -> [c]) in
let nt_str_meta_chr = disj
(pack (word "~~")
(fun l -> ['~']))
(disj_list [pack (word "\\\\") (fun s -> ['\\']);
pack (word_ci "\\n") (fun s -> ['\n']);
pack (word_ci "\\t") (fun s -> ['\t']);
pack (word_ci "\\r") (fun s -> ['\r']);
pack (word_ci "\\f") (fun s -> ['\012']);
(word "\\\"")]) in
let nt_str_hex_chr =
pack
(disj
(caten (word "\\x") (caten (plus nt_char_hex) (char ';')) )
(caten (word "\\X") (caten (plus nt_char_hex) (char ';')) ) )
(fun (a,(b, c) ) -> [(char_of_int (hex_list_to_int b) )] ) in
pack (plus (disj_list [nt_str_lit_chr;nt_str_meta_chr;nt_str_hex_chr]))
(fun l -> ScmString (list_to_string(List.fold_left (List.append) [] l))) str
and hex_list_to_int li =
match li with
| [] -> 0
| a::b -> begin
match a with
| char when ((int_of_char a) >= 48 && (int_of_char a) <= 57) ->
((int_of_float (16. ** (float_of_int ((List.length li) - 1)))) * ((int_of_char a) - (int_of_char '0'))) +
(hex_list_to_int b)
| char when ((int_of_char a) >= 65 && (int_of_char a) <= 70) ->
((int_of_float (16. ** (float_of_int ((List.length li) - 1)))) * ((int_of_char a) - 55)) +
(hex_list_to_int b)
| char when ((int_of_char a) >= 97 && (int_of_char a) <= 102) ->
((int_of_float (16. ** (float_of_int ((List.length li) - 1)))) * ((int_of_char a) - 87)) +
(hex_list_to_int b)
| _ -> raise X_no_match
end
and nt_str_interp str =
let nt1 = pack (caten (word "~{") (caten nt_skip_star (char '}')))
(fun _ -> ScmPair (ScmSymbol "format", ScmPair (ScmString "~a", ScmNil))) in
let nt2 = pack (caten (word "~{")
(caten (plus (make_skipped_star nt_sexpr))
(word "}")))
(fun (a,(b,c)) -> ScmPair (ScmSymbol "format" ,
(ScmPair (ScmString "~a" , List.fold_right (fun curr acc -> ScmPair (curr, acc)) b ScmNil)))) in
(disj nt1 nt2) str
and nt_string str =
let nt1 = disj nt_string_char nt_str_interp in
let nt2 = pack (word "\"") (fun _ -> [ScmVoid]) in
(pack (caten_list [nt2;(star nt1);nt2])
(fun l -> match l with
| _::l_2::_ -> begin
match l_2 with
| [] -> ScmString ""
| a::[] -> a
| l_2 -> ScmPair (ScmSymbol "string-append", List.fold_right (fun curr acc -> ScmPair (curr, acc)) l_2 ScmNil)
end
| _ -> raise X_no_match)) str
and nt_vector str =
let nt1 = word "#(" in
let nt2 = caten nt_skip_star (char ')') in
let nt2 = pack nt2 (fun _ -> ScmVector []) in
let nt3 = plus nt_sexpr in
let nt4 = char ')' in
let nt3 = caten nt3 nt4 in
let nt3 = pack nt3 (fun (sexprs, _) -> ScmVector sexprs) in
let nt2 = disj nt2 nt3 in
let nt1 = caten nt1 nt2 in
let nt1 = pack nt1 (fun (_, sexpr) -> sexpr) in
nt1 str
and nt_proper_list str =
let nt1 = caten (char '(') (caten nt_skip_star (char ')') ) in
let nt1 = pack nt1 (fun _ -> ScmNil) in
let nt2 = caten (char '(') (caten (plus (make_skipped_star nt_sexpr) ) (char ')') ) in
let nt2 = pack nt2 (fun (a, (b, c)) -> List.fold_right (fun curr acc -> ScmPair (curr, acc)) b ScmNil) in
let nt1 = disj nt1 nt2 in
nt1 str
and nt_improper_list str =
let nt1 = char '(' in
let nt1 = caten nt1 (plus (make_skipped_star nt_sexpr)) in
let nt1 = pack nt1 (fun (a, b) -> b) in
let nt1 = caten nt1 (char '.') in
let nt1 = pack nt1 (fun (a, b) -> a) in
let nt1 = caten nt1 (make_skipped_star nt_sexpr) in
let nt1 = pack nt1 (fun (a, b) -> List.fold_right (fun curr acc -> ScmPair (curr, acc) ) a b ) in
let nt1 = caten nt1 (char ')') in
let nt1 = pack nt1 (fun (a, b) -> a) in
nt1 str
and nt_list str =
let nt1 = disj nt_proper_list nt_improper_list in
nt1 str
and nt_Quoted str =
let nt1 = pack (caten nt_skip_star (char '\'')) (fun (a, b) -> ScmSymbol ("quote") ) in
let nt1 = pack (caten nt1 (make_skipped_star nt_sexpr) ) (fun (a, b) -> ScmPair (a, ScmPair (b, ScmNil))) in
nt1 str
and nt_Quasi_Quoted str =
let nt1 = pack (caten nt_skip_star (char '`')) (fun (a, b) -> ScmSymbol ("quasiquote") ) in
let nt1 = pack (caten nt1 (make_skipped_star nt_sexpr) ) (fun (a, b) -> ScmPair (a, ScmPair (b, ScmNil))) in
nt1 str
and nt_Unquoted str =
let nt1 = pack (caten nt_skip_star (char ',')) (fun (a, b) -> ScmSymbol ("unquote") ) in
let nt1 = pack (caten nt1 (make_skipped_star nt_sexpr) ) (fun (a, b) -> ScmPair (a, ScmPair (b, ScmNil))) in
nt1 str
and nt_Unquoted_And_Spliced str =
let nt1 = pack (caten nt_skip_star (word ",@")) (fun (a, b) -> ScmSymbol ("unquote-splicing") ) in
let nt1 = pack (caten nt1 (make_skipped_star nt_sexpr) ) (fun (a, b) -> ScmPair (a, ScmPair (b, ScmNil))) in
nt1 str
and nt_quoted_forms str =
let nt1 = disj_list [nt_Quoted; nt_Quasi_Quoted; nt_Unquoted; nt_Unquoted_And_Spliced] in
nt1 str
and nt_sexpr str =
let nt1 =
disj_list [nt_number; nt_boolean; nt_char; nt_symbol;
nt_string; nt_vector; nt_list; nt_quoted_forms] in
let nt1 = make_skipped_star nt1 in
nt1 str;;
end;; (* end of struct Reader *)
let rec string_of_sexpr = function
| ScmVoid -> "#<void>"
| ScmNil -> "()"
| ScmBoolean(false) -> "#f"
| ScmBoolean(true) -> "#t"
| ScmChar('\n') -> "#\\newline"
| ScmChar('\r') -> "#\\return"
| ScmChar('\012') -> "#\\page"
| ScmChar('\t') -> "#\\tab"
| ScmChar(' ') -> "#\\space"
| ScmChar(ch) ->
if (ch < ' ')
then let n = int_of_char ch in
Printf.sprintf "#\\x%x" n
else Printf.sprintf "#\\%c" ch
| ScmString(str) ->
Printf.sprintf "\"%s\""
(String.concat ""
(List.map
(function
| '\n' -> "\\n"
| '\012' -> "\\f"
| '\r' -> "\\r"
| '\t' -> "\\t"
| ch ->
if (ch < ' ')
then Printf.sprintf "\\x%x;" (int_of_char ch)
else Printf.sprintf "%c" ch)
(string_to_list str)))
| ScmSymbol(sym) -> sym
| ScmNumber(ScmRational(0, _)) -> "0"
| ScmNumber(ScmRational(num, 1)) -> Printf.sprintf "%d" num
| ScmNumber(ScmRational(num, -1)) -> Printf.sprintf "%d" (- num)
| ScmNumber(ScmRational(num, den)) -> Printf.sprintf "%d/%d" num den
| ScmNumber(ScmReal(x)) -> Printf.sprintf "%f" x
| ScmVector(sexprs) ->
let strings = List.map string_of_sexpr sexprs in
let inner_string = String.concat " " strings in
Printf.sprintf "#(%s)" inner_string
| ScmPair(ScmSymbol "quote",
ScmPair(sexpr, ScmNil)) ->
Printf.sprintf "'%s" (string_of_sexpr sexpr)
| ScmPair(ScmSymbol "quasiquote",
ScmPair(sexpr, ScmNil)) ->
Printf.sprintf "`%s" (string_of_sexpr sexpr)
| ScmPair(ScmSymbol "unquote",
ScmPair(sexpr, ScmNil)) ->
Printf.sprintf ",%s" (string_of_sexpr sexpr)
| ScmPair(ScmSymbol "unquote-splicing",
ScmPair(sexpr, ScmNil)) ->
Printf.sprintf ",@%s" (string_of_sexpr sexpr)
| ScmPair(car, cdr) ->
string_of_sexpr' (string_of_sexpr car) cdr
and string_of_sexpr' car_string = function
| ScmNil -> Printf.sprintf "(%s)" car_string
| ScmPair(cadr, cddr) ->
let new_car_string =
Printf.sprintf "%s %s" car_string (string_of_sexpr cadr) in
string_of_sexpr' new_car_string cddr
| cdr ->
let cdr_string = (string_of_sexpr cdr) in
Printf.sprintf "(%s . %s)" car_string cdr_string;;