stringify.ml 5.5 KB

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