stringify.ml 5.7 KB

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