
000001| (**

000002| This program is free software; you can redistribute it and / or modify

000003| it under the terms of the GNU General Public License as published by

000004| the Free Software Foundation; version 3 of the License.

000005|

000006| This program is distributed in the hope that it will be useful,

000007| but WITHOUT ANY WARRANTY; without even the implied warranty of

000008| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

000009| GNU General Public License for more details.

000010|

000011| Definition of the parser generated AST and the runtime AST

000012|

000013| @author Tony BenBrahim < tony.benbrahim at gmail.com >

000014|

000015| *)

000016| (** binary operation operators *)

000017| type operator = | Plus | Minus | Times | Divide | Modulo | And | Or

000018|

000019| (** binary comparaison operators *)

000020| type comparator =

000021| | LessThan

000022| | LessThanEqual

000023| | Equal

000024| | GreaterThanEqual

000025| | GreaterThan

000026| | NotEqual

000027|

000028| (**

000029| location for a variable in the runtime AST

000030| for globals, unique id * an index into the global variables array

000031| for locals, unique id * an index into the current stackframe * an index into the stack

000032| *)

000033| type variable_location =

000034| | GlobalVar of int * int | LocalVar of int * int * int

000035|

000036| (** string replacement specification in a template instruction *)

000037| type replacement =

000038| (string * expression)

000039|

000040| and (** list of replacements for a template instructions *)

000041| replacement_list =

000042| replacement list

000043|

000044| and (** conditional replacement criteria for a template instruction *)

000045| conditional_spec =

000046| | Once

000047| | When of expression

000048| | Loop of string * expression

000049| | CondLoop of expression * string * expression

000050|

000051| and (** a single instruction in a set of template instructions *)

000052| replacement_spec =

000053| (string * conditional_spec * replacement_list)

000054|

000055| and (** definition for a line in a template definition *)

000056| template_spec =

000057| ((string option) * string)

000058|

000059| and (** type of map variable, either a dictionary or an array *)

000060| map_subtype =

000061| | MapSubtype | ArraySubtype

000062|

000063| and (** variable values used in parsing AST *)

000064| variable_value =

000065| | IntegerValue of int

000066| | FloatValue of float

000067| | StringValue of string

000068| | BooleanValue of bool

000069| | FunctionValue of string list * statement list

000070| | MapValue of (string, variable_value) Hashtbl.t * map_subtype

000071| | Void

000072|

000073| and (** variable values used in runtime AST *)

000074| runtime_variable_value =

000075| | RIntegerValue of int

000076| | RFloatValue of float

000077| | RStringValue of string

000078| | RBooleanValue of bool

000079| | RFunctionValue of int * int * int * bool * runtime_statement list * (((int * int), runtime_variable_value) Hashtbl.t) option * runtime_expression option

000080| | RLibraryFunction of lib_function_def

000081| | RMapValue of (string, runtime_variable_value) Hashtbl.t * map_subtype

000082| | RVoid

000083| | RUndefined

000084|

000085| and (**

000086| The runtime environment.

000087| consists of a heap for globals and an array of stackframes to support nested functions

000088| *)

000089| runtime_env =

000090| { heap : (int * runtime_variable_value) array;

000091| stackframes : (runtime_variable_value array) array;

000092| mutable closure_vars :

000093| (((int * int), runtime_variable_value) Hashtbl.t) option;

000094| gnames : string array; mutable current_line : (string * int);

000095| callstack : (string * int) Stack.t;

000096| mutable skip_callstack_pop: bool;

000097| }

000098|

000099| and (**

000100| Definition for a library function

000101| *)

000102| lib_function_def =

000103| { name : string list; args : string list; num_args : int; vararg : bool;

000104| code : runtime_env -> unit

000105| }

000106|

000107| and (** expressions used in parsing AST *)

000108| expression =

000109| | Id of string

000110| | VarArg of string

000111| | BinaryOp of expression * operator * expression

000112| | CompOp of expression * comparator * expression

000113| | Not of expression

