| 1 |
{ |
| 2 |
Copyright 2001-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Base CastleScript structures: values, functions, expressions. |
| 17 |
|
| 18 |
It is designed to be extendable, so you can add new TCasScriptValue |
| 19 |
descendants and new TCasScriptFunction descendants, and register |
| 20 |
their handlers in FunctionHandlers instance (TCasScriptFunctionHandlers). |
| 21 |
|
| 22 |
Using structures here you can also build CastleScript expressions |
| 23 |
by Pascal code (that is, you don't have to parse them). For example |
| 24 |
this is an expression that calculates @code(sin(3) + 10 + 1): |
| 25 |
|
| 26 |
@longcode(# |
| 27 |
Expr := TCasScriptAdd.Create([ |
| 28 |
TCasScriptSin.Create([TCasScriptFloat.Create(false, 3)]), |
| 29 |
TCasScriptFloat.Create(false, 10), |
| 30 |
TCasScriptFloat.Create(false, 1) |
| 31 |
]); |
| 32 |
#) |
| 33 |
|
| 34 |
You can then call @code(Expr.Execute) to calculate such expression. |
| 35 |
|
| 36 |
To make a variable in the expression, just create and remember a |
| 37 |
TCasScriptFloat instance first, and then change it's value freely between |
| 38 |
@code(Expr.Execute) calls. For example |
| 39 |
|
| 40 |
@longcode(# |
| 41 |
MyVariable := TCasScriptFloat.Create(false, 3); |
| 42 |
Expr := TCasScriptAdd.Create([ |
| 43 |
TCasScriptSin.Create([MyVariable]), |
| 44 |
TCasScriptFloat.Create(false, 10), |
| 45 |
TCasScriptFloat.Create(false, 1) |
| 46 |
]); |
| 47 |
|
| 48 |
Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(3) + 10 + 1" |
| 49 |
|
| 50 |
MyVariable.Value := 4; |
| 51 |
Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(4) + 10 + 1" |
| 52 |
|
| 53 |
MyVariable.Value := 5; |
| 54 |
Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(5) + 10 + 1" |
| 55 |
#) |
| 56 |
|
| 57 |
Note that generally each TCasScriptExpression owns it's children |
| 58 |
expressions, so they will be automatically freed when parent is freed. |
| 59 |
Also, the values returned by Execute are owned by expression. |
| 60 |
So you can simply free whole thing by @code(Expr.Free). |
| 61 |
|
| 62 |
If you're want to parse CastleScript expression from a text |
| 63 |
file, see CastleScriptParser. |
| 64 |
} |
| 65 |
unit CastleScript; |
| 66 |
|
| 67 |
{$I castleconf.inc} |
| 68 |
|
| 69 |
interface |
| 70 |
|
| 71 |
uses SysUtils, Math, Contnrs, Classes, Generics.Collections, |
| 72 |
CastleUtils, CastleClassUtils; |
| 73 |
|
| 74 |
type |
| 75 |
{ } |
| 76 |
TCasScriptValue = class; |
| 77 |
|
| 78 |
ECasScriptError = class(Exception); |
| 79 |
ECasScriptAssignError = class(ECasScriptError); |
| 80 |
ECasScriptAnyMathError = class(ECasScriptError); |
| 81 |
|
| 82 |
{ @deprecated Deprecated name for ECasScriptAssignError. } |
| 83 |
EKamAssignValueError = ECasScriptAssignError deprecated; |
| 84 |
|
| 85 |
TCasScriptMessage = procedure (const S: string) of object; |
| 86 |
|
| 87 |
{ Various information that may be useful for implementing some |
| 88 |
function handlers, but that should be supplied from outside of |
| 89 |
CastleScript. } |
| 90 |
TCasScriptEnvironment = class |
| 91 |
private |
| 92 |
FBaseUrl: String; |
| 93 |
public |
| 94 |
{ Base URL to resolve relative URLs. Similar to TX3DNode.BaseUrl. } |
| 95 |
property BaseUrl: String read FBaseUrl write FBaseUrl; |
| 96 |
end; |
| 97 |
|
| 98 |
TCasScriptExpression = class |
| 99 |
private |
| 100 |
FEnvironment: TCasScriptEnvironment; |
| 101 |
protected |
| 102 |
{ More internal version of Execute. |
| 103 |
|
| 104 |
This doesn't necessarily check floating-point exceptions. |
| 105 |
Execute actually calls CoreExecute and then ClearExceptions. |
| 106 |
|
| 107 |
Also this doesn't try to convert EIntError and EMathError |
| 108 |
to ECasScriptAnyMathError. This is done by Execute. |
| 109 |
|
| 110 |
When one CastleScript CoreExecute calls another function, |
| 111 |
it can use CoreExecute instead of Execute. This way only one |
| 112 |
ClearExceptions will be needed for whole expression execution, |
| 113 |
instead of doing ClearExceptions after each function handler. } |
| 114 |
function CoreExecute: TCasScriptValue; virtual; abstract; |
| 115 |
public |
| 116 |
(*Execute and calculate this expression. |
| 117 |
|
| 118 |
Returned value is owned by this object. Which should be comfortable |
| 119 |
for you usually, as you do not have to worry about freeing it. |
| 120 |
Also, it allows us to make various optimizations to avoid |
| 121 |
creating/destroying lots of temporary TCasScriptExpression |
| 122 |
instances during calculation of complex expression. |
| 123 |
|
| 124 |
The disadvantage of this is that returned object value is valid |
| 125 |
only until you executed this same expression again, |
| 126 |
or until you freed this expression. If you need to remember the |
| 127 |
execute result for longer, you have to copy it somewhere. |
| 128 |
For example you can do |
| 129 |
|
| 130 |
@longCode(# |
| 131 |
{ This will always work, thanks to virtual TCasScriptValue.Create |
| 132 |
and AssignValue methods. } |
| 133 |
Copy := TCasScriptValue(ReturnedValue.ClassType).Create; |
| 134 |
Copy.AssignValue(ReturnedValue); |
| 135 |
#) |
| 136 |
|
| 137 |
@raises(ECasScriptError |
| 138 |
|
| 139 |
Execute is guaranteed to raise an ECasScriptError exception if some |
| 140 |
calculation fails because of invalid arguments. |
| 141 |
|
| 142 |
This means that when you run CastleScript expression provided |
| 143 |
by the user, you only have to catch ECasScriptError |
| 144 |
to be safe from errors produced by user. |
| 145 |
No need to catch something more general like Exception class. |
| 146 |
|
| 147 |
Also it's guaranteed that no hanging floating-point errors |
| 148 |
are left. Normally, there is no guarantee that |
| 149 |
floating-point errors are raised immediately, they may |
| 150 |
be raised at the next fp operation (this is needed for fp operations |
| 151 |
to proceed in parallel, and be much faster). |
| 152 |
For executing CastleScript, Execute calls Math.ClearExceptions(true) |
| 153 |
to make sure that all floating point errors are caught. |
| 154 |
This ensures that we can safely execute even invalid expressions |
| 155 |
(like 'ln(-3)') and get reliable exceptions. |
| 156 |
|
| 157 |
Floating-point errors of course also result in ECasScriptError descendants. |
| 158 |
More specifically, EIntError and EMathError result |
| 159 |
in ECasScriptAnyMathError.) |
| 160 |
*) |
| 161 |
function Execute: TCasScriptValue; |
| 162 |
|
| 163 |
{ Try to execute expression, or return @nil if a mathematical error occurred |
| 164 |
within expression. "Math error within expression" means that |
| 165 |
a ECasScriptAnyMathError exception occurred while calculating expression. |
| 166 |
|
| 167 |
This is useful to secure you against math arguments errors ('ln(-3)', |
| 168 |
'sqrt(-3)') but still raises normal exception on other ECasScriptError |
| 169 |
errors (like invalid argument type for function). } |
| 170 |
function TryExecuteMath: TCasScriptValue; |
| 171 |
|
| 172 |
{ Execute expression, return the result as a simple float value. |
| 173 |
It assumes that the expression is written to always return float. |
| 174 |
To easily create such expression, use @link(ParseFloatExpression). } |
| 175 |
function AsFloat(const ADefaultValue: Float = 0): Float; |
| 176 |
|
| 177 |
{ Execute expression, return the result as a simple integer value. |
| 178 |
It assumes that the expression is written to always return integer. |
| 179 |
To easily create such expression, use @link(ParseIntExpression). } |
| 180 |
function AsInt(const ADefaultValue: Int64 = 0): Int64; |
| 181 |
|
| 182 |
{ Execute expression, return the result as a simple string value. |
| 183 |
It assumes that the expression is written to always return string. |
| 184 |
To easily create such expression, use @link(ParseStringExpression). } |
| 185 |
function AsString(const ADefaultValue: String = ''): string; |
| 186 |
|
| 187 |
{ Execute expression, return the result as a simple boolean value. |
| 188 |
It assumes that the expression is written to always return boolean. |
| 189 |
To easily create such expression, use @link(ParseBoolExpression). } |
| 190 |
function AsBool(const ADefaultValue: boolean = false): boolean; |
| 191 |
|
| 192 |
{ Call Free, but only if this is not TCasScriptValue with |
| 193 |
OwnedByParentExpression = false. (This cannot be implemented |
| 194 |
cleanly, as virtual procedure, since it must work when Self is @nil, |
| 195 |
and then virtual method table is not available of course.) } |
| 196 |
procedure FreeByParentExpression; |
| 197 |
|
| 198 |
{ Environment (outside information) for this expression. |
| 199 |
May be @nil. This object is not owned by TCasScriptExpression, |
| 200 |
will not be freed by TCasScriptExpression and such. } |
| 201 |
property Environment: TCasScriptEnvironment read FEnvironment write FEnvironment; |
| 202 |
end; |
| 203 |
|
| 204 |
TCasScriptExpressionList = class({$ifdef FPC}specialize{$endif} TObjectList<TCasScriptExpression>) |
| 205 |
public |
| 206 |
procedure AddArray(const A: array of TCasScriptExpression); deprecated 'use AddRange'; |
| 207 |
procedure AddList(const Source: TCasScriptExpressionList); deprecated 'use AddRange'; |
| 208 |
procedure FreeContentsByParentExpression; |
| 209 |
end; |
| 210 |
|
| 211 |
TCasScriptValue = class(TCasScriptExpression) |
| 212 |
private |
| 213 |
FOwnedByParentExpression: boolean; |
| 214 |
FName: string; |
| 215 |
FValueAssigned: boolean; |
| 216 |
FWriteable: boolean; |
| 217 |
protected |
| 218 |
function CoreExecute: TCasScriptValue; override; |
| 219 |
public |
| 220 |
{ Internal note: don't make this constructor overloaded with Delphi |
| 221 |
"overload" keyword, descendants depend that this class hides |
| 222 |
the parameter-less constructor of ancestors. } |
| 223 |
{ } |
| 224 |
constructor Create(const AWriteable: boolean); virtual; |
| 225 |
|
| 226 |
{ Is this value writeable. |
| 227 |
If not, this will not be allowed to change by CastleScript assignment |
| 228 |
and such functions. Note that Writeable = @false will not prevent |
| 229 |
you from changing value internally, by AssignValue or changin |
| 230 |
Value property directly (that would be too uncomfortable). } |
| 231 |
property Writeable: boolean read FWriteable write FWriteable; |
| 232 |
|
| 233 |
property OwnedByParentExpression: boolean |
| 234 |
read FOwnedByParentExpression write FOwnedByParentExpression |
| 235 |
default true; |
| 236 |
|
| 237 |
{ Name of this value, or '' if not named. |
| 238 |
Named value can be recognized in expressions by CastleScriptParser. } |
| 239 |
property Name: string read FName write FName; |
| 240 |
|
| 241 |
{ Assign value from Source to Self. |
| 242 |
@raises(ECasScriptAssignError if assignment is not possible |
| 243 |
because types don't match.) } |
| 244 |
procedure AssignValue(Source: TCasScriptValue); virtual; abstract; |
| 245 |
|
| 246 |
{ Set to @true on each assign to Value. You can reset it at any time |
| 247 |
to @false. |
| 248 |
|
| 249 |
This allows the caller to know which variables were |
| 250 |
assigned during script execution, which is useful if changes to |
| 251 |
CastleScript variables should be propagated to some other things |
| 252 |
after the script finished execution. This is essential for behavior |
| 253 |
in VRML/X3D Script node. |
| 254 |
|
| 255 |
Descendants note: you have to set this to @true in SetValue. } |
| 256 |
property ValueAssigned: boolean read FValueAssigned write FValueAssigned |
| 257 |
default false; |
| 258 |
end; |
| 259 |
|
| 260 |
TCasScriptValueClass = class of TCasScriptValue; |
| 261 |
TCasScriptValueClassArray = array of TCasScriptValueClass; |
| 262 |
TCasScriptValuesArray = array of TCasScriptValue; |
| 263 |
|
| 264 |
TCasScriptValueList = class({$ifdef FPC}specialize{$endif} TObjectList<TCasScriptValue>) |
| 265 |
public |
| 266 |
procedure AddArray(const A: array of TCasScriptValue); deprecated 'use AddRange'; |
| 267 |
{ Find an item by Name. @nil if not found. } |
| 268 |
function FindName(const VariableName: string): TCasScriptValue; |
| 269 |
end; |
| 270 |
|
| 271 |
{ This is a very special CastleScript value, used to represent user-defined |
| 272 |
function parameter. This poses itself as a TCasScriptValue descendant, |
| 273 |
and it has working AssignValue and everything else. This way it can |
| 274 |
be used in "Variables" list for various CastleScriptParser functions. |
| 275 |
|
| 276 |
Except it's cheating: it doesn't actually store the value. |
| 277 |
Instead, it has SourceValue property that is used when doing |
| 278 |
AssignValue. So AssignValue is handled by SourceValue.AssignValue, |
| 279 |
and Execute is handled by SourceValue.Execute, and so reading/writing |
| 280 |
this works. |
| 281 |
|
| 282 |
The advantage: the exact type of function parameter is not known, |
| 283 |
and still we can parse the function expression. This is crucial |
| 284 |
for parser implementation: when parsing you need to create |
| 285 |
TCasScriptParameterValue instance, but you don't know actual |
| 286 |
type of parameter that will be passed here. } |
| 287 |
TCasScriptParameterValue = class(TCasScriptValue) |
| 288 |
private |
| 289 |
FSourceValue: TCasScriptValue; |
| 290 |
protected |
| 291 |
function CoreExecute: TCasScriptValue; override; |
| 292 |
public |
| 293 |
property SourceValue: TCasScriptValue read FSourceValue write FSourceValue; |
| 294 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 295 |
end; |
| 296 |
|
| 297 |
TCasScriptFloat = class; |
| 298 |
TCasScriptFunction = class; |
| 299 |
|
| 300 |
TCasScriptInteger = class(TCasScriptValue) |
| 301 |
private |
| 302 |
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 303 |
class procedure HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 304 |
class procedure HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 305 |
|
| 306 |
class procedure HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 307 |
class procedure HandleIntDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 308 |
class procedure HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 309 |
class procedure HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 310 |
|
| 311 |
class procedure HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 312 |
class procedure HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 313 |
class procedure HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 314 |
class procedure HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 315 |
class procedure HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 316 |
class procedure HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 317 |
|
| 318 |
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 319 |
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 320 |
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 321 |
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 322 |
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 323 |
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 324 |
|
| 325 |
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 326 |
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 327 |
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 328 |
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 329 |
|
| 330 |
private |
| 331 |
FPromoteToFloat: TCasScriptFloat; |
| 332 |
|
| 333 |
FValue: Int64; |
| 334 |
procedure SetValue(const AValue: Int64); |
| 335 |
public |
| 336 |
{ Comfortable constructor to set initial Value. |
| 337 |
Note that the inherited constructor (without AValue parameter) |
| 338 |
is also fine to use, it will set value to zero. } |
| 339 |
constructor Create(const AWriteable: boolean; const AValue: Int64); reintroduce; overload; |
| 340 |
constructor Create(const AWriteable: boolean); reintroduce; overload; override; |
| 341 |
destructor Destroy; override; |
| 342 |
|
| 343 |
property Value: Int64 read FValue write SetValue; |
| 344 |
|
| 345 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 346 |
|
| 347 |
{ Returns this integer promoted to float. |
| 348 |
|
| 349 |
This object is kept and owned by this TCasScriptInteger instance, |
| 350 |
so it's valid as long as this TCasScriptInteger instance is valid. |
| 351 |
This allows you to safely use this (since you may have to return |
| 352 |
PromoteToFloat as return value of some Execute expressions, |
| 353 |
so it desirable that it's valid object reference). } |
| 354 |
function PromoteToFloat: TCasScriptFloat; |
| 355 |
end; |
| 356 |
|
| 357 |
TCasScriptFloat = class(TCasScriptValue) |
| 358 |
private |
| 359 |
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 360 |
class procedure HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 361 |
class procedure HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 362 |
class procedure HandleFloatDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 363 |
class procedure HandleLerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 364 |
class procedure HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 365 |
class procedure HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 366 |
class procedure HandleSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 367 |
class procedure HandleCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 368 |
class procedure HandleTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 369 |
class procedure HandleCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 370 |
class procedure HandleArcSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 371 |
class procedure HandleArcCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 372 |
class procedure HandleArcTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 373 |
class procedure HandleArcCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 374 |
class procedure HandleSinh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 375 |
class procedure HandleCosh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 376 |
class procedure HandleTanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 377 |
class procedure HandleCotanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 378 |
class procedure HandleLog2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 379 |
class procedure HandleLn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 380 |
class procedure HandleLog(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 381 |
class procedure HandlePower2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 382 |
class procedure HandleExp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 383 |
class procedure HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 384 |
class procedure HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 385 |
class procedure HandleSqrt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 386 |
class procedure HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 387 |
class procedure HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 388 |
class procedure HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 389 |
class procedure HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 390 |
class procedure HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 391 |
class procedure HandleCeil(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 392 |
class procedure HandleFloor(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 393 |
class procedure HandleRound(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 394 |
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 395 |
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 396 |
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 397 |
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 398 |
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 399 |
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 400 |
|
| 401 |
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 402 |
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 403 |
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 404 |
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 405 |
|
| 406 |
private |
| 407 |
FValue: Float; |
| 408 |
procedure SetValue(const AValue: Float); |
| 409 |
public |
| 410 |
{ Comfortable constructor to set initial Value. |
| 411 |
Note that the inherited constructor (without AValue parameter) |
| 412 |
is also fine to use, it will set value to zero. } |
| 413 |
constructor Create(const AWriteable: boolean; const AValue: Float); reintroduce; overload; |
| 414 |
constructor Create(const AWriteable: boolean); reintroduce; overload; override; |
| 415 |
|
| 416 |
property Value: Float read FValue write SetValue; |
| 417 |
|
| 418 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 419 |
end; |
| 420 |
|
| 421 |
TCasScriptBoolean = class(TCasScriptValue) |
| 422 |
private |
| 423 |
class procedure HandleOr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 424 |
class procedure HandleAnd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 425 |
class procedure HandleNot(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 426 |
|
| 427 |
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 428 |
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 429 |
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 430 |
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 431 |
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 432 |
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 433 |
|
| 434 |
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 435 |
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 436 |
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 437 |
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 438 |
|
| 439 |
private |
| 440 |
FValue: boolean; |
| 441 |
procedure SetValue(const AValue: boolean); |
| 442 |
public |
| 443 |
{ Comfortable constructor to set initial Value. |
| 444 |
Note that the inherited constructor (without AValue parameter) |
| 445 |
is also fine to use, it will set value to false. } |
| 446 |
constructor Create(const AWriteable: boolean; const AValue: boolean); reintroduce; overload; |
| 447 |
constructor Create(const AWriteable: boolean); reintroduce; overload; override; |
| 448 |
|
| 449 |
property Value: boolean read FValue write SetValue; |
| 450 |
|
| 451 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 452 |
end; |
| 453 |
|
| 454 |
TCasScriptString = class(TCasScriptValue) |
| 455 |
private |
| 456 |
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 457 |
|
| 458 |
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 459 |
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 460 |
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 461 |
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 462 |
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 463 |
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 464 |
|
| 465 |
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 466 |
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 467 |
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 468 |
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 469 |
|
| 470 |
class procedure HandleWriteln(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 471 |
class procedure HandleCharacterFromCode(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 472 |
private |
| 473 |
FValue: String; |
| 474 |
procedure SetValue(const AValue: String); |
| 475 |
public |
| 476 |
{ Comfortable constructor to set initial Value. |
| 477 |
Note that the inherited constructor (without AValue parameter) |
| 478 |
is also fine to use, it will set value to ''. } |
| 479 |
constructor Create(const AWriteable: boolean; const AValue: String); reintroduce; overload; |
| 480 |
constructor Create(const AWriteable: boolean); reintroduce; overload; override; |
| 481 |
|
| 482 |
property Value: String read FValue write SetValue; |
| 483 |
|
| 484 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 485 |
end; |
| 486 |
|
| 487 |
TCasScriptRegisteredHandler = class; |
| 488 |
|
| 489 |
TCasScriptSearchArgumentClassesCache = record |
| 490 |
IsCache: boolean; |
| 491 |
QueryHandlersByArgument: Contnrs.TObjectList; |
| 492 |
QueryArgumentClasses: TCasScriptValueClassArray; |
| 493 |
Answer: boolean; |
| 494 |
AnswerArgumentIndex: Integer; |
| 495 |
AnswerHandler: TCasScriptRegisteredHandler; |
| 496 |
end; |
| 497 |
|
| 498 |
TCasScriptFunction = class(TCasScriptExpression) |
| 499 |
private |
| 500 |
FArgs: TCasScriptExpressionList; |
| 501 |
LastExecuteResult: TCasScriptValue; |
| 502 |
ParentOfLastExecuteResult: boolean; |
| 503 |
|
| 504 |
{ This is as returned by SearchFunctionClass } |
| 505 |
HandlersByArgument: Contnrs.TObjectList; |
| 506 |
|
| 507 |
{ Helper variables for Execute implementation. |
| 508 |
Initialized in CheckArguments, to optimize: profiling shows that when |
| 509 |
they are intialized in Execute, this takes quite a lot of Execute time. } |
| 510 |
ExecuteArguments: array of TCasScriptValue; |
| 511 |
ExecuteArgumentClasses: TCasScriptValueClassArray; |
| 512 |
|
| 513 |
{ Caches for SearchArgumentClasses, used to speed up Execute } |
| 514 |
Cache1: TCasScriptSearchArgumentClassesCache; |
| 515 |
Cache2: TCasScriptSearchArgumentClassesCache; |
| 516 |
protected |
| 517 |
{ Used by constructor to check are args valid. |
| 518 |
Also, right now this gets FunctionHandlersByArgument (this way we don't |
| 519 |
have to search it at each TCasScriptFunction.Execute call, |
| 520 |
so TCasScriptFunction.Execute may work much faster). |
| 521 |
@raises(ECasScriptFunctionArgumentsError on invalid Args passed to |
| 522 |
function.) } |
| 523 |
procedure CheckArguments; virtual; |
| 524 |
|
| 525 |
function CoreExecute: TCasScriptValue; override; |
| 526 |
public |
| 527 |
{ Constructor initializing Args from given TCasScriptExpressionList. |
| 528 |
AArgs list contents is copied, i.e. AArgs refence is not |
| 529 |
stored or freed by TCasScriptFunction. But items on AArags are not copied |
| 530 |
recursively, we copy references from AArags items, and so we become |
| 531 |
their owners. |
| 532 |
|
| 533 |
@raises(ECasScriptFunctionArgumentsError if you specified invalid |
| 534 |
number of arguments for this function.) |
| 535 |
} |
| 536 |
constructor Create(AArgs: TCasScriptExpressionList); overload; |
| 537 |
constructor Create(const AArgs: array of TCasScriptExpression); overload; |
| 538 |
destructor Destroy; override; |
| 539 |
|
| 540 |
{ Long function name for user. This is possibly with spaces, |
| 541 |
parenthesis and other funny characters. It will be used in |
| 542 |
error messages and such to describe this function. |
| 543 |
|
| 544 |
Default implementation in this class simply returns ShortName. |
| 545 |
This should be suitable for most "norma" functions. } |
| 546 |
class function Name: string; virtual; |
| 547 |
|
| 548 |
{ Short function name, for the parser. |
| 549 |
This is the name of the function for use in expressions |
| 550 |
like "function_name(arg_1, arg_2 ... , arg_n)". |
| 551 |
|
| 552 |
This can be an empty string ('') if no explicit name for this function |
| 553 |
exists. This is useful for operators, which are implemented |
| 554 |
just like normal functions (a descendant of TCasScriptFunction), |
| 555 |
but with a special support from parser (e.g. to turn "x + b" into |
| 556 |
a call to the TCasScriptAdd function). } |
| 557 |
class function ShortName: string; virtual; abstract; |
| 558 |
|
| 559 |
{ Function name when used as an infix operator. |
| 560 |
|
| 561 |
Empty string ('') if no such name for this function. |
| 562 |
This is returned by default implementation of this in this class. |
| 563 |
|
| 564 |
This does require cooperation from the parser to actually work, |
| 565 |
that is you cannot simply define new operators by |
| 566 |
registering new TCasScriptFunction with InfixOperatorName <> ''. |
| 567 |
For now. |
| 568 |
|
| 569 |
Note that at least one of ShortName and InfixOperatorName |
| 570 |
must not be empty. |
| 571 |
|
| 572 |
The only exception is the TCasScriptNegate function, that is neither |
| 573 |
infix operator nor a usual function that must be specified |
| 574 |
as "function_name(arguments)". So this is an exception, |
| 575 |
and if there will be a need, I shall fix this (probably |
| 576 |
by introducing some third field, like PrefixOperatorName ?) |
| 577 |
|
| 578 |
Note 2 things: |
| 579 |
|
| 580 |
@orderedList( |
| 581 |
@item( |
| 582 |
Function that can be used as infix operator (i.e. has |
| 583 |
InfixOperatorName <> '') is not necessary binary operator, |
| 584 |
i.e. InfixOperatorName <> '' does not determine the value of |
| 585 |
ArgsCount. This way I was able to define infix operators |
| 586 |
+, -, * etc. that take any number of arguments and operators |
| 587 |
like ^ and > that always take 2 arguments.) |
| 588 |
|
| 589 |
@item( |
| 590 |
Function may have both ShortName <> '' and InfixOperatorName <> ''. |
| 591 |
E.g. TCasScriptPower can be used as "Power(3, 1.5)" or "3 ^ 1.5".) |
| 592 |
) } |
| 593 |
class function InfixOperatorName: string; virtual; |
| 594 |
|
| 595 |
{ Specify which arguments are calculated before function handler |
| 596 |
is called. |
| 597 |
|
| 598 |
If = -1 (default value returned by implementation |
| 599 |
in this class) then all arguments are greedily calculated, |
| 600 |
which simply means that all arguments are calculated before |
| 601 |
executing function handler. This is the usual and expected |
| 602 |
behavior of normal functions. It's also a prerequisite |
| 603 |
for most of overloaded things to work, since we need to know |
| 604 |
types of @italic(calculated) arguments (TCasScriptValue classes) |
| 605 |
before we choose overloaded handler for function. |
| 606 |
|
| 607 |
If this is >= 0, then arguments with index >= of this will not |
| 608 |
be calculated before handler execution. |
| 609 |
Since their type is unknown, they will match any type in |
| 610 |
handler's ArgumentClasses. |
| 611 |
Your handler will receive @nil in their places, and is responsible |
| 612 |
for calling their Execute on it's own if needed. |
| 613 |
|
| 614 |
This is particularly suited for implementing control-flow |
| 615 |
instructions, like "if" and "while", as normal functions |
| 616 |
inside CastleScript. For example, "if" will have |
| 617 |
GreedyArgumentsCalculation = 1, so the first argument (condition) |
| 618 |
will be calculated, but the execution of 2nd or 3rd argument |
| 619 |
("then" code or "else" code) will be left to the handler. } |
| 620 |
class function GreedyArgumentsCalculation: Integer; virtual; |
| 621 |
|
| 622 |
{ Which arguments should be assignable by this function. |
| 623 |
|
| 624 |
Default implementation in TCasScriptFunction just returns @false |
| 625 |
always. If you're making a function that changes it's argument |
| 626 |
(like assignment operator, or vector_set, array_set and such) |
| 627 |
you want to override this. |
| 628 |
|
| 629 |
This is actually checked by CheckArguments, called from |
| 630 |
constructors. } |
| 631 |
class function ArgumentMustBeAssignable(const Index: Integer): boolean; virtual; |
| 632 |
|
| 633 |
{ Function arguments. Don't modify this list after function is created |
| 634 |
(although you can modify values inside arguments). } |
| 635 |
property Args: TCasScriptExpressionList read FArgs; |
| 636 |
end; |
| 637 |
|
| 638 |
TCasScriptFunctionClass = class of TCasScriptFunction; |
| 639 |
|
| 640 |
{ Calculate result on given function arguments Arguments. |
| 641 |
Place result in AResult. |
| 642 |
|
| 643 |
The current function is also passed here, although usually you don't need it |
| 644 |
(you already get a list of calculated Arguments, and you should |
| 645 |
register different procedures for different TCasScriptFunction classes, |
| 646 |
so you know what operation on arguments should be done). |
| 647 |
For functions when GreedyArgumentsCalculation >= 0, it may be useful |
| 648 |
to directly access AFunction.Args. |
| 649 |
|
| 650 |
If needed, previous value of AResult should be freed and new created. |
| 651 |
If current AResult is <> nil and it's of appropriate class, |
| 652 |
you may also reuse it and only change it's fields |
| 653 |
(this is helpful, to avoid many creations/destroying |
| 654 |
of class instances while calculating an expression many times). |
| 655 |
CreateValueIfNeeded may be helpful for implementing this. } |
| 656 |
TCasScriptFunctionHandler = procedure ( |
| 657 |
AFunction: TCasScriptFunction; |
| 658 |
const Arguments: array of TCasScriptValue; |
| 659 |
var AResult: TCasScriptValue; |
| 660 |
var ParentOfResult: boolean) of object; |
| 661 |
|
| 662 |
TCasScriptSequence = class(TCasScriptFunction) |
| 663 |
private |
| 664 |
class procedure HandleSequence(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 665 |
public |
| 666 |
class function Name: string; override; |
| 667 |
class function ShortName: string; override; |
| 668 |
class function InfixOperatorName: string; override; |
| 669 |
end; |
| 670 |
|
| 671 |
{ CastleScript assignment operator. This is a special function, |
| 672 |
that must have TCasScriptValue (with Writeable = true) as it's 1st argument. } |
| 673 |
TCasScriptAssignment = class(TCasScriptFunction) |
| 674 |
private |
| 675 |
class procedure HandleAssignment(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 676 |
public |
| 677 |
class function Name: string; override; |
| 678 |
class function ShortName: string; override; |
| 679 |
class function InfixOperatorName: string; override; |
| 680 |
class function ArgumentMustBeAssignable(const Index: Integer): boolean; override; |
| 681 |
end; |
| 682 |
|
| 683 |
TCasScriptIf = class(TCasScriptFunction) |
| 684 |
private |
| 685 |
class procedure HandleIf(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 686 |
public |
| 687 |
class function ShortName: string; override; |
| 688 |
class function GreedyArgumentsCalculation: Integer; override; |
| 689 |
end; |
| 690 |
|
| 691 |
TCasScriptWhen = class(TCasScriptFunction) |
| 692 |
private |
| 693 |
class procedure HandleWhen(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 694 |
public |
| 695 |
class function ShortName: string; override; |
| 696 |
class function GreedyArgumentsCalculation: Integer; override; |
| 697 |
end; |
| 698 |
|
| 699 |
TCasScriptWhile = class(TCasScriptFunction) |
| 700 |
private |
| 701 |
class procedure HandleWhile(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 702 |
public |
| 703 |
class function ShortName: string; override; |
| 704 |
class function GreedyArgumentsCalculation: Integer; override; |
| 705 |
end; |
| 706 |
|
| 707 |
TCasScriptFor = class(TCasScriptFunction) |
| 708 |
private |
| 709 |
class procedure HandleFor(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 710 |
public |
| 711 |
class function ShortName: string; override; |
| 712 |
class function GreedyArgumentsCalculation: Integer; override; |
| 713 |
class function ArgumentMustBeAssignable(const Index: Integer): boolean; override; |
| 714 |
end; |
| 715 |
|
| 716 |
TCasScriptCoalesce = class(TCasScriptFunction) |
| 717 |
private |
| 718 |
class procedure HandleCoalesce(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 719 |
public |
| 720 |
class function ShortName: string; override; |
| 721 |
class function GreedyArgumentsCalculation: Integer; override; |
| 722 |
end; |
| 723 |
|
| 724 |
TCasScriptRegisteredHandler = class |
| 725 |
private |
| 726 |
FHandler: TCasScriptFunctionHandler; |
| 727 |
FFunctionClass: TCasScriptFunctionClass; |
| 728 |
FArgumentClasses: TCasScriptValueClassArray; |
| 729 |
FVariableArgumentsCount: boolean; |
| 730 |
public |
| 731 |
constructor Create( |
| 732 |
AHandler: TCasScriptFunctionHandler; |
| 733 |
AFunctionClass: TCasScriptFunctionClass; |
| 734 |
const AArgumentClasses: TCasScriptValueClassArray; |
| 735 |
const AVariableArgumentsCount: boolean); |
| 736 |
property Handler: TCasScriptFunctionHandler read FHandler; |
| 737 |
property FunctionClass: TCasScriptFunctionClass read FFunctionClass; |
| 738 |
property ArgumentClasses: TCasScriptValueClassArray read FArgumentClasses; |
| 739 |
|
| 740 |
{ Is the handler able to receive any number of arguments. |
| 741 |
|
| 742 |
If yes, then the last argument class |
| 743 |
may be repeated any number of times (but must occur |
| 744 |
at least once). That is, the ArgumentClasses array |
| 745 |
dictates the required arguments, and more arguments are allowed. |
| 746 |
Note that this means that at least one argument |
| 747 |
must be allowed (we have to know the argument class that can |
| 748 |
be repeated at the end), otherwise the handler will not be able to receive |
| 749 |
variable number of arguments anyway. } |
| 750 |
property VariableArgumentsCount: boolean read FVariableArgumentsCount; |
| 751 |
end; |
| 752 |
|
| 753 |
{ This specifies for each type combination (array of TCasScriptValue classes) |
| 754 |
and for each function (TCasScriptFunction class) how they should |
| 755 |
be handled. You can think of this as a table that has a handler |
| 756 |
for each possible TCasScriptValue sequence and TCasScriptFunction |
| 757 |
combination. |
| 758 |
|
| 759 |
The idea is to allow programmer to extend KambiScipt by |
| 760 |
|
| 761 |
@orderedList( |
| 762 |
@item(Defining new types of values for CastleScript: |
| 763 |
add new TCasScriptValue class, and create handlers for known |
| 764 |
functions to handle this type. |
| 765 |
|
| 766 |
It may be comfortable to place these handlers as private methods |
| 767 |
within your new TCasScriptValue descendant, although this is your |
| 768 |
private decision.) |
| 769 |
|
| 770 |
@item(Defining new functions for CastleScript: |
| 771 |
add new TCasScriptFunction class, and create handlers for known |
| 772 |
values to be handled by this function. |
| 773 |
|
| 774 |
It may be comfortable to place these handlers as private methods |
| 775 |
within your new TCasScriptFunction descendant, although this is your |
| 776 |
private decision.) |
| 777 |
) |
| 778 |
|
| 779 |
You have a guarantee that every registered here Handler will be called |
| 780 |
only with AFunction of registstered type and all Arguments |
| 781 |
matching the array of registered types and satisfying |
| 782 |
VariableArgumentsCount setting. |
| 783 |
|
| 784 |
As a bonus, this also provides a list of all usable function classes. |
| 785 |
That's because you have to register at least one handler for each |
| 786 |
TCasScriptFunction descendant to make this function actually usable, |
| 787 |
so we know about it here. } |
| 788 |
TCasScriptFunctionHandlers = class |
| 789 |
private |
| 790 |
{ This is a list of another TObjectList lists. |
| 791 |
|
| 792 |
Each nested list has only TCasScriptRegisteredHandler items. |
| 793 |
It always has at least one item. |
| 794 |
Each nested list has only equal FunctionClass values. } |
| 795 |
FHandlersByFunction: Contnrs.TObjectList; |
| 796 |
|
| 797 |
function SearchFunctionClass( |
| 798 |
FunctionClass: TCasScriptFunctionClass; |
| 799 |
out FunctionIndex: Integer; |
| 800 |
out HandlersByArgument: Contnrs.TObjectList): boolean; overload; |
| 801 |
function SearchFunctionClass( |
| 802 |
FunctionClass: TCasScriptFunctionClass; |
| 803 |
out HandlersByArgument: Contnrs.TObjectList): boolean; overload; |
| 804 |
|
| 805 |
function SearchArgumentClasses( |
| 806 |
HandlersByArgument: Contnrs.TObjectList; |
| 807 |
const ArgumentClasses: TCasScriptValueClassArray; |
| 808 |
out ArgumentIndex: Integer; |
| 809 |
out Handler: TCasScriptRegisteredHandler): boolean; overload; |
| 810 |
function SearchArgumentClasses( |
| 811 |
HandlersByArgument: Contnrs.TObjectList; |
| 812 |
const ArgumentClasses: TCasScriptValueClassArray; |
| 813 |
out Handler: TCasScriptRegisteredHandler): boolean; overload; |
| 814 |
|
| 815 |
{ This uses Cache to speed up SearchArgumentClasses. |
| 816 |
The cache remembers last HandlersByArgument, ArgumentClasses, |
| 817 |
and answer for them (if @true). Use this if you suspect that |
| 818 |
SearchArgumentClasses will be called many times with the same |
| 819 |
HandlersByArgument, ArgumentClasses --- then this will use cache |
| 820 |
to give answer much faster. } |
| 821 |
function SearchArgumentClasses( |
| 822 |
HandlersByArgument: Contnrs.TObjectList; |
| 823 |
const ArgumentClasses: TCasScriptValueClassArray; |
| 824 |
out Handler: TCasScriptRegisteredHandler; |
| 825 |
var Cache: TCasScriptSearchArgumentClassesCache): boolean; overload; |
| 826 |
public |
| 827 |
constructor Create; |
| 828 |
destructor Destroy; override; |
| 829 |
|
| 830 |
procedure RegisterHandler( |
| 831 |
AHandler: TCasScriptFunctionHandler; |
| 832 |
AFunctionClass: TCasScriptFunctionClass; |
| 833 |
const AArgumentClasses: array of TCasScriptValueClass; |
| 834 |
const AVariableArgumentsCount: boolean); |
| 835 |
|
| 836 |
{ Search for function class with matching ShortName. |
| 837 |
Returns @nil if not found. } |
| 838 |
function SearchFunctionShortName(const AShortName: string): TCasScriptFunctionClass; |
| 839 |
end; |
| 840 |
|
| 841 |
ECasScriptFunctionArgumentsError = class(ECasScriptError); |
| 842 |
ECasScriptFunctionNoHandler = class(ECasScriptError); |
| 843 |
|
| 844 |
{ CastleScript user function definition. |
| 845 |
|
| 846 |
Not to be confused with TCasScriptFunction: TCasScriptFunction is |
| 847 |
an internal, built-in function or operator. This class represents |
| 848 |
functions defined by user. } |
| 849 |
TCasScriptUserFunction = class |
| 850 |
private |
| 851 |
FName: string; |
| 852 |
FParameters: TCasScriptValueList; |
| 853 |
FBody: TCasScriptExpression; |
| 854 |
public |
| 855 |
constructor Create; |
| 856 |
destructor Destroy; override; |
| 857 |
|
| 858 |
property Name: string read FName write FName; |
| 859 |
|
| 860 |
{ List of function parameters. |
| 861 |
|
| 862 |
Note that they are also referenced inside function Expression, |
| 863 |
so you simply change them to set value of this parameter within |
| 864 |
whole function body. |
| 865 |
|
| 866 |
These are always fresh variables, not referenced anywhere outside |
| 867 |
of Body. This means that they are owned (always, regardless of |
| 868 |
OwnedByParentExpression) by this class. |
| 869 |
|
| 870 |
They must always be of TCasScriptParameterValue class. } |
| 871 |
property Parameters: TCasScriptValueList read FParameters; |
| 872 |
|
| 873 |
{ Function body. } |
| 874 |
property Body: TCasScriptExpression read FBody write FBody; |
| 875 |
end; |
| 876 |
|
| 877 |
TCasScriptUserFunctionList = class({$ifdef FPC}specialize{$endif} TObjectList<TCasScriptUserFunction>) |
| 878 |
function IndexOfName(const FunctionName: string): Integer; |
| 879 |
end; |
| 880 |
|
| 881 |
ECasScriptMissingFunction = class(ECasScriptError); |
| 882 |
|
| 883 |
TCasScriptProgram = class |
| 884 |
private |
| 885 |
FFunctions: TCasScriptUserFunctionList; |
| 886 |
FEnvironment: TCasScriptEnvironment; |
| 887 |
public |
| 888 |
constructor Create; |
| 889 |
destructor Destroy; override; |
| 890 |
|
| 891 |
property Functions: TCasScriptUserFunctionList read FFunctions; |
| 892 |
|
| 893 |
{ Execute a user-defined function (from Functions list of this program). |
| 894 |
|
| 895 |
@unorderedList( |
| 896 |
@item(Looks for given FunctionName. |
| 897 |
|
| 898 |
IgnoreMissingFunction says what to do in case of missing function: |
| 899 |
if true, it will be simply ignored (ExecuteFunction will |
| 900 |
silently do nothng). If false (default) |
| 901 |
then we will raise exception ECasScriptMissingFunction.) |
| 902 |
|
| 903 |
@item(Sets function parameters to given values |
| 904 |
(number of parameters must match, otherwise ECasScriptError). |
| 905 |
Values from your parameters are set as our parameters |
| 906 |
TCasScriptParameterValue.SourceValue, so script can read and write |
| 907 |
your values.) |
| 908 |
|
| 909 |
@item(Finally executes function body.) |
| 910 |
) |
| 911 |
} |
| 912 |
procedure ExecuteFunction(const FunctionName: string; |
| 913 |
const Parameters: array of TCasScriptValue; |
| 914 |
const IgnoreMissingFunction: boolean = false); |
| 915 |
|
| 916 |
{ Environment (outside information) for this program. |
| 917 |
|
| 918 |
This will be created and is owned by this TCasScriptProgram instance. |
| 919 |
You should take care to copy this reference to all expressions |
| 920 |
within this program (for example ParseProgram does this), |
| 921 |
this way all expressions share the same Environment instance. } |
| 922 |
property Environment: TCasScriptEnvironment read FEnvironment write FEnvironment; |
| 923 |
end; |
| 924 |
|
| 925 |
function FunctionHandlers: TCasScriptFunctionHandlers; |
| 926 |
|
| 927 |
{ Make sure Value is assigned and of NeededClass. |
| 928 |
If Value is not assigned, or is not exactly of NeededClass, |
| 929 |
it will be freed and new will be created. } |
| 930 |
procedure CreateValueIfNeeded(var Value: TCasScriptValue; |
| 931 |
var ParentOfValue: boolean; |
| 932 |
NeededClass: TCasScriptValueClass); |
| 933 |
|
| 934 |
var |
| 935 |
{ Global method to output messages done by CastleScript @code(writeln()) |
| 936 |
function. If not assigned, we will use CastleLog.WritelnLog. } |
| 937 |
OnScriptMessage: TCasScriptMessage; |
| 938 |
|
| 939 |
{ In case of warnings/errors, output more verbose information |
| 940 |
about the script in which it occurred. } |
| 941 |
ScriptVerboseMessages: Boolean; |
| 942 |
|
| 943 |
implementation |
| 944 |
|
| 945 |
uses CastleScriptCoreFunctions, CastleLog; |
| 946 |
|
| 947 |
{ TCasScriptExpression ------------------------------------------------------- } |
| 948 |
|
| 949 |
procedure TCasScriptExpression.FreeByParentExpression; |
| 950 |
begin |
| 951 |
if (Self <> nil) and |
| 952 |
( (not (Self is TCasScriptValue)) or |
| 953 |
TCasScriptValue(Self).OwnedByParentExpression ) then |
| 954 |
Free; |
| 955 |
end; |
| 956 |
|
| 957 |
function TCasScriptExpression.Execute: TCasScriptValue; |
| 958 |
begin |
| 959 |
try |
| 960 |
Result := CoreExecute; |
| 961 |
|
| 962 |
{ Force raising pending exceptions by FP calculations } |
| 963 |
// on Nintendo Switch, this raises errors from previous innocent calculations |
| 964 |
{$ifndef CASTLE_NINTENDO_SWITCH} |
| 965 |
ClearExceptions(true); |
| 966 |
{$endif} |
| 967 |
except |
| 968 |
{ Convert EIntError and EMathError to ECasScriptAnyMathError } |
| 969 |
on E: EIntError do |
| 970 |
raise ECasScriptAnyMathError.CreateFmt('Integer error %s: %s', |
| 971 |
[E.ClassName, E.Message]); |
| 972 |
on E: EMathError do |
| 973 |
raise ECasScriptAnyMathError.CreateFmt('Math error %s: %s', |
| 974 |
[E.ClassName, E.Message]); |
| 975 |
end; |
| 976 |
|
| 977 |
{ In case some code will mask exceptions (and for cpui386 or cpux86_64, |
| 978 |
the OpenGL units must mask exceptions) then we need to check |
| 979 |
IsNan / IsInfinite. } |
| 980 |
{$I norqcheckbegin.inc} |
| 981 |
if (Result is TCasScriptFloat) and |
| 982 |
( IsNan(TCasScriptFloat(Result).Value) or |
| 983 |
IsInfinite(TCasScriptFloat(Result).Value) ) then |
| 984 |
raise ECasScriptAnyMathError.Create('Floating point error'); |
| 985 |
{$I norqcheckend.inc} |
| 986 |
end; |
| 987 |
|
| 988 |
function TCasScriptExpression.TryExecuteMath: TCasScriptValue; |
| 989 |
begin |
| 990 |
try |
| 991 |
Result := Execute; |
| 992 |
except |
| 993 |
on ECasScriptAnyMathError do |
| 994 |
Result := nil; |
| 995 |
end; |
| 996 |
end; |
| 997 |
|
| 998 |
function TCasScriptExpression.AsFloat(const ADefaultValue: Float): Float; |
| 999 |
var |
| 1000 |
Res: TCasScriptValue; |
| 1001 |
begin |
| 1002 |
try |
| 1003 |
Res := Execute; |
| 1004 |
except |
| 1005 |
on E: ECasScriptError do |
| 1006 |
begin |
| 1007 |
WritelnWarning('CastleScript', 'Error when executing CastleScript expression: ' + E.Message); |
| 1008 |
Result := ADefaultValue; |
| 1009 |
Exit; |
| 1010 |
end; |
| 1011 |
end; |
| 1012 |
|
| 1013 |
if Res is TCasScriptFloat then |
| 1014 |
Result := TCasScriptFloat(Res).Value else |
| 1015 |
begin |
| 1016 |
WritelnWarning('CastleScript', 'CastleScript expression result is not float'); |
| 1017 |
Result := ADefaultValue; |
| 1018 |
end; |
| 1019 |
end; |
| 1020 |
|
| 1021 |
function TCasScriptExpression.AsInt(const ADefaultValue: Int64): Int64; |
| 1022 |
var |
| 1023 |
Res: TCasScriptValue; |
| 1024 |
begin |
| 1025 |
try |
| 1026 |
Res := Execute; |
| 1027 |
except |
| 1028 |
on E: ECasScriptError do |
| 1029 |
begin |
| 1030 |
WritelnWarning('CastleScript', 'Error when executing CastleScript expression: ' + E.Message); |
| 1031 |
Result := ADefaultValue; |
| 1032 |
Exit; |
| 1033 |
end; |
| 1034 |
end; |
| 1035 |
|
| 1036 |
if Res is TCasScriptInteger then |
| 1037 |
Result := TCasScriptInteger(Res).Value else |
| 1038 |
begin |
| 1039 |
WritelnWarning('CastleScript', 'CastleScript expression result is not int'); |
| 1040 |
Result := ADefaultValue; |
| 1041 |
end; |
| 1042 |
end; |
| 1043 |
|
| 1044 |
function TCasScriptExpression.AsString(const ADefaultValue: String): string; |
| 1045 |
var |
| 1046 |
Res: TCasScriptValue; |
| 1047 |
begin |
| 1048 |
try |
| 1049 |
Res := Execute; |
| 1050 |
except |
| 1051 |
on E: ECasScriptError do |
| 1052 |
begin |
| 1053 |
WritelnWarning('CastleScript', 'Error when executing CastleScript expression: ' + E.Message); |
| 1054 |
Result := ADefaultValue; |
| 1055 |
Exit; |
| 1056 |
end; |
| 1057 |
end; |
| 1058 |
|
| 1059 |
if Res is TCasScriptString then |
| 1060 |
Result := TCasScriptString(Res).Value else |
| 1061 |
begin |
| 1062 |
WritelnWarning('CastleScript', 'CastleScript expression result is not string'); |
| 1063 |
Result := ADefaultValue; |
| 1064 |
end; |
| 1065 |
end; |
| 1066 |
|
| 1067 |
function TCasScriptExpression.AsBool(const ADefaultValue: boolean): boolean; |
| 1068 |
var |
| 1069 |
Res: TCasScriptValue; |
| 1070 |
begin |
| 1071 |
try |
| 1072 |
Res := Execute; |
| 1073 |
except |
| 1074 |
on E: ECasScriptError do |
| 1075 |
begin |
| 1076 |
WritelnWarning('CastleScript', 'Error when executing CastleScript expression: ' + E.Message); |
| 1077 |
Result := ADefaultValue; |
| 1078 |
Exit; |
| 1079 |
end; |
| 1080 |
end; |
| 1081 |
|
| 1082 |
if Res is TCasScriptBoolean then |
| 1083 |
Result := TCasScriptBoolean(Res).Value else |
| 1084 |
begin |
| 1085 |
WritelnWarning('CastleScript', 'CastleScript expression result is not boolean'); |
| 1086 |
Result := ADefaultValue; |
| 1087 |
end; |
| 1088 |
end; |
| 1089 |
|
| 1090 |
{ TCasScriptExpressionList -------------------------------------------------- } |
| 1091 |
|
| 1092 |
procedure TCasScriptExpressionList.AddArray(const A: array of TCasScriptExpression); |
| 1093 |
begin |
| 1094 |
AddRange(A); |
| 1095 |
end; |
| 1096 |
|
| 1097 |
procedure TCasScriptExpressionList.AddList(const Source: TCasScriptExpressionList); |
| 1098 |
begin |
| 1099 |
AddRange(Source); |
| 1100 |
end; |
| 1101 |
|
| 1102 |
procedure TCasScriptExpressionList.FreeContentsByParentExpression; |
| 1103 |
var |
| 1104 |
I: Integer; |
| 1105 |
begin |
| 1106 |
for I := 0 to Count - 1 do |
| 1107 |
begin |
| 1108 |
Items[I].FreeByParentExpression; |
| 1109 |
Items[I] := nil; |
| 1110 |
end; |
| 1111 |
end; |
| 1112 |
|
| 1113 |
{ TCasScriptValue ------------------------------------------------------------ } |
| 1114 |
|
| 1115 |
constructor TCasScriptValue.Create(const AWriteable: boolean); |
| 1116 |
begin |
| 1117 |
inherited Create; |
| 1118 |
FOwnedByParentExpression := true; |
| 1119 |
FWriteable := AWriteable; |
| 1120 |
end; |
| 1121 |
|
| 1122 |
function TCasScriptValue.CoreExecute: TCasScriptValue; |
| 1123 |
begin |
| 1124 |
{ Since we own Execute result, we can simply return self here. } |
| 1125 |
Result := Self; |
| 1126 |
end; |
| 1127 |
|
| 1128 |
{ TCasScriptValueList ------------------------------------------------------- } |
| 1129 |
|
| 1130 |
procedure TCasScriptValueList.AddArray(const A: array of TCasScriptValue); |
| 1131 |
begin |
| 1132 |
AddRange(A); |
| 1133 |
end; |
| 1134 |
|
| 1135 |
function TCasScriptValueList.FindName(const VariableName: string): TCasScriptValue; |
| 1136 |
var |
| 1137 |
I: Integer; |
| 1138 |
begin |
| 1139 |
for I := 0 to Count - 1 do |
| 1140 |
if Items[I].Name = VariableName then |
| 1141 |
Exit(Items[I]); |
| 1142 |
Result := nil; |
| 1143 |
end; |
| 1144 |
|
| 1145 |
{ TCasScriptParameterValue --------------------------------------------------- } |
| 1146 |
|
| 1147 |
function TCasScriptParameterValue.CoreExecute: TCasScriptValue; |
| 1148 |
begin |
| 1149 |
Result := SourceValue.CoreExecute; |
| 1150 |
end; |
| 1151 |
|
| 1152 |
procedure TCasScriptParameterValue.AssignValue(Source: TCasScriptValue); |
| 1153 |
begin |
| 1154 |
SourceValue.AssignValue(Source); |
| 1155 |
end; |
| 1156 |
|
| 1157 |
{ TCasScriptInteger ---------------------------------------------------------- } |
| 1158 |
|
| 1159 |
constructor TCasScriptInteger.Create(const AWriteable: boolean; const AValue: Int64); |
| 1160 |
begin |
| 1161 |
Create(AWriteable); |
| 1162 |
Value := AValue; |
| 1163 |
end; |
| 1164 |
|
| 1165 |
constructor TCasScriptInteger.Create(const AWriteable: boolean); |
| 1166 |
begin |
| 1167 |
inherited Create(AWriteable); |
| 1168 |
end; |
| 1169 |
|
| 1170 |
destructor TCasScriptInteger.Destroy; |
| 1171 |
begin |
| 1172 |
FPromoteToFloat.FreeByParentExpression; |
| 1173 |
inherited; |
| 1174 |
end; |
| 1175 |
|
| 1176 |
function TCasScriptInteger.PromoteToFloat: TCasScriptFloat; |
| 1177 |
begin |
| 1178 |
if FPromoteToFloat = nil then |
| 1179 |
FPromoteToFloat := TCasScriptFloat.Create(false, Value) else |
| 1180 |
FPromoteToFloat.Value := Value; |
| 1181 |
Result := FPromoteToFloat; |
| 1182 |
end; |
| 1183 |
|
| 1184 |
class procedure TCasScriptInteger.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1185 |
var |
| 1186 |
I: Integer; |
| 1187 |
begin |
| 1188 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1189 |
{ The function allows only >= 1 arguments, and this handler is |
| 1190 |
registered only for TCasScriptInteger values, so we can safely take |
| 1191 |
the first arg as TCasScriptInteger. } |
| 1192 |
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1193 |
for I := 1 to Length(Arguments) - 1 do |
| 1194 |
TCasScriptInteger(AResult).Value := |
| 1195 |
TCasScriptInteger(AResult).Value + TCasScriptInteger(Arguments[I]).Value; |
| 1196 |
end; |
| 1197 |
|
| 1198 |
class procedure TCasScriptInteger.HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1199 |
var |
| 1200 |
I: Integer; |
| 1201 |
begin |
| 1202 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1203 |
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1204 |
for I := 1 to Length(Arguments) - 1 do |
| 1205 |
TCasScriptInteger(AResult).Value := |
| 1206 |
TCasScriptInteger(AResult).Value - TCasScriptInteger(Arguments[I]).Value; |
| 1207 |
end; |
| 1208 |
|
| 1209 |
class procedure TCasScriptInteger.HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1210 |
var |
| 1211 |
I: Integer; |
| 1212 |
begin |
| 1213 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1214 |
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1215 |
for I := 1 to Length(Arguments) - 1 do |
| 1216 |
TCasScriptInteger(AResult).Value := |
| 1217 |
TCasScriptInteger(AResult).Value * TCasScriptInteger(Arguments[I]).Value; |
| 1218 |
end; |
| 1219 |
|
| 1220 |
class procedure TCasScriptInteger.HandleIntDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1221 |
var |
| 1222 |
I: Integer; |
| 1223 |
begin |
| 1224 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1225 |
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1226 |
for I := 1 to Length(Arguments) - 1 do |
| 1227 |
TCasScriptInteger(AResult).Value := |
| 1228 |
TCasScriptInteger(AResult).Value div TCasScriptInteger(Arguments[I]).Value; |
| 1229 |
end; |
| 1230 |
|
| 1231 |
class procedure TCasScriptInteger.HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1232 |
begin |
| 1233 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1234 |
TCasScriptInteger(AResult).Value := - TCasScriptInteger(Arguments[0]).Value; |
| 1235 |
end; |
| 1236 |
|
| 1237 |
class procedure TCasScriptInteger.HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1238 |
begin |
| 1239 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1240 |
TCasScriptInteger(AResult).Value := |
| 1241 |
TCasScriptInteger(Arguments[0]).Value mod |
| 1242 |
TCasScriptInteger(Arguments[1]).Value; |
| 1243 |
end; |
| 1244 |
|
| 1245 |
class procedure TCasScriptInteger.HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1246 |
begin |
| 1247 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1248 |
if (TCasScriptInteger(Arguments[0]).Value < 0) or |
| 1249 |
(TCasScriptInteger(Arguments[1]).Value < 0) then |
| 1250 |
raise ECasScriptError.Create('Power function on integer operands expects both arguments to be >= 0'); |
| 1251 |
|
| 1252 |
TCasScriptInteger(AResult).Value := NatNatPower( |
| 1253 |
TCasScriptInteger(Arguments[0]).Value, |
| 1254 |
TCasScriptInteger(Arguments[1]).Value ); |
| 1255 |
end; |
| 1256 |
|
| 1257 |
class procedure TCasScriptInteger.HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1258 |
begin |
| 1259 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1260 |
TCasScriptInteger(AResult).Value := Sqr( TCasScriptInteger(Arguments[0]).Value ); |
| 1261 |
end; |
| 1262 |
|
| 1263 |
class procedure TCasScriptInteger.HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1264 |
var |
| 1265 |
I: Integer; |
| 1266 |
begin |
| 1267 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1268 |
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1269 |
for I := 1 to Length(Arguments) - 1 do |
| 1270 |
TCasScriptInteger(AResult).Value := Max( |
| 1271 |
TCasScriptInteger(AResult).Value, TCasScriptInteger(Arguments[I]).Value); |
| 1272 |
end; |
| 1273 |
|
| 1274 |
class procedure TCasScriptInteger.HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1275 |
var |
| 1276 |
I: Integer; |
| 1277 |
begin |
| 1278 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1279 |
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1280 |
for I := 1 to Length(Arguments) - 1 do |
| 1281 |
TCasScriptInteger(AResult).Value := Min( |
| 1282 |
TCasScriptInteger(AResult).Value, TCasScriptInteger(Arguments[I]).Value); |
| 1283 |
end; |
| 1284 |
|
| 1285 |
class procedure TCasScriptInteger.HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1286 |
begin |
| 1287 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1288 |
TCasScriptInteger(AResult).Value := Sign( TCasScriptInteger(Arguments[0]).Value ); |
| 1289 |
end; |
| 1290 |
|
| 1291 |
class procedure TCasScriptInteger.HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1292 |
begin |
| 1293 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1294 |
TCasScriptInteger(AResult).Value := Abs( TCasScriptInteger(Arguments[0]).Value ); |
| 1295 |
end; |
| 1296 |
|
| 1297 |
class procedure TCasScriptInteger.HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1298 |
begin |
| 1299 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1300 |
TCasScriptInteger(AResult).Value := Random( TCasScriptInteger(Arguments[0]).Value ); |
| 1301 |
end; |
| 1302 |
|
| 1303 |
class procedure TCasScriptInteger.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1304 |
begin |
| 1305 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1306 |
TCasScriptBoolean(AResult).Value := |
| 1307 |
TCasScriptInteger(Arguments[0]).Value > |
| 1308 |
TCasScriptInteger(Arguments[1]).Value; |
| 1309 |
end; |
| 1310 |
|
| 1311 |
class procedure TCasScriptInteger.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1312 |
begin |
| 1313 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1314 |
TCasScriptBoolean(AResult).Value := |
| 1315 |
TCasScriptInteger(Arguments[0]).Value < |
| 1316 |
TCasScriptInteger(Arguments[1]).Value; |
| 1317 |
end; |
| 1318 |
|
| 1319 |
class procedure TCasScriptInteger.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1320 |
begin |
| 1321 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1322 |
TCasScriptBoolean(AResult).Value := |
| 1323 |
TCasScriptInteger(Arguments[0]).Value >= |
| 1324 |
TCasScriptInteger(Arguments[1]).Value; |
| 1325 |
end; |
| 1326 |
|
| 1327 |
class procedure TCasScriptInteger.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1328 |
begin |
| 1329 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1330 |
TCasScriptBoolean(AResult).Value := |
| 1331 |
TCasScriptInteger(Arguments[0]).Value <= |
| 1332 |
TCasScriptInteger(Arguments[1]).Value; |
| 1333 |
end; |
| 1334 |
|
| 1335 |
class procedure TCasScriptInteger.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1336 |
begin |
| 1337 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1338 |
TCasScriptBoolean(AResult).Value := |
| 1339 |
TCasScriptInteger(Arguments[0]).Value = |
| 1340 |
TCasScriptInteger(Arguments[1]).Value; |
| 1341 |
end; |
| 1342 |
|
| 1343 |
class procedure TCasScriptInteger.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1344 |
begin |
| 1345 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1346 |
TCasScriptBoolean(AResult).Value := |
| 1347 |
TCasScriptInteger(Arguments[0]).Value <> |
| 1348 |
TCasScriptInteger(Arguments[1]).Value; |
| 1349 |
end; |
| 1350 |
|
| 1351 |
class procedure TCasScriptInteger.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1352 |
begin |
| 1353 |
if ParentOfResult then |
| 1354 |
AResult.FreeByParentExpression else |
| 1355 |
AResult := nil; |
| 1356 |
|
| 1357 |
AResult := Arguments[0]; |
| 1358 |
Assert(AResult is TCasScriptInteger); |
| 1359 |
ParentOfResult := false; |
| 1360 |
end; |
| 1361 |
|
| 1362 |
class procedure TCasScriptInteger.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1363 |
var |
| 1364 |
F: Float; |
| 1365 |
begin |
| 1366 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1367 |
{ Can't use Int function, as it returns float value } |
| 1368 |
|
| 1369 |
F := TCasScriptFloat(Arguments[0]).Value; |
| 1370 |
if F >= 0 then |
| 1371 |
TCasScriptInteger(AResult).Value := Floor(F) else |
| 1372 |
TCasScriptInteger(AResult).Value := Ceil(F); |
| 1373 |
end; |
| 1374 |
|
| 1375 |
class procedure TCasScriptInteger.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1376 |
const |
| 1377 |
BoolTo01: array [boolean] of Int64 = (0, 1); |
| 1378 |
begin |
| 1379 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1380 |
TCasScriptInteger(AResult).Value := BoolTo01[TCasScriptBoolean(Arguments[0]).Value]; |
| 1381 |
end; |
| 1382 |
|
| 1383 |
class procedure TCasScriptInteger.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1384 |
begin |
| 1385 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1386 |
try |
| 1387 |
TCasScriptInteger(AResult).Value := StrToInt64(TCasScriptString(Arguments[0]).Value); |
| 1388 |
except |
| 1389 |
on E: EConvertError do |
| 1390 |
{ Change EConvertError to ECasScriptError } |
| 1391 |
raise ECasScriptError.CreateFmt('Error when converting string "%s" to integer: %s', |
| 1392 |
[TCasScriptString(Arguments[0]).Value, E.Message]); |
| 1393 |
end; |
| 1394 |
end; |
| 1395 |
|
| 1396 |
procedure TCasScriptInteger.AssignValue(Source: TCasScriptValue); |
| 1397 |
begin |
| 1398 |
if Source is TCasScriptInteger then |
| 1399 |
Value := TCasScriptInteger(Source).Value else |
| 1400 |
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]); |
| 1401 |
end; |
| 1402 |
|
| 1403 |
procedure TCasScriptInteger.SetValue(const AValue: Int64); |
| 1404 |
begin |
| 1405 |
FValue := AValue; |
| 1406 |
ValueAssigned := true; |
| 1407 |
end; |
| 1408 |
|
| 1409 |
{ TCasScriptFloat ------------------------------------------------------- } |
| 1410 |
|
| 1411 |
constructor TCasScriptFloat.Create(const AWriteable: boolean; const AValue: Float); |
| 1412 |
begin |
| 1413 |
Create(AWriteable); |
| 1414 |
Value := AValue; |
| 1415 |
end; |
| 1416 |
|
| 1417 |
constructor TCasScriptFloat.Create(const AWriteable: boolean); |
| 1418 |
begin |
| 1419 |
inherited Create(AWriteable); |
| 1420 |
end; |
| 1421 |
|
| 1422 |
class procedure TCasScriptFloat.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1423 |
var |
| 1424 |
I: Integer; |
| 1425 |
begin |
| 1426 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1427 |
{ The function allows only >= 1 arguments, and this handler is |
| 1428 |
registered only for TCasScriptFloat values, so we can safely take |
| 1429 |
the first arg as TCasScriptFloat. } |
| 1430 |
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value; |
| 1431 |
for I := 1 to Length(Arguments) - 1 do |
| 1432 |
TCasScriptFloat(AResult).Value := |
| 1433 |
TCasScriptFloat(AResult).Value + TCasScriptFloat(Arguments[I]).Value; |
| 1434 |
end; |
| 1435 |
|
| 1436 |
class procedure TCasScriptFloat.HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1437 |
var |
| 1438 |
I: Integer; |
| 1439 |
begin |
| 1440 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1441 |
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value; |
| 1442 |
for I := 1 to Length(Arguments) - 1 do |
| 1443 |
TCasScriptFloat(AResult).Value := |
| 1444 |
TCasScriptFloat(AResult).Value - TCasScriptFloat(Arguments[I]).Value; |
| 1445 |
end; |
| 1446 |
|
| 1447 |
class procedure TCasScriptFloat.HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1448 |
var |
| 1449 |
I: Integer; |
| 1450 |
begin |
| 1451 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1452 |
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value; |
| 1453 |
for I := 1 to Length(Arguments) - 1 do |
| 1454 |
TCasScriptFloat(AResult).Value := |
| 1455 |
TCasScriptFloat(AResult).Value * TCasScriptFloat(Arguments[I]).Value; |
| 1456 |
end; |
| 1457 |
|
| 1458 |
class procedure TCasScriptFloat.HandleFloatDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1459 |
var |
| 1460 |
I: Integer; |
| 1461 |
begin |
| 1462 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1463 |
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value; |
| 1464 |
for I := 1 to Length(Arguments) - 1 do |
| 1465 |
TCasScriptFloat(AResult).Value := |
| 1466 |
TCasScriptFloat(AResult).Value / TCasScriptFloat(Arguments[I]).Value; |
| 1467 |
end; |
| 1468 |
|
| 1469 |
class procedure TCasScriptFloat.HandleLerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1470 |
begin |
| 1471 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1472 |
TCasScriptFloat(AResult).Value := Lerp( |
| 1473 |
TCasScriptFloat(Arguments[0]).Value, |
| 1474 |
TCasScriptFloat(Arguments[1]).Value, |
| 1475 |
TCasScriptFloat(Arguments[2]).Value); |
| 1476 |
end; |
| 1477 |
|
| 1478 |
class procedure TCasScriptFloat.HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1479 |
begin |
| 1480 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1481 |
TCasScriptFloat(AResult).Value := - TCasScriptFloat(Arguments[0]).Value; |
| 1482 |
end; |
| 1483 |
|
| 1484 |
class procedure TCasScriptFloat.HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1485 |
begin |
| 1486 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1487 |
TCasScriptFloat(AResult).Value := |
| 1488 |
TCasScriptFloat(Arguments[0]).Value - |
| 1489 |
Floor( TCasScriptFloat(Arguments[0]).Value / |
| 1490 |
TCasScriptFloat(Arguments[1]).Value ) |
| 1491 |
* TCasScriptFloat(Arguments[1]).Value; |
| 1492 |
end; |
| 1493 |
|
| 1494 |
class procedure TCasScriptFloat.HandleSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1495 |
begin |
| 1496 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1497 |
TCasScriptFloat(AResult).Value := Sin( TCasScriptFloat(Arguments[0]).Value ); |
| 1498 |
end; |
| 1499 |
|
| 1500 |
class procedure TCasScriptFloat.HandleCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1501 |
begin |
| 1502 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1503 |
TCasScriptFloat(AResult).Value := Cos( TCasScriptFloat(Arguments[0]).Value ); |
| 1504 |
end; |
| 1505 |
|
| 1506 |
class procedure TCasScriptFloat.HandleTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1507 |
begin |
| 1508 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1509 |
TCasScriptFloat(AResult).Value := Tan( TCasScriptFloat(Arguments[0]).Value ); |
| 1510 |
end; |
| 1511 |
|
| 1512 |
class procedure TCasScriptFloat.HandleCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1513 |
begin |
| 1514 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1515 |
TCasScriptFloat(AResult).Value := CastleCoTan( TCasScriptFloat(Arguments[0]).Value ); |
| 1516 |
end; |
| 1517 |
|
| 1518 |
class procedure TCasScriptFloat.HandleArcSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1519 |
begin |
| 1520 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1521 |
TCasScriptFloat(AResult).Value := ArcSin( TCasScriptFloat(Arguments[0]).Value ); |
| 1522 |
end; |
| 1523 |
|
| 1524 |
class procedure TCasScriptFloat.HandleArcCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1525 |
begin |
| 1526 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1527 |
TCasScriptFloat(AResult).Value := ArcCos( TCasScriptFloat(Arguments[0]).Value ); |
| 1528 |
end; |
| 1529 |
|
| 1530 |
class procedure TCasScriptFloat.HandleArcTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1531 |
begin |
| 1532 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1533 |
TCasScriptFloat(AResult).Value := ArcTan( TCasScriptFloat(Arguments[0]).Value ); |
| 1534 |
end; |
| 1535 |
|
| 1536 |
class procedure TCasScriptFloat.HandleArcCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1537 |
begin |
| 1538 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1539 |
TCasScriptFloat(AResult).Value := ArcCot( TCasScriptFloat(Arguments[0]).Value ); |
| 1540 |
end; |
| 1541 |
|
| 1542 |
class procedure TCasScriptFloat.HandleSinh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1543 |
begin |
| 1544 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1545 |
TCasScriptFloat(AResult).Value := SinH( TCasScriptFloat(Arguments[0]).Value ); |
| 1546 |
end; |
| 1547 |
|
| 1548 |
class procedure TCasScriptFloat.HandleCosh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1549 |
begin |
| 1550 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1551 |
TCasScriptFloat(AResult).Value := CosH( TCasScriptFloat(Arguments[0]).Value ); |
| 1552 |
end; |
| 1553 |
|
| 1554 |
class procedure TCasScriptFloat.HandleTanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1555 |
begin |
| 1556 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1557 |
TCasScriptFloat(AResult).Value := TanH( TCasScriptFloat(Arguments[0]).Value ); |
| 1558 |
end; |
| 1559 |
|
| 1560 |
class procedure TCasScriptFloat.HandleCotanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1561 |
begin |
| 1562 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1563 |
TCasScriptFloat(AResult).Value := 1 / TanH( TCasScriptFloat(Arguments[0]).Value ); |
| 1564 |
end; |
| 1565 |
|
| 1566 |
class procedure TCasScriptFloat.HandleLog2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1567 |
begin |
| 1568 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1569 |
TCasScriptFloat(AResult).Value := Log2( TCasScriptFloat(Arguments[0]).Value ); |
| 1570 |
end; |
| 1571 |
|
| 1572 |
class procedure TCasScriptFloat.HandleLn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1573 |
begin |
| 1574 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1575 |
TCasScriptFloat(AResult).Value := Ln( TCasScriptFloat(Arguments[0]).Value ); |
| 1576 |
end; |
| 1577 |
|
| 1578 |
class procedure TCasScriptFloat.HandleLog(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1579 |
begin |
| 1580 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1581 |
TCasScriptFloat(AResult).Value := Logn( TCasScriptFloat(Arguments[0]).Value, |
| 1582 |
TCasScriptFloat(Arguments[1]).Value ); |
| 1583 |
end; |
| 1584 |
|
| 1585 |
class procedure TCasScriptFloat.HandlePower2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1586 |
begin |
| 1587 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1588 |
TCasScriptFloat(AResult).Value := Power(2, TCasScriptFloat(Arguments[0]).Value); |
| 1589 |
end; |
| 1590 |
|
| 1591 |
class procedure TCasScriptFloat.HandleExp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1592 |
begin |
| 1593 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1594 |
TCasScriptFloat(AResult).Value := Exp( TCasScriptFloat(Arguments[0]).Value ); |
| 1595 |
end; |
| 1596 |
|
| 1597 |
class procedure TCasScriptFloat.HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1598 |
begin |
| 1599 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1600 |
TCasScriptFloat(AResult).Value := Power( |
| 1601 |
TCasScriptFloat(Arguments[0]).Value, |
| 1602 |
TCasScriptFloat(Arguments[1]).Value ); |
| 1603 |
end; |
| 1604 |
|
| 1605 |
class procedure TCasScriptFloat.HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1606 |
begin |
| 1607 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1608 |
TCasScriptFloat(AResult).Value := Sqr( TCasScriptFloat(Arguments[0]).Value ); |
| 1609 |
end; |
| 1610 |
|
| 1611 |
class procedure TCasScriptFloat.HandleSqrt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1612 |
begin |
| 1613 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1614 |
TCasScriptFloat(AResult).Value := Sqrt( TCasScriptFloat(Arguments[0]).Value ); |
| 1615 |
end; |
| 1616 |
|
| 1617 |
class procedure TCasScriptFloat.HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1618 |
var |
| 1619 |
I: Integer; |
| 1620 |
begin |
| 1621 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1622 |
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value; |
| 1623 |
for I := 1 to Length(Arguments) - 1 do |
| 1624 |
TCasScriptFloat(AResult).Value := Max( |
| 1625 |
TCasScriptFloat(AResult).Value, TCasScriptFloat(Arguments[I]).Value); |
| 1626 |
end; |
| 1627 |
|
| 1628 |
class procedure TCasScriptFloat.HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1629 |
var |
| 1630 |
I: Integer; |
| 1631 |
begin |
| 1632 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1633 |
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value; |
| 1634 |
for I := 1 to Length(Arguments) - 1 do |
| 1635 |
TCasScriptFloat(AResult).Value := Min( |
| 1636 |
TCasScriptFloat(AResult).Value, TCasScriptFloat(Arguments[I]).Value); |
| 1637 |
end; |
| 1638 |
|
| 1639 |
class procedure TCasScriptFloat.HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1640 |
begin |
| 1641 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1642 |
TCasScriptInteger(AResult).Value := Sign( TCasScriptFloat(Arguments[0]).Value ); |
| 1643 |
end; |
| 1644 |
|
| 1645 |
class procedure TCasScriptFloat.HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1646 |
begin |
| 1647 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1648 |
TCasScriptFloat(AResult).Value := Abs( TCasScriptFloat(Arguments[0]).Value ); |
| 1649 |
end; |
| 1650 |
|
| 1651 |
class procedure TCasScriptFloat.HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1652 |
begin |
| 1653 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1654 |
TCasScriptFloat(AResult).Value := Random(); |
| 1655 |
end; |
| 1656 |
|
| 1657 |
class procedure TCasScriptFloat.HandleCeil(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1658 |
begin |
| 1659 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1660 |
TCasScriptInteger(AResult).Value := Ceil( TCasScriptFloat(Arguments[0]).Value ); |
| 1661 |
end; |
| 1662 |
|
| 1663 |
class procedure TCasScriptFloat.HandleFloor(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1664 |
begin |
| 1665 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1666 |
TCasScriptInteger(AResult).Value := Floor( TCasScriptFloat(Arguments[0]).Value ); |
| 1667 |
end; |
| 1668 |
|
| 1669 |
class procedure TCasScriptFloat.HandleRound(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1670 |
begin |
| 1671 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 1672 |
TCasScriptInteger(AResult).Value := Round( TCasScriptFloat(Arguments[0]).Value ); |
| 1673 |
end; |
| 1674 |
|
| 1675 |
class procedure TCasScriptFloat.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1676 |
begin |
| 1677 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1678 |
TCasScriptBoolean(AResult).Value := |
| 1679 |
TCasScriptFloat(Arguments[0]).Value > |
| 1680 |
TCasScriptFloat(Arguments[1]).Value; |
| 1681 |
end; |
| 1682 |
|
| 1683 |
class procedure TCasScriptFloat.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1684 |
begin |
| 1685 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1686 |
TCasScriptBoolean(AResult).Value := |
| 1687 |
TCasScriptFloat(Arguments[0]).Value < |
| 1688 |
TCasScriptFloat(Arguments[1]).Value; |
| 1689 |
end; |
| 1690 |
|
| 1691 |
class procedure TCasScriptFloat.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1692 |
begin |
| 1693 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1694 |
TCasScriptBoolean(AResult).Value := |
| 1695 |
TCasScriptFloat(Arguments[0]).Value >= |
| 1696 |
TCasScriptFloat(Arguments[1]).Value; |
| 1697 |
end; |
| 1698 |
|
| 1699 |
class procedure TCasScriptFloat.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1700 |
begin |
| 1701 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1702 |
TCasScriptBoolean(AResult).Value := |
| 1703 |
TCasScriptFloat(Arguments[0]).Value <= |
| 1704 |
TCasScriptFloat(Arguments[1]).Value; |
| 1705 |
end; |
| 1706 |
|
| 1707 |
class procedure TCasScriptFloat.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1708 |
begin |
| 1709 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1710 |
TCasScriptBoolean(AResult).Value := |
| 1711 |
TCasScriptFloat(Arguments[0]).Value = |
| 1712 |
TCasScriptFloat(Arguments[1]).Value; |
| 1713 |
end; |
| 1714 |
|
| 1715 |
class procedure TCasScriptFloat.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1716 |
begin |
| 1717 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1718 |
TCasScriptBoolean(AResult).Value := |
| 1719 |
TCasScriptFloat(Arguments[0]).Value <> |
| 1720 |
TCasScriptFloat(Arguments[1]).Value; |
| 1721 |
end; |
| 1722 |
|
| 1723 |
class procedure TCasScriptFloat.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1724 |
begin |
| 1725 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1726 |
TCasScriptFloat(AResult).Value := TCasScriptInteger(Arguments[0]).Value; |
| 1727 |
end; |
| 1728 |
|
| 1729 |
class procedure TCasScriptFloat.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1730 |
begin |
| 1731 |
if ParentOfResult then |
| 1732 |
AResult.FreeByParentExpression else |
| 1733 |
AResult := nil; |
| 1734 |
|
| 1735 |
AResult := Arguments[0]; |
| 1736 |
Assert(AResult is TCasScriptFloat); |
| 1737 |
ParentOfResult := false; |
| 1738 |
end; |
| 1739 |
|
| 1740 |
class procedure TCasScriptFloat.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1741 |
const |
| 1742 |
BoolTo01: array [boolean] of Float = (0, 1); |
| 1743 |
begin |
| 1744 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1745 |
TCasScriptFloat(AResult).Value := BoolTo01[TCasScriptBoolean(Arguments[0]).Value]; |
| 1746 |
end; |
| 1747 |
|
| 1748 |
class procedure TCasScriptFloat.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1749 |
begin |
| 1750 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 1751 |
try |
| 1752 |
TCasScriptFloat(AResult).Value := StrToFloatDot(TCasScriptString(Arguments[0]).Value); |
| 1753 |
except |
| 1754 |
on E: EConvertError do |
| 1755 |
{ Change EConvertError to ECasScriptError } |
| 1756 |
raise ECasScriptError.CreateFmt('Error when converting string "%s" to float: %s', |
| 1757 |
[TCasScriptString(Arguments[0]).Value, E.Message]); |
| 1758 |
end; |
| 1759 |
end; |
| 1760 |
|
| 1761 |
procedure TCasScriptFloat.AssignValue(Source: TCasScriptValue); |
| 1762 |
begin |
| 1763 |
if Source is TCasScriptFloat then |
| 1764 |
Value := TCasScriptFloat(Source).Value else |
| 1765 |
{ This allows for type promotion integer->float at assignment. } |
| 1766 |
if Source is TCasScriptInteger then |
| 1767 |
Value := TCasScriptInteger(Source).Value else |
| 1768 |
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]); |
| 1769 |
end; |
| 1770 |
|
| 1771 |
procedure TCasScriptFloat.SetValue(const AValue: Float); |
| 1772 |
begin |
| 1773 |
FValue := AValue; |
| 1774 |
ValueAssigned := true; |
| 1775 |
end; |
| 1776 |
|
| 1777 |
{ TCasScriptBoolean ---------------------------------------------------------- } |
| 1778 |
|
| 1779 |
constructor TCasScriptBoolean.Create(const AWriteable: boolean; const AValue: boolean); |
| 1780 |
begin |
| 1781 |
Create(AWriteable); |
| 1782 |
Value := AValue; |
| 1783 |
end; |
| 1784 |
|
| 1785 |
constructor TCasScriptBoolean.Create(const AWriteable: boolean); |
| 1786 |
begin |
| 1787 |
inherited Create(AWriteable); |
| 1788 |
end; |
| 1789 |
|
| 1790 |
class procedure TCasScriptBoolean.HandleOr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1791 |
var |
| 1792 |
I: Integer; |
| 1793 |
begin |
| 1794 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1795 |
TCasScriptBoolean(AResult).Value := TCasScriptBoolean(Arguments[0]).Value; |
| 1796 |
for I := 1 to Length(Arguments) - 1 do |
| 1797 |
TCasScriptBoolean(AResult).Value := |
| 1798 |
TCasScriptBoolean(AResult).Value or TCasScriptBoolean(Arguments[I]).Value; |
| 1799 |
end; |
| 1800 |
|
| 1801 |
class procedure TCasScriptBoolean.HandleAnd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1802 |
var |
| 1803 |
I: Integer; |
| 1804 |
begin |
| 1805 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1806 |
TCasScriptBoolean(AResult).Value := TCasScriptBoolean(Arguments[0]).Value; |
| 1807 |
for I := 1 to Length(Arguments) - 1 do |
| 1808 |
TCasScriptBoolean(AResult).Value := |
| 1809 |
TCasScriptBoolean(AResult).Value and TCasScriptBoolean(Arguments[I]).Value; |
| 1810 |
end; |
| 1811 |
|
| 1812 |
class procedure TCasScriptBoolean.HandleNot(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1813 |
begin |
| 1814 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1815 |
TCasScriptBoolean(AResult).Value := not TCasScriptBoolean(Arguments[0]).Value; |
| 1816 |
end; |
| 1817 |
|
| 1818 |
class procedure TCasScriptBoolean.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1819 |
begin |
| 1820 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1821 |
TCasScriptBoolean(AResult).Value := |
| 1822 |
TCasScriptBoolean(Arguments[0]).Value > |
| 1823 |
TCasScriptBoolean(Arguments[1]).Value; |
| 1824 |
end; |
| 1825 |
|
| 1826 |
class procedure TCasScriptBoolean.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1827 |
begin |
| 1828 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1829 |
TCasScriptBoolean(AResult).Value := |
| 1830 |
TCasScriptBoolean(Arguments[0]).Value < |
| 1831 |
TCasScriptBoolean(Arguments[1]).Value; |
| 1832 |
end; |
| 1833 |
|
| 1834 |
class procedure TCasScriptBoolean.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1835 |
begin |
| 1836 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1837 |
TCasScriptBoolean(AResult).Value := |
| 1838 |
TCasScriptBoolean(Arguments[0]).Value >= |
| 1839 |
TCasScriptBoolean(Arguments[1]).Value; |
| 1840 |
end; |
| 1841 |
|
| 1842 |
class procedure TCasScriptBoolean.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1843 |
begin |
| 1844 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1845 |
TCasScriptBoolean(AResult).Value := |
| 1846 |
TCasScriptBoolean(Arguments[0]).Value <= |
| 1847 |
TCasScriptBoolean(Arguments[1]).Value; |
| 1848 |
end; |
| 1849 |
|
| 1850 |
class procedure TCasScriptBoolean.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1851 |
begin |
| 1852 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1853 |
TCasScriptBoolean(AResult).Value := |
| 1854 |
TCasScriptBoolean(Arguments[0]).Value = |
| 1855 |
TCasScriptBoolean(Arguments[1]).Value; |
| 1856 |
end; |
| 1857 |
|
| 1858 |
class procedure TCasScriptBoolean.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1859 |
begin |
| 1860 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1861 |
TCasScriptBoolean(AResult).Value := |
| 1862 |
TCasScriptBoolean(Arguments[0]).Value <> |
| 1863 |
TCasScriptBoolean(Arguments[1]).Value; |
| 1864 |
end; |
| 1865 |
|
| 1866 |
class procedure TCasScriptBoolean.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1867 |
begin |
| 1868 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1869 |
TCasScriptBoolean(AResult).Value := TCasScriptInteger(Arguments[0]).Value <> 0; |
| 1870 |
end; |
| 1871 |
|
| 1872 |
class procedure TCasScriptBoolean.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1873 |
begin |
| 1874 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1875 |
TCasScriptBoolean(AResult).Value := TCasScriptFloat(Arguments[0]).Value <> 0; |
| 1876 |
end; |
| 1877 |
|
| 1878 |
class procedure TCasScriptBoolean.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1879 |
begin |
| 1880 |
if ParentOfResult then |
| 1881 |
AResult.FreeByParentExpression else |
| 1882 |
AResult := nil; |
| 1883 |
|
| 1884 |
AResult := Arguments[0]; |
| 1885 |
Assert(AResult is TCasScriptBoolean); |
| 1886 |
ParentOfResult := false; |
| 1887 |
end; |
| 1888 |
|
| 1889 |
class procedure TCasScriptBoolean.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1890 |
var |
| 1891 |
S: string; |
| 1892 |
begin |
| 1893 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1894 |
S := LowerCase(TCasScriptString(Arguments[0]).Value); |
| 1895 |
if S = 'false' then |
| 1896 |
TCasScriptBoolean(AResult).Value := false else |
| 1897 |
if S = 'true' then |
| 1898 |
TCasScriptBoolean(AResult).Value := true else |
| 1899 |
raise ECasScriptError.CreateFmt('Error when converting string "%s" to boolean: invalid value, must be "false" or "true"', |
| 1900 |
[TCasScriptString(Arguments[0]).Value]); |
| 1901 |
end; |
| 1902 |
|
| 1903 |
procedure TCasScriptBoolean.AssignValue(Source: TCasScriptValue); |
| 1904 |
begin |
| 1905 |
if Source is TCasScriptBoolean then |
| 1906 |
Value := TCasScriptBoolean(Source).Value else |
| 1907 |
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]); |
| 1908 |
end; |
| 1909 |
|
| 1910 |
procedure TCasScriptBoolean.SetValue(const AValue: Boolean); |
| 1911 |
begin |
| 1912 |
FValue := AValue; |
| 1913 |
ValueAssigned := true; |
| 1914 |
end; |
| 1915 |
|
| 1916 |
{ TCasScriptString ---------------------------------------------------------- } |
| 1917 |
|
| 1918 |
constructor TCasScriptString.Create(const AWriteable: boolean; const AValue: String); |
| 1919 |
begin |
| 1920 |
Create(AWriteable); |
| 1921 |
Value := AValue; |
| 1922 |
end; |
| 1923 |
|
| 1924 |
constructor TCasScriptString.Create(const AWriteable: boolean); |
| 1925 |
begin |
| 1926 |
inherited Create(AWriteable); |
| 1927 |
end; |
| 1928 |
|
| 1929 |
class procedure TCasScriptString.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1930 |
var |
| 1931 |
I: Integer; |
| 1932 |
begin |
| 1933 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString); |
| 1934 |
{ The function allows only >= 1 arguments, and this handler is |
| 1935 |
registered only for TCasScriptString values, so we can safely take |
| 1936 |
the first arg as TCasScriptString. } |
| 1937 |
TCasScriptString(AResult).Value := TCasScriptString(Arguments[0]).Value; |
| 1938 |
for I := 1 to Length(Arguments) - 1 do |
| 1939 |
TCasScriptString(AResult).Value := |
| 1940 |
TCasScriptString(AResult).Value + TCasScriptString(Arguments[I]).Value; |
| 1941 |
end; |
| 1942 |
|
| 1943 |
class procedure TCasScriptString.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1944 |
begin |
| 1945 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1946 |
TCasScriptBoolean(AResult).Value := |
| 1947 |
TCasScriptString(Arguments[0]).Value > |
| 1948 |
TCasScriptString(Arguments[1]).Value; |
| 1949 |
end; |
| 1950 |
|
| 1951 |
class procedure TCasScriptString.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1952 |
begin |
| 1953 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1954 |
TCasScriptBoolean(AResult).Value := |
| 1955 |
TCasScriptString(Arguments[0]).Value < |
| 1956 |
TCasScriptString(Arguments[1]).Value; |
| 1957 |
end; |
| 1958 |
|
| 1959 |
class procedure TCasScriptString.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1960 |
begin |
| 1961 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1962 |
TCasScriptBoolean(AResult).Value := |
| 1963 |
TCasScriptString(Arguments[0]).Value >= |
| 1964 |
TCasScriptString(Arguments[1]).Value; |
| 1965 |
end; |
| 1966 |
|
| 1967 |
class procedure TCasScriptString.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1968 |
begin |
| 1969 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1970 |
TCasScriptBoolean(AResult).Value := |
| 1971 |
TCasScriptString(Arguments[0]).Value <= |
| 1972 |
TCasScriptString(Arguments[1]).Value; |
| 1973 |
end; |
| 1974 |
|
| 1975 |
class procedure TCasScriptString.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1976 |
begin |
| 1977 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1978 |
TCasScriptBoolean(AResult).Value := |
| 1979 |
TCasScriptString(Arguments[0]).Value = |
| 1980 |
TCasScriptString(Arguments[1]).Value; |
| 1981 |
end; |
| 1982 |
|
| 1983 |
class procedure TCasScriptString.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1984 |
begin |
| 1985 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 1986 |
TCasScriptBoolean(AResult).Value := |
| 1987 |
TCasScriptString(Arguments[0]).Value <> |
| 1988 |
TCasScriptString(Arguments[1]).Value; |
| 1989 |
end; |
| 1990 |
|
| 1991 |
class procedure TCasScriptString.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1992 |
begin |
| 1993 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString); |
| 1994 |
TCasScriptString(AResult).Value := IntToStr(TCasScriptInteger(Arguments[0]).Value); |
| 1995 |
end; |
| 1996 |
|
| 1997 |
class procedure TCasScriptString.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 1998 |
begin |
| 1999 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString); |
| 2000 |
TCasScriptString(AResult).Value := FloatToStrDot(TCasScriptFloat(Arguments[0]).Value); |
| 2001 |
end; |
| 2002 |
|
| 2003 |
class procedure TCasScriptString.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2004 |
const |
| 2005 |
BoolTo01: array [boolean] of string = ('false', 'true'); |
| 2006 |
begin |
| 2007 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString); |
| 2008 |
TCasScriptString(AResult).Value := BoolTo01[TCasScriptBoolean(Arguments[0]).Value]; |
| 2009 |
end; |
| 2010 |
|
| 2011 |
class procedure TCasScriptString.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2012 |
begin |
| 2013 |
if ParentOfResult then |
| 2014 |
AResult.FreeByParentExpression else |
| 2015 |
AResult := nil; |
| 2016 |
|
| 2017 |
AResult := Arguments[0]; |
| 2018 |
Assert(AResult is TCasScriptString); |
| 2019 |
ParentOfResult := false; |
| 2020 |
end; |
| 2021 |
|
| 2022 |
class procedure TCasScriptString.HandleWriteln(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2023 |
var |
| 2024 |
S: string; |
| 2025 |
begin |
| 2026 |
if ParentOfResult then |
| 2027 |
AResult.FreeByParentExpression else |
| 2028 |
AResult := nil; |
| 2029 |
|
| 2030 |
AResult := Arguments[0]; |
| 2031 |
ParentOfResult := false; |
| 2032 |
|
| 2033 |
S := TCasScriptString(Arguments[0]).Value; |
| 2034 |
|
| 2035 |
if Assigned(OnScriptMessage) then |
| 2036 |
OnScriptMessage(S) else |
| 2037 |
WritelnLog('CastleScript', 'Writeln: '+ S); |
| 2038 |
end; |
| 2039 |
|
| 2040 |
class procedure TCasScriptString.HandleCharacterFromCode(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2041 |
var |
| 2042 |
CharCode: Int64; |
| 2043 |
begin |
| 2044 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString); |
| 2045 |
|
| 2046 |
CharCode := TCasScriptInteger(Arguments[0]).Value; |
| 2047 |
|
| 2048 |
if Between(CharCode, Low(Byte), High(Byte)) then |
| 2049 |
TCasScriptString(AResult).Value := Chr(CharCode) else |
| 2050 |
TCasScriptString(AResult).Value := ''; |
| 2051 |
end; |
| 2052 |
|
| 2053 |
procedure TCasScriptString.AssignValue(Source: TCasScriptValue); |
| 2054 |
begin |
| 2055 |
if Source is TCasScriptString then |
| 2056 |
Value := TCasScriptString(Source).Value else |
| 2057 |
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]); |
| 2058 |
end; |
| 2059 |
|
| 2060 |
procedure TCasScriptString.SetValue(const AValue: String); |
| 2061 |
begin |
| 2062 |
FValue := AValue; |
| 2063 |
ValueAssigned := true; |
| 2064 |
end; |
| 2065 |
|
| 2066 |
{ TCasScriptFunction --------------------------------------------------------- } |
| 2067 |
|
| 2068 |
constructor TCasScriptFunction.Create(AArgs: TCasScriptExpressionList); |
| 2069 |
begin |
| 2070 |
inherited Create; |
| 2071 |
FArgs := TCasScriptExpressionList.Create(false); |
| 2072 |
FArgs.AddRange(AArgs); |
| 2073 |
CheckArguments; |
| 2074 |
end; |
| 2075 |
|
| 2076 |
constructor TCasScriptFunction.Create(const AArgs: array of TCasScriptExpression); |
| 2077 |
begin |
| 2078 |
inherited Create; |
| 2079 |
FArgs := TCasScriptExpressionList.Create(false); |
| 2080 |
FArgs.AddRange(AArgs); |
| 2081 |
CheckArguments; |
| 2082 |
end; |
| 2083 |
|
| 2084 |
procedure TCasScriptFunction.CheckArguments; |
| 2085 |
var |
| 2086 |
I: Integer; |
| 2087 |
begin |
| 2088 |
for I := 0 to Args.Count - 1 do |
| 2089 |
if ArgumentMustBeAssignable(I) and |
| 2090 |
not ( (Args[I] is TCasScriptValue) and |
| 2091 |
TCasScriptValue(Args[I]).Writeable ) then |
| 2092 |
raise ECasScriptFunctionArgumentsError.CreateFmt('Argument %d of function %s must be a writeable operand (but is not)', |
| 2093 |
[I, Name]); |
| 2094 |
|
| 2095 |
if not FunctionHandlers.SearchFunctionClass( |
| 2096 |
TCasScriptFunctionClass(Self.ClassType), HandlersByArgument) then |
| 2097 |
raise ECasScriptFunctionNoHandler.CreateFmt('No handler defined for function "%s"', [Name]); |
| 2098 |
|
| 2099 |
SetLength(ExecuteArguments, Args.Count); |
| 2100 |
SetLength(ExecuteArgumentClasses, Args.Count); |
| 2101 |
end; |
| 2102 |
|
| 2103 |
destructor TCasScriptFunction.Destroy; |
| 2104 |
begin |
| 2105 |
if FArgs <> nil then |
| 2106 |
begin |
| 2107 |
FArgs.FreeContentsByParentExpression; |
| 2108 |
FreeAndNil(FArgs); |
| 2109 |
end; |
| 2110 |
|
| 2111 |
if ParentOfLastExecuteResult then |
| 2112 |
LastExecuteResult.FreeByParentExpression; |
| 2113 |
LastExecuteResult := nil; |
| 2114 |
|
| 2115 |
inherited; |
| 2116 |
end; |
| 2117 |
|
| 2118 |
class function TCasScriptFunction.Name: string; |
| 2119 |
begin |
| 2120 |
Result := ShortName; |
| 2121 |
end; |
| 2122 |
|
| 2123 |
class function TCasScriptFunction.InfixOperatorName: string; |
| 2124 |
begin |
| 2125 |
Result := ''; |
| 2126 |
end; |
| 2127 |
|
| 2128 |
class function TCasScriptFunction.GreedyArgumentsCalculation: Integer; |
| 2129 |
begin |
| 2130 |
Result := -1; |
| 2131 |
end; |
| 2132 |
|
| 2133 |
class function TCasScriptFunction.ArgumentMustBeAssignable(const Index: Integer): boolean; |
| 2134 |
begin |
| 2135 |
Result := false; |
| 2136 |
end; |
| 2137 |
|
| 2138 |
function TCasScriptFunction.CoreExecute: TCasScriptValue; |
| 2139 |
|
| 2140 |
function ArgumentClassesToStr(const A: TCasScriptValueClassArray): string; |
| 2141 |
var |
| 2142 |
I: Integer; |
| 2143 |
begin |
| 2144 |
Result := ''; |
| 2145 |
for I := 0 to Length(A) - 1 do |
| 2146 |
begin |
| 2147 |
if I > 0 then Result := Result + ', '; |
| 2148 |
{ A[I] = nil may happen in case of GreedyArgumentsCalculation >= 0, |
| 2149 |
and it means then that any type of arg will be accepted. } |
| 2150 |
if A[I] = nil then |
| 2151 |
Result := Result + 'anything' |
| 2152 |
else |
| 2153 |
Result := Result + A[I].ClassName; |
| 2154 |
end; |
| 2155 |
Result := '(' + Result + ')'; |
| 2156 |
end; |
| 2157 |
|
| 2158 |
var |
| 2159 |
Handler: TCasScriptRegisteredHandler; |
| 2160 |
I, GreedyArguments: Integer; |
| 2161 |
begin |
| 2162 |
GreedyArguments := Args.Count; |
| 2163 |
if GreedyArgumentsCalculation <> -1 then |
| 2164 |
MinVar(GreedyArguments, GreedyArgumentsCalculation); |
| 2165 |
|
| 2166 |
{ We have to calculate arguments first, to know their type, |
| 2167 |
to decide which handler is suitable. |
| 2168 |
Actually, we calculate only first GreedyArguments, rest is left as nil. } |
| 2169 |
for I := 0 to GreedyArguments - 1 do |
| 2170 |
begin |
| 2171 |
ExecuteArguments[I] := Args[I].CoreExecute; |
| 2172 |
ExecuteArgumentClasses[I] := TCasScriptValueClass(ExecuteArguments[I].ClassType); |
| 2173 |
end; |
| 2174 |
for I := GreedyArguments to Args.Count - 1 do |
| 2175 |
begin |
| 2176 |
ExecuteArguments[I] := nil; |
| 2177 |
ExecuteArgumentClasses[I] := nil; |
| 2178 |
end; |
| 2179 |
|
| 2180 |
{ calculate Handler } |
| 2181 |
if not FunctionHandlers.SearchArgumentClasses( |
| 2182 |
HandlersByArgument, ExecuteArgumentClasses, Handler, Cache1) then |
| 2183 |
begin |
| 2184 |
{ try promoting integer arguments to float, see if it will work then } |
| 2185 |
for I := 0 to Length(ExecuteArgumentClasses) - 1 do |
| 2186 |
if (ExecuteArgumentClasses[I] <> nil) and |
| 2187 |
(ExecuteArgumentClasses[I].InheritsFrom(TCasScriptInteger)) then |
| 2188 |
ExecuteArgumentClasses[I] := TCasScriptFloat; |
| 2189 |
|
| 2190 |
if FunctionHandlers.SearchArgumentClasses( |
| 2191 |
HandlersByArgument, ExecuteArgumentClasses, Handler, Cache2) then |
| 2192 |
begin |
| 2193 |
{ So I found a handler, that will be valid if all integer args will |
| 2194 |
get promoted to float. Cool, let's do it. |
| 2195 |
|
| 2196 |
I use PromoteToFloat method, that will keep it's result valid |
| 2197 |
for some time, since (depending on function handler) we may |
| 2198 |
return PromoteToFloat result to the user. } |
| 2199 |
for I := 0 to Length(ExecuteArguments) - 1 do |
| 2200 |
if (ExecuteArguments[I] <> nil) and |
| 2201 |
(ExecuteArguments[I] is TCasScriptInteger) then |
| 2202 |
ExecuteArguments[I] := TCasScriptInteger(ExecuteArguments[I]).PromoteToFloat; |
| 2203 |
end else |
| 2204 |
raise ECasScriptFunctionNoHandler.CreateFmt('Function "%s" is not defined for this combination of arguments: %s', |
| 2205 |
[Name, ArgumentClassesToStr(ExecuteArgumentClasses)]); |
| 2206 |
end; |
| 2207 |
|
| 2208 |
Handler.Handler(Self, ExecuteArguments, LastExecuteResult, ParentOfLastExecuteResult); |
| 2209 |
|
| 2210 |
Result := LastExecuteResult; |
| 2211 |
end; |
| 2212 |
|
| 2213 |
{ TCasScriptRegisteredHandler ------------------------------------------------ } |
| 2214 |
|
| 2215 |
constructor TCasScriptRegisteredHandler.Create( |
| 2216 |
AHandler: TCasScriptFunctionHandler; |
| 2217 |
AFunctionClass: TCasScriptFunctionClass; |
| 2218 |
const AArgumentClasses: TCasScriptValueClassArray; |
| 2219 |
const AVariableArgumentsCount: boolean); |
| 2220 |
begin |
| 2221 |
FHandler := AHandler; |
| 2222 |
FFunctionClass := AFunctionClass; |
| 2223 |
FArgumentClasses := AArgumentClasses; |
| 2224 |
FVariableArgumentsCount := AVariableArgumentsCount; |
| 2225 |
end; |
| 2226 |
|
| 2227 |
{ TCasScriptSequence --------------------------------------------------------- } |
| 2228 |
|
| 2229 |
class function TCasScriptSequence.Name: string; |
| 2230 |
begin |
| 2231 |
Result := 'sequence (;)'; |
| 2232 |
end; |
| 2233 |
|
| 2234 |
class function TCasScriptSequence.ShortName: string; |
| 2235 |
begin |
| 2236 |
Result := ''; |
| 2237 |
end; |
| 2238 |
|
| 2239 |
class function TCasScriptSequence.InfixOperatorName: string; |
| 2240 |
begin |
| 2241 |
Result := ';'; |
| 2242 |
end; |
| 2243 |
|
| 2244 |
class procedure TCasScriptSequence.HandleSequence( |
| 2245 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2246 |
begin |
| 2247 |
if ParentOfResult then |
| 2248 |
AResult.FreeByParentExpression else |
| 2249 |
AResult := nil; |
| 2250 |
|
| 2251 |
AResult := Arguments[High(Arguments)]; |
| 2252 |
ParentOfResult := false; |
| 2253 |
end; |
| 2254 |
|
| 2255 |
{ TCasScriptAssignment --------------------------------------------------------- } |
| 2256 |
|
| 2257 |
class function TCasScriptAssignment.Name: string; |
| 2258 |
begin |
| 2259 |
Result := 'assignment (:=)'; |
| 2260 |
end; |
| 2261 |
|
| 2262 |
class function TCasScriptAssignment.ShortName: string; |
| 2263 |
begin |
| 2264 |
Result := ''; |
| 2265 |
end; |
| 2266 |
|
| 2267 |
class function TCasScriptAssignment.InfixOperatorName: string; |
| 2268 |
begin |
| 2269 |
Result := ':='; |
| 2270 |
end; |
| 2271 |
|
| 2272 |
class procedure TCasScriptAssignment.HandleAssignment( |
| 2273 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2274 |
begin |
| 2275 |
if ParentOfResult then |
| 2276 |
AResult.FreeByParentExpression else |
| 2277 |
AResult := nil; |
| 2278 |
|
| 2279 |
(Arguments[0] as TCasScriptValue).AssignValue(Arguments[1]); |
| 2280 |
|
| 2281 |
AResult := Arguments[0] as TCasScriptValue; |
| 2282 |
ParentOfResult := false; |
| 2283 |
end; |
| 2284 |
|
| 2285 |
class function TCasScriptAssignment.ArgumentMustBeAssignable(const Index: Integer): boolean; |
| 2286 |
begin |
| 2287 |
Result := Index = 0; |
| 2288 |
end; |
| 2289 |
|
| 2290 |
{ TCasScriptIf --------------------------------------------------------- } |
| 2291 |
|
| 2292 |
class function TCasScriptIf.ShortName: string; |
| 2293 |
begin |
| 2294 |
Result := 'if'; |
| 2295 |
end; |
| 2296 |
|
| 2297 |
class procedure TCasScriptIf.HandleIf( |
| 2298 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2299 |
begin |
| 2300 |
if ParentOfResult then |
| 2301 |
AResult.FreeByParentExpression; |
| 2302 |
AResult := nil; |
| 2303 |
ParentOfResult := false; |
| 2304 |
|
| 2305 |
if TCasScriptBoolean(Arguments[0]).Value then |
| 2306 |
AResult := AFunction.Args[1].CoreExecute else |
| 2307 |
AResult := AFunction.Args[2].CoreExecute; |
| 2308 |
end; |
| 2309 |
|
| 2310 |
class function TCasScriptIf.GreedyArgumentsCalculation: Integer; |
| 2311 |
begin |
| 2312 |
Result := 1; |
| 2313 |
end; |
| 2314 |
|
| 2315 |
{ TCasScriptWhen --------------------------------------------------------- } |
| 2316 |
|
| 2317 |
class function TCasScriptWhen.ShortName: string; |
| 2318 |
begin |
| 2319 |
Result := 'when'; |
| 2320 |
end; |
| 2321 |
|
| 2322 |
class procedure TCasScriptWhen.HandleWhen( |
| 2323 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2324 |
begin |
| 2325 |
if ParentOfResult then |
| 2326 |
AResult.FreeByParentExpression; |
| 2327 |
AResult := nil; |
| 2328 |
ParentOfResult := false; |
| 2329 |
|
| 2330 |
if TCasScriptBoolean(Arguments[0]).Value then |
| 2331 |
AResult := AFunction.Args[1].CoreExecute else |
| 2332 |
begin |
| 2333 |
{ "when" returns simple const false on "else" condition } |
| 2334 |
AResult := TCasScriptBoolean.Create(false); |
| 2335 |
ParentOfResult := true; |
| 2336 |
end; |
| 2337 |
end; |
| 2338 |
|
| 2339 |
class function TCasScriptWhen.GreedyArgumentsCalculation: Integer; |
| 2340 |
begin |
| 2341 |
Result := 1; |
| 2342 |
end; |
| 2343 |
|
| 2344 |
{ TCasScriptWhile --------------------------------------------------------- } |
| 2345 |
|
| 2346 |
class function TCasScriptWhile.ShortName: string; |
| 2347 |
begin |
| 2348 |
Result := 'while'; |
| 2349 |
end; |
| 2350 |
|
| 2351 |
class procedure TCasScriptWhile.HandleWhile( |
| 2352 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2353 |
|
| 2354 |
function ExecuteCondition: boolean; |
| 2355 |
var |
| 2356 |
Condition: TCasScriptValue; |
| 2357 |
begin |
| 2358 |
Condition := AFunction.Args[0].CoreExecute; |
| 2359 |
if Condition is TCasScriptBoolean then |
| 2360 |
Result := TCasScriptBoolean(Condition).Value else |
| 2361 |
raise ECasScriptError.Create('"if" function "condition" must return a boolean value'); |
| 2362 |
end; |
| 2363 |
|
| 2364 |
begin |
| 2365 |
if ParentOfResult then |
| 2366 |
AResult.FreeByParentExpression; |
| 2367 |
AResult := nil; |
| 2368 |
ParentOfResult := false; |
| 2369 |
|
| 2370 |
while ExecuteCondition do |
| 2371 |
AResult := AFunction.Args[1].CoreExecute; |
| 2372 |
|
| 2373 |
if AResult = nil then |
| 2374 |
begin |
| 2375 |
{ not executed even once? return const false } |
| 2376 |
AResult := TCasScriptBoolean.Create(false); |
| 2377 |
ParentOfResult := true; |
| 2378 |
end; |
| 2379 |
end; |
| 2380 |
|
| 2381 |
class function TCasScriptWhile.GreedyArgumentsCalculation: Integer; |
| 2382 |
begin |
| 2383 |
Result := 0; |
| 2384 |
end; |
| 2385 |
|
| 2386 |
{ TCasScriptFor --------------------------------------------------------- } |
| 2387 |
|
| 2388 |
class function TCasScriptFor.ShortName: string; |
| 2389 |
begin |
| 2390 |
Result := 'for'; |
| 2391 |
end; |
| 2392 |
|
| 2393 |
class procedure TCasScriptFor.HandleFor( |
| 2394 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2395 |
var |
| 2396 |
BeginVal, EndVal: Int64; |
| 2397 |
I: Integer; |
| 2398 |
begin |
| 2399 |
if ParentOfResult then |
| 2400 |
AResult.FreeByParentExpression; |
| 2401 |
AResult := nil; |
| 2402 |
ParentOfResult := false; |
| 2403 |
|
| 2404 |
BeginVal := TCasScriptInteger(Arguments[1]).Value; |
| 2405 |
EndVal := TCasScriptInteger(Arguments[2]).Value; |
| 2406 |
|
| 2407 |
for I := BeginVal to EndVal do |
| 2408 |
begin |
| 2409 |
{ We use Arguments[0] here, not AFunction.Args[0], this way |
| 2410 |
we know we really have TCasScriptInteger here. While CheckArguments |
| 2411 |
makes sure Args[0] is TCasScriptValue, it may be TCasScriptParameterValue. |
| 2412 |
We know that TCasScriptParameterValue.Execute returns actual value, |
| 2413 |
so Arguments[0] is Ok here. } |
| 2414 |
(Arguments[0] as TCasScriptInteger).Value := I; |
| 2415 |
|
| 2416 |
AResult := AFunction.Args[3].CoreExecute; |
| 2417 |
end; |
| 2418 |
|
| 2419 |
if AResult = nil then |
| 2420 |
begin |
| 2421 |
{ not executed even once? return const false } |
| 2422 |
AResult := TCasScriptBoolean.Create(false); |
| 2423 |
ParentOfResult := true; |
| 2424 |
end; |
| 2425 |
end; |
| 2426 |
|
| 2427 |
class function TCasScriptFor.GreedyArgumentsCalculation: Integer; |
| 2428 |
begin |
| 2429 |
Result := 3; |
| 2430 |
end; |
| 2431 |
|
| 2432 |
class function TCasScriptFor.ArgumentMustBeAssignable(const Index: Integer): boolean; |
| 2433 |
begin |
| 2434 |
{ This will cause checking whether Args[0] is assignable TCasScriptValue. |
| 2435 |
|
| 2436 |
Note that I cannot check in CheckArguments whether is |
| 2437 |
Args[0] is TCasScriptInteger, |
| 2438 |
as it may be TCasScriptParameterValue, and so the actual runtime type |
| 2439 |
(TCasScriptParameterValue.SourceValue) may be not set yet. |
| 2440 |
That's Ok, in HandleFor this will be automatically checked by AssignValue. } |
| 2441 |
|
| 2442 |
Result := Index = 0; |
| 2443 |
end; |
| 2444 |
|
| 2445 |
{ TCasScriptCoalesce --------------------------------------------------------- } |
| 2446 |
|
| 2447 |
class function TCasScriptCoalesce.ShortName: string; |
| 2448 |
begin |
| 2449 |
Result := 'coalesce'; |
| 2450 |
end; |
| 2451 |
|
| 2452 |
class procedure TCasScriptCoalesce.HandleCoalesce( |
| 2453 |
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 2454 |
var |
| 2455 |
I: Integer; |
| 2456 |
begin |
| 2457 |
if ParentOfResult then |
| 2458 |
AResult.FreeByParentExpression; |
| 2459 |
AResult := nil; |
| 2460 |
ParentOfResult := false; |
| 2461 |
|
| 2462 |
for I := 0 to High(Arguments) - 1 do |
| 2463 |
begin |
| 2464 |
AResult := AFunction.Args[I].CoreExecute; |
| 2465 |
if (AResult as TCasScriptString).Value <> '' then |
| 2466 |
Exit; |
| 2467 |
{ Do not free it, it would cause SEGFAULT: |
| 2468 |
AResult.FreeByParentExpression; |
| 2469 |
AResult := nil; |
| 2470 |
} |
| 2471 |
end; |
| 2472 |
|
| 2473 |
AResult := AFunction.Args[High(Arguments)].CoreExecute; |
| 2474 |
end; |
| 2475 |
|
| 2476 |
class function TCasScriptCoalesce.GreedyArgumentsCalculation: Integer; |
| 2477 |
begin |
| 2478 |
Result := 0; |
| 2479 |
end; |
| 2480 |
|
| 2481 |
{ TCasScriptFunctionHandlers ------------------------------------------------- } |
| 2482 |
|
| 2483 |
constructor TCasScriptFunctionHandlers.Create; |
| 2484 |
begin |
| 2485 |
inherited; |
| 2486 |
FHandlersByFunction := Contnrs.TObjectList.Create(true); |
| 2487 |
end; |
| 2488 |
|
| 2489 |
destructor TCasScriptFunctionHandlers.Destroy; |
| 2490 |
begin |
| 2491 |
FreeAndNil(FHandlersByFunction); |
| 2492 |
inherited; |
| 2493 |
end; |
| 2494 |
|
| 2495 |
function TCasScriptFunctionHandlers.SearchFunctionClass( |
| 2496 |
FunctionClass: TCasScriptFunctionClass; |
| 2497 |
out FunctionIndex: Integer; |
| 2498 |
out HandlersByArgument: Contnrs.TObjectList): boolean; |
| 2499 |
var |
| 2500 |
I: Integer; |
| 2501 |
begin |
| 2502 |
for I := 0 to FHandlersByFunction.Count - 1 do |
| 2503 |
begin |
| 2504 |
HandlersByArgument := FHandlersByFunction[I] as Contnrs.TObjectList; |
| 2505 |
if FunctionClass = (HandlersByArgument[0] as |
| 2506 |
TCasScriptRegisteredHandler).FunctionClass then |
| 2507 |
begin |
| 2508 |
FunctionIndex := I; |
| 2509 |
Result := true; |
| 2510 |
Exit; |
| 2511 |
end; |
| 2512 |
end; |
| 2513 |
Result := false; |
| 2514 |
end; |
| 2515 |
|
| 2516 |
function TCasScriptFunctionHandlers.SearchFunctionClass( |
| 2517 |
FunctionClass: TCasScriptFunctionClass; |
| 2518 |
out HandlersByArgument: Contnrs.TObjectList): boolean; |
| 2519 |
var |
| 2520 |
FunctionIndex: Integer; |
| 2521 |
begin |
| 2522 |
Result := SearchFunctionClass( |
| 2523 |
FunctionClass, FunctionIndex, HandlersByArgument); |
| 2524 |
end; |
| 2525 |
|
| 2526 |
function TCasScriptFunctionHandlers.SearchArgumentClasses( |
| 2527 |
HandlersByArgument: Contnrs.TObjectList; |
| 2528 |
const ArgumentClasses: TCasScriptValueClassArray; |
| 2529 |
out ArgumentIndex: Integer; |
| 2530 |
out Handler: TCasScriptRegisteredHandler): boolean; |
| 2531 |
var |
| 2532 |
I, J: Integer; |
| 2533 |
begin |
| 2534 |
for I := 0 to HandlersByArgument.Count - 1 do |
| 2535 |
begin |
| 2536 |
Handler := HandlersByArgument[I] as TCasScriptRegisteredHandler; |
| 2537 |
|
| 2538 |
{ First, check do we have enough arguments: at least |
| 2539 |
Length(Handler.ArgumentClasses) are always required. } |
| 2540 |
Result := Length(ArgumentClasses) >= Length(Handler.ArgumentClasses); |
| 2541 |
|
| 2542 |
if Result then |
| 2543 |
begin |
| 2544 |
for J := 0 to Length(ArgumentClasses) - 1 do |
| 2545 |
begin |
| 2546 |
Assert(Result); |
| 2547 |
|
| 2548 |
{ Always accept ArgumentClasses[J] = nil, this means that they |
| 2549 |
are "lazy" arguments (not calculated before handler actually executed), |
| 2550 |
so they are simply always assumed Ok. } |
| 2551 |
|
| 2552 |
if ArgumentClasses[J] <> nil then |
| 2553 |
begin |
| 2554 |
if J < Length(Handler.ArgumentClasses) then |
| 2555 |
Result := ArgumentClasses[J].InheritsFrom(Handler.ArgumentClasses[J]) else |
| 2556 |
{ This is more than required number of arguments. |
| 2557 |
Still it's Ok if it matches last argument and function allows variable |
| 2558 |
number of arguments. } |
| 2559 |
Result := Handler.VariableArgumentsCount and |
| 2560 |
(Length(Handler.ArgumentClasses) > 0) and |
| 2561 |
ArgumentClasses[J].InheritsFrom( |
| 2562 |
Handler.ArgumentClasses[High(Handler.ArgumentClasses)]); |
| 2563 |
end; |
| 2564 |
|
| 2565 |
if not Result then Break; |
| 2566 |
end; |
| 2567 |
|
| 2568 |
if Result then |
| 2569 |
begin |
| 2570 |
ArgumentIndex := I; |
| 2571 |
Exit; |
| 2572 |
end; |
| 2573 |
end; |
| 2574 |
end; |
| 2575 |
Result := false; |
| 2576 |
end; |
| 2577 |
|
| 2578 |
function TCasScriptFunctionHandlers.SearchArgumentClasses( |
| 2579 |
HandlersByArgument: Contnrs.TObjectList; |
| 2580 |
const ArgumentClasses: TCasScriptValueClassArray; |
| 2581 |
out Handler: TCasScriptRegisteredHandler): boolean; |
| 2582 |
var |
| 2583 |
ArgumentIndex: Integer; |
| 2584 |
begin |
| 2585 |
Result := SearchArgumentClasses( |
| 2586 |
HandlersByArgument, ArgumentClasses, ArgumentIndex, Handler); |
| 2587 |
end; |
| 2588 |
|
| 2589 |
function TCasScriptFunctionHandlers.SearchArgumentClasses( |
| 2590 |
HandlersByArgument: Contnrs.TObjectList; |
| 2591 |
const ArgumentClasses: TCasScriptValueClassArray; |
| 2592 |
out Handler: TCasScriptRegisteredHandler; |
| 2593 |
var Cache: TCasScriptSearchArgumentClassesCache): boolean; |
| 2594 |
|
| 2595 |
function ArgumentClassesEqual(const A1, A2: TCasScriptValueClassArray): boolean; |
| 2596 |
begin |
| 2597 |
Result := (Length(A1) = Length(A2)) and |
| 2598 |
CompareMem(Pointer(A1), Pointer(A2), SizeOf(TCasScriptValueClass) * Length(A1)); |
| 2599 |
end; |
| 2600 |
|
| 2601 |
begin |
| 2602 |
if Cache.IsCache and |
| 2603 |
(Cache.QueryHandlersByArgument = HandlersByArgument) and |
| 2604 |
ArgumentClassesEqual(Cache.QueryArgumentClasses, ArgumentClasses) then |
| 2605 |
begin |
| 2606 |
{ Use the cached result } |
| 2607 |
Handler := Cache.AnswerHandler; |
| 2608 |
{ ArgumentIndex := Cache.ArgumentIndex; not returned here } |
| 2609 |
Result := Cache.Answer; |
| 2610 |
end else |
| 2611 |
begin |
| 2612 |
{ Result not in the cache. So calculate it, and record in the cache. } |
| 2613 |
Cache.IsCache := true; |
| 2614 |
Cache.QueryHandlersByArgument := HandlersByArgument; |
| 2615 |
{ Copying the reference here, by |
| 2616 |
Cache.QueryArgumentClasses := ArgumentClasses; |
| 2617 |
would be incorrect: our argument may be a long-lived instance in |
| 2618 |
TCasScriptFunction.ExecuteArgumentClasses, that is changed in |
| 2619 |
TCasScriptFunction.CoreExecute. With a reference copy here, |
| 2620 |
the CoreExecute would accidentally change also our cache state, |
| 2621 |
which will cause trouble later. |
| 2622 |
|
| 2623 |
Testcase: demo_models/castle_script/edit_texture.x3dv, |
| 2624 |
key "e", would cause errors, because suddenly CoreExecute may decide |
| 2625 |
that TCasScriptFloat.HandleAdd may be called without promoting int to float. } |
| 2626 |
Cache.QueryArgumentClasses := Copy(ArgumentClasses, 0, Length(ArgumentClasses)); |
| 2627 |
Cache.Answer := SearchArgumentClasses( |
| 2628 |
HandlersByArgument, ArgumentClasses, |
| 2629 |
Cache.AnswerArgumentIndex, Cache.AnswerHandler); |
| 2630 |
|
| 2631 |
{ Use the cached result } |
| 2632 |
Handler := Cache.AnswerHandler; |
| 2633 |
{ ArgumentIndex := Cache.ArgumentIndex; not returned here } |
| 2634 |
Result := Cache.Answer; |
| 2635 |
end; |
| 2636 |
end; |
| 2637 |
|
| 2638 |
procedure TCasScriptFunctionHandlers.RegisterHandler( |
| 2639 |
AHandler: TCasScriptFunctionHandler; |
| 2640 |
AFunctionClass: TCasScriptFunctionClass; |
| 2641 |
const AArgumentClasses: array of TCasScriptValueClass; |
| 2642 |
const AVariableArgumentsCount: boolean); |
| 2643 |
var |
| 2644 |
HandlersByArgument: Contnrs.TObjectList; |
| 2645 |
Handler: TCasScriptRegisteredHandler; |
| 2646 |
ArgumentClassesDyn: TCasScriptValueClassArray; |
| 2647 |
begin |
| 2648 |
SetLength(ArgumentClassesDyn, High(AArgumentClasses) + 1); |
| 2649 |
if Length(ArgumentClassesDyn) > 0 then |
| 2650 |
Move(AArgumentClasses[0], ArgumentClassesDyn[0], |
| 2651 |
SizeOf(TCasScriptValueClass) * Length(ArgumentClassesDyn)); |
| 2652 |
|
| 2653 |
if SearchFunctionClass(AFunctionClass, HandlersByArgument) then |
| 2654 |
begin |
| 2655 |
if not SearchArgumentClasses(HandlersByArgument, ArgumentClassesDyn, Handler) then |
| 2656 |
begin |
| 2657 |
Handler := TCasScriptRegisteredHandler.Create( |
| 2658 |
AHandler, AFunctionClass, ArgumentClassesDyn, AVariableArgumentsCount); |
| 2659 |
HandlersByArgument.Add(Handler); |
| 2660 |
end; |
| 2661 |
end else |
| 2662 |
begin |
| 2663 |
HandlersByArgument := Contnrs.TObjectList.Create(true); |
| 2664 |
FHandlersByFunction.Add(HandlersByArgument); |
| 2665 |
|
| 2666 |
Handler := TCasScriptRegisteredHandler.Create( |
| 2667 |
AHandler, AFunctionClass, ArgumentClassesDyn, AVariableArgumentsCount); |
| 2668 |
HandlersByArgument.Add(Handler); |
| 2669 |
end; |
| 2670 |
end; |
| 2671 |
|
| 2672 |
function TCasScriptFunctionHandlers.SearchFunctionShortName( |
| 2673 |
const AShortName: string): TCasScriptFunctionClass; |
| 2674 |
var |
| 2675 |
I: Integer; |
| 2676 |
HandlersByArgument: Contnrs.TObjectList; |
| 2677 |
begin |
| 2678 |
for I := 0 to FHandlersByFunction.Count - 1 do |
| 2679 |
begin |
| 2680 |
HandlersByArgument := FHandlersByFunction[I] as Contnrs.TObjectList; |
| 2681 |
Result := (HandlersByArgument[0] as |
| 2682 |
TCasScriptRegisteredHandler).FunctionClass; |
| 2683 |
if SameText(AShortName, Result.ShortName) then |
| 2684 |
Exit; |
| 2685 |
end; |
| 2686 |
Result := nil; |
| 2687 |
end; |
| 2688 |
|
| 2689 |
{ TCasScriptUserFunction ----------------------------------------------- } |
| 2690 |
|
| 2691 |
constructor TCasScriptUserFunction.Create; |
| 2692 |
begin |
| 2693 |
inherited; |
| 2694 |
FParameters := TCasScriptValueList.Create(true); |
| 2695 |
end; |
| 2696 |
|
| 2697 |
destructor TCasScriptUserFunction.Destroy; |
| 2698 |
begin |
| 2699 |
if Body <> nil then |
| 2700 |
Body.FreeByParentExpression; |
| 2701 |
FreeAndNil(FParameters); |
| 2702 |
inherited; |
| 2703 |
end; |
| 2704 |
|
| 2705 |
{ TCasScriptUserFunctionList ------------------------------------------ } |
| 2706 |
|
| 2707 |
function TCasScriptUserFunctionList.IndexOfName(const FunctionName: string): Integer; |
| 2708 |
begin |
| 2709 |
for Result := 0 to Count - 1 do |
| 2710 |
if SameText(FunctionName, Items[Result].Name) then |
| 2711 |
Exit; |
| 2712 |
Result := -1; |
| 2713 |
end; |
| 2714 |
|
| 2715 |
{ TCasScriptProgram ---------------------------------------------------------- } |
| 2716 |
|
| 2717 |
constructor TCasScriptProgram.Create; |
| 2718 |
begin |
| 2719 |
inherited; |
| 2720 |
FFunctions := TCasScriptUserFunctionList.Create(true); |
| 2721 |
FEnvironment := TCasScriptEnvironment.Create; |
| 2722 |
end; |
| 2723 |
|
| 2724 |
destructor TCasScriptProgram.Destroy; |
| 2725 |
begin |
| 2726 |
FreeAndNil(FFunctions); |
| 2727 |
FreeAndNil(FEnvironment); |
| 2728 |
inherited; |
| 2729 |
end; |
| 2730 |
|
| 2731 |
procedure TCasScriptProgram.ExecuteFunction(const FunctionName: string; |
| 2732 |
const Parameters: array of TCasScriptValue; |
| 2733 |
const IgnoreMissingFunction: boolean); |
| 2734 |
var |
| 2735 |
Func: TCasScriptUserFunction; |
| 2736 |
FuncIndex, I: Integer; |
| 2737 |
begin |
| 2738 |
FuncIndex := Functions.IndexOfName(FunctionName); |
| 2739 |
if FuncIndex = -1 then |
| 2740 |
begin |
| 2741 |
if IgnoreMissingFunction then |
| 2742 |
Exit else |
| 2743 |
raise ECasScriptMissingFunction.CreateFmt('CastleScript function "%s" is not defined', [FunctionName]); |
| 2744 |
end; |
| 2745 |
Func := Functions[FuncIndex]; |
| 2746 |
|
| 2747 |
if High(Parameters) <> Func.Parameters.Count - 1 then |
| 2748 |
raise ECasScriptError.CreateFmt('CastleScript function "%s" requires %d parameters, but passed %d parameters', |
| 2749 |
[FunctionName, Func.Parameters.Count, High(Parameters) + 1]); |
| 2750 |
|
| 2751 |
for I := 0 to High(Parameters) do |
| 2752 |
(Func.Parameters[I] as TCasScriptParameterValue).SourceValue := Parameters[I]; |
| 2753 |
|
| 2754 |
Func.Body.Execute; |
| 2755 |
|
| 2756 |
{ Just for safety, clear SourceValue references. |
| 2757 |
|
| 2758 |
This is safe, since no code can access Parameters instances |
| 2759 |
(trying to get their value directly or by AssignValue) after |
| 2760 |
ExecuteFunction. Code from the outside can only access it's own |
| 2761 |
global variables after execution, which have values directly stored. |
| 2762 |
|
| 2763 |
This *could* become a problem if we want to return function's value |
| 2764 |
in the future, then this will possibly have to be removed, |
| 2765 |
as Func.Body.Execute may directly return one of our Parameters. } |
| 2766 |
|
| 2767 |
for I := 0 to High(Parameters) do |
| 2768 |
(Func.Parameters[I] as TCasScriptParameterValue).SourceValue := nil; |
| 2769 |
end; |
| 2770 |
|
| 2771 |
{ procedural utils ----------------------------------------------------------- } |
| 2772 |
|
| 2773 |
procedure CreateValueIfNeeded(var Value: TCasScriptValue; |
| 2774 |
var ParentOfValue: boolean; |
| 2775 |
NeededClass: TCasScriptValueClass); |
| 2776 |
begin |
| 2777 |
if Value = nil then |
| 2778 |
begin |
| 2779 |
Value := NeededClass.Create(false); |
| 2780 |
ParentOfValue := true; |
| 2781 |
end else |
| 2782 |
if Value.ClassType <> NeededClass then |
| 2783 |
begin |
| 2784 |
if ParentOfValue then |
| 2785 |
Value.FreeByParentExpression else |
| 2786 |
Value := nil; |
| 2787 |
|
| 2788 |
Value := NeededClass.Create(false); |
| 2789 |
ParentOfValue := true; |
| 2790 |
end; |
| 2791 |
end; |
| 2792 |
|
| 2793 |
{ unit init/fini ------------------------------------------------------------- } |
| 2794 |
|
| 2795 |
var |
| 2796 |
FFunctionHandlers: TCasScriptFunctionHandlers; |
| 2797 |
|
| 2798 |
function FunctionHandlers: TCasScriptFunctionHandlers; |
| 2799 |
begin |
| 2800 |
{ Create on-demand, just in case CastleScriptVectors initialization |
| 2801 |
is executed earlier than CastleScript initialization. } |
| 2802 |
if FFunctionHandlers = nil then |
| 2803 |
FFunctionHandlers := TCasScriptFunctionHandlers.Create; |
| 2804 |
Result := FFunctionHandlers; |
| 2805 |
end; |
| 2806 |
|
| 2807 |
initialization |
| 2808 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptSequence{$ifdef FPC}(nil){$endif}.HandleSequence, TCasScriptSequence, [TCasScriptValue], true); |
| 2809 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptAssignment{$ifdef FPC}(nil){$endif}.HandleAssignment, TCasScriptAssignment, [TCasScriptValue, TCasScriptValue], false); |
| 2810 |
|
| 2811 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptIf{$ifdef FPC}(nil){$endif}.HandleIf, TCasScriptIf, [TCasScriptBoolean, TCasScriptValue, TCasScriptValue], false); |
| 2812 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptWhen{$ifdef FPC}(nil){$endif}.HandleWhen, TCasScriptWhen, [TCasScriptBoolean, TCasScriptValue], false); |
| 2813 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptWhile{$ifdef FPC}(nil){$endif}.HandleWhile, TCasScriptWhile, [TCasScriptBoolean, TCasScriptValue], false); |
| 2814 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFor{$ifdef FPC}(nil){$endif}.HandleFor, TCasScriptFor, [TCasScriptInteger, TCasScriptInteger, TCasScriptInteger, TCasScriptValue], false); |
| 2815 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptCoalesce{$ifdef FPC}(nil){$endif}.HandleCoalesce, TCasScriptCoalesce, [TCasScriptString], true); |
| 2816 |
|
| 2817 |
{ Register handlers for TCasScriptInteger for functions in |
| 2818 |
CastleScriptCoreFunctions. } |
| 2819 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleAdd, TCasScriptAdd, [TCasScriptInteger], true); |
| 2820 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleSubtract, TCasScriptSubtract, [TCasScriptInteger], true); |
| 2821 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleNegate, TCasScriptNegate, [TCasScriptInteger], false); |
| 2822 |
|
| 2823 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleMultiply, TCasScriptMultiply, [TCasScriptInteger], true); |
| 2824 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleIntDivide, TCasScriptIntDivide, [TCasScriptInteger], true); |
| 2825 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleModulo, TCasScriptModulo, [TCasScriptInteger, TCasScriptInteger], false); |
| 2826 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandlePower, TCasScriptPower, [TCasScriptInteger, TCasScriptInteger], false); |
| 2827 |
|
| 2828 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleMax, TCasScriptMax, [TCasScriptInteger], true); |
| 2829 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleMin, TCasScriptMin, [TCasScriptInteger], true); |
| 2830 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleSqr, TCasScriptSqr, [TCasScriptInteger], false); |
| 2831 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleSgn, TCasScriptSgn, [TCasScriptInteger], false); |
| 2832 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleAbs, TCasScriptAbs, [TCasScriptInteger], false); |
| 2833 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleRandom, TCasScriptRandom, [TCasScriptInteger], false); |
| 2834 |
|
| 2835 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleGreater, TCasScriptGreater, [TCasScriptInteger, TCasScriptInteger], false); |
| 2836 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleLesser, TCasScriptLesser, [TCasScriptInteger, TCasScriptInteger], false); |
| 2837 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptInteger, TCasScriptInteger], false); |
| 2838 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleLesserEq, TCasScriptLesserEq, [TCasScriptInteger, TCasScriptInteger], false); |
| 2839 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleEqual, TCasScriptEqual, [TCasScriptInteger, TCasScriptInteger], false); |
| 2840 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.HandleNotEqual, TCasScriptNotEqual, [TCasScriptInteger, TCasScriptInteger], false); |
| 2841 |
|
| 2842 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.ConvertFromInt , TCasScriptInt, [TCasScriptInteger], false); |
| 2843 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.ConvertFromFloat , TCasScriptInt, [TCasScriptFloat], false); |
| 2844 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.ConvertFromBool , TCasScriptInt, [TCasScriptBoolean], false); |
| 2845 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptInteger{$ifdef FPC}(nil){$endif}.ConvertFromString, TCasScriptInt, [TCasScriptString], false); |
| 2846 |
|
| 2847 |
{ Register handlers for TCasScriptFloat for functions in |
| 2848 |
CastleScriptCoreFunctions. } |
| 2849 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleAdd, TCasScriptAdd, [TCasScriptFloat], true); |
| 2850 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleSubtract, TCasScriptSubtract, [TCasScriptFloat], true); |
| 2851 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleMultiply, TCasScriptMultiply, [TCasScriptFloat], true); |
| 2852 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleFloatDivide, TCasScriptFloatDivide, [TCasScriptFloat], true); |
| 2853 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleLerp, TCasScriptLerp, [TCasScriptFloat, TCasScriptFloat, TCasScriptFloat], false); |
| 2854 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleNegate, TCasScriptNegate, [TCasScriptFloat], false); |
| 2855 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleModulo, TCasScriptModulo, [TCasScriptFloat, TCasScriptFloat], false); |
| 2856 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleSin, TCasScriptSin, [TCasScriptFloat], false); |
| 2857 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleCos, TCasScriptCos, [TCasScriptFloat], false); |
| 2858 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleTan, TCasScriptTan, [TCasScriptFloat], false); |
| 2859 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleCotan, TCasScriptCotan, [TCasScriptFloat], false); |
| 2860 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleArcSin, TCasScriptArcSin, [TCasScriptFloat], false); |
| 2861 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleArcCos, TCasScriptArcCos, [TCasScriptFloat], false); |
| 2862 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleArcTan, TCasScriptArcTan, [TCasScriptFloat], false); |
| 2863 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleArcCotan, TCasScriptArcCotan, [TCasScriptFloat], false); |
| 2864 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleSinh, TCasScriptSinh, [TCasScriptFloat], false); |
| 2865 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleCosh, TCasScriptCosh, [TCasScriptFloat], false); |
| 2866 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleTanh, TCasScriptTanh, [TCasScriptFloat], false); |
| 2867 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleCotanh, TCasScriptCotanh, [TCasScriptFloat], false); |
| 2868 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleLog2, TCasScriptLog2, [TCasScriptFloat], false); |
| 2869 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleLn, TCasScriptLn, [TCasScriptFloat], false); |
| 2870 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleLog, TCasScriptLog, [TCasScriptFloat, TCasScriptFloat], false); |
| 2871 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandlePower2, TCasScriptPower2, [TCasScriptFloat], false); |
| 2872 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleExp, TCasScriptExp, [TCasScriptFloat], false); |
| 2873 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandlePower, TCasScriptPower, [TCasScriptFloat, TCasScriptFloat], false); |
| 2874 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleSqr, TCasScriptSqr, [TCasScriptFloat], false); |
| 2875 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleSqrt, TCasScriptSqrt, [TCasScriptFloat], false); |
| 2876 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleMax, TCasScriptMax, [TCasScriptFloat], true); |
| 2877 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleMin, TCasScriptMin, [TCasScriptFloat], true); |
| 2878 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleSgn, TCasScriptSgn, [TCasScriptFloat], false); |
| 2879 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleAbs, TCasScriptAbs, [TCasScriptFloat], false); |
| 2880 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleRandom, TCasScriptRandom, [], false); |
| 2881 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleCeil, TCasScriptCeil, [TCasScriptFloat], false); |
| 2882 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleFloor, TCasScriptFloor, [TCasScriptFloat], false); |
| 2883 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleRound, TCasScriptRound, [TCasScriptFloat], false); |
| 2884 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleGreater, TCasScriptGreater, [TCasScriptFloat, TCasScriptFloat], false); |
| 2885 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleLesser, TCasScriptLesser, [TCasScriptFloat, TCasScriptFloat], false); |
| 2886 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptFloat, TCasScriptFloat], false); |
| 2887 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleLesserEq, TCasScriptLesserEq, [TCasScriptFloat, TCasScriptFloat], false); |
| 2888 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleEqual, TCasScriptEqual, [TCasScriptFloat, TCasScriptFloat], false); |
| 2889 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.HandleNotEqual, TCasScriptNotEqual, [TCasScriptFloat, TCasScriptFloat], false); |
| 2890 |
|
| 2891 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.ConvertFromInt , TCasScriptFloatFun, [TCasScriptInteger], false); |
| 2892 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.ConvertFromFloat , TCasScriptFloatFun, [TCasScriptFloat], false); |
| 2893 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.ConvertFromBool , TCasScriptFloatFun, [TCasScriptBoolean], false); |
| 2894 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptFloat{$ifdef FPC}(nil){$endif}.ConvertFromString, TCasScriptFloatFun, [TCasScriptString], false); |
| 2895 |
|
| 2896 |
{ Register handlers for TCasScriptBoolean for functions in |
| 2897 |
CastleScriptCoreFunctions. } |
| 2898 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleOr, TCasScriptOr, [TCasScriptBoolean], true); |
| 2899 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleAnd, TCasScriptAnd, [TCasScriptBoolean], true); |
| 2900 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleNot, TCasScriptNot, [TCasScriptBoolean], false); |
| 2901 |
|
| 2902 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleGreater, TCasScriptGreater, [TCasScriptBoolean, TCasScriptBoolean], false); |
| 2903 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleLesser, TCasScriptLesser, [TCasScriptBoolean, TCasScriptBoolean], false); |
| 2904 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptBoolean, TCasScriptBoolean], false); |
| 2905 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleLesserEq, TCasScriptLesserEq, [TCasScriptBoolean, TCasScriptBoolean], false); |
| 2906 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleEqual, TCasScriptEqual, [TCasScriptBoolean, TCasScriptBoolean], false); |
| 2907 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.HandleNotEqual, TCasScriptNotEqual, [TCasScriptBoolean, TCasScriptBoolean], false); |
| 2908 |
|
| 2909 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.ConvertFromInt , TCasScriptBool, [TCasScriptInteger], false); |
| 2910 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.ConvertFromFloat , TCasScriptBool, [TCasScriptFloat], false); |
| 2911 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.ConvertFromBool , TCasScriptBool, [TCasScriptBoolean], false); |
| 2912 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptBoolean{$ifdef FPC}(nil){$endif}.ConvertFromString, TCasScriptBool, [TCasScriptString], false); |
| 2913 |
|
| 2914 |
{ Register handlers for TCasScriptString for functions in |
| 2915 |
CastleScriptCoreFunctions. } |
| 2916 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleAdd, TCasScriptAdd, [TCasScriptString, TCasScriptString], false); |
| 2917 |
|
| 2918 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleGreater, TCasScriptGreater, [TCasScriptString, TCasScriptString], false); |
| 2919 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleLesser, TCasScriptLesser, [TCasScriptString, TCasScriptString], false); |
| 2920 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptString, TCasScriptString], false); |
| 2921 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleLesserEq, TCasScriptLesserEq, [TCasScriptString, TCasScriptString], false); |
| 2922 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleEqual, TCasScriptEqual, [TCasScriptString, TCasScriptString], false); |
| 2923 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleNotEqual, TCasScriptNotEqual, [TCasScriptString, TCasScriptString], false); |
| 2924 |
|
| 2925 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.ConvertFromInt , TCasScriptStringFun, [TCasScriptInteger], false); |
| 2926 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.ConvertFromFloat , TCasScriptStringFun, [TCasScriptFloat], false); |
| 2927 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.ConvertFromBool , TCasScriptStringFun, [TCasScriptBoolean], false); |
| 2928 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.ConvertFromString, TCasScriptStringFun, [TCasScriptString], false); |
| 2929 |
|
| 2930 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleWriteln, TCasScriptWriteln, [TCasScriptString], false); |
| 2931 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptString{$ifdef FPC}(nil){$endif}.HandleCharacterFromCode, TCasScriptCharacterFromCode, [TCasScriptInteger], false); |
| 2932 |
|
| 2933 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptShortcut{$ifdef FPC}(nil){$endif}.Handle, TCasScriptShortcut, [TCasScriptString], false); |
| 2934 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptDeg{$ifdef FPC}(nil){$endif}.Handle, TCasScriptDeg, [TCasScriptFloat], false); |
| 2935 |
finalization |
| 2936 |
FreeAndNil(FFunctionHandlers); |
| 2937 |
end. |