1
1
open ! IStd
2
2
open Go_ast_to_json_t
3
3
4
- let funcs_map = Int.Table. create () ~size: 8
4
+ let func_decls_map = Int.Table. create () ~size: 8
5
+ let func_types_map = Int.Table. create () ~size: 8
6
+ let fields_map = Int.Table. create () ~size: 8
5
7
let labeled_stmts_map = Int.Table. create () ~size: 8
6
8
7
9
let concatmap sep fn l = String. concat ~sep: (sep) (List. map ~f: (fn) l)
8
10
9
11
let rec pretty_file go_file =
12
+ List. iter ~f: (get_def) go_file.defs;
10
13
concatmap " \n " pretty_decl go_file.decls
11
14
15
+ and get_def = function
16
+ | `FuncType (t ) -> Hashtbl. replace func_types_map t.uid t
17
+ | `Field (field ) -> Hashtbl. replace fields_map field.uid field
18
+
12
19
and pretty_decl = function
13
20
| `GenDecl (decl ) -> pretty_gen_decl decl
14
- | `FuncDecl (decl ) -> pretty_func_decl decl
15
- | `FuncDeclRef (ref ) ->
16
- (match Hashtbl. find funcs_map ref with
17
- | None -> raise (Failure " Should not happen" )
18
- | Some (s ) -> s )
21
+ | `FuncDecl (decl ) -> Hashtbl. replace func_decls_map decl.uid decl; pretty_func_decl decl
22
+ | `FuncDeclRef (ref ) -> pretty_func_decl (Hashtbl. find_exn func_decls_map ref )
19
23
20
24
and pretty_func_decl fdecl =
21
- match Hashtbl. find funcs_map fdecl.uid with
22
- | None -> " func " ^ (pretty_ident fdecl.name) ^ (pretty_func_type fdecl.func_type) ^ " {\n " ^ (pretty_stmt_type fdecl.body) ^ " \n }\n "
23
- | Some (s ) -> s
25
+ " func " ^ (pretty_ident fdecl.name) ^ (pretty_func_type (get_func_type fdecl.func_desc)) ^ " {\n " ^ (pretty_stmt_type fdecl.body) ^ " \n }\n "
24
26
25
27
and pretty_value_spec (vspec : value_spec_type ) : string =
26
28
if (List. length vspec.names > 1 ) then raise (Failure " Only single variable declaration supported for now" ) else (
@@ -41,7 +43,7 @@ and pretty_value_spec (vspec : value_spec_type) : string =
41
43
)
42
44
43
45
and pretty_spec = function
44
- | `ValueSpec (vspec ) -> pretty_value_spec vspec
46
+ | `ValueSpec (spec : value_spec_type ) -> pretty_value_spec spec
45
47
46
48
and pretty_gen_decl gdecl =
47
49
if (List. length gdecl.specs > 1 ) then raise (Failure " Only one declaration specification supported for now" ) else (
@@ -54,13 +56,11 @@ and pretty_ident ident =
54
56
| Some (o ) ->
55
57
match o with
56
58
| `FuncDecl (decl ) ->
57
- let s = pretty_func_decl decl in
58
- Hashtbl. replace funcs_map ~key: decl.uid ~data: s;
59
- ident.id
59
+ Hashtbl. replace func_decls_map decl.uid decl;
60
+ ident.id
60
61
| `LabeledStmt (stmt ) ->
61
- let s = pretty_labeled_stmt stmt in
62
- Hashtbl. replace labeled_stmts_map ~key: stmt.uid ~data: s;
63
- ident.id
62
+ Hashtbl. replace labeled_stmts_map stmt.uid stmt;
63
+ ident.id
64
64
| _ -> ident.id
65
65
66
66
and pretty_star_expr (expr : star_expr_type ) : string =
@@ -77,11 +77,15 @@ and pretty_expr = function
77
77
| `BasicLit (lit ) -> lit.value
78
78
| `CallExpr (expr ) -> pretty_call_expr expr
79
79
80
- and pretty_res_typ = function
81
- | `Field (field ) -> pretty_expr field.t
80
+ and get_field = function
81
+ | `FieldRef (ref ) -> Hashtbl. find_exn fields_map ref
82
+
83
+ and pretty_res_typ f =
84
+ let field = get_field f in
85
+ pretty_expr field.t
82
86
83
- and pretty_param = function
84
- | `Field ( field ) ->
87
+ and pretty_param f =
88
+ let field = get_field f in
85
89
if (List. length field.names > 1 ) then raise (Failure " Function parameter can have only one name" ) else (
86
90
pretty_ident (List. nth_exn field.names 0 ) ^ " " ^ pretty_expr field.t
87
91
)
@@ -91,6 +95,11 @@ and pretty_func_type func_type =
91
95
" (" ^ (concatmap " , " pretty_param func_type.params) ^ " ) " ^ (pretty_res_typ (List. nth_exn func_type.results 0 ))
92
96
)
93
97
98
+ and get_func_type = function
99
+ | `FuncTypeRef (ref ) ->
100
+ Hashtbl. find_exn func_types_map ref
101
+
102
+
94
103
and pretty_decl_stmt stmt =
95
104
pretty_decl stmt.decl
96
105
@@ -128,9 +137,7 @@ and pretty_labeled_stmt stmt =
128
137
pretty_ident stmt.label ^ " :\n " ^ pretty_stmt stmt.stmt
129
138
130
139
and pretty_labeled_stmt_ref ref =
131
- (match Hashtbl. find labeled_stmts_map ref with
132
- | None -> raise (Failure " Should not happen" )
133
- | Some (s ) -> s )
140
+ pretty_labeled_stmt (Hashtbl. find_exn labeled_stmts_map ref )
134
141
135
142
136
143
and pretty_stmt = function
@@ -142,7 +149,7 @@ and pretty_stmt = function
142
149
| `ForStmt (stmt ) -> pretty_for_stmt stmt
143
150
| `IncDecStmt (stmt ) -> pretty_inc_dec_stmt stmt
144
151
| `BranchStmt (stmt ) -> pretty_branch_stmt stmt
145
- | `LabeledStmt (stmt ) -> pretty_labeled_stmt stmt
152
+ | `LabeledStmt (stmt ) -> Hashtbl. replace labeled_stmts_map stmt.uid stmt; pretty_labeled_stmt stmt
146
153
| `LabeledStmtRef (ref ) -> pretty_labeled_stmt_ref ref
147
154
| `EmptyStmt (stmt ) -> " "
148
155
0 commit comments