000114| | FunctionCall of expression * expression list

000115| | MapExpr of (string * expression) list

000116| | ArrayExpr of expression list

000117| | Value of variable_value

000118| | UnboundVar of string

000119| | Assignment of expression * expression

000120| | Declaration of expression * expression

000121| | MemberExpr of expression * expression

000122| | PostFixSum of expression * int

000123| | TernaryCond of expression * expression * expression

000124|

000125| and (** expressions used in runtime AST *)

000126| runtime_expression =

000127| | RVariable of variable_location

000128| | RVarArg of variable_location

000129| | RBinaryOp of runtime_expression * operator * runtime_expression

000130| | RCompOp of runtime_expression * comparator * runtime_expression

000131| | RNot of runtime_expression

000132| | RFunctionCall of runtime_expression * runtime_expression list

000133| | RMapExpr of (string * runtime_expression) list

000134| | RArrayExpr of runtime_expression list

000135| | RValue of runtime_variable_value

000136| | RAssignment of runtime_expression * runtime_expression

000137| | RDeclaration of runtime_expression * runtime_expression

000138| | RMemberExpr of runtime_expression * runtime_expression

000139| | RPostFixSum of runtime_expression * int

000140| | RTernaryCond of runtime_expression * runtime_expression

000141| * runtime_expression

000142|

000143| and (** statements used in parsing AST *)

000144| statement =

000145| | ForEach of string * expression * statement * (string * int)

000146| | For of expression * expression * expression * statement * (string * int)

000147| | ExpressionStatement of expression * (string * int)

000148| | Break of (string * int)

000149| | Continue of (string * int)

000150| | Noop

000151| | Return of expression * (string * int)

000152| | If of expression * statement * statement * (string * int)

000153| | TemplateDef of string * template_spec list * (string * int)

000154| | Instructions of string * string list * replacement_spec list

000155| * (string * int)

000156| | StatementBlock of statement list

000157| | Program of statement list

000158| | Import of string * (string * int)

000159| | Switch of expression * statement list * (string * int)

000160| | Case of expression option * (string * int)

000161| | TryCatch of statement * string * statement * (string * int)

000162| | TryFinally of statement * statement * (string * int)

000163| | Throw of expression * (string * int)

000164|

000165| and (** statements used in runtime AST *)

000166| runtime_statement =

000167| | RForEach of variable_location * runtime_expression * runtime_statement * (string * int)

000168| | RFor of runtime_expression * runtime_expression * runtime_expression * runtime_statement * (string * int)

000169| | RExpressionStatement of runtime_expression * (string * int)

000170| | RBreak of (string * int)

000171| | RContinue of (string * int)

000172| | RNoop

000173| | RReturn of runtime_expression * (string * int)

000174| | RIf of runtime_expression * runtime_statement * runtime_statement * (string * int)

000175| | RStatementBlock of runtime_statement list

000176| | RProgram of runtime_statement list

000177| | RSwitch of runtime_expression * runtime_statement list * (string * int)

000178| | RCase of runtime_expression option * (string * int)

000179| | RTryCatch of runtime_statement * variable_location * runtime_statement * (string * int)

000180| | RTryFinally of runtime_statement * runtime_statement * (string * int)

000181| | RThrow of runtime_expression * (string * int)

000182| | RFastIterator of variable_location * int * int * int * runtime_statement * (string * int)

000183|

000184| (**

000185| determines if a variable is a varag

000186| @param varname the variable name

000187| @return true if the variable is a vararg, false otherwise

000188| *)

000189| let is_vararg varname = (*[96]*)varname.[0] = '['

000190|

000191| (**

000192| retuns the name for a vararg

000193| *)

000194| let vararg_formalname varname =

000195| (*[8]*)String.sub varname 1 ((String.length varname) - 1)

000196|

000197| exception CFReturn of runtime_variable_value

000198|

000199| exception CFBreak

000200|

000201| exception CFContinue

000202|

000203| exception CFUserException of runtime_variable_value * string

000204|