stringify.ml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, _) -> name
  24. | _ -> raise InvalidNode
  25. (* operator -> string *)
  26. let op2str = function
  27. | Neg -> "-"
  28. | Not -> "!"
  29. | Add -> "+"
  30. | Sub -> "-"
  31. | Mul -> "*"
  32. | Div -> "/"
  33. | Mod -> "%"
  34. | Eq -> "=="
  35. | Ne -> "!="
  36. | Lt -> "<"
  37. | Le -> "<="
  38. | Gt -> ">"
  39. | Ge -> ">="
  40. | And -> "&&"
  41. | Or -> "||"
  42. (* ctype -> string *)
  43. let rec type2str = function
  44. | Void -> "void"
  45. | Bool -> "bool"
  46. | Int -> "int"
  47. | Float -> "float"
  48. | ArrayDims (t, dims) -> (type2str t) ^ "[" ^ (concat ", " dims) ^ "]"
  49. | Array t -> (type2str t) ^ "[]"
  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 1, _) -> ""
  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 = function
  108. | [] -> ""
  109. | [last] -> last
  110. | "" :: tl -> append tl
  111. | hd :: tl -> hd ^ "\n" ^ append tl
  112. in
  113. "{\n" ^ indent (append (List.map str body)) ^ "\n}"
  114. (* Expressions *)
  115. | Const (c, _) -> const2str c
  116. | ArrayConst (dims, _) -> "[" ^ concat ", " dims ^ "]"
  117. | Var (v, None, _) -> v
  118. | Var (name, Some dims, _) -> name ^ (str (ArrayConst (dims, [])))
  119. | Monop (op, opnd, _) -> op2str op ^ str opnd
  120. | Binop (op, left, right, _) ->
  121. "(" ^ str left ^ " " ^ op2str op ^ " " ^ str right ^ ")"
  122. | TypeCast (ctype, value, _) -> "(" ^ type2str ctype ^ ")" ^ str value
  123. | FunCall (name, args, _) -> name ^ "(" ^ (concat ", " args) ^ ")"
  124. | Cond (cond, t, f, _) -> "(" ^ (str cond) ^ " ? " ^ str t ^ " : " ^ str f ^ ")"
  125. (* Annotation nodes print more information at higher verbosity, for
  126. * debugging purposes *)
  127. | VarLet (dec, dims, value, _) when args.verbose >= 3 ->
  128. "<let:" ^ node2str (Assign (nameof dec, dims, value, [])) ^ ">"
  129. | VarUse (dec, dims, _) when args.verbose >= 3 ->
  130. "<use:" ^ node2str (Var (nameof dec, dims, [])) ^ ">"
  131. | FunUse (dec, params, _) when args.verbose >= 3 ->
  132. "<use:" ^ node2str (FunCall (nameof dec, params, [])) ^ ">"
  133. | Dim (name, _) when args.verbose >= 3 ->
  134. "<dim:" ^ name ^ ">"
  135. | ArrayScalar value when args.verbose >= 3 ->
  136. "<scalar:" ^ str value ^ ">"
  137. | Arg node when args.verbose >= 3 ->
  138. "<arg:" ^ str node ^ ">"
  139. (*
  140. | VarDecs nodes when args.verbose >= 3 ->
  141. String.concat "\n" ("// vardecs" :: List.map str nodes)
  142. | LocalFuns nodes when args.verbose >= 3 ->
  143. String.concat "\n" ("// localfuns" :: List.map str nodes)
  144. *)
  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)