ast.ml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. | Type of ctype
  45. (* container for command-line arguments *)
  46. type args = {
  47. mutable infile : string option;
  48. mutable outfile : string option;
  49. mutable verbose : int;
  50. mutable cpp : bool;
  51. }
  52. (* intermediate representations between phases *)
  53. type intermediate =
  54. | Args of args
  55. | FileContent of string * string * args
  56. | Ast of node * args
  57. | Assembly of string list * args
  58. (* exceptions *)
  59. exception LocError of loc * string
  60. exception NodeError of node * string
  61. exception CompileError of string
  62. exception EmptyError
  63. exception InvalidNode
  64. exception InvalidInput of string
  65. (*
  66. * Template for node matching follows below.
  67. *
  68. * let rec visit = function
  69. * | Program (decls, loc) ->
  70. * | Param (ctype, name, loc) ->
  71. * | FunDec (ret_type, name, params, loc) ->
  72. * | FunDef (export, ret_type, name, params, body, loc) ->
  73. * | GlobalDec (ctype, name, loc) ->
  74. * | GlobalDef (export, ctype, name, None, loc) ->
  75. * | GlobalDef (export, ctype, name, Some init, loc) ->
  76. *
  77. * | VarDec (ctype, name, None, loc) ->
  78. * | VarDec (ctype, name, Some init, loc) ->
  79. * | Assign (name, value, loc) ->
  80. * | Return (value, loc) ->
  81. * | If (cond, body, loc) ->
  82. * | IfElse (cond, true_body, false_body, loc) ->
  83. * | While (cond, body, loc) ->
  84. * | DoWhile (cond, body, loc) ->
  85. * | For (counter, start, stop, step, body, loc) ->
  86. * | Expr (value) ->
  87. *
  88. * | BoolConst (value, loc) ->
  89. * | IntConst (value, loc) ->
  90. * | FloatConst (value, loc) ->
  91. * | ArrayConst (dims, loc) ->
  92. * | ArrayScalar (value, loc) ->
  93. * | Var (name, loc) ->
  94. * | Deref (name, dims, loc) ->
  95. * | Monop (op, value, loc) ->
  96. * | Binop (op, left, right, loc) ->
  97. * | Cond (cond, true_expr, false_expr, loc) ->
  98. * | TypeCast (ctype, value, loc) ->
  99. * | FunCall (name, args, loc) ->
  100. *
  101. * | Statements (stats) ->
  102. *
  103. * | node -> transform visit node
  104. *
  105. *)