-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpretty.ml
65 lines (62 loc) · 1.61 KB
/
pretty.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
open Ast
let character_of_number number =
try Printf.sprintf " (%C)" (Char.chr number)
with Invalid_argument _ -> ""
let name_of_stmt stmt =
match stmt with
| Push _ -> "Push"
| Copy _ -> "Copy"
| Slide _ -> "Slide"
| Mark _ -> "Mark"
| Call _ -> "Call"
| Jump _ -> "Jump"
| JumpIfZero _ -> "Jump if Zero"
| JumpIfNegative _ -> "Jump if Negative"
| Duplicate -> "Duplicate"
| Swap -> "Swap"
| Discard -> "Discard"
| Add -> "Add"
| Subtract -> "Subtract"
| Multiply -> "Multiply"
| Divide -> "Divide"
| Modulo -> "Modulo"
| Store -> "Store"
| Retrieve -> "Retrieve"
| EndSubroutine -> "End Subroutine"
| EndProgram -> "End Program"
| OutputCharacter -> "Output Character"
| OutputNumber -> "Output Number"
| ReadCharacter -> "Read Character"
| ReadNumber -> "Read Number"
let string_of_stmt stmt =
let name = name_of_stmt stmt in
match stmt with
| Push number -> begin
let character =
try Printf.sprintf " (%C)" (Char.chr number)
with Invalid_argument _ -> "" in
Printf.sprintf "%s %d%s" name number character
end
| Copy number
| Slide number -> Printf.sprintf "%s %d" name number
| Mark label
| Call label
| Jump label
| JumpIfZero label
| JumpIfNegative label -> Printf.sprintf "%s %s" name label
| Duplicate
| Swap
| Discard
| Add
| Subtract
| Multiply
| Divide
| Modulo
| Store
| Retrieve
| EndSubroutine
| EndProgram
| OutputCharacter
| OutputNumber
| ReadCharacter
| ReadNumber -> name