stringify.ml 5.0 KB

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