-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.ml
75 lines (69 loc) · 1.99 KB
/
types.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
type value =
QuotedList of value list |
Symbol of string |
Keyword of string |
Variable of string |
Number of int |
Boolean of bool |
String of string |
Plus | Minus | Divide | Multiply | Modulo |
LT | LTE | GT | GTE | EQ | NEQ |
AND | OR |
LParen | RParen | (* TODO: Get rid of this *)
Quote |
EOF
(* S-expression type definition *)
type sexp = Atom of value | List of sexp list
(* Debug functions *)
(* Scheme considers only #f to be false and anything else to be true *)
let boolean_of_string s =
match s with
| "#f" -> false
| _ -> true
exception Unknown_keyword
let rec debug_string_of_value value =
match value with
| QuotedList values ->
"'("^(String.concat ", " (List.map (fun value -> string_of_value value) values))^")"
| Symbol s -> "Symbol("^s^")"
| Variable s -> "Variable("^s^")"
| Keyword kw -> "Keyword("^kw^")"
| Number s -> "Number("^(string_of_int s)^")"
| Quote -> "Quote"
| String s -> "String("^s^")"
| _ -> string_of_value value
and debug_string_of_values values =
"["^(String.concat ", " (List.map (fun value -> debug_string_of_value value) values))^"]"
and string_of_value value =
match value with
| Symbol s -> s
| Keyword kw -> kw
| Variable s -> s
| Number s -> (string_of_int s)
| Boolean s -> if s then "#t" else "#f"
| String s -> s
| LParen -> "LParen"
| RParen -> "RParen"
| Plus -> "+"
| Minus -> "-"
| Divide -> "/"
| Multiply -> "*"
| Modulo -> "%"
| EOF -> "\n"
| LT -> "<"
| LTE -> "<="
| GT -> ">"
| GTE -> ">="
| EQ -> "="
| NEQ -> "/="
| AND -> "and"
| OR -> "or"
| Quote -> "'"
| QuotedList values ->
"("^(String.concat " " (List.map (fun value -> string_of_value value) values))^")"
and string_of_values values =
"["^(String.concat " " (List.map (fun value -> string_of_value value) values))^"]"
let rec string_of_sexp sexpr =
match sexpr with
| Atom x -> "Atom("^(string_of_value x)^")"
| List xs -> "List("^(String.concat " " (List.map string_of_sexp xs))^")"