ast.ml 3.6 KB

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