Class TCasScriptFunction

Unit

Declaration

type TCasScriptFunction = class(TCasScriptExpression)

Description

No description available, ancestor TCasScriptExpression description follows

Hierarchy

Overview

Methods

Protected procedure CheckArguments; virtual;
Protected function CoreExecute: TCasScriptValue; override;
Public constructor Create(AArgs: TCasScriptExpressionList); overload;
Public constructor Create(const AArgs: array of TCasScriptExpression); overload;
Public destructor Destroy; override;
Public class function Name: string; virtual;
Public class function ShortName: string; virtual; abstract;
Public class function InfixOperatorName: string; virtual;
Public class function GreedyArgumentsCalculation: Integer; virtual;
Public class function ArgumentMustBeAssignable(const Index: Integer): boolean; virtual;

Properties

Public property Args: TCasScriptExpressionList read FArgs;

Description

Methods

Protected procedure CheckArguments; virtual;

Used by constructor to check are args valid. Also, right now this gets FunctionHandlersByArgument (this way we don't have to search it at each TCasScriptFunction.Execute call, so TCasScriptFunction.Execute may work much faster).

Exceptions raised
ECasScriptFunctionArgumentsError
on invalid Args passed to function.
Protected function CoreExecute: TCasScriptValue; override;
 
Public constructor Create(AArgs: TCasScriptExpressionList); overload;

Constructor initializing Args from given TCasScriptExpressionList. AArgs list contents is copied, i.e. AArgs refence is not stored or freed by TCasScriptFunction. But items on AArags are not copied recursively, we copy references from AArags items, and so we become their owners.

Exceptions raised
ECasScriptFunctionArgumentsError
if you specified invalid number of arguments for this function.
Public constructor Create(const AArgs: array of TCasScriptExpression); overload;
 
Public destructor Destroy; override;
 
Public class function Name: string; virtual;

Long function name for user. This is possibly with spaces, parenthesis and other funny characters. It will be used in error messages and such to describe this function.

Default implementation in this class simply returns ShortName. This should be suitable for most "norma" functions.

Public class function ShortName: string; virtual; abstract;

Short function name, for the parser. This is the name of the function for use in expressions like "function_name(arg_1, arg_2 ... , arg_n)".

This can be an empty string ('') if no explicit name for this function exists. This is useful for operators, which are implemented just like normal functions (a descendant of TCasScriptFunction), but with a special support from parser (e.g. to turn "x + b" into a call to the TCasScriptAdd function).

Public class function InfixOperatorName: string; virtual;

Function name when used as an infix operator.

Empty string ('') if no such name for this function. This is returned by default implementation of this in this class.

This does require cooperation from the parser to actually work, that is you cannot simply define new operators by registering new TCasScriptFunction with InfixOperatorName <> ''. For now.

Note that at least one of ShortName and InfixOperatorName must not be empty.

The only exception is the TCasScriptNegate function, that is neither infix operator nor a usual function that must be specified as "function_name(arguments)". So this is an exception, and if there will be a need, I shall fix this (probably by introducing some third field, like PrefixOperatorName ?)

Note 2 things:

  1. Function that can be used as infix operator (i.e. has InfixOperatorName <> '') is not necessary binary operator, i.e. InfixOperatorName <> '' does not determine the value of ArgsCount. This way I was able to define infix operators +, -, * etc. that take any number of arguments and operators like ˆ and > that always take 2 arguments.

  2. Function may have both ShortName <> '' and InfixOperatorName <> ''. E.g. TCasScriptPower can be used as "Power(3, 1.5)" or "3 ˆ 1.5".

Public class function GreedyArgumentsCalculation: Integer; virtual;

Specify which arguments are calculated before function handler is called.

If = -1 (default value returned by implementation in this class) then all arguments are greedily calculated, which simply means that all arguments are calculated before executing function handler. This is the usual and expected behavior of normal functions. It's also a prerequisite for most of overloaded things to work, since we need to know types of calculated arguments (TCasScriptValue classes) before we choose overloaded handler for function.

If this is >= 0, then arguments with index >= of this will not be calculated before handler execution. Since their type is unknown, they will match any type in handler's ArgumentClasses. Your handler will receive Nil in their places, and is responsible for calling their Execute on it's own if needed.

This is particularly suited for implementing control-flow instructions, like "if" and "while", as normal functions inside CastleScript. For example, "if" will have GreedyArgumentsCalculation = 1, so the first argument (condition) will be calculated, but the execution of 2nd or 3rd argument ("then" code or "else" code) will be left to the handler.

Public class function ArgumentMustBeAssignable(const Index: Integer): boolean; virtual;

Which arguments should be assignable by this function.

Default implementation in TCasScriptFunction just returns False always. If you're making a function that changes it's argument (like assignment operator, or vector_set, array_set and such) you want to override this.

This is actually checked by CheckArguments, called from constructors.

Properties

Public property Args: TCasScriptExpressionList read FArgs;

Function arguments. Don't modify this list after function is created (although you can modify values inside arguments).


Generated by PasDoc 0.16.0.