stringify.ml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. | ArrayDec (t, dims)
  29. | ArrayDef (t, dims) -> (type2str t) ^ "[" ^ (concat ", " dims) ^ "]"
  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, value, _) ->
  63. name ^ " = " ^ (str value) ^ ";"
  64. | Expr expr ->
  65. str expr ^ ";"
  66. | Return (value, _) ->
  67. "return " ^ (str value) ^ ";"
  68. | If (cond, body, _) ->
  69. "if (" ^ str cond ^ ") " ^ str body
  70. | IfElse (cond, true_body, false_body, _) ->
  71. "if (" ^ str cond ^ ") " ^ str true_body ^ " else " ^ str false_body
  72. | While (cond, body, _) ->
  73. "while (" ^ str cond ^ ") " ^ str body
  74. | DoWhile (cond, body, _) ->
  75. "do " ^ str body ^ " while (" ^ str cond ^ ");"
  76. | For (counter, start, stop, step, body, _) ->
  77. let step = match step with
  78. | IntConst (1, _) -> ""
  79. | value -> ", " ^ str value
  80. in
  81. let range = str start ^ ", " ^ str stop ^ step in
  82. "for (int " ^ counter ^ " = " ^ range ^ ") " ^ str body
  83. | Allocate (name, dims, _) ->
  84. name ^ " = __allocate(" ^ concat ", " dims ^ ");"
  85. | Block body -> "{\n" ^ indent (concat "\n" body) ^ "\n}"
  86. (* Expressions *)
  87. | BoolConst (b, _) -> string_of_bool b
  88. | IntConst (i, _) -> string_of_int i
  89. | FloatConst (f, _) -> string_of_float f
  90. | ArrayConst (dims, _) -> "[" ^ concat ", " dims ^ "]"
  91. | ArrayScalar (value, _) -> str value
  92. | Var (v, _) -> v
  93. | Deref (name, dims, _) -> name ^ (str (ArrayConst (dims, noloc)))
  94. | Monop (op, opnd, _) -> op2str op ^ str opnd
  95. | Binop (op, left, right, _) ->
  96. "(" ^ str left ^ " " ^ op2str op ^ " " ^ str right ^ ")"
  97. | Cond (cond, t, f, _) -> (str cond) ^ " ? " ^ str t ^ " : " ^ str f
  98. | TypeCast (ctype, value, _) -> "(" ^ type2str ctype ^ ")" ^ str value
  99. | FunCall (name, args, _) -> name ^ "(" ^ (concat ", " args) ^ ")"
  100. | Arg node
  101. | VarUse (node, _, _)
  102. | FunUse (node, _, _) -> str node
  103. | Type (node, ctype) -> str node ^ ":" ^ type2str ctype
  104. | _ -> raise InvalidNode
  105. (* ctype list -> string *)
  106. let rec types2str = function
  107. | [] -> ""
  108. | [ctype] -> type2str ctype
  109. | ctype :: tail -> type2str ctype ^ " or " ^ (types2str tail)