ast.ml 3.7 KB

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