stringify.ml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. | Dim (name, _) -> name
  46. | FunDec (ret_type, name, params, _) ->
  47. let params = concat ", " params in
  48. "extern " ^ type2str ret_type ^ " " ^ name ^ "(" ^ params ^ ");"
  49. | FunDef (export, ret_type, name, params, body, _) ->
  50. let export = if export then "export " else "" in
  51. let params = "(" ^ (concat ", " params) ^ ")" in
  52. export ^ type2str ret_type ^ " " ^ name ^ params ^ " " ^ str body
  53. | GlobalDec (var_type, name, _) ->
  54. "extern " ^ type2str var_type ^ " " ^ name ^ ";"
  55. | GlobalDef (export, ret_type, name, init, _) ->
  56. let export = if export then "export " else "" in
  57. let init = match init with
  58. | Some value -> " = " ^ str value
  59. | None -> ""
  60. in
  61. export ^ (type2str ret_type) ^ " " ^ name ^ init ^ ";"
  62. (* Statements *)
  63. | VarDec (var_type, name, None, _) ->
  64. (type2str var_type) ^ " " ^ name ^ ";"
  65. | VarDec (var_type, name, Some init, _) ->
  66. (type2str var_type) ^ " " ^ name ^ " = " ^ (str init) ^ ";"
  67. | Assign (name, None, value, _) ->
  68. name ^ " = " ^ (str value) ^ ";"
  69. | Assign (name, Some dims, value, _) ->
  70. name ^ "[" ^ (concat ", " dims) ^ "] = " ^ (str value) ^ ";"
  71. | Expr expr ->
  72. str expr ^ ";"
  73. | Return (value, _) ->
  74. "return " ^ (str value) ^ ";"
  75. | If (cond, body, _) ->
  76. "if (" ^ str cond ^ ") " ^ str body
  77. | IfElse (cond, true_body, false_body, _) ->
  78. "if (" ^ str cond ^ ") " ^ str true_body ^ " else " ^ str false_body
  79. | While (cond, body, _) ->
  80. "while (" ^ str cond ^ ") " ^ str body
  81. | DoWhile (cond, body, _) ->
  82. "do " ^ str body ^ " while (" ^ str cond ^ ");"
  83. | For (counter, start, stop, step, body, _) ->
  84. let step = match step with
  85. | IntConst (1, _) -> ""
  86. | value -> ", " ^ str value
  87. in
  88. let range = str start ^ ", " ^ str stop ^ step in
  89. "for (int " ^ counter ^ " = " ^ range ^ ") " ^ str body
  90. | Allocate (name, dims, _, _) ->
  91. name ^ " := <allocate>(" ^ concat ", " dims ^ ");"
  92. | Block body -> "{\n" ^ indent (concat "\n" body) ^ "\n}"
  93. (* Expressions *)
  94. | BoolConst (b, _) -> string_of_bool b
  95. | IntConst (i, _) -> string_of_int i
  96. | FloatConst (f, _) -> float2string f
  97. | ArrayConst (dims, _) -> "[" ^ concat ", " dims ^ "]"
  98. | Var (v, _) -> v
  99. | Deref (name, dims, _) -> name ^ (str (ArrayConst (dims, noloc)))
  100. | Monop (op, opnd, _) -> op2str op ^ str opnd
  101. | Binop (op, left, right, _) ->
  102. "(" ^ str left ^ " " ^ op2str op ^ " " ^ str right ^ ")"
  103. | TypeCast (ctype, value, _) -> "(" ^ type2str ctype ^ ")" ^ str value
  104. | FunCall (name, args, _) -> name ^ "(" ^ (concat ", " args) ^ ")"
  105. | Cond (cond, t, f, _) -> (str cond) ^ " ? " ^ str t ^ " : " ^ str f
  106. (* Some intermediate nodes print more information at higher verbosity, for
  107. * debugging purposes *)
  108. | ArrayScalar value when args.verbose >= 3 ->
  109. "<scalar>(" ^ str value ^ ")"
  110. | Arg node when args.verbose >= 3 ->
  111. "<arg>(" ^ str node ^ ")"
  112. | Type (node, ctype) when args.verbose >= 3 ->
  113. str node ^ ":" ^ type2str ctype
  114. | VarUse (value, ctype, _) when args.verbose >= 3 ->
  115. "<use:" ^ type2str ctype ^ ">(" ^ str value ^ ")"
  116. | FunUse (value, _, _) when args.verbose >= 3 ->
  117. "<use>(" ^ str value ^ ")"
  118. | ArrayScalar node
  119. | ArrayInit (node, _)
  120. | Arg node
  121. | Type (node, _)
  122. | FunUse (node, _, _)
  123. | VarLet (node, _, _)
  124. | VarUse (node, _, _) -> str node
  125. | _ -> raise InvalidNode
  126. (* ctype list -> string *)
  127. let rec types2str = function
  128. | [] -> ""
  129. | [ctype] -> type2str ctype
  130. | ctype :: tail -> type2str ctype ^ " or " ^ (types2str tail)