Unit CastleScript
Description
Base CastleScript
structures: values, functions, expressions.
It is designed to be extendable, so you can add new TCasScriptValue descendants and new TCasScriptFunction descendants, and register their handlers in FunctionHandlers instance (TCasScriptFunctionHandlers).
Using structures here you can also build CastleScript
expressions by Pascal code (that is, you don't have to parse them). For example this is an expression that calculates sin(3) + 10 + 1
:
Expr := TCasScriptAdd.Create([ TCasScriptSin.Create([TCasScriptFloat.Create(false, 3)]), TCasScriptFloat.Create(false, 10), TCasScriptFloat.Create(false, 1) ]);
You can then call Expr.Execute
to calculate such expression.
To make a variable in the expression, just create and remember a TCasScriptFloat instance first, and then change it's value freely between Expr.Execute
calls. For example
MyVariable := TCasScriptFloat.Create(false, 3); Expr := TCasScriptAdd.Create([ TCasScriptSin.Create([MyVariable]), TCasScriptFloat.Create(false, 10), TCasScriptFloat.Create(false, 1) ]); Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(3) + 10 + 1" MyVariable.Value := 4; Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(4) + 10 + 1" MyVariable.Value := 5; Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(5) + 10 + 1"
Note that generally each TCasScriptExpression owns it's children expressions, so they will be automatically freed when parent is freed. Also, the values returned by Execute are owned by expression. So you can simply free whole thing by Expr.Free
.
If you're want to parse CastleScript
expression from a text file, see CastleScriptParser.
Uses
- SysUtils
- Math
- Contnrs
- Classes
- Generics.Collections
- CastleUtils
- CastleClassUtils
Overview
Classes, Interfaces, Objects and Records
Functions and Procedures
function FunctionHandlers: TCasScriptFunctionHandlers; |
procedure CreateValueIfNeeded(var Value: TCasScriptValue; var ParentOfValue: boolean; NeededClass: TCasScriptValueClass); |
Types
EKamAssignValueError = ECasScriptAssignError deprecated; |
TCasScriptMessage = procedure (const S: string) of object; |
TCasScriptValueClass = class of TCasScriptValue; |
TCasScriptValueClassArray = array of TCasScriptValueClass; |
TCasScriptValuesArray = array of TCasScriptValue; |
TCasScriptFunctionClass = class of TCasScriptFunction; |
TCasScriptFunctionHandler = procedure ( AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean) of object; |
Variables
OnScriptMessage: TCasScriptMessage; |
ScriptVerboseMessages: Boolean; |
Description
Functions and Procedures
function FunctionHandlers: TCasScriptFunctionHandlers; |
procedure CreateValueIfNeeded(var Value: TCasScriptValue; var ParentOfValue: boolean; NeededClass: TCasScriptValueClass); |
Make sure Value is assigned and of NeededClass. If Value is not assigned, or is not exactly of NeededClass, it will be freed and new will be created. |
Types
EKamAssignValueError = ECasScriptAssignError deprecated; |
Warning: this symbol is deprecated. Deprecated name for ECasScriptAssignError. |
TCasScriptMessage = procedure (const S: string) of object; |
TCasScriptValueClass = class of TCasScriptValue; |
TCasScriptValueClassArray = array of TCasScriptValueClass; |
TCasScriptValuesArray = array of TCasScriptValue; |
TCasScriptFunctionClass = class of TCasScriptFunction; |
TCasScriptFunctionHandler = procedure ( AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean) of object; |
Calculate result on given function arguments Arguments. Place result in AResult. The current function is also passed here, although usually you don't need it (you already get a list of calculated Arguments, and you should register different procedures for different TCasScriptFunction classes, so you know what operation on arguments should be done). For functions when GreedyArgumentsCalculation >= 0, it may be useful to directly access AFunction.Args. If needed, previous value of AResult should be freed and new created. If current AResult is <> nil and it's of appropriate class, you may also reuse it and only change it's fields (this is helpful, to avoid many creations/destroying of class instances while calculating an expression many times). CreateValueIfNeeded may be helpful for implementing this. |
Variables
OnScriptMessage: TCasScriptMessage; |
Global method to output messages done by CastleScript |
ScriptVerboseMessages: Boolean; |
In case of warnings/errors, output more verbose information about the script in which it occurred. |
Generated by PasDoc 0.16.0.