ast.ml 2.9 KB

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