|
1 | 1 | open Core
|
2 | 2 |
|
3 | 3 | (* Lexical tokens *)
|
4 |
| -type token = |
5 |
| - | Space |
6 |
| - | Tab |
7 |
| - | LineFeed |
8 |
| - | EOF |
| 4 | +type token = Space | Tab | LineFeed | EOF |
9 | 5 |
|
10 | 6 | (* Stack manipulation instructions *)
|
11 |
| -type stack_manipulation = |
12 |
| - | Push of int |
13 |
| - | Duplicate |
14 |
| - | Swap |
15 |
| - | Discard |
| 7 | +type stack_manipulation = Push of int | Duplicate | Swap | Discard |
16 | 8 |
|
17 | 9 | (* Arithmetic instructions *)
|
18 | 10 | type arithmetic =
|
19 |
| - | Addtion of int * int |
20 |
| - | Subtraction of int * int |
21 |
| - | Multiplication of int * int |
22 |
| - | Division of int * int |
23 |
| - | Modulo of int * int |
| 11 | + | Addtion of int * int |
| 12 | + | Subtraction of int * int |
| 13 | + | Multiplication of int * int |
| 14 | + | Division of int * int |
| 15 | + | Modulo of int * int |
24 | 16 |
|
25 | 17 | (* Heap access instructions *)
|
26 |
| -type heap_acces = |
27 |
| - | Store |
28 |
| - | Retrieve |
| 18 | +type heap_acces = Store | Retrieve |
29 | 19 |
|
30 | 20 | (* Flow control instructions *)
|
31 | 21 | type flow_control =
|
32 |
| - | Mark of int |
33 |
| - | Call of int |
34 |
| - | UnconditionalJump of int |
35 |
| - | JumpZero of int |
36 |
| - | JumpNegative of int |
37 |
| - | EndSubroutine |
38 |
| - | EndProgram |
| 22 | + | Mark of int |
| 23 | + | Call of int |
| 24 | + | UnconditionalJump of int |
| 25 | + | JumpZero of int |
| 26 | + | JumpNegative of int |
| 27 | + | EndSubroutine |
| 28 | + | EndProgram |
39 | 29 |
|
40 | 30 | (* IO instructions *)
|
41 |
| -type io = |
42 |
| - | OutputCharacter |
43 |
| - | OutputNumber |
44 |
| - | ReadCharacter |
45 |
| - | ReadNumber |
| 31 | +type io = OutputCharacter | OutputNumber | ReadCharacter | ReadNumber |
46 | 32 |
|
47 | 33 | (* Instruction modification parameters *)
|
48 | 34 | type imp =
|
49 |
| - | StackManipulation of stack_manipulation |
50 |
| - | Arithmetic of arithmetic |
51 |
| - | HeapAccess of heap_acces |
52 |
| - | FlowControl of flow_control |
53 |
| - | IO of io |
| 35 | + | StackManipulation of stack_manipulation |
| 36 | + | Arithmetic of arithmetic |
| 37 | + | HeapAccess of heap_acces |
| 38 | + | FlowControl of flow_control |
| 39 | + | IO of io |
54 | 40 |
|
55 | 41 | (* Print the string representation of a token *)
|
56 | 42 | let print_token token =
|
57 |
| - match token with |
58 |
| - | Space -> print_endline "Space" |
59 |
| - | Tab -> print_endline "Tab" |
60 |
| - | LineFeed -> print_endline "LineFeed" |
61 |
| - | EOF -> print_endline "EOF" |
| 43 | + match token with |
| 44 | + | Space -> print_endline "Space" |
| 45 | + | Tab -> print_endline "Tab" |
| 46 | + | LineFeed -> print_endline "LineFeed" |
| 47 | + | EOF -> print_endline "EOF" |
62 | 48 |
|
63 | 49 | (* Convert a char to a token *)
|
64 | 50 | let char_to_token c =
|
65 |
| - match c with |
66 |
| - | ' ' -> Space |
67 |
| - | '\t' -> Tab |
68 |
| - | '\n' -> LineFeed |
69 |
| - | _ -> raise (Invalid_argument "unexpected character") |
| 51 | + match c with |
| 52 | + | ' ' -> Space |
| 53 | + | '\t' -> Tab |
| 54 | + | '\n' -> LineFeed |
| 55 | + | _ -> raise (Invalid_argument "unexpected character") |
70 | 56 |
|
71 | 57 | (* Scan a list of tokens from a string *)
|
72 | 58 | let scan input =
|
73 |
| - let chars = List.init (String.length input) ~f:(String.get input) in |
74 |
| - List.map chars ~f:(fun c -> char_to_token c) |
75 |
| - |
| 59 | + let chars = List.init (String.length input) ~f:(String.get input) in |
| 60 | + List.map chars ~f:(fun c -> char_to_token c) |
0 commit comments