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

Overview

Classes, Interfaces, Objects and Records

Name Description
Class ECasScriptError  
Class ECasScriptAssignError  
Class ECasScriptAnyMathError  
Class TCasScriptEnvironment Various information that may be useful for implementing some function handlers, but that should be supplied from outside of CastleScript.
Class TCasScriptExpression  
Class TCasScriptExpressionList  
Class TCasScriptValue  
Class TCasScriptValueList  
Class TCasScriptParameterValue This is a very special CastleScript value, used to represent user-defined function parameter.
Class TCasScriptInteger  
Class TCasScriptFloat  
Class TCasScriptBoolean  
Class TCasScriptString  
Record TCasScriptSearchArgumentClassesCache  
Class TCasScriptFunction  
Class TCasScriptSequence  
Class TCasScriptAssignment CastleScript assignment operator.
Class TCasScriptIf  
Class TCasScriptWhen  
Class TCasScriptWhile  
Class TCasScriptFor  
Class TCasScriptCoalesce  
Class TCasScriptRegisteredHandler  
Class TCasScriptFunctionHandlers This specifies for each type combination (array of TCasScriptValue classes) and for each function (TCasScriptFunction class) how they should be handled.
Class ECasScriptFunctionArgumentsError  
Class ECasScriptFunctionNoHandler  
Class TCasScriptUserFunction CastleScript user function definition.
Class TCasScriptUserFunctionList  
Class ECasScriptMissingFunction  
Class TCasScriptProgram  

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;

This item has no description.

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;

This item has no description.

TCasScriptValueClass = class of TCasScriptValue;

This item has no description.

TCasScriptValueClassArray = array of TCasScriptValueClass;

This item has no description.

TCasScriptValuesArray = array of TCasScriptValue;

This item has no description.

TCasScriptFunctionClass = class of TCasScriptFunction;

This item has no description.

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 writeln() function. If not assigned, we will use CastleLog.WritelnLog.

ScriptVerboseMessages: Boolean;

In case of warnings/errors, output more verbose information about the script in which it occurred.


Generated by PasDoc 0.16.0-snapshot.