stringify.ml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. open Types
  2. open Globals
  3. let tab = " "
  4. (* string -> string *)
  5. let indent = Str.global_replace (Str.regexp "^\\(.\\)") (tab ^ "\\1")
  6. (* const -> string *)
  7. let const2str = function
  8. | BoolVal b -> string_of_bool b
  9. | IntVal i -> string_of_int i
  10. | FloatVal f ->
  11. (* Add a trailing zero to a float stringification *)
  12. begin
  13. match string_of_float f with
  14. | s when s.[String.length s - 1] = '.' -> s ^ "0"
  15. | s -> s
  16. end
  17. (* Copied from util.ml to avoid circular dependency *)
  18. let nameof = function
  19. | GlobalDec (_, name, _)
  20. | GlobalDef (_, _, name, _, _)
  21. | FunDec (_, name, _, _)
  22. | FunDef (_, _, name, _, _, _)
  23. | VarDec (_, name, _, _)
  24. | Param (_, name, _)
  25. | Dim (name, _) -> name
  26. | _ -> raise InvalidNode
  27. (* operator -> string *)
  28. let op2str = function
  29. | Neg -> "-"
  30. | Not -> "!"
  31. | Add -> "+"
  32. | Sub -> "-"
  33. | Mul -> "*"
  34. | Div -> "/"
  35. | Mod -> "%"
  36. | Eq -> "=="
  37. | Ne -> "!="
  38. | Lt -> "<"
  39. | Le -> "<="
  40. | Gt -> ">"
  41. | Ge -> ">="
  42. | And -> "&&"
  43. | Or -> "||"
  44. (* ctype -> string *)
  45. let rec type2str = function
  46. | Void -> "void"
  47. | Bool -> "bool"
  48. | Int -> "int"
  49. | Float -> "float"
  50. | ArrayDims (t, dims) -> (type2str t) ^ "[" ^ (concat ", " dims) ^ "]"
  51. | Array t -> (type2str t) ^ "[]"
  52. and concat sep nodes = String.concat sep (List.map node2str nodes)
  53. (* node -> string *)
  54. and node2str node =
  55. let str = node2str in
  56. match node with
  57. (* Global *)
  58. | Program (decls, _) ->
  59. concat "\n\n" decls
  60. | Param (param_type, name, _) ->
  61. (type2str param_type) ^ " " ^ name
  62. | FunDec (ret_type, name, params, _) ->
  63. let params = concat ", " params in
  64. "extern " ^ type2str ret_type ^ " " ^ name ^ "(" ^ params ^ ");"
  65. | FunDef (export, ret_type, name, params, body, _) ->
  66. let export = if export then "export " else "" in
  67. let params = "(" ^ (concat ", " params) ^ ")" in
  68. export ^ type2str ret_type ^ " " ^ name ^ params ^ " " ^ str body
  69. | GlobalDec (var_type, name, _) ->
  70. "extern " ^ type2str var_type ^ " " ^ name ^ ";"
  71. | GlobalDef (export, ret_type, name, init, _) ->
  72. let export = if export then "export " else "" in
  73. let init = match init with
  74. | Some value -> " = " ^ str value
  75. | None -> ""
  76. in
  77. export ^ (type2str ret_type) ^ " " ^ name ^ init ^ ";"
  78. (* Statements *)
  79. | VarDec (var_type, name, None, _) ->
  80. (type2str var_type) ^ " " ^ name ^ ";"
  81. | VarDec (var_type, name, Some init, _) ->
  82. (type2str var_type) ^ " " ^ name ^ " = " ^ str init ^ ";"
  83. | Assign (name, None, value, _) ->
  84. name ^ " = " ^ (str value) ^ ";"
  85. | Assign (name, Some dims, value, _) ->
  86. name ^ "[" ^ (concat ", " dims) ^ "] = " ^ (str value) ^ ";"
  87. | Expr expr ->
  88. str expr ^ ";"
  89. | Return (value, _) ->
  90. "return " ^ (str value) ^ ";"
  91. | If (cond, body, _) ->
  92. "if (" ^ str cond ^ ") " ^ str body
  93. | IfElse (cond, true_body, false_body, _) ->
  94. "if (" ^ str cond ^ ") " ^ str true_body ^ " else " ^ str false_body
  95. | While (cond, body, _) ->
  96. "while (" ^ str cond ^ ") " ^ str body
  97. | DoWhile (cond, body, _) ->
  98. "do " ^ str body ^ " while (" ^ str cond ^ ");"
  99. | For (counter, start, stop, step, body, _) ->
  100. let step = match step with
  101. | Const (IntVal 1, _) -> ""
  102. | value -> ", " ^ str value
  103. in
  104. let range = str start ^ ", " ^ str stop ^ step in
  105. "for (int " ^ counter ^ " = " ^ range ^ ") " ^ str body
  106. | Allocate (dec, dims, _) ->
  107. nameof dec ^ " := <allocate>(" ^ concat ", " dims ^ ");"
  108. | Block body ->
  109. let rec append = function
  110. | [] -> ""
  111. | [last] -> last
  112. | "" :: tl -> append tl
  113. | hd :: tl -> hd ^ "\n" ^ append tl
  114. in
  115. "{\n" ^ indent (append (List.map str body)) ^ "\n}"
  116. (* Expressions *)
  117. | Const (c, _) -> const2str c
  118. | ArrayConst (dims, _) -> "[" ^ concat ", " dims ^ "]"
  119. | Var (v, None, _) -> v
  120. | Var (name, Some dims, _) -> name ^ (str (ArrayConst (dims, [])))
  121. | Monop (op, opnd, _) -> op2str op ^ str opnd
  122. | Binop (op, left, right, _) ->
  123. "(" ^ str left ^ " " ^ op2str op ^ " " ^ str right ^ ")"
  124. | TypeCast (ctype, value, _) -> "(" ^ type2str ctype ^ ")" ^ str value
  125. | FunCall (name, args, _) -> name ^ "(" ^ (concat ", " args) ^ ")"
  126. | Cond (cond, t, f, _) -> "(" ^ (str cond) ^ " ? " ^ str t ^ " : " ^ str f ^ ")"
  127. (* Annotation nodes print more information at higher verbosity, for
  128. * debugging purposes *)
  129. | VarLet (dec, dims, value, _) when args.verbose >= 3 ->
  130. "<let:" ^ node2str (Assign (nameof dec, dims, value, [])) ^ ">"
  131. | VarUse (dec, dims, _) when args.verbose >= 3 ->
  132. "<use:" ^ node2str (Var (nameof dec, dims, [])) ^ ">"
  133. | FunUse (dec, params, _) when args.verbose >= 3 ->
  134. "<use:" ^ node2str (FunCall (nameof dec, params, [])) ^ ">"
  135. | Dim (name, _) when args.verbose >= 3 ->
  136. "<dim:" ^ name ^ ">"
  137. | ArrayScalar value when args.verbose >= 3 ->
  138. "<scalar:" ^ str value ^ ">"
  139. | Arg node when args.verbose >= 3 ->
  140. "<arg:" ^ str node ^ ">"
  141. | VarDecs nodes when args.verbose >= 3 ->
  142. String.concat "\n" ("// vardecs" :: List.map str nodes)
  143. | LocalFuns nodes when args.verbose >= 3 ->
  144. String.concat "\n" ("// localfuns" :: List.map str nodes)
  145. | VarLet (dec, dims, value, _) ->
  146. node2str (Assign (nameof dec, dims, value, []))
  147. | VarUse (dec, dims, _) ->
  148. node2str (Var (nameof dec, dims, []))
  149. | FunUse (dec, args, _) ->
  150. node2str (FunCall (nameof dec, args, []))
  151. | Dim (name, _) -> name
  152. | ArrayScalar node
  153. | ArrayInit (node, _)
  154. | Arg node -> str node
  155. | VarDecs nodes
  156. | LocalFuns nodes -> concat "\n" nodes
  157. | DummyNode -> "<dummy>"
  158. (* ctype list -> string *)
  159. let rec types2str = function
  160. | [] -> ""
  161. | [ctype] -> type2str ctype
  162. | ctype :: tail -> type2str ctype ^ " or " ^ (types2str tail)