-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdriver.ml
68 lines (57 loc) · 1.82 KB
/
driver.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
66
67
68
open Printf
open Platform
let print_banner s =
let rec dashes n = if n = 0 then "" else "-"^(dashes (n-1)) in
printf "%s %s\n%!" (dashes (79 - (String.length s))) s
let print_ll file ll_ast =
print_banner (file ^ ".ll");
print_endline (Ll.string_of_prog ll_ast)
let print_x86 file asm_str =
print_banner file;
print_endline asm_str
let read_file (file:string) : string =
let lines = ref [] in
let channel = open_in file in
try while true; do
lines := input_line channel :: !lines
done; ""
with End_of_file ->
close_in channel;
String.concat "\n" (List.rev !lines)
let write_file (file:string) (out:string) =
let channel = open_out file in
fprintf channel "%s" out;
close_out channel
let parse_file filename =
let program = read_file filename |>
Lexing.from_string |>
Llparser.prog Lllexer.token
in
program
let interpret program args : string =
let result = Llinterp.interp_prog program args in
Llinterp.string_of_sval result
let run_executable arg pr =
let cmd = sprintf "%s%s %s" dot_path pr arg in
sh cmd (fun _ i -> i)
let run_executable_to_tmpfile arg pr tmp =
let cmd = sprintf "%s%s %d > %s 2>&1" dot_path pr arg tmp in
sh cmd ignore_error
let string_of_file (f:in_channel) : string =
let rec _string_of_file (stream:string list) (f:in_channel) : string list=
try
let s = input_line f in
_string_of_file (s::stream) f
with
| End_of_file -> stream
in
String.concat "\n" (List.rev (_string_of_file [] f))
let run_program (args:string) (executable:string) (tmp_out:string) : string =
let _ =
let cmd = sprintf "%s%s %s > %s 2>&1" dot_path executable args tmp_out in
sh cmd ignore_error
in
let fi = open_in tmp_out in
let result = string_of_file fi in
let _ = close_in fi in
result