ast.ml 3.3 KB

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