stringify.ml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. open Types
  2. let tab = " "
  3. (* string -> string *)
  4. let indent = Str.global_replace (Str.regexp "^\\(.\\)") (tab ^ "\\1")
  5. (* const -> string *)
  6. let const2str = function
  7. | BoolVal b -> string_of_bool b
  8. | IntVal i -> string_of_int i
  9. | FloatVal f ->
  10. (* Add a trailing zero to a float stringification *)
  11. (match string_of_float f with
  12. | s when s.[String.length s - 1] = '.' -> s ^ "0"
  13. | s -> s
  14. )
  15. (* Copied from util.ml to avoid circular dependency *)
  16. let nameof = function
  17. | GlobalDec (_, name, _)
  18. | GlobalDef (_, _, name, _, _)
  19. | FunDec (_, name, _, _)
  20. | FunDef (_, _, name, _, _, _)
  21. | VarDec (_, name, _, _)
  22. | Param (_, name, _)
  23. | Dim (name, _)
  24. | DimDec (name, _, _) -> name
  25. | _ -> raise InvalidNode
  26. (* operator -> string *)
  27. let op2str = function
  28. | Neg -> "-"
  29. | Not -> "!"
  30. | Add -> "+"
  31. | Sub -> "-"
  32. | Mul -> "*"
  33. | Div -> "/"
  34. | Mod -> "%"
  35. | Eq -> "=="
  36. | Ne -> "!="
  37. | Lt -> "<"
  38. | Le -> "<="
  39. | Gt -> ">"
  40. | Ge -> ">="
  41. | And -> "&&"
  42. | Or -> "||"
  43. (* ctype -> string *)
  44. let rec type2str = function
  45. | Void -> "void"
  46. | Bool -> "bool"
  47. | Int -> "int"
  48. | Float -> "float"
  49. | Array (t, dims) -> (type2str t) ^ "[" ^ (concat ", " dims) ^ "]"
  50. | ArrayDepth (t, ndims) -> (type2str t) ^ "[" ^ string_of_int ndims ^ "]"
  51. | FlatArray 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. | DimDec (name, None, _) when args.verbose >= 3 ->
  142. type2str Int ^ " <dim:" ^ name ^ ">;"
  143. | DimDec (name, Some init, _) when args.verbose >= 3 ->
  144. type2str Int ^ " <dim:" ^ name ^ "> = " ^ str init ^ ";"
  145. | DimDec (name, None, _) ->
  146. type2str Int ^ " " ^ name ^ ";"
  147. | DimDec (name, Some init, _) ->
  148. type2str Int ^ " " ^ name ^ " = " ^ str init ^ ";"
  149. | VarLet (dec, dims, value, _) ->
  150. node2str (Assign (nameof dec, dims, value, []))
  151. | VarUse (dec, dims, _) ->
  152. node2str (Var (nameof dec, dims, []))
  153. | FunUse (dec, args, _) ->
  154. node2str (FunCall (nameof dec, args, []))
  155. | Dim (name, _) -> name
  156. | ArrayScalar node
  157. | ArrayInit (node, _)
  158. | Arg node -> str node
  159. | VarDecs nodes
  160. | LocalFuns nodes -> concat "\n" nodes
  161. | DummyNode -> "<dummy>"
  162. (* ctype list -> string *)
  163. let rec types2str = function
  164. | [] -> ""
  165. | [ctype] -> type2str ctype
  166. | ctype :: tail -> type2str ctype ^ " or " ^ (types2str tail)