types.mli 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. (** Type definitions for abstract syntax tree, assembly code, and exceptions. *)
  2. (** *)
  3. type location = string * int * int * int * int
  4. (** *)
  5. type operator =
  6. | Neg | Not
  7. | Add | Sub | Mul | Div | Mod
  8. | Eq | Ne | Lt | Le | Gt | Ge
  9. | And | Or
  10. (** *)
  11. type const =
  12. | BoolVal of bool
  13. | IntVal of int
  14. | FloatVal of float
  15. (** *)
  16. type ctype =
  17. | Void | Bool | Int | Float | Array of ctype
  18. | ArrayDims of ctype * node list
  19. (** *)
  20. and annotation =
  21. | Loc of location
  22. | Depth of int
  23. | Index of int
  24. | Type of ctype
  25. | LabelName of string
  26. (** Shorthand for annotation list, only to be used by {! Types.node} definitions
  27. below. *)
  28. and ann = annotation list
  29. (** *)
  30. and node =
  31. (* Global *)
  32. | Program of node list * ann
  33. (* list of declarations *)
  34. | FunDec of ctype * string * node list * ann
  35. (* ret_type, name, params *)
  36. | FunDef of bool * ctype * string * node list * node * ann
  37. (* export, ret_type, name, params, body *)
  38. | GlobalDec of ctype * string * ann
  39. (* type, name *)
  40. | GlobalDef of bool * ctype * string * node option * ann
  41. (* export, type, name, initialisation? *)
  42. | Param of ctype * string * ann
  43. (* type, name *)
  44. | Dim of string * ann
  45. (* name *)
  46. | VarDecs of node list
  47. | LocalFuns of node list
  48. (* Statements *)
  49. | VarDec of ctype * string * node option * ann
  50. (* type, name, initialisation? *)
  51. | Assign of string * node list option * node * ann
  52. (* name, indices?, value *)
  53. | For of string * node * node * node * node * ann
  54. (* counter, start, stop, step, body *)
  55. | Allocate of node * node list * ann
  56. (* dec, dims # name = __allocate(dims) *)
  57. | Return of node * ann (* return <value>; *)
  58. | Expr of node (* <expr>; *)
  59. | Block of node list (* { <body> } *)
  60. | If of node * node * ann (* cond, body *)
  61. | IfElse of node * node * node * ann (* cond, true_body, false_body *)
  62. | While of node * node * ann (* cond, body *)
  63. | DoWhile of node * node * ann (* cond, body *)
  64. (* Expressions *)
  65. | Const of const * ann (* bool|int|float value *)
  66. | ArrayConst of node list * ann (* [<exprs>] *)
  67. | Var of string * node list option * ann (* <name> [<indices>]? *)
  68. | Monop of operator * node * ann (* op, operand *)
  69. | Binop of operator * node * node * ann (* op, left, right *)
  70. | TypeCast of ctype * node * ann (* (type) operand *)
  71. | FunCall of string * node list * ann (* name(args) *)
  72. | Arg of node (* function argument *)
  73. (* Additional types for convenience in traversals
  74. * Mostly used to annotate existing nodes with information from declarations *)
  75. | VarUse of node * node list option * ann (* Same as Var, but with decl. *)
  76. | FunUse of node * node list * ann (* Same as FunCall, but with decl. *)
  77. | VarLet of node * node list option * node * ann (* replacement for Assign *)
  78. | ArrayScalar of node (* (Bool|Int|Float)Const *)
  79. | ArrayInit of node * node list (* Array(Scalar|Const), dimensions *)
  80. | Cond of node * node * node * ann (* cond, true_expr, false_expr *)
  81. | DummyNode (* null node, pruned by traversals *)
  82. type stack_scope = Glob | Local | Rel of int | Current
  83. type rtn_scope = ExternFun of int | LocalFun of int * string
  84. type instr =
  85. | Comment of string (* # <comment> *)
  86. | InlineComment of instr * string (* <instr> # <comment> *)
  87. | Label of string (* <label>: *)
  88. (* Directives *)
  89. (* .export "<name>" <ret_type> [ <arg_type>; ... ] <label> *)
  90. | Export of string * ctype * ctype list * string
  91. (* .import "<name>" <ret_type> [ <arg_type>; ... ] *)
  92. | Import of string * ctype * ctype list
  93. (* .const <value> *)
  94. | ConstDef of const
  95. (* .global <type> *)
  96. | Global of ctype
  97. | Store of ctype * stack_scope * int (* [ifba]store[ gn] *)
  98. | Load of ctype * stack_scope * int (* [ifb]load[ gn] G *)
  99. | LoadConst of ctype * int (* [ifb]loadc C *)
  100. | LoadImm of const (* [ifb]load_[01tf] <value> *)
  101. (* Operators *)
  102. | Op of operator * ctype (* [ifb]() *)
  103. | Convert of ctype * ctype (* i2f|f2i *)
  104. | Inc of int * int (* iinc L C *)
  105. | Dec of int * int (* idec L C *)
  106. | IncOne of int (* iinc_1 L *)
  107. | DecOne of int (* idec_1 L *)
  108. (* Control flow *)
  109. | RtnInit of stack_scope
  110. | RtnJmp of rtn_scope
  111. | RtnEnter of int
  112. | Ret of ctype
  113. | Branch of bool * string
  114. | Jump of string
  115. (* Stack management *)
  116. | Pop of ctype (* [ifb]pop *)
  117. (* Arrays *)
  118. | NewArray of ctype * int
  119. | LoadArray of ctype
  120. | StoreArray of ctype
  121. | EmptyLine
  122. | DummyInstr
  123. (* Intermediate representations between phases *)
  124. type intermediate =
  125. | Empty
  126. | FileContent of string * string
  127. | Ast of node
  128. | Assembly of instr list
  129. (** Container for command-line arguments. *)
  130. type args_record = {
  131. mutable infile : string option;
  132. mutable outfile : string option;
  133. mutable verbose : int;
  134. mutable cpp : bool;
  135. mutable optimize : bool;
  136. mutable endphase : string;
  137. }
  138. (* Exceptions *)
  139. exception LocError of location * string
  140. exception NodeError of node * string
  141. exception CompileError of string
  142. exception EmptyError
  143. exception InvalidNode
  144. exception InvalidInput of string