-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNiceDebug.ml
123 lines (113 loc) · 3.29 KB
/
NiceDebug.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
open Core.Std
open Ast
open Format
open Identifier
open Types
open Symbol
let show_offsets = true
let debug_flag = ref false
let debugStrings = ref []
let addDebugString str =
debugStrings := str :: !debugStrings
let print_debug_strings () =
List.rev !debugStrings |> List.iter ~f:(printf "%s\n")
let rec pretty_typ ppf typ =
let pretty_pointer n =
let rec aux str = function
| 0 -> str
| n -> aux (str ^ "*") (n-1)
in aux "" n
in
match typ with
| TYPE_none ->
fprintf ppf "<undefined>"
| TYPE_int n ->
fprintf ppf "int %s" (pretty_pointer n)
| TYPE_char n->
fprintf ppf "char %s" (pretty_pointer n)
| TYPE_bool n->
fprintf ppf "bool %s" (pretty_pointer n)
| TYPE_double n ->
fprintf ppf "double %s" (pretty_pointer n)
| TYPE_array (et, sz) ->
pretty_typ ppf et;
if sz > 0 then
fprintf ppf " [%d]" sz
else
fprintf ppf " []"
| TYPE_void ->
fprintf ppf "proc"
| TYPE_null ->
fprintf ppf "null pointer"
let pretty_mode ppf mode =
match mode with
| PASS_BY_REFERENCE ->
fprintf ppf "reference "
| _ ->
()
let printSymbolTable () =
printf "-------[ PRINTING SYMBOL TABLE ]--------\n";
let rec walk ppf scp =
if scp.sco_nesting <> 0 then begin
fprintf ppf "scope: ";
let entry ppf e =
fprintf ppf "%a" pretty_id e.entry_id;
match e.entry_info with
| ENTRY_none ->
fprintf ppf "<none>"
| ENTRY_variable inf ->
if show_offsets then
fprintf ppf "[%d]" inf.variable_offset
| ENTRY_label _ -> fprintf ppf ""
| ENTRY_function inf ->
let param ppf e =
match e.entry_info with
| ENTRY_parameter inf ->
fprintf ppf "%a%a : %a"
pretty_mode inf.parameter_mode
pretty_id e.entry_id
pretty_typ inf.parameter_type
| _ ->
fprintf ppf "<invalid>" in
let rec params ppf ps =
match ps with
| [p] ->
fprintf ppf "%a" param p
| p :: ps ->
fprintf ppf "%a; %a" param p params ps;
| [] ->
() in
fprintf ppf "(%a) : %a"
params inf.function_paramlist
pretty_typ inf.function_result
| ENTRY_parameter inf ->
if show_offsets then
fprintf ppf "[%d]" inf.parameter_offset
| ENTRY_temporary inf ->
if show_offsets then
fprintf ppf "[%d]" inf.temporary_offset in
let rec entries ppf es =
match es with
| [e] ->
fprintf ppf "%a" entry e
| e :: es ->
fprintf ppf "%a, %a" entry e entries es;
| [] ->
() in
match scp.sco_parent with
| Some scpar ->
fprintf ppf "%a\n%a"
entries scp.sco_entries
walk scpar
| None ->
fprintf ppf "<impossible>\n"
end in
let scope ppf scp =
if scp.sco_nesting = 0 then
fprintf ppf "no scope\n"
else
walk ppf scp in
printf "%a----------------------------------------\n"
scope !currentScope
let printSymbolTableIfDebug () =
if !debug_flag then printSymbolTable ()