TatPascalScripter组件执行脚本的Pascal语法。Pascal语法支持:
•begin .. end
•procedure and function
•if .. then .. else
•for .. to .. do .. step
•while .. do
•repeat .. until
•try .. except and try .. finally
•case
•array constructors (x:=[ 1, 2, 3 ];)
•^ , * , / , and , + , - , or , <> , >=, <= , = , > , < , div , mod , xor , shl , shr
•访问对象属性和方法ObjectName.SubObject.Property)
有效的标识符:
VarName
_Some
V1A2
赋值声明
赋值语句和Pascal一样,赋值语句是使用“:=”。例子:
MyVar:=2;
Button.Caption:='This ' + 'is ok.';
字符串
字符串是同pascal一样使用单引号(')字符。双引号(")不能使用。你也能使用(#)字符宣布一个字符在一个字符串的内部,不需要使用(
+)操作符增加一个字符到一个字符串。一些例子:
A:='This is a text';
Str:='Text '+'concat';
B:='String with CR and LF char at the end'#13#10;
C:='String with '#33#34' characters in the middle';
注释
在脚本内能插入注释。你能使用//字符或(* *)或{}。使用//字符注释只能注释一行。
//This is a comment before ShowMessage
ShowMessage('Ok');
(* This is another comment *)
ShowMessage('More ok!');
{ And this is a comment
with two lines }
ShowMessage('End of okays');
变量
不需要在脚本中声明变数类型。这样,你宣布变量只需要使用var命令和变量的名字。如果脚本的OptionExplicit属性被设置为false,那么就不
需要声明变量。在这种情况下,变数是暗示声明。如果你想要脚本上有更多的控制,设置OptionExplicit属性为true。如果OptionExplicit属性
为true,而在脚本中使用了变量但不进行声明,将会唤醒一个编译错误。例子:
脚本1:
procedure Msg;
var S;
begin
S:='Hello world!';
ShowMessage(S);
end;
脚本2:
var A;
begin
A:=0;
A:=A+1;
end;
脚本3:
var S;
S:='Hello World!';
ShowMessage(S);
如果脚本属性OptionExplicit被设置为false,那么var声明不需要出现在任何脚本的上方。
索引
字符串,数组和数组属性可以使用索引“[”并且“]”字符。更多例子:
MyChar:=MyStr[2];
MyStr[1]:='A';
MyArray[1,2]:=1530;
Lines.Strings[2]:='Some text';
数组
脚本支持数组和variant数组,使用方法参考Pascal。注意:数组的索引是从0开始的。更多例子:
NewArray := [ 2,4,6,8 ];
Num:=NewArray[1]; //Num receives "4"
MultiArray := [ ['green','red','blue'] , ['apple','orange','lemon'] ];
Str:=MultiArray[0,2]; //Str receives 'blue'
MultiArray[1,1]:='new orange';
If语句
if语句有2种形式:if..then和if..then..else。更多例子:
if J <> 0 then Result := I/J;
if J = 0 then Exit else Result := I/J;
if J <> 0 then
begin
Result := I/J;
Count := Count + 1;
end
else
Done := True;
While语句
同Pascal。更多例子:
while Data[I] <> X do I := I + 1;
while I > 0 do
begin
if Odd(I) then Z := Z * X;
I := I div 2;
X := Sqr(X);
end;
while not Eof(InputFile) do
begin
Readln(InputFile, Line);
Process(Line);
end;
repeat语句
同Pascal。更多例子:
repeat
K := I mod J;
I := J;
J := K;
until J = 0;
repeat
Write('Enter a value (0..9): ');
Readln(I);
until (I >= 0) and (I <= 9);
for语句
同Pascal。更多例子:
脚本1:
for c:=1 to 10 do
a:=a+c;
脚本2:
for i:=a to b do
begin
j:=i^2;
sum:=sum+j;
end;
case语句
同Pascal。更多例子:
case uppercase(Fruit) of
'lime': ShowMessage('green');
'orange': ShowMessage('orange');
'apple': ShowMessage('red');
else
ShowMessage('black');
end;
函数和过程说明
函数和过程的宣告同Delphi的非常相似,唯一的差别只在于不需要指定变量的类型。函数的返回值是使用暗示的Result变量。参数传地址也能被
使用,只是不需要规定变量类型。一些例子:
procedure HelloWord;
begin
ShowMessage('Hello world!');
end;
procedure UpcaseMessage(Msg);
begin
ShowMessage(Uppercase(Msg));
end;
function TodayAsString;
begin
result:=DateToStr(Date);
end;
function Max(A,B);
begin
if A>B then
result:=A
else
result:=B;
end;
procedure SwapValues(var A, B);
Var Temp;
begin
Temp:=A;
A:=B;
B:=Temp;
end;