Module Environment


module Environment: sig .. end
Operations on AST analysis and runtime environments.
Author(s): Tony BenBrahim < tony.benbrahim at gmail.com >

module StringMap: Map.Make(String)
type var_info = int * int 
Variable information, tuple of index into scope and unique id

type rec_varmap = {
   variable_map : var_info StringMap.t; (*map of variable name to variable info*)
   parent : rec_varmap option; (*parent scope variable map, or None if top level scope*)
}
represents variables map in a global or local scope, and reference to parent scope

type var_prop = {
   written_after_declared : bool; (*is the variable assigned after it is declared*)
   read_after_declared : bool; (*is the variable read after declared*)
   declaration_loc : string * int; (*tuple of file where variable is declared and line number*)
}
Properties of variable locations
type label_pos = int * int * int * int 
position of a label with within a template spec, tuple of start begin, start end, end begin, end ending
type template_spec_def = Ast.template_spec list * (string, label_pos) Hashtbl.t *
(string * int)
definition of a template specidifcation, used during validity checking. tuple of sepecfication list and map of labels to label position

type analysis_env = {
   globals : rec_varmap; (*map of global variables*)
   num_globals : int; (*number of globals*)
   locals : rec_varmap list; (*recursive list of stack frames*)
   num_locals : int list; (*number of locals in current stack frame*)
   sdepth : int; (*current stack depth*)
   max_depth : int; (*maximum stack depth encountered*)
   errors : string list; (*list of errors found during analysis*)
   warnings : string list; (*list of warning generated during analysis*)
   unique_id : int; (*counter for next unique id*)
   names : string list; (*list of names encountered*)
   varprops : (int, var_prop) Hashtbl.t; (*properties of variables*)
   imported : string list; (*list of files already imported*)
   templates : (string, template_spec_def) Hashtbl.t; (*map of template names to template definitions*)
   constants : (int, Ast.runtime_variable_value) Hashtbl.t; (*map of variables unique id to declared value*)
}
The analysis environment
val new_analysis_environment : unit -> analysis_env
returns a newly initialized analysis environment
Returns analysis_env
val get_constant_value : analysis_env -> int -> Ast.runtime_variable_value
gets the constant value for a variable
Returns runtime value of variable
env : analysis environment
uid : unique id of variable
val set_constant_value : analysis_env -> int -> Ast.runtime_variable_value -> unit
sets the declaration value for a variable
Returns unit
env : analysis environment
uid : unique id of variable
value : runtime value of variable
val is_constant : analysis_env -> int -> bool
returns whether the variable is a constant
Returns true if the variable is a constant
env : analysis environment
uid : unique id of variable
val declare_variable : analysis_env ->
StringMap.key -> analysis_env * int
declare a variable if it does not exist or create a new entry and return new index
Returns a tuple of the modified environment and uid
env : analysis environment
name : name of variable to declare
val declare_variable_and_value : analysis_env ->
StringMap.key ->
Ast.runtime_variable_value -> analysis_env
declare a variable if it does not exist or create a new entry and return new index, then sets constant value
Returns the modified environment
name : name of variable to declare
env : analysis environment
value : the value to initialize the variable with
exception Variable_not_found of string
internal exception used during analysis
val resolve_variable : StringMap.key ->
analysis_env -> Ast.variable_location
Find variable in analysis scope
Raises Variable_not_found when the variable is not found
Returns location
name : the variable name
env : the analysis environment
val uid_from_loc : Ast.variable_location -> int
returns uid from location
Returns the unique id of the variable
val resolve_variable_value : StringMap.key ->
analysis_env ->
Ast.runtime_variable_value * Ast.variable_location
Find variable and value in analysis scope
Raises Variable_not_found when the variable is not found
Returns tuple of value and location
name : the variable name
env : the analysis environment
val new_analysis_scope : analysis_env -> analysis_env
Setups a new scope within the same global or local scope
Returns a new analysis environment setup for the new scope
env : analysis environment
val pop_scope : analysis_env -> analysis_env
Pops the analysis scope
Returns a new environment with the last scope popped
env : analysis environment
val new_analysis_stackframe : analysis_env -> analysis_env
Create a new stackframe
Returns a new analysis environment with a new stackframe
env : analysis environment
val get_depth : analysis_env -> int
Returns the depth of the current stack frame
Returns the depth of the current stack frame, 0 indexed
env : analysis environment
val add_error : analysis_env ->
string * int -> string -> analysis_env
Add an error to the analysis environemnt
Returns an analysis environment with the error added
env : the analysis environment
codeloc : a filename, line number tuple
message : the error message
val add_warning : analysis_env ->
string * int -> string -> analysis_env
Add a warning to the analysis environemnt
Returns an analysis environment with the warning added
env : the analysis environment
codeloc : a filename, line number tuple
message : the warning message
val has_errors : analysis_env -> bool
Returns true if there are errors in the environment
Returns true if there are errors, false otherwise
env : the analysis environment
val add_import : analysis_env -> string -> analysis_env
adds an import to the list of imports
Returns the modified environment
env : analysis environment
filename : the filename

type var_op_type =
| ReadOp (*variable is read*)
| WriteOp (*variable is written*)
| DeclareOp of (string * int) (*variable is declared*)
| DeclareWriteOp of (string * int) (*variable is declared and written, used for function args*)
type of operation performed on variable
val record_usage : analysis_env ->
Ast.variable_location -> var_op_type -> unit
Records a variables property
Returns unit
env : analysis environment
loc : variable location
val add_template : analysis_env ->
string ->
Ast.template_spec list ->
(string, label_pos) Hashtbl.t ->
string * int -> analysis_env
Adds a template to the environment
Returns a new environment *
name : template name
spec_list : list of line specifications
labels : label positions
val has_import : analysis_env -> string -> bool
checks if a file has already been imported
Returns true if already imported, false otherwise
env : analysis environment
filename : the filename to check
val get_value : Ast.runtime_env -> Ast.variable_location -> Ast.runtime_variable_value
Retrieves a value at a location
Returns the value at the selected location
env : a runtime environment
val set_value : Ast.runtime_env ->
Ast.runtime_variable_value ->
Ast.variable_location -> Ast.runtime_variable_value
Sets a value at a location
Returns the value that was set
env : a runtime environment
value : the value to set
val get_loc_name : Ast.runtime_env -> Ast.variable_location -> string
Returns the name of a location
Returns the name of the variable at location loc
env : the runtime environment