ast.ml 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. type monop = Neg | Not
  2. type binop = Add | Sub | Mul | Div | Mod
  3. | Eq | Ne | Lt | Le | Gt | Ge
  4. | And | Or
  5. type ctype = Void | Bool | Int | Float
  6. | ArrayDec of ctype * string list
  7. | ArrayDef of ctype * node list
  8. and node =
  9. (* Global *)
  10. | Program of node list
  11. | Param of ctype * string
  12. | FunDec of ctype * string * node list
  13. | FunDef of bool * ctype * string * node list * node list
  14. | GlobalDec of ctype * string
  15. | GlobalDef of bool * ctype * string * node option
  16. (* Statements *)
  17. | VarDec of ctype * string * node option
  18. | Assign of string * node
  19. | Return of node
  20. | If of node * node list
  21. | IfElse of node * node list * node list
  22. | While of node * node list
  23. | DoWhile of node * node list
  24. | For of string * node * node * node * node list
  25. | Expr of node
  26. (* Expressions *)
  27. | BoolConst of bool
  28. | IntConst of int
  29. | FloatConst of float
  30. | ArrayConst of node list
  31. | Var of string
  32. | Deref of string * node list
  33. | Monop of monop * node
  34. | Binop of binop * node * node
  35. | Cond of node * node * node
  36. | TypeCast of ctype * node
  37. | FunCall of string * node list
  38. (* Intermediate representations between phases *)
  39. type repr =
  40. | Inputfile of string option * int (* filename, verbose *)
  41. | Node of node * int (* ast, verbose *)
  42. | Assembly of string list * int (* instructions *)
  43. exception CompileError of string
  44. (*
  45. * Template for node matching follows below.
  46. *
  47. * let rec visit = function
  48. * | Program (decls) ->
  49. * | Param (ctype, name) ->
  50. * | FunDec (ret_type, name, params) ->
  51. * | FunDef (export, ret_type, name, params, body) ->
  52. * | GlobalDec (ctype, name) ->
  53. * | GlobalDef (export, ctype, name, None) ->
  54. * | GlobalDef (export, ctype, name, Some init) ->
  55. *
  56. * | VarDec (ctype, name, None) ->
  57. * | VarDec (ctype, name, Some init) ->
  58. * | Assign (name, value) ->
  59. * | Return (value) ->
  60. * | If (cond, body) ->
  61. * | IfElse (cond, true_body, false_body) ->
  62. * | While (cond, body) ->
  63. * | DoWhile (cond, body) ->
  64. * | For (counter, start, stop, step, body) ->
  65. * | Expr (value) ->
  66. *
  67. * | BoolConst (value) ->
  68. * | IntConst (value) ->
  69. * | FloatConst (value) ->
  70. * | Var (name) ->
  71. * | Monop (op, value) ->
  72. * | Binop (op, left, right) ->
  73. * | Cond (cond, true_expr, false_expr) ->
  74. * | TypeCast (ctype, value) ->
  75. * | FunCall (name, args) ->
  76. *
  77. * | node -> transform visit node
  78. *
  79. *)