-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlow_level_tree_printer.ml
More file actions
24 lines (20 loc) · 1.01 KB
/
low_level_tree_printer.ml
File metadata and controls
24 lines (20 loc) · 1.01 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
open Low_level_tree
let string_of_value = function
| Memory offset -> string_of_memory offset
| Const x -> string_of_int x
let rec string_of_statement = function
| Set (offset, value) -> string_of_memory offset ^ " = " ^ string_of_value value ^ ";\n"
| Add (offset, value) -> string_of_memory offset ^ " += " ^ string_of_value value ^ ";\n"
| Move offset -> "ptr += " ^ string_of_int offset ^ ";\n"
| Output value -> "putchar( " ^ string_of_value value ^ " );\n"
| Input offset -> string_of_memory offset ^ " = getchar();\n"
| Loop body -> "while(*ptr) {\n" ^ string_of_statements body ^ "}\n"
| BalancedLoop (offset, body) -> "while(" ^ string_of_memory offset ^ ") {\n" ^ string_of_statements body ^ "}\n"
| Load offset -> "load(" ^ string_of_memory offset ^ ");\n"
| Store -> "store();\n"
and string_of_statements statements =
String.concat "" (List.map string_of_statement statements)
let print title statements =
print_endline title;
print_string (string_of_statements statements);
print_newline ()