ast.ml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. (* intermediate representations between phases *)
  45. type repr =
  46. | Inputfile of string option * int (* filename, verbose *)
  47. | Node of node * int (* ast, verbose *)
  48. | Assembly of string list * int (* instructions *)
  49. exception LocError of string * loc
  50. exception CompileError of string
  51. exception InvalidNode
  52. (*
  53. * Template for node matching follows below.
  54. *
  55. * let rec visit = function
  56. * | Program (decls, loc) ->
  57. * | Param (ctype, name, loc) ->
  58. * | FunDec (ret_type, name, params, loc) ->
  59. * | FunDef (export, ret_type, name, params, body, loc) ->
  60. * | GlobalDec (ctype, name, loc) ->
  61. * | GlobalDef (export, ctype, name, None, loc) ->
  62. * | GlobalDef (export, ctype, name, Some init, loc) ->
  63. *
  64. * | VarDec (ctype, name, None, loc) ->
  65. * | VarDec (ctype, name, Some init, loc) ->
  66. * | Assign (name, value, loc) ->
  67. * | Return (value, loc) ->
  68. * | If (cond, body, loc) ->
  69. * | IfElse (cond, true_body, false_body, loc) ->
  70. * | While (cond, body, loc) ->
  71. * | DoWhile (cond, body, loc) ->
  72. * | For (counter, start, stop, step, body, loc) ->
  73. * | Expr (value) ->
  74. *
  75. * | BoolConst (value, loc) ->
  76. * | IntConst (value, loc) ->
  77. * | FloatConst (value, loc) ->
  78. * | ArrayConst (dims, loc) ->
  79. * | ArrayScalar (value, loc) ->
  80. * | Var (name, loc) ->
  81. * | Deref (name, dims, loc) ->
  82. * | Monop (op, value, loc) ->
  83. * | Binop (op, left, right, loc) ->
  84. * | Cond (cond, true_expr, false_expr, loc) ->
  85. * | TypeCast (ctype, value, loc) ->
  86. * | FunCall (name, args, loc) ->
  87. *
  88. * | Statements (stats) ->
  89. *
  90. * | node -> transform visit node
  91. *
  92. *)