-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ml
More file actions
41 lines (40 loc) · 1.17 KB
/
parser.ml
File metadata and controls
41 lines (40 loc) · 1.17 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
open Parse_utils
open Ast
let parse str =
let simple_statement char =
match char with
| '<' -> Some MoveLeft
| '>' -> Some MoveRight
| '+' -> Some Increase
| '-' -> Some Decrease
| '.' -> Some Output
| ',' -> Some Input
| _ -> None
in
let rec parse' pos result =
if pos.index == String.length str
then
result, None
else
let char = str.[pos.index] in
let next_pos = increment_position pos char in
match simple_statement char with
| Some statement ->
parse' next_pos (statement :: result)
| None ->
match char with
| '[' ->
begin
match parse' next_pos [] with
| inner, Some pos_of_closing_bracket ->
let pos_after_loop = increment_position pos_of_closing_bracket ']' in
parse' pos_after_loop (Loop (List.rev inner) :: result)
| _, None ->
syntax_error "Unclosed '['" pos
end
| ']' -> result, Some pos
| _ -> parse' next_pos result
in
match parse' zero_position [] with
| statements, None -> List.rev statements
| _, Some pos -> syntax_error "Unexpected ']'" pos