summary refs log tree commit diff
path: root/source/xi_lib/ast_printer.ml
blob: 67b4dd5acd38532679b803063ba97303ed930baf (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
open Ast


let string_of_binop = function
  | BINOP_And -> "&"
  | BINOP_Or -> "|"
  | BINOP_Add -> "+"
  | BINOP_Sub -> "-"
  | BINOP_Mult -> "*"
  | BINOP_Div -> "/"
  | BINOP_Rem -> "%"

let string_of_relop = function
  | RELOP_Eq -> "=="
  | RELOP_Ne -> "!="
  | RELOP_Lt -> "<"
  | RELOP_Gt -> ">"
  | RELOP_Le -> "<="
  | RELOP_Ge -> ">="

let string_of_unop = function
  | UNOP_Not -> "!"
  | UNOP_Neg -> "-"

let indent x = "  " ^ x

let indentxs = List.map indent 

let rec show_expression = function
  | EXPR_Id {id; _} ->
    string_of_identifier id


  | EXPR_Int {value; _} ->
    Int32.to_string value

  | EXPR_Char {value; _} ->
    Format.sprintf "%c" value
 
  | EXPR_String {value; _} ->
    value
  
  | EXPR_Bool {value; _} ->
    string_of_bool value

  | EXPR_Binop {op; lhs; rhs; _} ->
    String.concat ""
      [ "("
      ; show_expression lhs
      ; " "
      ; string_of_binop op
      ; " "
      ; show_expression rhs
      ; ")"
      ]

  | EXPR_Relation {op; lhs; rhs; _} ->
    String.concat ""
      [ "("
      ; show_expression lhs
      ; " "
      ; string_of_relop op
      ; " "
      ; show_expression rhs
      ; ")"
      ]
  
  | EXPR_Length {arg; _} ->
    String.concat ""
      [ "length("
      ; show_expression arg
      ; ")"
      ]

  | EXPR_Unop {op; sub; _} ->
    String.concat ""
      [ string_of_unop op
      ; show_expression sub
      ]

  | EXPR_Call call ->
    show_call call

  | EXPR_Index {expr; index; _} ->
    String.concat ""
      [ show_expression expr
      ; "["
      ; show_expression index
      ; "]"
      ]

  | EXPR_Struct {elements; _} ->
    String.concat ""
      [ "{"
      ; String.concat ", " (List.map show_expression elements)
      ; "}"
      ]

and show_call (Call {callee; arguments; _}) =
  String.concat ""
    [ string_of_identifier callee
    ; "("
    ; String.concat ", " (List.map show_expression arguments)
    ; ")"
    ]

let rec show_type_expression = function
  | TEXPR_Int _ ->
    "int"

  | TEXPR_Bool _ ->
    "bool"

  | TEXPR_Array {sub;dim;_} ->
    String.concat ""
       [ show_type_expression sub
       ; "["
       ; (match dim with | None -> "" | Some e -> show_expression e)
       ; "]"
       ]

let show_var_declaration (VarDecl {id; tp; _}) =
  String.concat ""
    [ string_of_identifier id
    ; ":"
    ; show_type_expression tp
    ]

let show_var_declaration_opt = function
  | Some v -> show_var_declaration v
  | None -> "_"

let show_lvalue = function
  | LVALUE_Id {id; _} ->
    string_of_identifier id
  | LVALUE_Index {sub; index; _} ->
    String.concat ""
      [ show_expression sub
      ; "["
      ; show_expression index
      ; "]"
      ]


let rec showxs_statement = function
  | STMT_Call call ->
    [ show_call call
    ]
  
  | STMT_VarDecl {var; init=None} ->
    [ show_var_declaration var
    ]

  | STMT_VarDecl {var; init=Some v} ->
    [ String.concat " " 
      [ show_var_declaration var
      ; "="
      ; show_expression v
      ]
    ]

  | STMT_Return {values; _} ->
    [ String.concat " " 
      [ "return"
      ; String.concat ", " (List.map show_expression values)
      ]
    ]
  
  | STMT_Block blok ->
    showxs_block blok

  | STMT_While {cond; body; _} ->
    List.concat
      [ [ String.concat " "
        [ "while"
        ; "("
        ; show_expression cond
        ; ")"
        ] ]
      ; showxs_statement_as_block body
      ]

  | STMT_If {cond; then_branch; else_branch=None; _} ->
    List.concat
      [ [ String.concat " "
        [ "if"
        ; "("
        ; show_expression cond
        ; ")"
        ] ]
      ; showxs_statement_as_block then_branch
      ]

  | STMT_If {cond; then_branch; else_branch=Some else_branch; _} ->
    List.concat
      [ [ String.concat " "
        [ "if"
        ; "("
        ; show_expression cond
        ; ")"
        ] ]
      ; showxs_statement_as_block then_branch
      ; [ "else" ]
      ; showxs_statement_as_block else_branch
      ]
    | STMT_Assign {lhs; rhs; _} ->
      [ String.concat " "
        [ show_lvalue lhs
        ; "="
        ; show_expression rhs
        ]
      ]
    | STMT_MultiVarDecl {vars; init; _} ->
      [ String.concat " "
        [ String.concat ", " (List.map show_var_declaration_opt vars)
        ; "="
        ; show_call init
        ]
      ]

and showxs_block = function
  | STMTBlock {body; _} ->
    List.concat
    [ ["{"]
    ; indentxs @@ List.concat @@ List.map showxs_statement body
    ; ["}"]
    ]

and showxs_statement_as_block = function
  | STMT_Block blok ->
    showxs_block blok
  |  s ->
    List.concat
    [ ["{"]
    ; indentxs @@ showxs_statement s
    ; ["}"]
    ]

let show_formal_parameters params =
  String.concat ", " @@ List.map show_var_declaration params


let show_return_types = function
  | [] ->
    ""
  | return_types ->
    ": " ^ String.concat ", " (List.map show_type_expression return_types)

let showxs_global_declaration = function
  | GDECL_Function {id; body; formal_parameters; return_types; _} ->
    List.concat
      [ [ String.concat ""
        [ string_of_identifier id
        ; "("
        ; show_formal_parameters formal_parameters
        ; ")"
        ; show_return_types return_types
        ] ]
      ; match body with
        | Some body -> showxs_block body
        | None -> []
      ]

let showxs_module_definition (ModuleDefinition {global_declarations; _}) = 
  let f x =
    showxs_global_declaration x @ [""]
  in
  List.flatten (List.map f global_declarations)

let show_module_definition m =
  String.concat "\n" @@ showxs_module_definition m