stringify.ml 4.7 KB

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