ast.ml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. type loc = string * int * int * int * int
  2. let noloc = ("", 0, 0, 0, 0)
  3. type operator = Neg | Not
  4. | 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 * node list
  9. | ArrayDef of ctype * node list
  10. | ArraySpec of ctype * int
  11. and node =
  12. (* global *)
  13. | Program of node list * loc
  14. | Param of ctype * string * loc
  15. | Dim of string * loc
  16. | FunDec of ctype * string * node list * loc
  17. | FunDef of bool * ctype * string * node list * node * loc
  18. | GlobalDec of ctype * string * loc
  19. | GlobalDef of bool * ctype * string * node option * loc
  20. (* statements *)
  21. | VarDec of ctype * string * node option * loc
  22. | Assign of string * node list option * node * loc
  23. | Return of node * loc
  24. | If of node * node * loc
  25. | IfElse of node * node * node * loc
  26. | While of node * node * loc
  27. | DoWhile of node * node * loc
  28. | For of string * node * node * node * node * loc
  29. | Allocate of string * node list * node * loc
  30. | Expr of node
  31. | Block of node list
  32. (* expressions *)
  33. | BoolConst of bool * loc
  34. | IntConst of int * loc
  35. | FloatConst of float * loc
  36. | ArrayConst of node list * loc
  37. | ArrayScalar of node * loc
  38. | Var of string * loc
  39. | Deref of string * node list * loc
  40. | Monop of operator * node * loc
  41. | Binop of operator * node * node * loc
  42. | Cond of node * node * node * loc
  43. | TypeCast of ctype * node * loc
  44. | FunCall of string * node list * loc
  45. | Arg of node
  46. (* additional types for convenience in traversals *)
  47. | VarLet of node * ctype * int
  48. | VarUse of node * ctype * int
  49. | FunUse of node * node * int
  50. | DimDec of node
  51. | Type of node * ctype
  52. | DummyNode
  53. (* container for command-line arguments *)
  54. type args = {
  55. mutable infile : string option;
  56. mutable outfile : string option;
  57. mutable verbose : int;
  58. mutable cpp : bool;
  59. }
  60. (* intermediate representations between phases *)
  61. type intermediate =
  62. | Args of args
  63. | FileContent of string * string * args
  64. | Ast of node * args
  65. | Assembly of string list * args
  66. (* exceptions *)
  67. exception LocError of loc * string
  68. exception NodeError of node * string
  69. exception CompileError of string
  70. exception EmptyError
  71. exception InvalidNode
  72. exception InvalidInput of string
  73. (*
  74. * Template for node matching follows below.
  75. *
  76. * let rec visit = function
  77. * | Program (decls, loc) ->
  78. * | Param (ctype, name, loc) ->
  79. * | FunDec (ret_type, name, params, loc) ->
  80. * | FunDef (export, ret_type, name, params, body, loc) ->
  81. * | GlobalDec (ctype, name, loc) ->
  82. * | GlobalDef (export, ctype, name, None, loc) ->
  83. * | GlobalDef (export, ctype, name, Some init, loc) ->
  84. *
  85. * | VarDec (ctype, name, None, loc) ->
  86. * | VarDec (ctype, name, Some init, loc) ->
  87. * | Assign (name, value, loc) ->
  88. * | Return (value, loc) ->
  89. * | If (cond, body, loc) ->
  90. * | IfElse (cond, true_body, false_body, loc) ->
  91. * | While (cond, body, loc) ->
  92. * | DoWhile (cond, body, loc) ->
  93. * | For (counter, start, stop, step, body, loc) ->
  94. * | Expr (value) ->
  95. * | Block (stats) ->
  96. *
  97. * | BoolConst (value, loc) ->
  98. * | IntConst (value, loc) ->
  99. * | FloatConst (value, loc) ->
  100. * | ArrayConst (dims, loc) ->
  101. * | ArrayScalar (value, loc) ->
  102. * | Var (name, loc) ->
  103. * | Deref (name, dims, loc) ->
  104. * | Monop (op, value, loc) ->
  105. * | Binop (op, left, right, loc) ->
  106. * | Cond (cond, true_expr, false_expr, loc) ->
  107. * | TypeCast (ctype, value, loc) ->
  108. * | FunCall (name, args, loc) ->
  109. *
  110. * | node -> transform visit node
  111. *
  112. *)