ast.ml 3.6 KB

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