Module Ast


module Ast: sig .. end
Definition of the parser generated AST and the runtime AST
Author(s): Tony BenBrahim < tony.benbrahim at gmail.com >


type operator =
| Plus
| Minus
| Times
| Divide
| Modulo
| And
| Or
binary operation operators

type comparator =
| LessThan
| LessThanEqual
| Equal
| GreaterThanEqual
| GreaterThan
| NotEqual
binary comparaison operators

type variable_location =
| GlobalVar of int * int
| LocalVar of int * int * int
location for a variable in the runtime AST for globals, unique id * an index into the global variables array for locals, unique id * an index into the current stackframe * an index into the stack
type replacement = string * expression 
string replacement specification in a template instruction
type replacement_list = replacement list 
list of replacements for a template instructions

type conditional_spec =
| Once
| When of expression
| Loop of string * expression
| CondLoop of expression * string * expression
conditional replacement criteria for a template instruction
type replacement_spec = string * conditional_spec * replacement_list 
a single instruction in a set of template instructions
type template_spec = string option * string 
definition for a line in a template definition

type map_subtype =
| MapSubtype
| ArraySubtype
type of map variable, either a dictionary or an array

type variable_value =
| IntegerValue of int
| FloatValue of float
| StringValue of string
| BooleanValue of bool
| FunctionValue of string list * statement list
| MapValue of (string, variable_value) Hashtbl.t * map_subtype
| Void
variable values used in parsing AST

type runtime_variable_value =
| RIntegerValue of int
| RFloatValue of float
| RStringValue of string
| RBooleanValue of bool
| RFunctionValue of int * int * int * bool * runtime_statement list
* (int * int, runtime_variable_value) Hashtbl.t option
* runtime_expression option
| RLibraryFunction of lib_function_def
| RMapValue of (string, runtime_variable_value) Hashtbl.t * map_subtype
| RVoid
| RUndefined
variable values used in runtime AST

type runtime_env = {
   heap : (int * runtime_variable_value) array; (*heap, arary of tuple of uid and value*)
   stackframes : runtime_variable_value array array; (*array of stackframes*)
   mutable closure_vars : (int * int, runtime_variable_value) Hashtbl.t option; (*map of closure variables*)
   gnames : string array; (*array of global names, indexed by uid*)
   mutable current_line : string * int; (*file and line currently interpreted*)
   callstack : (string * int) Stack.t; (*callstack*)
   mutable skip_callstack_pop : bool; (*to indicate whether the call stack entry was skipped in a recursive call*)
}
The runtime environment. consists of a heap for globals and an array of stackframes to support nested functions

type lib_function_def = {
   name : string list; (*namespace and name of function*)
   args : string list; (*list of arguments*)
   num_args : int; (*number of arguments*)
   vararg : bool; (*flag indicating whether the last argument is vararg*)
   code : runtime_env -> unit; (*function call to invoked the function*)
}
Definition for a library function

type expression =
| Id of string
| VarArg of string
| BinaryOp of expression * operator * expression
| CompOp of expression * comparator * expression
| Not of expression
| FunctionCall of expression * expression list
| MapExpr of (string * expression) list
| ArrayExpr of expression list
| Value of variable_value
| UnboundVar of string
| Assignment of expression * expression
| Declaration of expression * expression
| MemberExpr of expression * expression
| PostFixSum of expression * int
| TernaryCond of expression * expression * expression
expressions used in parsing AST

type runtime_expression =
| RVariable of variable_location
| RVarArg of variable_location
| RBinaryOp of runtime_expression * operator * runtime_expression
| RCompOp of runtime_expression * comparator * runtime_expression
| RNot of runtime_expression
| RFunctionCall of runtime_expression * runtime_expression list
| RMapExpr of (string * runtime_expression) list
| RArrayExpr of runtime_expression list
| RValue of runtime_variable_value
| RAssignment of runtime_expression * runtime_expression
| RDeclaration of runtime_expression * runtime_expression
| RMemberExpr of runtime_expression * runtime_expression
| RPostFixSum of runtime_expression * int
| RTernaryCond of runtime_expression * runtime_expression * runtime_expression
expressions used in runtime AST

type statement =
| ForEach of string * expression * statement * (string * int)
| For of expression * expression * expression * statement
* (string * int)
| ExpressionStatement of expression * (string * int)
| Break of (string * int)
| Continue of (string * int)
| Noop
| Return of expression * (string * int)
| If of expression * statement * statement * (string * int)
| TemplateDef of string * template_spec list * (string * int)
| Instructions of string * string list * replacement_spec list * (string * int)
| StatementBlock of statement list
| Program of statement list
| Import of string * (string * int)
| Switch of expression * statement list * (string * int)
| Case of expression option * (string * int)
| TryCatch of statement * string * statement * (string * int)
| TryFinally of statement * statement * (string * int)
| Throw of expression * (string * int)
statements used in parsing AST

type runtime_statement =
| RForEach of variable_location * runtime_expression * runtime_statement
* (string * int)
| RFor of runtime_expression * runtime_expression * runtime_expression
* runtime_statement * (string * int)
| RExpressionStatement of runtime_expression * (string * int)
| RBreak of (string * int)
| RContinue of (string * int)
| RNoop
| RReturn of runtime_expression * (string * int)
| RIf of runtime_expression * runtime_statement * runtime_statement
* (string * int)
| RStatementBlock of runtime_statement list
| RProgram of runtime_statement list
| RSwitch of runtime_expression * runtime_statement list * (string * int)
| RCase of runtime_expression option * (string * int)
| RTryCatch of runtime_statement * variable_location * runtime_statement
* (string * int)
| RTryFinally of runtime_statement * runtime_statement * (string * int)
| RThrow of runtime_expression * (string * int)
| RFastIterator of variable_location * int * int * int * runtime_statement
* (string * int)
statements used in runtime AST
val is_vararg : string -> bool
determines if a variable is a varag
Returns true if the variable is a vararg, false otherwise
varname : the variable name
val vararg_formalname : string -> string
retuns the name for a vararg
exception CFReturn of runtime_variable_value
control flow exception for return instruction
exception CFBreak
control flow exception for break instruction
exception CFContinue
control flow exception for continue instruction
exception CFUserException of runtime_variable_value * string
exception generated by interpreted throw exception