stringify.ml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 (None, _) ->
  88. "return;"
  89. | Return (Some 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 1l, _) -> ""
  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 pre = function
  110. | [] -> pre
  111. | [last] -> pre ^ last
  112. | "" :: tl -> append pre tl
  113. | hd :: tl -> append (pre ^ hd ^ "\n") tl
  114. in
  115. "{\n" ^ indent (append "" (List.map str body)) ^ "\n}"
  116. (* Expressions *)
  117. | Const (value, _) ->
  118. const2str value
  119. | ArrayConst (dims, _) ->
  120. "[" ^ concat ", " dims ^ "]"
  121. | Var (name, None, _) -> name
  122. | Var (name, Some dims, _) ->
  123. name ^ "[" ^ concat ", " dims ^ "]"
  124. | Monop (op, opnd, _) ->
  125. op2str op ^ str opnd
  126. | Binop (op, left, right, _) ->
  127. "(" ^ str left ^ " " ^ op2str op ^ " " ^ str right ^ ")"
  128. | TypeCast (ctype, value, _) ->
  129. "(" ^ type2str ctype ^ ")" ^ str value
  130. | FunCall (name, args, _) ->
  131. name ^ "(" ^ concat ", " args ^ ")"
  132. | Cond (cond, t, f, _) -> "(" ^ str cond ^ " ? " ^ str t ^ " : " ^ str f ^ ")"
  133. (* Annotation nodes print more information at higher verbosity, for
  134. * debugging purposes *)
  135. | VarLet (dec, dims, value, _) when Globals.args.verbose >= 3 ->
  136. "<let:" ^ node2str (Assign (nameof dec, dims, value, [])) ^ ">"
  137. | VarUse (dec, dims, _) when Globals.args.verbose >= 3 ->
  138. "<use:" ^ node2str (Var (nameof dec, dims, [])) ^ ">"
  139. | FunUse (dec, params, _) when Globals.args.verbose >= 3 ->
  140. "<use:" ^ node2str (FunCall (nameof dec, params, [])) ^ ">"
  141. | Dim (name, _) when Globals.args.verbose >= 3 ->
  142. "<dim:" ^ name ^ ">"
  143. | Arg node when Globals.args.verbose >= 3 ->
  144. "<arg:" ^ str node ^ ">"
  145. | VarDecs nodes when Globals.args.verbose >= 3 ->
  146. String.concat "\n" ("// vardecs" :: List.map str nodes)
  147. | LocalFuns nodes when Globals.args.verbose >= 3 ->
  148. String.concat "\n" ("// localfuns" :: List.map str nodes)
  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. | Arg node -> str node
  157. | VarDecs nodes
  158. | LocalFuns nodes -> concat "\n" nodes
  159. | DummyNode -> "<dummy>"