-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.ml
212 lines (181 loc) · 7.25 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
(*|
| | Tokenization / lexical parser.
|*)
type token =
| Name of string (* Used for function || variable names. *)
| Number of float (* Used for all numbers, integer or float. *)
| String of string (* String lits converted here. *)
| Boolean of bool (* Boolean values. *)
| Bracket of bool (* True if opening, False if closing. *)
| ListBegin (* Special bracket type for square brackets. *)
| Special of char (* Anything that doesn't fall into one of those goes here. *)
;;
let tokenize characters =
let tokens = ref ( [] : token list ) in (* Stores the entire list of tokens. *)
let buffer = Buffer.create 16 in (* Stores metadata for strings and names for identifiers. *)
let accumulator = ref 0.0 in (* Stores metadata for Numbers *)
let length = String.length characters in
let index = ref 0 in
while !index < length do
Buffer.clear buffer;
accumulator := 0.0;
match characters.[!index] with
| (' ' | '\n' | '\r' | '\t') -> index := !index + 1 (* Whitespace? Ignore it. *)
| ('#') -> ( (* Skip over comment lines. *)
while !index < length && characters.[!index] != '\n' do
index := !index + 1
done
)
(* Square Bracket. *)
| ('[') -> (
index := !index + 1;
tokens := !tokens @ [ListBegin]
)
(* Brackets. *)
| ('{' | '(') -> (
index := !index + 1;
tokens := !tokens @ [Bracket true]
)
| (')' | '}' | ']') -> (
index := !index + 1;
tokens := !tokens @ [Bracket false]
)
| ('\'') -> (
(* Multi line string literals.*)
index := !index + 1;
while characters.[!index] != '\'' do
if characters.[!index] = '\\' then ( (* Escape handler. *)
index := !index + 1;
);
Buffer.add_char buffer characters.[!index];
index := !index + 1
done;
index := !index + 1;
tokens := !tokens @ [String (Buffer.contents buffer)]
)
| ('"') -> (
(* One line string literals. *)
index := !index + 1;
while !index < length && characters.[!index] != '\n' do
Buffer.add_char buffer characters.[!index];
index := !index + 1
done;
index := !index + 1;
tokens := !tokens @ [String (Buffer.contents buffer)]
)
| ('=' | ':') -> (
tokens := !tokens @ [Special characters.[!index]];
index := !index + 1
)
| ('0' .. '9') -> (
(* Numeric literals. *)
while
!index < length &&
match characters.[!index] with '0' .. '9' -> true | _ -> false
do
accumulator := !accumulator *. 10.0 +. float_of_int (
int_of_char characters.[!index] - 48
);
index := !index + 1
done;
if !index < length && characters.[!index] = '.' then (
index := !index + 1;
let precision = ref 10.0 in
while
!index < length &&
match characters.[!index] with '0' .. '9' -> true | _ -> false
do
accumulator := !accumulator +. float_of_int (
int_of_char characters.[!index] - 48
) /. !precision;
precision := !precision *. 10.0;
index := !index + 1
done;
);
tokens := !tokens @ [Number !accumulator]
)
| _ -> (
while
!index < length &&
not( String.contains " \n\r\t(){}[]'\"=:" characters.[!index] )
do
Buffer.add_char buffer characters.[!index];
index := !index + 1
done;
tokens := !tokens @ [
if (Buffer.contents buffer) = "true" then Boolean true
else if Buffer.contents buffer = "false" then Boolean false
else Name (Buffer.contents buffer)
]
)
;
done;
!tokens
;;
(*|
| | Abstract syntax tree generation / central parser.
|*)
type tree =
| Call of string * tree list (* Function calls are strings with a list of args. *)
| List of tree list
| Number of float
| String of string
| Boolean of bool
;;
let infixKeywords =
let table = Hashtbl.create 15 in
Hashtbl.add table (Name "is") ("is");
Hashtbl.add table (Name "index") ("index");
Hashtbl.add table (Special '=') ("function");
table
;;
let rec parserLoop tokens =
if tokens = [] || List.hd tokens = Bracket false then
if tokens = [] then ([], []) (* SAFETY DANCE *)
else ([], List.tl tokens)
else
let tree, leftover = parser tokens in
let treelist, leftover = parserLoop leftover in
(tree :: treelist, leftover)
and parser tokens = match tokens with
| Name value::Special '='::leftover ->
let tree, leftover = parser leftover in
( Call ("set", (String value)::[tree]), leftover )
| Name value::Special ':'::leftover ->
let tree, leftover = parser leftover in
( Call (value, [tree]), leftover )
| Name value::Bracket true::leftover ->
let trees, leftover = parserLoop leftover in
infixParser (Call (value, trees)) leftover
| ListBegin::leftover ->
let trees, leftover = parserLoop leftover in
infixParser (List trees) leftover
| Name value::Name infix::leftover -> (
if Hashtbl.mem infixKeywords (Name infix) then
let newTree, leftover = parser leftover in
( Call (Hashtbl.find infixKeywords (Name infix),
(Call (value, [])) :: [newTree]
),
leftover)
else ( Call (value, []), Name infix::leftover )
)
| Name value::leftover -> ( Call (value, []), leftover )
| Number value::leftover -> ( Number value, leftover )
| String value::leftover -> ( String value, leftover )
| Boolean value::leftover -> ( Boolean value, leftover )
| Bracket false::leftover ->
( print_endline "You used an unexpected closing bracket!";
Number 0.0, [] )
| other ->
( print_endline "You mistyped something!";
Number 0.0, [] )
and infixParser tree leftover =
if (List.length leftover < 2) then
(tree, leftover)
else if Hashtbl.mem infixKeywords (List.hd leftover) then
let operator = Hashtbl.find infixKeywords (List.hd leftover) in
let newTree, leftover = parser (List.tl leftover) in
(Call (operator, tree::[newTree]), leftover)
else
(tree, leftover)
;;