Skip to content

Commit 2c0847c

Browse files
committed
Add .ocamlformat and apply style
1 parent f1f5035 commit 2c0847c

File tree

4 files changed

+43
-61
lines changed

4 files changed

+43
-61
lines changed

.ocamlformat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
profile = ocamlformat
2+
break-cases = fit

blank.ml

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ open Core
22

33
(* Get the input filename *)
44
let get_filename =
5-
try Some (Array.get (Sys.get_argv ()) 1)
6-
with Invalid_argument _ -> None
5+
try Some (Array.get (Sys.get_argv ()) 1) with Invalid_argument _ -> None
76

87
let () =
9-
match get_filename with
10-
| Some filename ->
11-
let tokens = Parse.scan (In_channel.read_all filename) in
12-
List.iter tokens ~f:(fun token -> Parse.print_token token)
13-
| None -> print_endline "usage: blank FILENAME"
8+
match get_filename with
9+
| Some filename ->
10+
let tokens = Parse.scan (In_channel.read_all filename) in
11+
List.iter tokens ~f:(fun token -> Parse.print_token token)
12+
| None -> print_endline "usage: blank FILENAME"

lib/parse.ml

+33-48
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,60 @@
11
open Core
22

33
(* Lexical tokens *)
4-
type token =
5-
| Space
6-
| Tab
7-
| LineFeed
8-
| EOF
4+
type token = Space | Tab | LineFeed | EOF
95

106
(* 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
168

179
(* Arithmetic instructions *)
1810
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
2416

2517
(* Heap access instructions *)
26-
type heap_acces =
27-
| Store
28-
| Retrieve
18+
type heap_acces = Store | Retrieve
2919

3020
(* Flow control instructions *)
3121
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
3929

4030
(* IO instructions *)
41-
type io =
42-
| OutputCharacter
43-
| OutputNumber
44-
| ReadCharacter
45-
| ReadNumber
31+
type io = OutputCharacter | OutputNumber | ReadCharacter | ReadNumber
4632

4733
(* Instruction modification parameters *)
4834
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
5440

5541
(* Print the string representation of a token *)
5642
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"
6248

6349
(* Convert a char to a token *)
6450
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")
7056

7157
(* Scan a list of tokens from a string *)
7258
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)

test/test.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ let test_scan _ =
77
(* Check correct tokens are scanned. *)
88
assert_equal [Space; Tab; LineFeed; Space] (scan input)
99

10-
let suite =
11-
"ScannerTests" >::: [
12-
"test_scan" >:: test_scan
13-
]
10+
let suite = "ScannerTests" >::: ["test_scan" >:: test_scan]
1411

15-
let () =
16-
run_test_tt_main suite
12+
let () = run_test_tt_main suite

0 commit comments

Comments
 (0)