Source file Basic_parse.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
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
type op = Add | Sub | Mul | Div | Pow | Eq | Ne | Lt | Le | Gt | Ge | And | Or
type expr =
| Num of float
| Str of string
| Var of string
| Index of string * expr list
| Call of string * expr list
| Fn of string * expr
| Neg of expr
| Not of expr
| Bin of op * expr * expr
type item = Expr of expr | Tab of expr | Spc of expr
type sep = Semi | Comma | Newline
type lvalue = Scalar of string | Elem of string * expr list
type datum = D_num of float | D_str of string
type stmt =
| Print of (item * sep) list
| Input of string option * lvalue list
| Let of lvalue * expr
| If of expr
| Goto of expr
| Gosub of expr
| On of expr * bool * int list
| Return
| For of string * expr * expr * expr option
| Next of string list
| Dim of (string * expr list) list
| Data of datum list
| Read of lvalue list
| Restore
| Def of string * string * expr
| End
| Stop
| Rem
| List
| Run
| New
| Bye
| Fp
| Int
| Catalog
| Load of string
| Save of string
| Run_file of string
type line = Numbered of int * stmt list option | Direct of stmt list
let functions = [ "INT"; "ABS"; "SGN"; "SQR"; "RND"; "SIN"; "COS"; "TAN"; "ATN"; "EXP"; "LOG"; "LEN"; "VAL"; "ASC"; "CHR$"; "STR$"; "LEFT$"; "RIGHT$"; "MID$" ]
let keywords =
[ "PRINT"; "INPUT"; "LET"; "IF"; "THEN"; "GOTO"; "GOSUB"; "ON"; "RETURN"; "FOR"; "TO"; "STEP"; "NEXT"; "DIM"; "DATA"; "READ";
"RESTORE"; "DEF"; "FN"; "END"; "STOP"; "REM"; "AND"; "OR"; "NOT"; "TAB("; "SPC(" ]
@ functions
let is_string (name : string) : bool = name <> "" && name.[String.length name - 1] = '$'
let capitals (s : string) : string =
let inside = ref false in
String.map
(fun c ->
if c = '"' then inside := not !inside;
if !inside then c else Char.uppercase_ascii c)
s
type p = { s : string; mutable i : int }
exception Syntax of string
let fail (msg : string) = raise (Syntax msg)
let skip (p : p) : unit =
while p.i < String.length p.s && p.s.[p.i] = ' ' do
p.i <- p.i + 1
done
let peek (p : p) : char option =
skip p;
if p.i < String.length p.s then Some p.s.[p.i] else None
let at_end (p : p) : bool = peek p = None
let at_stmt_end (p : p) : bool = match peek p with None | Some ':' -> true | _ -> false
let accept (p : p) (c : char) : bool =
if peek p = Some c then begin
p.i <- p.i + 1;
true
end
else false
let expect (p : p) (c : char) : unit = if not (accept p c) then fail (Printf.sprintf "%c EXPECTED" c)
let keyword (p : p) (k : string) : bool =
let start = p.i in
let ok = String.for_all (fun c -> accept p c) k in
if not ok then p.i <- start;
ok
let looking_at_keyword (p : p) : bool =
List.exists
(fun k ->
let start = p.i in
let found = keyword p k in
p.i <- start;
found)
keywords
let is_digit c = c >= '0' && c <= '9'
let is_letter c = c >= 'A' && c <= 'Z'
let number (p : p) : float =
skip p;
let start = p.i in
let digits () =
while p.i < String.length p.s && is_digit p.s.[p.i] do
p.i <- p.i + 1
done
in
digits ();
if p.i < String.length p.s && p.s.[p.i] = '.' then begin
p.i <- p.i + 1;
digits ()
end;
if p.i < String.length p.s && p.s.[p.i] = 'E' then begin
let before = p.i in
p.i <- p.i + 1;
if p.i < String.length p.s && (p.s.[p.i] = '+' || p.s.[p.i] = '-') then p.i <- p.i + 1;
let digits_start = p.i in
digits ();
if p.i = digits_start then p.i <- before
end;
match float_of_string_opt (String.sub p.s start (p.i - start)) with
| Some f when p.i > start -> f
| _ -> fail "NUMBER EXPECTED"
let line_number (p : p) : int =
let f = number p in
if Float.is_integer f && f >= 0. && f <= 63999. then int_of_float f else fail "BAD LINE NUMBER"
let name (p : p) : string =
match peek p with
| Some c when is_letter c && not (looking_at_keyword p) ->
let start = p.i in
p.i <- p.i + 1;
while
p.i < String.length p.s
&& (is_letter p.s.[p.i] || is_digit p.s.[p.i])
&& not (looking_at_keyword p)
do
p.i <- p.i + 1
done;
if p.i < String.length p.s && p.s.[p.i] = '$' then p.i <- p.i + 1;
String.sub p.s start (p.i - start)
| _ -> fail "VARIABLE EXPECTED"
let string_lit (p : p) : string =
expect p '"';
match String.index_from_opt p.s p.i '"' with
| Some j ->
let s = String.sub p.s p.i (j - p.i) in
p.i <- j + 1;
s
| None ->
let s = String.sub p.s p.i (String.length p.s - p.i) in
p.i <- String.length p.s;
s
let rec expr (p : p) : expr =
let rec more acc = if keyword p "OR" then more (Bin (Or, acc, conj p)) else acc in
more (conj p)
and conj (p : p) : expr =
let rec more acc = if keyword p "AND" then more (Bin (And, acc, negation p)) else acc in
more (negation p)
and negation (p : p) : expr = if keyword p "NOT" then Not (negation p) else comparison p
and comparison (p : p) : expr =
let relop () =
if accept p '<' then Some (if accept p '>' then Ne else if accept p '=' then Le else Lt)
else if accept p '>' then Some (if accept p '=' then Ge else if accept p '<' then Ne else Gt)
else if accept p '=' then Some (if accept p '<' then Le else if accept p '>' then Ge else Eq)
else None
in
let rec more acc = match relop () with Some op -> more (Bin (op, acc, sum p)) | None -> acc in
more (sum p)
and sum (p : p) : expr =
let rec more acc =
if accept p '+' then more (Bin (Add, acc, product p)) else if accept p '-' then more (Bin (Sub, acc, product p)) else acc
in
more (product p)
and product (p : p) : expr =
let rec more acc =
if accept p '*' then more (Bin (Mul, acc, unary p)) else if accept p '/' then more (Bin (Div, acc, unary p)) else acc
in
more (unary p)
and unary (p : p) : expr = if accept p '-' then Neg (unary p) else if accept p '+' then unary p else power p
and power (p : p) : expr =
let rec more acc = if accept p '^' then more (Bin (Pow, acc, if accept p '-' then Neg (atom p) else atom p)) else acc in
more (atom p)
and args (p : p) : expr list =
expect p '(';
let rec rest acc = if accept p ',' then rest (expr p :: acc) else List.rev acc in
let first = expr p in
let l = rest [ first ] in
expect p ')';
l
and atom (p : p) : expr =
match peek p with
| Some '(' ->
p.i <- p.i + 1;
let e = expr p in
expect p ')';
e
| Some '"' -> Str (string_lit p)
| Some c when is_digit c || c = '.' -> Num (number p)
| Some _ when keyword p "FN" ->
let f = name p in
(match args p with [ e ] -> Fn (f, e) | _ -> fail "ONE ARGUMENT EXPECTED")
| Some _ -> (
match List.find_opt (fun f -> keyword p f) functions with
| Some f -> Call (f, args p)
| None ->
let v = name p in
if peek p = Some '(' then Index (v, args p) else Var v)
| None -> fail "EXPRESSION EXPECTED"
let lvalue (p : p) : lvalue =
let v = name p in
if peek p = Some '(' then Elem (v, args p) else Scalar v
let rec comma_list (f : p -> 'a) (p : p) : 'a list =
let x = f p in
if accept p ',' then x :: comma_list f p else [ x ]
let rec print_items (p : p) : (item * sep) list =
if at_stmt_end p then []
else if accept p ',' then (Expr (Str ""), Comma) :: print_items p
else if accept p ';' then print_items p
else
let item =
if keyword p "TAB(" then begin
let e = expr p in
expect p ')';
Tab e
end
else if keyword p "SPC(" then begin
let e = expr p in
expect p ')';
Spc e
end
else Expr (expr p)
in
if accept p ',' then (item, Comma) :: print_items p
else if accept p ';' then (item, Semi) :: print_items p
else if at_stmt_end p then [ (item, Newline) ]
else (item, Semi) :: print_items p
let datum (p : p) : datum =
if peek p = Some '"' then D_str (string_lit p)
else begin
let start = p.i in
while p.i < String.length p.s && p.s.[p.i] <> ',' && p.s.[p.i] <> ':' do
p.i <- p.i + 1
done;
let s = String.trim (String.sub p.s start (p.i - start)) in
match float_of_string_opt s with Some f when s <> "" -> D_num f | _ -> D_str s
end
let rec statement (p : p) : stmt list =
let command k c = if keyword p k && at_end p then Some c else None in
let commands = [ ("LIST", List); ("RUN", Run); ("NEW", New); ("BYE", Bye); ("FP", Fp); ("INT", Int) ] in
let start = p.i in
let file k c =
p.i <- start;
if keyword p k && not (at_end p) then begin
let name = String.trim (String.sub p.s p.i (String.length p.s - p.i)) in
p.i <- String.length p.s;
Some (c name)
end
else None
in
let files = [ ("LOAD", fun n -> Load n); ("SAVE", fun n -> Save n); ("RUN", fun n -> Run_file n) ] in
match
match List.find_map (fun (k, c) -> p.i <- start; command k c) (("CATALOG", Catalog) :: commands) with
| Some c -> Some c
| None -> List.find_map (fun (k, c) -> file k c) files
with
| Some c -> [ c ]
| None ->
p.i <- start;
if keyword p "PRINT" then [ Print (print_items p) ]
else if accept p '?' then [ Print (print_items p) ]
else if keyword p "INPUT" then begin
let prompt =
if peek p = Some '"' then begin
let s = string_lit p in
if not (accept p ';' || accept p ',') then fail "; EXPECTED";
Some s
end
else None
in
[ Input (prompt, comma_list lvalue p) ]
end
else if keyword p "IF" then begin
let c = expr p in
if keyword p "THEN" then
match peek p with Some d when is_digit d -> [ If c; Goto (Num (float_of_int (line_number p))) ] | _ -> If c :: statement p
else if keyword p "GOTO" then [ If c; Goto (Num (float_of_int (line_number p))) ]
else fail "THEN EXPECTED"
end
else if keyword p "GOTO" then [ Goto (expr p) ]
else if keyword p "GOSUB" then [ Gosub (expr p) ]
else if keyword p "ON" then begin
let e = expr p in
let sub = if keyword p "GOSUB" then true else if keyword p "GOTO" then false else fail "GOTO EXPECTED" in
[ On (e, sub, comma_list line_number p) ]
end
else if keyword p "RETURN" then [ Return ]
else if keyword p "FOR" then begin
let v = name p in
expect p '=';
let a = expr p in
if not (keyword p "TO") then fail "TO EXPECTED";
let b = expr p in
let step = if keyword p "STEP" then Some (expr p) else None in
[ For (v, a, b, step) ]
end
else if keyword p "NEXT" then [ Next (if at_stmt_end p then [] else comma_list name p) ]
else if keyword p "DIM" then
[ Dim (comma_list (fun p -> let v = name p in (v, args p)) p) ]
else if keyword p "DATA" then [ Data (comma_list datum p) ]
else if keyword p "READ" then [ Read (comma_list lvalue p) ]
else if keyword p "RESTORE" then [ Restore ]
else if keyword p "DEF" then begin
if not (keyword p "FN") then fail "FN EXPECTED";
let f = name p in
expect p '(';
let x = name p in
expect p ')';
expect p '=';
[ Def (f, x, expr p) ]
end
else if keyword p "END" then [ End ]
else if keyword p "STOP" then [ Stop ]
else if keyword p "REM" then begin
p.i <- String.length p.s;
[ Rem ]
end
else begin
ignore (keyword p "LET");
let v = lvalue p in
expect p '=';
[ Let (v, expr p) ]
end
let rec statements (p : p) : stmt list =
let first = statement p in
if accept p ':' then first @ statements p else if at_end p then first else fail "SYNTAX"
let parse_line (line : string) : (line, string) result =
let p = { s = capitals line; i = 0 } in
try
match peek p with
| Some c when is_digit c ->
let n = line_number p in
Ok (if at_end p then Numbered (n, None) else Numbered (n, Some (statements p)))
| _ -> Ok (Direct (statements p))
with Syntax msg -> Error msg