📝 docs(tsl): align syntax annotations and examples
This commit is contained in:
@@ -14,66 +14,66 @@ description: "TSL/TSF 语法与工程实践指南(基础语法/高级特性/
|
||||
### 变量与常量
|
||||
|
||||
```tsl
|
||||
A := 1;
|
||||
Name := "test";
|
||||
Items := array(1,2,3);
|
||||
Table := array("Code":"0001","Price":12.3);
|
||||
Const MaxRetries = 3;
|
||||
a := 1;
|
||||
name := "test";
|
||||
items := array(1, 2, 3);
|
||||
table_data := array("Code": "0001", "Price": 12.3);
|
||||
const kMaxRetries = 3;
|
||||
```
|
||||
|
||||
### 函数
|
||||
|
||||
```tsl
|
||||
Function Add(a,b);
|
||||
Begin
|
||||
Return a + b;
|
||||
End;
|
||||
function Add(a, b);
|
||||
begin
|
||||
return a + b;
|
||||
end;
|
||||
```
|
||||
|
||||
```tsl
|
||||
Function Parse(const s, var out_value);
|
||||
Begin
|
||||
out_value := StrToInt(s);
|
||||
Return out_value;
|
||||
End;
|
||||
function Parse(const s, var out_value);
|
||||
begin
|
||||
out_value := StrToInt(s);
|
||||
return out_value;
|
||||
end;
|
||||
```
|
||||
|
||||
### 控制流
|
||||
|
||||
```tsl
|
||||
If x>0 then
|
||||
y := 1
|
||||
else if x=0 then
|
||||
y := 0
|
||||
if x > 0 then
|
||||
y := 1;
|
||||
else if x = 0 then
|
||||
y := 0;
|
||||
else
|
||||
y := -1;
|
||||
y := -1;
|
||||
|
||||
For i := 0 to 9 do
|
||||
sum := sum + i;
|
||||
for i := 0 to 9 do
|
||||
sum := sum + i;
|
||||
|
||||
For idx,v in Items do
|
||||
total := total + v;
|
||||
for idx, v in items do
|
||||
total := total + v;
|
||||
```
|
||||
|
||||
### 异常处理
|
||||
|
||||
```tsl
|
||||
Try
|
||||
v := StrToInt(s);
|
||||
Except
|
||||
v := 0;
|
||||
Writeln(ExceptObject.ErrInfo);
|
||||
End;
|
||||
try
|
||||
v := StrToInt(s);
|
||||
except
|
||||
v := 0;
|
||||
WriteLn(ExceptObject.ErrInfo);
|
||||
end;
|
||||
```
|
||||
|
||||
### 数组与索引
|
||||
|
||||
```tsl
|
||||
arr := array(10,20,30);
|
||||
arr := array(10, 20, 30);
|
||||
value := arr[0];
|
||||
|
||||
m := array((1,2),(3,4));
|
||||
col0 := m[:,0];
|
||||
matrix := array((1, 2), (3, 4));
|
||||
col_0 := matrix[:, 0];
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -46,16 +46,16 @@
|
||||
### 赋值
|
||||
|
||||
```tsl
|
||||
A := 1;
|
||||
B := 2.5;
|
||||
S := "hello";
|
||||
a := 1;
|
||||
b := 2.5;
|
||||
s := "hello";
|
||||
```
|
||||
|
||||
### 常量
|
||||
|
||||
```tsl
|
||||
Const PI = 3.14159;
|
||||
Const MaxRetry = 3;
|
||||
const kPi = 3.14159;
|
||||
const kMaxRetry = 3;
|
||||
```
|
||||
|
||||
### 显式变量声明(配合 `{$Explicit+}`)
|
||||
@@ -63,9 +63,9 @@ Const MaxRetry = 3;
|
||||
```tsl
|
||||
{$Explicit+}
|
||||
|
||||
Var A,B; // 多变量声明
|
||||
Var C := 10; // 声明并初始化
|
||||
Var D:Integer; // 类型可写可省(类型信息不做强校验)
|
||||
var a, b; // 多变量声明
|
||||
var c := 10; // 声明并初始化
|
||||
var d: integer; // 类型可写可省(类型信息不做强校验)
|
||||
```
|
||||
|
||||
---
|
||||
@@ -89,19 +89,19 @@ Var D:Integer; // 类型可写可省(类型信息不做强校验)
|
||||
示例:
|
||||
|
||||
```tsl
|
||||
I := 42;
|
||||
R := 3.14;
|
||||
B := true;
|
||||
T := Date();
|
||||
S := 'hello';
|
||||
N := nil;
|
||||
i := 42;
|
||||
r := 3.14;
|
||||
b := true;
|
||||
t := Date();
|
||||
s := 'hello';
|
||||
n := nil;
|
||||
```
|
||||
|
||||
字符串支持单引号或双引号,索引访问从 0 开始:
|
||||
|
||||
```tsl
|
||||
S := "ABC";
|
||||
Ch := S[0];
|
||||
s := "ABC";
|
||||
ch := s[0];
|
||||
```
|
||||
|
||||
---
|
||||
@@ -111,22 +111,22 @@ Ch := S[0];
|
||||
### 一维数组(0-based)
|
||||
|
||||
```tsl
|
||||
Arr := array(2,3,5,7,11);
|
||||
First := Arr[0];
|
||||
arr := array(2, 3, 5, 7, 11);
|
||||
first := arr[0];
|
||||
```
|
||||
|
||||
### 字符串下标(表数组)
|
||||
|
||||
```tsl
|
||||
Row := array("Code":"0001","Price":12.3);
|
||||
Code := Row["Code"];
|
||||
row := array("Code": "0001", "Price": 12.3);
|
||||
code := row["Code"];
|
||||
```
|
||||
|
||||
### 多维数组
|
||||
|
||||
```tsl
|
||||
Mat := array((1,2,3),(4,5,6));
|
||||
Value := Mat[0][1];
|
||||
matrix := array((1, 2, 3), (4, 5, 6));
|
||||
value := matrix[0][1];
|
||||
```
|
||||
|
||||
---
|
||||
@@ -144,9 +144,9 @@ Value := Mat[0][1];
|
||||
示例:
|
||||
|
||||
```tsl
|
||||
C := A + B;
|
||||
Ok := (A = B) or (A > 0);
|
||||
X := (A > B) ? A : B;
|
||||
c := a + b;
|
||||
ok := (a = b) or (a > 0);
|
||||
x := (a > b) ? a : b;
|
||||
```
|
||||
|
||||
矩阵/数组相关的 `.=`、`.<` 等算符在扩展语法中说明(见 `06_extended_syntax.md`)。
|
||||
@@ -159,54 +159,68 @@ X := (A > B) ? A : B;
|
||||
### 基本结构
|
||||
|
||||
```tsl
|
||||
Function HelloTSL();
|
||||
Begin
|
||||
D := Date();
|
||||
Return DateToStr(D);
|
||||
End;
|
||||
function HelloTsl();
|
||||
begin
|
||||
d := Date();
|
||||
return DateToStr(d);
|
||||
end;
|
||||
```
|
||||
|
||||
### 参数分隔与类型注解
|
||||
|
||||
类型注解仅用于说明与阅读,编译器不做类型检查。
|
||||
|
||||
- 无类型注解时用逗号分隔参数
|
||||
- 带类型注解时用分号分隔参数
|
||||
- 返回值类型写在 `)` 之后
|
||||
|
||||
```tsl
|
||||
function F1(a, b);
|
||||
function F2(a: string; b: array of real);
|
||||
function F3(): void;
|
||||
```
|
||||
|
||||
### 形参与实参(in/const/var/out)
|
||||
|
||||
```tsl
|
||||
Function Calc(in a, var b, out c);
|
||||
Begin
|
||||
b := b + a;
|
||||
c := b * 2;
|
||||
Return c;
|
||||
End;
|
||||
function Calc(in a, var b, out c);
|
||||
begin
|
||||
b := b + a;
|
||||
c := b * 2;
|
||||
return c;
|
||||
end;
|
||||
```
|
||||
|
||||
### 缺省参数(新一代语法)
|
||||
|
||||
```tsl
|
||||
Function Foo(a,b=a+1);
|
||||
Begin
|
||||
Return b;
|
||||
End;
|
||||
function Foo(a, b = a + 1);
|
||||
begin
|
||||
return b;
|
||||
end;
|
||||
```
|
||||
|
||||
### 命名参数(新一代语法)
|
||||
|
||||
```tsl
|
||||
Function FuncA(a,b,c);
|
||||
Begin
|
||||
Return array(a,b,c);
|
||||
End;
|
||||
function FuncA(a, b, c);
|
||||
begin
|
||||
return array(a, b, c);
|
||||
end;
|
||||
|
||||
Result := FuncA(1, c:3); // b 默认为 nil
|
||||
result := FuncA(1, c: 3); // b 默认为 nil
|
||||
```
|
||||
|
||||
### 变参(`...`)与 Params
|
||||
|
||||
```tsl
|
||||
Function SumAll(...);
|
||||
Begin
|
||||
Total := 0;
|
||||
For i,v in Params do
|
||||
Total := Total + v;
|
||||
Return Total;
|
||||
End;
|
||||
function SumAll(...);
|
||||
begin
|
||||
total := 0;
|
||||
for i, v in Params do
|
||||
total := total + v;
|
||||
return total;
|
||||
end;
|
||||
```
|
||||
|
||||
相关内置:`Params`, `ParamCount`, `RealParamCount`。
|
||||
@@ -214,14 +228,14 @@ End;
|
||||
### 返回与退出
|
||||
|
||||
```tsl
|
||||
Return X; // 返回
|
||||
Exit; // 退出函数
|
||||
return value; // 返回
|
||||
exit; // 退出函数
|
||||
```
|
||||
|
||||
### 外部函数声明(external)
|
||||
|
||||
```tsl
|
||||
Function GetTickCount():Integer; stdcall; external "kernel32.dll" name "GetTickCount" KeepResident;
|
||||
function GetTickCount(): integer; stdcall; external "kernel32.dll" name "GetTickCount" keepresident;
|
||||
```
|
||||
|
||||
更多外部接口与调用方式见 `03_functions.md`。
|
||||
@@ -233,75 +247,75 @@ Function GetTickCount():Integer; stdcall; external "kernel32.dll" name "GetTickC
|
||||
### if / else if
|
||||
|
||||
```tsl
|
||||
If x>0 then
|
||||
y := 1
|
||||
else if x=0 then
|
||||
y := 0
|
||||
if x > 0 then
|
||||
y := 1;
|
||||
else if x = 0 then
|
||||
y := 0;
|
||||
else
|
||||
y := -1;
|
||||
y := -1;
|
||||
```
|
||||
|
||||
### if 表达式(新一代语法)
|
||||
|
||||
```tsl
|
||||
Ret := if x>0 then x*x else 0;
|
||||
ret := if x > 0 then x * x else 0;
|
||||
```
|
||||
|
||||
### case
|
||||
|
||||
```tsl
|
||||
Case Age Of
|
||||
0: Writeln("Baby");
|
||||
1: Writeln("Toddler");
|
||||
case age of
|
||||
0: WriteLn("Baby");
|
||||
1: WriteLn("Toddler");
|
||||
else
|
||||
Writeln("Other");
|
||||
End;
|
||||
WriteLn("Other");
|
||||
end;
|
||||
```
|
||||
|
||||
### while / repeat
|
||||
|
||||
```tsl
|
||||
While cond do
|
||||
DoSomething();
|
||||
while cond do
|
||||
DoSomething();
|
||||
|
||||
Repeat
|
||||
DoSomething();
|
||||
Until cond;
|
||||
repeat
|
||||
DoSomething();
|
||||
until cond;
|
||||
```
|
||||
|
||||
### for(to / downto / step)
|
||||
|
||||
```tsl
|
||||
For i := 1 to 10 do
|
||||
Sum := Sum + i;
|
||||
for i := 1 to 10 do
|
||||
sum := sum + i;
|
||||
|
||||
For i := 10 downto 1 step 2 do
|
||||
Sum := Sum + i;
|
||||
for i := 10 downto 1 step 2 do
|
||||
sum := sum + i;
|
||||
```
|
||||
|
||||
### for ... in(数组遍历)
|
||||
|
||||
```tsl
|
||||
Data := array('a':1,'b':5,'c':3);
|
||||
Sum := 0;
|
||||
data := array('a': 1, 'b': 5, 'c': 3);
|
||||
sum := 0;
|
||||
|
||||
For k,v in Data do
|
||||
Sum := Sum + v;
|
||||
for k, v in data do
|
||||
sum := sum + v;
|
||||
```
|
||||
|
||||
### break / continue / goto
|
||||
|
||||
```tsl
|
||||
While cond do
|
||||
Begin
|
||||
If stop then Break;
|
||||
If skip then Continue;
|
||||
// ...
|
||||
End;
|
||||
while cond do
|
||||
begin
|
||||
if stop then break;
|
||||
if skip then continue;
|
||||
// ...
|
||||
end;
|
||||
|
||||
Goto Finded;
|
||||
label Finded;
|
||||
Writeln("jumped");
|
||||
goto found;
|
||||
label found;
|
||||
WriteLn("jumped");
|
||||
```
|
||||
|
||||
---
|
||||
@@ -311,28 +325,28 @@ label Finded;
|
||||
### Try / Except
|
||||
|
||||
```tsl
|
||||
Try
|
||||
V := StrToInt(S);
|
||||
Except
|
||||
V := 0;
|
||||
Writeln(ExceptObject.ErrInfo);
|
||||
End;
|
||||
try
|
||||
v := StrToInt(s);
|
||||
except
|
||||
v := 0;
|
||||
WriteLn(ExceptObject.ErrInfo);
|
||||
end;
|
||||
```
|
||||
|
||||
### Try / Finally
|
||||
|
||||
```tsl
|
||||
Try
|
||||
DoWork();
|
||||
Finally
|
||||
Cleanup();
|
||||
End;
|
||||
try
|
||||
DoWork();
|
||||
finally
|
||||
Cleanup();
|
||||
end;
|
||||
```
|
||||
|
||||
### Raise
|
||||
|
||||
```tsl
|
||||
If A < 0 then Raise "A 不能小于 0";
|
||||
if a < 0 then raise "A 不能小于 0";
|
||||
```
|
||||
|
||||
异常信息可通过 `ExceptObject.ErrInfo/ErrLine/ErrNo` 获取。
|
||||
@@ -342,13 +356,13 @@ If A < 0 then Raise "A 不能小于 0";
|
||||
## 调试与性能相关语句
|
||||
|
||||
```tsl
|
||||
DebugReturn 1;
|
||||
debugreturn 1;
|
||||
```
|
||||
|
||||
```tsl
|
||||
T1 := MTIC;
|
||||
t1 := mtic;
|
||||
// ...
|
||||
Elapsed := MTOC(T1);
|
||||
elapsed := mtoc(t1);
|
||||
```
|
||||
|
||||
更多调试/性能语法见 `02_control_flow.md` 与 `07_debug_and_profiler.md`。
|
||||
|
||||
Reference in New Issue
Block a user