Module Pascal_ast


module Pascal_ast: sig  end
Abstract syntax tree with a Pascal looking-like structure.
Author(s): Samuel Mimram, Julien Cristau

exception Parse_error of (string * (int * int))
A parsing error occured. The arguments are the column and line numbers.

type pascal_type =
| Integer
| Boolean
| Record of (string * pascal_type) list
| Array of (pascal_type * expression * expression)
| User_type of string
| Unit
| Type_type
| Unknown_type
Types of values.

type nature =
| Varb
| Cst
| Fun
| Unknown_nature

type variable = {
   var_name : string;
   mutable var_type : pascal_type;
   mutable var_def : definition option;
   mutable var_nature : nature;
   var_pos : int * int;
}
A reference to an identifier. var_by_name is only meaningful for procedures' and functions' arguments (yet) and should be set to false else.

type expression = {
   expr_val : expr;
   mutable expr_type : pascal_type;
   expr_pos : int * int; (*position in the source code (line, column)*)
}
A typed expression.

type expr =
| Lt of (expression * expression) (*less than*)
| Le of (expression * expression) (*less or equal*)
| Eq of (expression * expression) (*equal*)
| Neq of (expression * expression) (*not equal*)
| Uminus of expression (*unary minus*)
| Plus of (expression * expression) (*plus*)
| Minus of (expression * expression) (*minus*)
| Or of (expression * expression) (*logical or*)
| Mult of (expression * expression) (*times*)
| Div of (expression * expression) (*divided by*)
| Mod of (expression * expression) (*modulo*)
| And of (expression * expression) (*logical and*)
| Not of expression (*logical not*)
| Int of int (*an integer*)
| Bool of bool (*a boolean*)
| Var of (variable * expression list) (*a variable or a function*)
| Arr of (expression * expression) (*an array*)
| Rec of (expression * string) (*a member of a structure*)
An expression.

type stt =
| Assign of (variable * expression) (*assignation of a value to a variable*)
| Proc of (variable * expression list) (*procedure call*)
| ExtProc of (string * expression list) (*an external procedure call (writeInt, etc.)*)
| If of (expression * statement * statement) (*if ... then ... else ... (if there is no else then the second statement should be Noop)*)
| While of (expression * statement)
| Compound of statement list
| Noop (*take some rest*)
A statement.

type statement = {
   stt_value : stt;
   stt_pos : int * int; (*position in source code*)
   mutable stt_asm : (Asm_ast.variable_def list * Asm_ast.meta_instr) option; (*for 'if' and 'while'*)
}
type environment = (string * definition) list 

type block = {
   mutable block_env : environment; (*the preliminary variables declarations*)
   block_stts : statement list; (*the instructions*)
}
A block (i.e. a list of statements in an environment).
type procedure = definition list * block 
A procedure (arguments, code).

type value =
| Constant of expression
| Variable (*a varaiable containing a value (not passed by reference)*)
| Variable_ref (*a variable passed by reference*)
| Procedure of procedure
| Function of (definition list * block)
| Type of pascal_type
What an identifier can be.

type asm_value =
| Asm_var of Asm_ast.variable_def
| Asm_proc of Asm_ast.procedure
| Asm_none
Definition in the assembly side.

type definition = {
   def_name : string;
   mutable def_type : pascal_type;
   def_nature : nature;
   mutable def_value : value;
   def_pos : int * int;
   mutable def_coord : int list; (*lexical position*)
   mutable def_used : bool; (*for basic dead-code elimination purposes*)
   mutable def_asm : asm_value;
}
An identifier definition.
exception Already_defined
A variable is being redefined.
val create_environment : unit -> environment
Create a new empty environment.
val fold_env : (string -> definition -> 'a -> 'a) ->
'a -> environment -> 'a
Fold a function throughout an environment.
val iter_env : (string -> definition -> unit) -> environment -> unit
Iter a function on all elements of an environment.
val env_of_def_list : definition list -> (string * definition) list
val add_def : environment ->
string -> definition -> (string * definition) list
Add a definition to an environment.
Raises Already_defined if a previous definition was already made with the same name.
val get_def : environment -> string -> definition
Get a the value associated to a name in an environment. Remark: don't forget that procedures and functions are binders and should therefore be added to the current environment.
Raises Not_found if no definition was made for that name.
val concat_env : bool ->
(string * definition) list ->
environment -> environment
Concatenate two environments. The first argument determines whereas the function should check for duplicate definitions or not.
val string_of_type : pascal_type -> string
type program = string * block 
A program.
val check_block : block -> environment list -> unit
Do several checks on a block:
Raises Parse_error if an error was detected.
val check : 'a * block -> unit
Check that the program is syntaxically and lexically correct.
val pretty_print : string * block -> string
Get a nice human-readable representation of the AST.
exception Asm_error of string
An error occured during the pascal-to-asm conversion.
val eval_expr_int : expression -> int
Compute the value of a constant integer expression.
val eval_expr_bool : expression -> bool
val eval_expr : expression -> Asm_ast.value
val sizeof : pascal_type -> int
Get the size needed by a type (in bytes).
val regc : int Pervasives.ref
val fresh_reg : unit -> int
val asm_of_proc : string ->
procedure ->
int list -> Asm_ast.procedure list * Asm_ast.procedure
Get an asm-like AST corresponding to a pascal-like AST.
Returns a list of procedures incidently defined and the Asm_ast.procedure corresponding to the procedure.
val asm_of_prog : 'a * block -> Asm_ast.procedure list * Asm_ast.procedure