ast.ml 3.1 KB

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