tsl-devkit/lsp-server/test/tree_sitter_tsf/examples/syntax.tsf

365 lines
9.0 KiB
Plaintext

// Example TSF program demonstrating various language features
// Variable declarations
var x, y, z: int;
var name: string;
var matrix: array of real;
// Constants
const PI = 3.14159;
const MAX_SIZE: int = 100;
const GREETING = "Hello, World!";
// Global and static variables
global config: object;
static counter := 0;
// Basic arithmetic
x := 10;
y := 20;
z := x + y * 2; // z = 50
// String operations
name := "John" $ " " $ "Doe";
// Boolean operations
var isValid := x > 0 and y < 100;
var isReady := not (z = 0) or name <> "";
// Bitwise operations
var flags := 0b1010;
flags := flags .| 0b0101; // flags = 0b1111
flags := flags shl 2; // flags = 0b111100
// Matrix operations
var A, B, C: matrix;
C := A :* B; // Matrix multiplication
C := A :^ 2; // Matrix power
// Set operations
var set1, set2, result: set;
result := set1 union set2;
result := set1 intersect set2;
// Ternary operator
var max := x > y ? x : y;
var sign := x > 0 ? 1 : x < 0 ? -1 : 0;
// Prefix and postfix operators
++counter;
var oldValue := counter++;
// Augmented assignments
x += 10;
y *= 2;
flags .&= 0xFF;
set1 union= set2;
obj.a := 123;
obj["a"] := 456;
// Special values
var nothing := nil;
var infinity := +inf;
var negInfinity := -inf;
// array
arr := array();
arr := array(1, 2, 3);
arr := array(1, "abc", true, nil);
arr := array("key1": "value1", "key2": "value2");
arr := array((1, "a"), array(2, "b"));
arr := array(array(1, 2), array(3, 4));
arr := array(array(array(1, 2), array(3, 4)), array(array(5, 6), array(7, 8)));
arr := array(("key1": array(1, 2, 3)), ("key2": (4, 5, 6)));
arr := array("outer": array("inner1": 1, "inner2": 2), "data": array(1, 2, 3));
arr := array((array(1, 2), array("a", "b")), (array(3, 4), array("c", "d")));
arr := array("data": array((1, "first"), (2, "second")), "meta": array("count": 2, "type": "test"));
arr := array(var1, var2, array(var3, var4));
arr := array(func1(), func2(arg), array(func3(), func4(arg1, arg2)));
arr := array(func(), (a + b));
arr := array((x + y), z, (a * b));
arr := array(func(), (a + b), var1, (c * d));
arr := array((a, b, c), (x + y), ("key": "value"));
arr := array(1, func(), (expr), (a, b), "key": (value));
data := array(
"users": array(
("id": 1, "name": "Alice", "scores": array(85, 92, 78)),
("id": 2, "name": "Bob", "scores": array(91, 87, 95))
),
"metadata": array(
"total": 2,
"average": array(88.0, 89.5, 86.5),
"nested": array(
"level1": array(
"level2": array(1, 2, 3),
"level2b": (true, false, nil)
)
)
)
);
// 访问嵌套数据
first_user := data["users"][0];
first_score := first_user["scores"][0];
nested_value := data["metadata"]["nested"]["level1"]["level2"][1];
echo "First user score:", first_score;
echo "Nested value:", nested_value;
// Function calls (hypothetical functions)
process(data);
calculate(x: 10, y: 20, mode: "fast");
##process(data);
// Attribute access (hypothetical objects)
// var length := myString.length;
// config.settings.timeout := 3000;
// Array subscripting and slicing
// var first := array[0];
// var subArray := array[1:10];
// var tail := array[5:];
// Complex expressions with precedence
var result1 := 2 + 3 * 4 ^ 2; // 2 + 3 * 16 = 50
var result2 := (2 + 3) * 4 ^ 2; // 5 * 16 = 80
var result3 := not a > b and c <= d or e = f;
// Derivative operator (mathematical)
// var derivative := !f;
// Expression operators
// var reference := @value;
// var address := &variable;
{ This is a block comment
It can span multiple lines
and contain any text }
(* This is a nested comment
It can also span multiple lines
and is Pascal-style *)
// Chained comparisons
var inRange := 0 <= x <= 100;
var ordered := a < b < c;
// Nested assignments
var a := var b := var c := 0;
// Complex type specifications
var complexType: array of array of real;
var functionType: procedure of integer;
// const
const a: real = 10;
const b = "123";
// augmented_assignment
a += 10;
b += func();
c /= ma[1];
// return
return;
return 10;
return f(10);
// break
break;
// continue
continue;
// echo
echo abc $ def $ "123" $ 456, funcstr(), "\n";
// raise
raise abc() $ "123";
// inherited
inherited abc();
// new
obj := new abc();
// if
if condition then
f1();
else
raise "abc";
if condition1 then
begin
if f1() then echo 1;
else echo 2;
end
else if condition2 then
begin
echo 3;
end
else begin
echo 4;
end
// for
for i:=0 to 10 do
echo i, "\n";
for k,v in arr do
begin
echo "k = 0", "\n";
echo "v = 1", "\n";
end
// while
while true do
echo 123;
// repeat
repeat
echo 1;
a++;
until a > 10;
// case
case x of
1,2: return abc;
"acde": begin
return def;
end
else begin
a := 1;
return a;
end
end;
// try
try
a := 1;
except
a := 2;
end
// function
function foo(a, b, c): real
begin
end
function foo(a, b: integer; c: string);
begin
end;
function foo(a: boolean; b: integer; c: string): real;
begin
end
function foo(a: boolean = true; b: integer = 123);
begin
end;
// function pointer
pf := function(a, b)
begin
echo a;
end;
// type
type A = class
public
function create()
begin
end
function foo1(a: real; b: real);virtual;
function foo2(a: real; b: real);virtual;
begin
end
function operator[](index);
property row read readrow;
property col read readcol write wirtecol;
private
[weakref]a1: real;
static a2: real;
a3: tslobj;
end;
function A.create();
begin
pf := aa;
end;
function operator A.[](index);
begin
a := 1;
end;
function A.foo1(a: real; b: real);virtual;
begin
pf := function();
begin
end
end
// select
select * from abc end;
select *, ["abc"] from abc end;
select *, ["abc"] as "def" from abc end;
select * from abc where func(["abc"]) end;
select * from abc where ["abc"] > 1 end;
R1 := select *,RoundTo((["英语成绩"]+["语文成绩"]+["历史成绩"]+["地理成绩"])/4,2) as "文科成绩" from ScoreList end;
return select ['stockid'],['date'],['close']*['vol'] as nil from markettable end;
A := select *,ThisOrder as "Order" from A order by ["AAA"] end;
B := select * from EnglishScore where ["英语成绩"] > 85 order by ["英语成绩"] end;
B := select drange(0 to 9) * from EnglishScore order by ["英语成绩"] desc end;
B := select drange(1 of 10) from EnglishScore order by ["英语成绩"] desc end;
a := select * from abc where ["abc"] > 1 end;
b := select * from abc group by ["abc"] end;
c := select * from abc group by func(["acbb"]) end;
b := select * from abc group by ["abc"], ["def"] end;
e := select * from abc where func(["abc"]) order by ["A"] desc end;
select AvgOf(["英语成绩"]) from EnglishScore where ["英语成绩"]>85 end;
return select ["性别"],AvgOf(["英语成绩"]),CountOf( * ),groupfunc(["英语成绩"]) from EnglishScore group by ["性别"],groupfunc(["英语成绩"]) end;
return select AvgOf(["英语成绩"]), CountOf( * ), groupfunc(["英语成绩"]) from EnglishScore group by groupfunc(["英语成绩"]) having CountOf( * ) >1 end;
R := select [1].*,[2].["英语成绩"] from A join B on [1].["学号"]=[2].["学号"] end;
R := select [1].*,[2].["英语成绩"] from A cross join B where [1].["学号"]=[2].["学号"] end;
R := select [1].*,[2].["英语成绩"] from A, B where [1].["学号"]=[2].["学号"] end;
R := select [1].*,[2].["英语成绩"] from A join B with ([1].["学号"] on [2].["学号"]) end;
R := select [1].*,[2].["英语成绩"] from A join B with ([1].["学号"],[1].["班级"] on [2].["学号"],[2].["班级"]) end;
R := select [1].*,[2].["英语成绩"],[3].["语文成绩"] from A join B on [1].["学号"]=[2].["学号"] join C on [1].["学号"]=[3].["学号"] end;
R := select [1].*,[2].["英语成绩"],[3].["俄语成绩"] from A left join B on [1].["学号"]=[2].["学号"] left join C on [1].["学号"]=[3].["学号"] end;
R := select [1].["学号"]?:[2].["学号"] as "学号",[1].["英语成绩"],[2].["俄语成绩"] from B full join C on [1].["学号"]=[2].["学号"] end;
R1 := select ThisRow from R where ThisRow>5 end;
R2 := sselect ThisRow from R where ThisRow>5 end;
R2 := vselect SumOf( ["英语成绩"] ) from B end;
R1 := select ["性别"],["年龄"], AvgOf(["身高"]), select * from ThisGroup end as "详细信息" from R group by ["性别"],["年龄"] end;
// update
update B set ["英语成绩"] = 79 where ["学号"] = "03" end;
update B set ["英语成绩"]=79,["语文成绩"]=80 where ["学号"]="03" end;
update children set ['origin_field'] = ['field'] end;
update children set ['field'] = uppercase(['prefix']) + ['field'] where not ifnil(['ml']) and ['field'] in fields end;
update children set ['new_field'] = format('XmlChild%s', ['field']) end;
// delete
delete from A where ["学号"] = "01";
delete from A;
// insert
insert into a values("06","路人甲");
insert into a insertfields(["学号"],["姓名"],["英语成绩"]) values("06","路人甲",80);
// []
A[2,3];
A[2:5,3:6];
A[:,3:6];
A[3,:];
A[:,"columnName"];
A[array(2,4,6)];
A[array(2,3),array(1,2)];
[a,b,c] := arr;
// End of example