-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.ml
44 lines (42 loc) · 874 Bytes
/
reader.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
exception Unexpected_right_paren
type expression =
| List of expression list
| Number of int * int
| String of string
| Symbol of string
| Quote of expression
| True
| False
| Nil
| Eof
let rec read scanner =
let token = Scanner.scan scanner in
match token with
| Token.LParen ->
read_list scanner
| Token.RParen ->
raise Unexpected_right_paren
| Token.String s ->
String s
| Token.Number (n, d) ->
Number ((int_of_string n), (int_of_string d))
| Token.Identifier id -> (
match id with
| "True" -> True
| "False" -> False
| "Nil" -> Nil
| name -> Symbol name
)
| Token.Eof ->
Eof
| Token.Quote ->
Quote (read scanner)
and read_list scanner =
let rec loop exp =
try
loop (read scanner :: exp)
with
| Unexpected_right_paren ->
exp
in
List (List.rev (loop []))