ast.ml 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. | Array of ctype * node list
  9. | ArrayDepth of ctype * int
  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 list option * 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 * node * 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. | Var of string * loc
  37. | Deref of string * node list * loc
  38. | Monop of operator * node * loc
  39. | Binop of operator * node * node * loc
  40. | TypeCast of ctype * node * loc
  41. | FunCall of string * node list * loc
  42. | Arg of node
  43. (* additional types for convenience in traversals *)
  44. | ArrayScalar of node
  45. | ArrayInit of node * ctype
  46. | Cond of node * node * node * loc
  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_record = {
  55. mutable infile : string option;
  56. mutable outfile : string option;
  57. mutable verbose : int;
  58. mutable cpp : bool;
  59. }
  60. (* Default config *)
  61. let verbosity_default = 2 (* TODO: set to 1 when done with debugging *)
  62. let verbosity_debug = 3
  63. (* Commandline args are stored in a global struct
  64. * (yes, it IS dirty, but I'd rather have this than having to pass [args] around
  65. * everywhere) *)
  66. let args = {
  67. infile = None;
  68. outfile = None;
  69. verbose = verbosity_default;
  70. cpp = true;
  71. }
  72. (* intermediate representations between phases *)
  73. type intermediate =
  74. | Empty
  75. | FileContent of string * string
  76. | Ast of node
  77. | Assembly of string list
  78. (* exceptions *)
  79. exception LocError of loc * string
  80. exception NodeError of node * string
  81. exception CompileError of string
  82. exception EmptyError
  83. exception InvalidNode
  84. exception InvalidInput of string