#!/bin/bash
# test_tsf.sh - TSF 语法综合测试
echo "=== TSF 基础语法验证 ==="

echo "步骤1: 生成解析器"
tree-sitter generate
if [ $? -ne 0 ]; then
    echo "❌ 生成解析器失败"
    exit 1
fi

echo -e "\n步骤2: 测试最简单的完整结构"

echo "2.1 测试简单函数声明"
echo 'function test(); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 简单函数声明解析正常"
else
    echo "   ❌ 简单函数声明解析失败"
    echo "   详细错误："
    echo 'function test(); begin end;' | tree-sitter parse
fi

echo "2.2 测试带参数的函数（逗号分隔）"
echo 'function test(a, b); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 带参数函数（逗号）解析正常"
else
    echo "   ❌ 带参数函数（逗号）解析失败"
    echo "   详细错误："
    echo 'function test(a, b); begin end;' | tree-sitter parse
fi

echo "2.3 测试参数分隔符规则对比"
echo "   2.3a 简单参数用逗号："
echo 'function test(a, b, c); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "      ✅ 逗号分隔正常"
else
    echo "      ❌ 逗号分隔失败"
fi

echo "   2.3b 有类型用分号："
echo 'function test(a: real; b: string); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "      ✅ 分号分隔正常"
else
    echo "      ❌ 分号分隔失败"
fi

echo -e "\n步骤3: 测试函数体内容"

echo "3.1 测试end后有分号"
echo 'function test(); begin x := 1; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ end后有分号解析正常"
else
    echo "   ❌ end后有分号解析失败"
    echo "   详细错误："
    echo 'function test(); begin x := 1; end;' | tree-sitter parse
fi

echo "3.2 测试end后无分号"
echo 'function test(); begin x := 1; end' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ end后无分号解析正常"
else
    echo "   ❌ end后无分号解析失败"
    echo "   详细错误："
    echo 'function test(); begin x := 1; end' | tree-sitter parse
fi

echo "3.3 测试简单赋值"
echo 'function test(); begin x := 1; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 简单赋值解析正常"
else
    echo "   ❌ 简单赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin x := 1; end;' | tree-sitter parse
fi

echo "3.4 测试字符串赋值"
echo 'function test(); begin x := "hello"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 字符串赋值解析正常"
else
    echo "   ❌ 字符串赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin x := "hello"; end;' | tree-sitter parse
fi

echo -e "\n步骤4: 测试类型注解"

echo "4.1 测试基础类型"
echo 'function test(x: real); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础类型注解解析正常"
else
    echo "   ❌ 基础类型注解解析失败"
    echo "   详细错误："
    echo 'function test(x: real); begin end;' | tree-sitter parse
fi

echo "4.2 测试多个参数都有类型"
echo 'function test(x: real; y: string; z: integer); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 多个类型注解解析正常"
else
    echo "   ❌ 多个类型注解解析失败"
    echo "   详细错误："
    echo 'function test(x: real; y: string; z: integer); begin end;' | tree-sitter parse
fi

echo "4.3 测试混合类型和无类型参数（用分号）"
echo 'function test(a; b: real; c; d: string); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 混合参数类型解析正常"
else
    echo "   ❌ 混合参数类型解析失败"
    echo "   详细错误："
    echo 'function test(a; b: real; c; d: string); begin end;' | tree-sitter parse
fi

echo "4.4 测试复杂类型"
echo 'function test(x: array of string); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂类型注解解析正常"
else
    echo "   ❌ 复杂类型注解解析失败"
    echo "   详细错误："
    echo 'function test(x: array of string); begin end;' | tree-sitter parse
fi

echo "4.5 测试自定义类型"
echo 'function test(x: abc.def); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 自定义类型注解解析正常"
else
    echo "   ❌ 自定义类型注解解析失败"
    echo "   详细错误："
    echo 'function test(x: abc.def); begin end;' | tree-sitter parse
fi

echo "4.6 测试复杂类型组合"
echo 'function test(items: array of MyType; callback: CallbackType; data: MyCustomType); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂类型组合解析正常"
else
    echo "   ❌ 复杂类型组合解析失败"
    echo "   详细错误："
    echo 'function test(items: array of MyType; callback: CallbackType; data: MyCustomType); begin end;' | tree-sitter parse
fi

echo -e "\n步骤5: 测试默认值"

echo "5.1 测试数字默认值"
echo 'function test(x: real = 1.0); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 数字默认值解析正常"
else
    echo "   ❌ 数字默认值解析失败"
    echo "   详细错误："
    echo 'function test(x: real = 1.0); begin end;' | tree-sitter parse
fi

echo "5.2 测试字符串默认值"
echo 'function test(x: string = "hello"); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 字符串默认值解析正常"
else
    echo "   ❌ 字符串默认值解析失败"
    echo "   详细错误："
    echo 'function test(x: string = "hello"); begin end;' | tree-sitter parse
fi

echo "5.3 测试qualified identifier默认值"
echo 'function test(x: MyType = abc.def); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ qualified identifier默认值解析正常"
else
    echo "   ❌ qualified identifier默认值解析失败"
    echo "   详细错误："
    echo 'function test(x: MyType = abc.def); begin end;' | tree-sitter parse
fi

echo "5.4 测试多个参数都有默认值"
echo 'function test(x: real = 1.0; y: string = "hello"; z: integer = 42); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 多个默认值解析正常"
else
    echo "   ❌ 多个默认值解析失败"
    echo "   详细错误："
    echo 'function test(x: real = 1.0; y: string = "hello"; z: integer = 42); begin end;' | tree-sitter parse
fi

echo "5.5 测试混合有默认值和无默认值"
echo 'function test(a: real; b: string = "default"; c: integer; d: boolean = true); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 混合默认值解析正常"
else
    echo "   ❌ 混合默认值解析失败"
    echo "   详细错误："
    echo 'function test(a: real; b: string = "default"; c: integer; d: boolean = true); begin end;' | tree-sitter parse
fi

echo "5.6 测试无类型但有默认值"
echo 'function test(a = 1.0; b = "hello"); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 无类型默认值解析正常"
else
    echo "   ❌ 无类型默认值解析失败"
    echo "   详细错误："
    echo 'function test(a = 1.0; b = "hello"); begin end;' | tree-sitter parse
fi

echo "5.7 测试复杂混合参数"
echo 'function test(a; b: real; c = "default"; d: array of string = "item1"; e: MyType); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂混合参数解析正常"
else
    echo "   ❌ 复杂混合参数解析失败"
    echo "   详细错误："
    echo 'function test(a; b: real; c = "default"; d: array of string = "item1"; e: MyType); begin end;' | tree-sitter parse
fi

echo -e "\n步骤6: 测试注释"

echo "6.1 测试单行注释"
echo '// 这是注释
function test(); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 单行注释解析正常"
else
    echo "   ❌ 单行注释解析失败"
    echo "   详细错误："
    echo '// 这是注释
function test(); begin end;' | tree-sitter parse
fi

echo "6.2 测试行末注释"
echo 'function test(); begin x := 1; // 赋值
end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 行末注释解析正常"
else
    echo "   ❌ 行末注释解析失败"
    echo "   详细错误："
    echo 'function test(); begin x := 1; // 赋值
end;' | tree-sitter parse
fi

echo "6.3 测试块注释1"
echo '{ 这是块注释 }
function test(); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 块注释1解析正常"
else
    echo "   ❌ 块注释1解析失败"
fi

echo "6.4 测试块注释2"
echo '(* 这是块注释2 *)
function test(); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 块注释2解析正常"
else
    echo "   ❌ 块注释2解析失败"
fi

echo -e "\n步骤7: 测试数组构造（重点新增）"

echo "7.1 测试基础数组"
echo 'function test(); begin arr := array(1, 2, 3); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础数组解析正常"
else
    echo "   ❌ 基础数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(1, 2, 3); end;' | tree-sitter parse
fi

echo "7.2 测试混合类型数组"
echo 'function test(); begin arr := array(1, "abc", true, nil); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 混合类型数组解析正常"
else
    echo "   ❌ 混合类型数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(1, "abc", true, nil); end;' | tree-sitter parse
fi

echo "7.3 测试键值对数组"
echo 'function test(); begin arr := array("key1": "value1", "key2": "value2"); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 键值对数组解析正常"
else
    echo "   ❌ 键值对数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array("key1": "value1", "key2": "value2"); end;' | tree-sitter parse
fi

echo "7.4 测试元组数组"
echo 'function test(); begin arr := array((1, "a"), (2, "b")); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 元组数组解析正常"
else
    echo "   ❌ 元组数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array((1, "a"), (2, "b")); end;' | tree-sitter parse
fi

echo "7.5 测试嵌套数组"
echo 'function test(); begin arr := array(array(1, 2), array(3, 4)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 嵌套数组解析正常"
else
    echo "   ❌ 嵌套数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(array(1, 2), array(3, 4)); end;' | tree-sitter parse
fi

echo "7.6 测试深层嵌套数组"
echo 'function test(); begin arr := array(array(array(1, 2), array(3, 4)), array(array(5, 6), array(7, 8))); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 深层嵌套数组解析正常"
else
    echo "   ❌ 深层嵌套数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(array(array(1, 2), array(3, 4)), array(array(5, 6), array(7, 8))); end;' | tree-sitter parse
fi

echo "7.7 测试混合键值对和元组"
echo 'function test(); begin arr := array(("key1": array(1, 2, 3)), ("key2": (4, 5, 6))); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 混合键值对和元组解析正常"
else
    echo "   ❌ 混合键值对和元组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(("key1": array(1, 2, 3)), ("key2": (4, 5, 6))); end;' | tree-sitter parse
fi

echo "7.8 测试复杂嵌套键值对"
echo 'function test(); begin arr := array("outer": array("inner1": 1, "inner2": 2), "data": array(1, 2, 3)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂嵌套键值对解析正常"
else
    echo "   ❌ 复杂嵌套键值对解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array("outer": array("inner1": 1, "inner2": 2), "data": array(1, 2, 3)); end;' | tree-sitter parse
fi

echo "7.9 测试元组内嵌套数组"
echo 'function test(); begin arr := array((array(1, 2), array("a", "b")), (array(3, 4), array("c", "d"))); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 元组内嵌套数组解析正常"
else
    echo "   ❌ 元组内嵌套数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array((array(1, 2), array("a", "b")), (array(3, 4), array("c", "d"))); end;' | tree-sitter parse
fi

echo "7.10 测试复杂混合结构"
echo 'function test(); begin arr := array("data": array((1, "first"), (2, "second")), "meta": array("count": 2, "type": "test")); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂混合结构解析正常"
else
    echo "   ❌ 复杂混合结构解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array("data": array((1, "first"), (2, "second")), "meta": array("count": 2, "type": "test")); end;' | tree-sitter parse
fi

echo "7.11 测试空数组"
echo 'function test(); begin arr := array(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 空数组解析正常"
else
    echo "   ❌ 空数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(); end;' | tree-sitter parse
fi

echo "7.12 测试变量作为数组元素"
echo 'function test(); begin arr := array(var1, var2, array(var3, var4)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 变量作为数组元素解析正常"
else
    echo "   ❌ 变量作为数组元素解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(var1, var2, array(var3, var4)); end;' | tree-sitter parse
fi

echo "7.13 测试函数调用作为数组元素"
echo 'function test(); begin arr := array(func1(), func2(arg), array(func3(), func4(arg1, arg2))); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 函数调用作为数组元素解析正常"
else
    echo "   ❌ 函数调用作为数组元素解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(func1(), func2(arg), array(func3(), func4(arg1, arg2))); end;' | tree-sitter parse
fi

echo "7.14 测试括号表达式在array中的语义（单元素数组）"
echo 'function test(); begin arr := array(func(), (a + b)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 函数调用+括号表达式解析正常"
    echo "   📝 语义：array(ret1, (c)) - (a+b)在array中被视为单元素数组"
else
    echo "   ❌ 函数调用+括号表达式解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(func(), (a + b)); end;' | tree-sitter parse
fi

echo "7.15 测试括号在array中的不同语义"
echo 'function test(); begin arr := array((x + y), z, (a * b)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 多个括号表达式解析正常"
    echo "   📝 语义：每个(expr)在array中都是单元素数组"
else
    echo "   ❌ 多个括号表达式解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array((x + y), z, (a * b)); end;' | tree-sitter parse
fi

echo "7.16 测试混合函数调用、变量和括号表达式"
echo 'function test(); begin arr := array(func(), (a + b), var1, (c * d)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 混合类型解析正常"
    echo "   📝 语义：array(ret1, (result1), var1, (result2))"
else
    echo "   ❌ 混合类型解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(func(), (a + b), var1, (c * d)); end;' | tree-sitter parse
fi

echo "7.17 测试元组vs单元素数组的区别"
echo 'function test(); begin arr := array((a, b, c), (x + y), ("key": "value")); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 元组vs单元素数组解析正常"
    echo "   📝 语义：(a,b,c)是元组，(x+y)是单元素数组，第三个是键值对"
else
    echo "   ❌ 元组vs单元素数组解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array((a, b, c), (x + y), ("key": "value")); end;' | tree-sitter parse
fi

echo "7.18 测试复杂混合：函数、表达式、元组、键值对"
echo 'function test(); begin arr := array(1, func(), (expr), (a, b), "key": (value)); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂混合解析正常"
    echo "   📝 包含：字面量、函数调用、单元素数组、元组、键值对"
else
    echo "   ❌ 复杂混合解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr := array(1, func(), (expr), (a, b), "key": (value)); end;' | tree-sitter parse
fi

echo -e "\n步骤8: 测试数字格式"

echo "8.1 测试基础数字"
echo 'function test(); begin x := 123; y := 123.456; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础数字解析正常"
else
    echo "   ❌ 基础数字解析失败"
fi

echo "8.2 测试科学记数法"
echo 'function test(); begin x := 1E2; y := -3.4e34; z := 2.4e-23; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 科学记数法解析正常"
else
    echo "   ❌ 科学记数法解析失败"
fi

echo "8.3 测试不同进制"
echo 'function test(); begin x := 0x1A; y := 0b100; z := 0o64; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 不同进制解析正常"
else
    echo "   ❌ 不同进制解析失败"
fi

echo "8.4 测试特殊字面量"
echo 'function test(); begin x := nil; y := -INF; z := +INF; w := NAN; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 特殊字面量解析正常"
else
    echo "   ❌ 特殊字面量解析失败"
fi

echo -e "\n步骤9: 测试运算符"

echo "9.1 测试基础算术运算符"
echo 'function test(); begin x := a + b * c / d - e % f; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础算术运算符解析正常"
else
    echo "   ❌ 基础算术运算符解析失败"
fi

echo "9.2 测试特殊运算符"
echo 'function test(); begin x := a mod b; y := a div b; z := a ^ b; w := a ~ b; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 特殊运算符解析正常"
else
    echo "   ❌ 特殊运算符解析失败"
fi

echo "9.3 测试逻辑运算符"
echo 'function test(); begin x := a .&& b; y := a .|| b; z := .!! a; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 逻辑运算符解析正常"
else
    echo "   ❌ 逻辑运算符解析失败"
fi

echo "9.4 测试位运算符"
echo 'function test(); begin x := a .& b; y := a .| b; z := .! a; w := a .^ b; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 位运算符解析正常"
else
    echo "   ❌ 位运算符解析失败"
fi

echo "9.5 测试位移运算符"
echo 'function test(); begin x := a shl 2; y := a shr 3; z := a rol 1; w := a ror 4; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 位移运算符解析正常"
else
    echo "   ❌ 位移运算符解析失败"
fi

echo "9.6 测试自增自减"
echo 'function test(); begin ++a; ++b; --c; --d; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 前缀自增自减解析正常"
else
    echo "   ❌ 前缀自增自减解析失败"
fi

echo "9.7 测试后缀自增自减（作为表达式语句）"
echo 'function test(); begin a++; b++; c--; d--; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 后缀自增自减（表达式语句）解析正常"
else
    echo "   ❌ 后缀自增自减（表达式语句）解析失败"
    echo "   详细错误："
    echo 'function test(); begin a++; b++; c--; d--; end;' | tree-sitter parse
fi

echo "9.8 测试复合赋值"
echo 'function test(); begin a += 1; b -= 2; c *= 3; d /= 4; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复合赋值解析正常"
else
    echo "   ❌ 复合赋值解析失败"
fi

echo -e "\n步骤10: 测试三元表达式"

echo "10.1 测试基础三元表达式"
echo 'function test(); begin result := a > b ? a : b; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础三元表达式解析正常"
else
    echo "   ❌ 基础三元表达式解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := a > b ? a : b; end;' | tree-sitter parse
fi

echo "10.2 测试嵌套三元表达式"
echo 'function test(); begin result := a > 0 ? (b > 0 ? "positive" : "zero") : "negative"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 嵌套三元表达式解析正常"
else
    echo "   ❌ 嵌套三元表达式解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := a > 0 ? (b > 0 ? "positive" : "zero") : "negative"; end;' | tree-sitter parse
fi

echo "10.3 测试三元表达式链"
echo 'function test(); begin grade := score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : "F"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 三元表达式链解析正常"
else
    echo "   ❌ 三元表达式链解析失败"
fi

echo -e "\n步骤11: 测试数组索引和切片"

echo "11.1 测试数组索引访问"
echo 'function test(); begin item := arr[0]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 数组索引访问解析正常"
else
    echo "   ❌ 数组索引访问解析失败"
    echo "   详细错误："
    echo 'function test(); begin item := arr[0]; end;' | tree-sitter parse
fi

echo "11.2 测试多维数组访问"
echo 'function test(); begin item := matrix[i][j]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 多维数组访问解析正常"
else
    echo "   ❌ 多维数组访问解析失败"
    echo "   详细错误："
    echo 'function test(); begin item := matrix[i][j]; end;' | tree-sitter parse
fi

echo "11.3 测试数组切片"
echo 'function test(); begin slice := arr[1:5]; part := arr[:3]; tail := arr[2:]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 数组切片解析正常"
else
    echo "   ❌ 数组切片解析失败"
    echo "   详细错误："
    echo 'function test(); begin slice := arr[1:5]; part := arr[:3]; tail := arr[2:]; end;' | tree-sitter parse
fi

echo "11.4 测试复合数组访问"
echo 'function test(); begin item := obj.data[index]; value := func().result[0]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复合数组访问解析正常"
else
    echo "   ❌ 复合数组访问解析失败"
    echo "   详细错误："
    echo 'function test(); begin item := obj.data[index]; value := func().result[0]; end;' | tree-sitter parse
fi

echo "11.5 测试数组赋值"
echo 'function test(); begin arr[index] := value; matrix[i][j] := data; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 数组赋值解析正常"
else
    echo "   ❌ 数组赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin arr[index] := value; matrix[i][j] := data; end;' | tree-sitter parse
fi

echo -e "\n步骤12: 测试控制流"

echo "12.1 测试if语句"
echo 'function test(); begin if a > b then x := a; else x := b; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ if语句解析正常"
else
    echo "   ❌ if语句解析失败"
fi

echo "12.2 测试复杂if语句"
cat > temp_if_test.tsf << 'EOF'
function test();
begin
    if a > 0 then
    begin
        x := a;
    end
    else if a < 0 then
    begin
        x := -a;
    end
    else begin
        x := 0;
    end;
end
EOF

tree-sitter parse temp_if_test.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂if语句解析正常"
else
    echo "   ❌ 复杂if语句解析失败"
fi

echo "12.3 测试case语句"
cat > temp_case_test.tsf << 'EOF'
function test();
begin
    case age of
        10: result := "ten";
        11,12: begin
            result := "eleven or twelve";
        end
        else
            result := "other";
    end;
end
EOF

tree-sitter parse temp_case_test.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ case语句解析正常"
else
    echo "   ❌ case语句解析失败"
fi

echo "12.4 测试for循环"
echo 'function test(); begin for k,v in arr do x := v; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ for循环解析正常"
else
    echo "   ❌ for循环解析失败"
fi

echo "12.5 测试for...to循环"
echo 'function test(); begin for i:=0 to 10 do x := i; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ for...to循环解析正常"
else
    echo "   ❌ for...to循环解析失败"
fi

echo "12.6 测试while循环"
echo 'function test(); begin while x > 0 do x := x - 1; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ while循环解析正常"
else
    echo "   ❌ while循环解析失败"
fi

echo "12.7 测试repeat循环"
echo 'function test(); begin repeat x := x + 1; until x > 10; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ repeat循环解析正常"
else
    echo "   ❌ repeat循环解析失败"
fi

echo -e "\n步骤13: 测试特殊查询语法"

echo "13.1 测试vselect查询"
echo "function test(); begin ret := vselect maxof(['refs']) from v end; end;" | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ vselect查询解析正常"
else
    echo "   ❌ vselect查询解析失败"
    echo "   详细错误："
    echo "function test(); begin ret := vselect maxof(['refs']) from v end; end;" | tree-sitter parse
fi

echo "13.2 测试select查询"
echo "function test(); begin data := select sumof(['amount']) from table end; end;" | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ select查询解析正常"
else
    echo "   ❌ select查询解析失败"
fi

echo "13.3 测试sselect查询"
echo "function test(); begin result := sselect minof(['price']) from source end; end;" | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ sselect查询解析正常"
else
    echo "   ❌ sselect查询解析失败"
fi

echo "13.4 测试聚合函数变体"
echo "function test(); begin result := vselect countof(['id']) from data end; end;" | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 聚合函数变体解析正常"
else
    echo "   ❌ 聚合函数变体解析失败"
fi

echo -e "\n步骤14: 测试类语法"

echo "14.1 测试最简单的类声明"
echo 'type Base = class end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 简单类声明解析正常"
else
    echo "   ❌ 简单类声明解析失败"
    echo "   详细错误："
    echo 'type Base = class end;' | tree-sitter parse
fi

echo "14.2 测试带继承的类声明"
echo 'type Child = class(Base) end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 继承类声明解析正常"
else
    echo "   ❌ 继承类声明解析失败"
    echo "   详细错误："
    echo 'type Child = class(Base) end;' | tree-sitter parse
fi

echo "14.3 测试访问修饰符"
cat > temp_class_access.tsf << 'EOF'
type TestClass = class
public
    member1;
private
    member2: string;
protected
    member3: integer;
end;
EOF

tree-sitter parse temp_class_access.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 访问修饰符解析正常"
else
    echo "   ❌ 访问修饰符解析失败"
    echo "   详细错误："
    tree-sitter parse temp_class_access.tsf
fi

echo "14.4 测试成员变量标签"
cat > temp_class_tags.tsf << 'EOF'
type TestClass = class
public
    normal_member;
    typed_member: string;
    [weakref]weak_member;
    [weakref]weak_typed: obj;
end;
EOF

tree-sitter parse temp_class_tags.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 成员变量标签解析正常"
else
    echo "   ❌ 成员变量标签解析失败"
    echo "   详细错误："
    tree-sitter parse temp_class_tags.tsf
fi

echo "14.5 测试方法声明（类内实现）"
cat > temp_class_methods.tsf << 'EOF'
type TestClass = class
public
    function Create();
    begin
        member := 123;
    end

    function Destroy();
    begin
    end

    function func1();
    begin
        result := member;
    end
private
    member: integer;
end;
EOF

tree-sitter parse temp_class_methods.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 类内方法实现解析正常"
else
    echo "   ❌ 类内方法实现解析失败"
    echo "   详细错误："
    tree-sitter parse temp_class_methods.tsf
fi

echo "14.6 测试方法修饰符"
cat > temp_class_modifiers.tsf << 'EOF'
type TestClass = class
public
    function vfunc();virtual;
    function ofunc();overload;
    function ofunc(a);overload;
end;
EOF

tree-sitter parse temp_class_modifiers.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 方法修饰符解析正常"
else
    echo "   ❌ 方法修饰符解析失败"
    echo "   详细错误："
    tree-sitter parse temp_class_modifiers.tsf
fi

echo "14.7 测试类外方法实现"
cat > temp_class_external.tsf << 'EOF'
type TestClass = class
public
    function Create();
    function Process(data: string);virtual;
end;

function TestClass.Create();
begin
    member := 0;
end;

function TestClass.Process(data: string);virtual;
begin
    result := data;
end;
EOF

tree-sitter parse temp_class_external.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 类外方法实现解析正常"
else
    echo "   ❌ 类外方法实现解析失败"
    echo "   详细错误："
    tree-sitter parse temp_class_external.tsf
fi

echo -e "\n步骤15: 测试对象创建和成员访问"

echo "15.1 测试new创建对象"
echo 'function test(); begin obj := new MyClass(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ new创建对象解析正常"
else
    echo "   ❌ new创建对象解析失败"
    echo "   详细错误："
    echo 'function test(); begin obj := new MyClass(); end;' | tree-sitter parse
fi

echo "15.2 测试带参数的new"
echo 'function test(); begin obj := new MyClass(param1, param2); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 带参数new解析正常"
else
    echo "   ❌ 带参数new解析失败"
    echo "   详细错误："
    echo 'function test(); begin obj := new MyClass(param1, param2); end;' | tree-sitter parse
fi

echo "15.3 测试成员访问"
echo 'function test(); begin x := obj.property; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 成员访问解析正常"
else
    echo "   ❌ 成员访问解析失败"
    echo "   详细错误："
    echo 'function test(); begin x := obj.property; end;' | tree-sitter parse
fi

echo "15.4 测试成员赋值"
echo 'function test(); begin obj.property := 123; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 成员赋值解析正常"
else
    echo "   ❌ 成员赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin obj.property := 123; end;' | tree-sitter parse
fi

echo "15.5 测试方法调用"
echo 'function test(); begin obj.method(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 方法调用解析正常"
else
    echo "   ❌ 方法调用解析失败"
    echo "   详细错误："
    echo 'function test(); begin obj.method(); end;' | tree-sitter parse
fi

echo "15.6 测试链式调用"
echo 'function test(); begin result := obj.method1().method2(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 链式调用解析正常"
else
    echo "   ❌ 链式调用解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := obj.method1().method2(); end;' | tree-sitter parse
fi

echo "15.7 测试成员自增"
echo 'function test(); begin obj.count++; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 成员自增解析正常"
else
    echo "   ❌ 成员自增解析失败"
    echo "   详细错误："
    echo 'function test(); begin obj.count++; end;' | tree-sitter parse
fi

echo "15.8 测试成员复合赋值"
echo 'function test(); begin obj.value += 10; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 成员复合赋值解析正常"
else
    echo "   ❌ 成员复合赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin obj.value += 10; end;' | tree-sitter parse
fi

echo -e "\n步骤16: 测试Unit（模块）特性"

echo "16.1 测试最简单的Unit"
cat > temp_unit_simple.tsf << 'EOF'
unit TestUnit;
interface
implementation
end.
EOF

tree-sitter parse temp_unit_simple.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 最简单Unit解析正常"
else
    echo "   ❌ 最简单Unit解析失败"
    echo "   详细错误："
    tree-sitter parse temp_unit_simple.tsf
fi

echo "16.2 测试带成员的Unit"
cat > temp_unit_members.tsf << 'EOF'
unit TestUnit;
interface
    var x, y;
    const MAX = 100;
    function process();
implementation
    var z;
    function process();
    begin
        x := MAX;
    end;
end.
EOF

tree-sitter parse temp_unit_members.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 带成员Unit解析正常"
else
    echo "   ❌ 带成员Unit解析失败"
    echo "   详细错误："
    tree-sitter parse temp_unit_members.tsf
fi

echo "16.3 测试完整Unit"
cat > temp_unit_complete.tsf << 'EOF'
unit DeMo_Unit_Test01;

interface
    var Ua, Ub;
    const CS = 888;
    function addv();
    function NumbJO(v);
    function print();

implementation
    const CN = array("奇数", "偶数");
    var Uc;

    function print();
    begin
        echo "当前值-Ua:", Ua, " Ub:", Ub, " Uc:", Uc, " CS:", CS;
    end

    function addv();
    begin
        Ua += 10;
        Uc += 1;
    end;

    function NumbJO(v);
    begin
        if (v mod 2) = 0 then
            echo v, "是一个", CN[1];
        else
            echo v, "是一个", CN[0];
    end;

initialization
    Ua := 100;
    Ub := "Tinysoft Unit";
    Uc := 1;

finalization
    echo "DeMo_Unit_Test01 End.";

end.
EOF

tree-sitter parse temp_unit_complete.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 完整Unit解析正常"
else
    echo "   ❌ 完整Unit解析失败"
    echo "   详细错误："
    tree-sitter parse temp_unit_complete.tsf
fi

echo "16.4 测试uses语句"
echo 'uses TestUnit; function main(); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ uses语句解析正常"
else
    echo "   ❌ uses语句解析失败"
    echo "   详细错误："
    echo 'uses TestUnit; function main(); begin end;' | tree-sitter parse
fi

echo "16.5 测试多个uses"
echo 'uses Unit1, Unit2, Unit3; function main(); begin end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 多个uses解析正常"
else
    echo "   ❌ 多个uses解析失败"
    echo "   详细错误："
    echo 'uses Unit1, Unit2, Unit3; function main(); begin end;' | tree-sitter parse
fi

echo "16.6 测试echo语句"
echo 'function test(); begin echo "Hello", " ", "World"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ echo语句解析正常"
else
    echo "   ❌ echo语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin echo "Hello", " ", "World"; end;' | tree-sitter parse
fi

echo -e "\n步骤17: 测试复杂混合示例"

echo "17.1 测试复杂数组嵌套示例"
cat > temp_complex_array.tsf << 'EOF'
function complex_array_test();
begin
    // 复杂嵌套数组结构
    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;
end
EOF

tree-sitter parse temp_complex_array.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂数组嵌套示例解析正常"
else
    echo "   ❌ 复杂数组嵌套示例解析失败"
    echo "   详细错误："
    tree-sitter parse temp_complex_array.tsf
fi

echo "17.2 测试完整应用示例"
cat > temp_full_app.tsf << 'EOF'
// 完整应用示例
uses Utils, DataTypes;

type DataProcessor = class(BaseProcessor)
public
    function Create();overload;
    function Create(config: ConfigType);overload;
    function Process(data: array of string);
private
    cache: CacheType;
    settings: array of Setting;
end;

function DataProcessor.Create();overload;
begin
    cache := new CacheType();
    settings := array();
end;

function DataProcessor.Process(data: array of string);
begin
    // 处理数据数组
    result := array();
    for i := 0 to length(data) - 1 do
    begin
        item := data[i];
        processed := item ? transform(item) : default_value;
        result[i] := processed;
    end;

    return result;
end;

function main();
begin
    // 创建复杂数据结构
    config := array(
        "input": array("file1.txt", "file2.txt"),
        "output": "result.txt",
        "options": array(
            "mode": "batch",
            "parallel": true,
            "workers": 4
        )
    );

    // 创建处理器
    processor := new DataProcessor();
    processor.Create();

    // 处理数据
    input_data := config["input"];
    results := processor.Process(input_data);

    // 输出结果
    echo "Processing completed. Results:", results.length, "items";

    return results;
end;
EOF

tree-sitter parse temp_full_app.tsf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 完整应用示例解析正常"
else
    echo "   ❌ 完整应用示例解析失败"
    echo "   详细错误："
    tree-sitter parse temp_full_app.tsf
fi

# 🆕 新增步骤18：测试解包（destructuring）语法
echo -e "\n步骤18: 测试解包（destructuring）语法"

echo "18.1 测试基础解包赋值"
echo 'function test(); begin [a, b, c] := array(1, 2, 3); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础解包赋值解析正常"
else
    echo "   ❌ 基础解包赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin [a, b, c] := array(1, 2, 3); end;' | tree-sitter parse
fi

echo "18.2 测试函数返回值解包"
echo 'function test(); begin [x, y] := getCoordinates(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 函数返回值解包解析正常"
else
    echo "   ❌ 函数返回值解包解析失败"
    echo "   详细错误："
    echo 'function test(); begin [x, y] := getCoordinates(); end;' | tree-sitter parse
fi

echo "18.3 测试嵌套数组解包"
echo 'function test(); begin [first, second, third] := matrix[0]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 嵌套数组解包解析正常"
else
    echo "   ❌ 嵌套数组解包解析失败"
    echo "   详细错误："
    echo 'function test(); begin [first, second, third] := matrix[0]; end;' | tree-sitter parse
fi

echo "18.4 测试复杂解包示例"
echo 'function test(); begin [name, age, scores] := user_data["user1"]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂解包示例解析正常"
else
    echo "   ❌ 复杂解包示例解析失败"
    echo "   详细错误："
    echo 'function test(); begin [name, age, scores] := user_data["user1"]; end;' | tree-sitter parse
fi

# 🆕 新增步骤19：测试关键字语句（echo、raise等）
echo -e "\n步骤19: 测试关键字语句（echo、raise等）"

echo "19.1 测试echo语句"
echo 'function test(); begin echo "Hello", " ", "World"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ echo多参数语句解析正常"
else
    echo "   ❌ echo多参数语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin echo "Hello", " ", "World"; end;' | tree-sitter parse
fi

echo "19.2 测试echo单参数"
echo 'function test(); begin echo variable; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ echo单参数解析正常"
else
    echo "   ❌ echo单参数解析失败"
fi

echo "19.3 测试echo表达式和函数调用"
echo 'function test(); begin echo "Count:", count + 1, func(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ echo表达式和函数调用解析正常"
else
    echo "   ❌ echo表达式和函数调用解析失败"
fi

echo "19.4 测试基础raise语句"
echo 'function test(); begin raise "Error occurred"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础raise语句解析正常"
else
    echo "   ❌ 基础raise语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin raise "Error occurred"; end;' | tree-sitter parse
fi

echo "19.5 测试raise变量"
echo 'function test(); begin raise error_message; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ raise变量解析正常"
else
    echo "   ❌ raise变量解析失败"
fi

echo "19.6 测试raise表达式"
echo 'function test(); begin raise "Error: " + error_code; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ raise表达式解析正常"
else
    echo "   ❌ raise表达式解析失败"
fi

echo "19.7 测试raise函数调用"
echo 'function test(); begin raise getErrorMessage(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ raise函数调用解析正常"
else
    echo "   ❌ raise函数调用解析失败"
fi

# 🆕 新增步骤20：测试$连接符（万能连接符）
echo -e "\n步骤20: 测试\$连接符（万能连接符）"

echo "20.1 测试基础字符串连接"
echo 'function test(); begin result := "Hello" $ " " $ "World"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础字符串连接解析正常"
else
    echo "   ❌ 基础字符串连接解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := "Hello" $ " " $ "World"; end;' | tree-sitter parse
fi

echo "20.2 测试字符串与数字连接"
echo 'function test(); begin message := "Count: " $ count $ " items"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 字符串与数字连接解析正常"
else
    echo "   ❌ 字符串与数字连接解析失败"
fi

echo "20.3 测试\$连接符在raise中使用"
echo 'function test(); begin raise "Error code " $ error_code $ " occurred"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ \$连接符在raise中使用解析正常"
else
    echo "   ❌ \$连接符在raise中使用解析失败"
fi

echo "20.4 测试\$连接符在echo中使用"
echo 'function test(); begin echo "Value: " $ value $ ", Type: " $ type $ "!"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ \$连接符在echo中使用解析正常"
else
    echo "   ❌ \$连接符在echo中使用解析失败"
fi

echo "20.5 测试复杂\$连接表达式"
echo 'function test(); begin result := name $ "(" $ age $ ")" $ " - " $ status; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂\$连接表达式解析正常"
else
    echo "   ❌ 复杂\$连接表达式解析失败"
fi

echo "20.6 测试\$连接符优先级"
echo 'function test(); begin result := a + b $ " = " $ (a + b); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ \$连接符优先级解析正常"
    echo "   📝 语义：(a + b) $ \" = \" $ (a + b)，加法优先于连接"
else
    echo "   ❌ \$连接符优先级解析失败"
fi

# 🆕 新增步骤21：测试判断语法（is class 和 in）
echo -e "\n步骤21: 测试判断语法（is class 和 in）"

echo "21.1 测试基础is class判断"
echo 'function test(); begin result := obj is class(MyClass); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础is class判断解析正常"
else
    echo "   ❌ 基础is class判断解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := obj is class(MyClass); end;' | tree-sitter parse
fi

echo "21.2 测试is class与qualified identifier"
echo 'function test(); begin result := instance is class(namespace.ClassName); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ is class与qualified identifier解析正常"
else
    echo "   ❌ is class与qualified identifier解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := instance is class(namespace.ClassName); end;' | tree-sitter parse
fi

echo "21.3 测试is class在if语句中"
echo 'function test(); begin if obj is class(Person) then echo "Is Person"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ is class在if语句中解析正常"
else
    echo "   ❌ is class在if语句中解析失败"
    echo "   详细错误："
    echo 'function test(); begin if obj is class(Person) then echo "Is Person"; end;' | tree-sitter parse
fi

echo "21.4 测试基础in判断（数组字面量）"
echo 'function test(); begin result := 1 in array(1, 2, 3); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 基础in判断（数组字面量）解析正常"
else
    echo "   ❌ 基础in判断（数组字面量）解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := 1 in array(1, 2, 3); end;' | tree-sitter parse
fi

echo "21.5 测试in判断（变量）"
echo 'function test(); begin result := item in my_array; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ in判断（变量）解析正常"
else
    echo "   ❌ in判断（变量）解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := item in my_array; end;' | tree-sitter parse
fi

echo "21.6 测试字符串in判断"
echo 'function test(); begin result := "apple" in array("apple", "banana", "orange"); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 字符串in判断解析正常"
else
    echo "   ❌ 字符串in判断解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := "apple" in array("apple", "banana", "orange"); end;' | tree-sitter parse
fi

echo "21.7 测试in判断在if语句中"
echo 'function test(); begin if value in valid_values then echo "Valid"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ in判断在if语句中解析正常"
else
    echo "   ❌ in判断在if语句中解析失败"
    echo "   详细错误："
    echo 'function test(); begin if value in valid_values then echo "Valid"; end;' | tree-sitter parse
fi

echo "21.8 测试复杂表达式的in判断"
echo 'function test(); begin result := (x + y) in calculateRange(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂表达式in判断解析正常"
else
    echo "   ❌ 复杂表达式in判断解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := (x + y) in calculateRange(); end;' | tree-sitter parse
fi

echo "21.9 测试嵌套数组的in判断"
echo 'function test(); begin result := target in data["items"]["list"]; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 嵌套数组in判断解析正常"
else
    echo "   ❌ 嵌套数组in判断解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := target in data["items"]["list"]; end;' | tree-sitter parse
fi

echo "21.10 测试判断语法组合使用"
echo 'function test(); begin if obj is class(MyClass) and value in valid_list then echo "Valid object and value"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 判断语法组合使用解析正常"
else
    echo "   ❌ 判断语法组合使用解析失败"
    echo "   详细错误："
    echo 'function test(); begin if obj is class(MyClass) and value in valid_list then echo "Valid object and value"; end;' | tree-sitter parse
fi

echo "21.11 测试is class与三元表达式"
echo 'function test(); begin type_name := obj is class(Person) ? "Person" : "Other"; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ is class与三元表达式解析正常"
else
    echo "   ❌ is class与三元表达式解析失败"
    echo "   详细错误："
    echo 'function test(); begin type_name := obj is class(Person) ? "Person" : "Other"; end;' | tree-sitter parse
fi

# 🆕 新增步骤22：测试矩阵运算
echo -e "\n步骤22: 测试矩阵运算"

echo "22.1 测试矩阵转置运算符（前缀）"
echo 'function test(); begin A := array((1,2,3),(2,3,4)); B := `A; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵转置运算符（前缀）解析正常"
else
    echo "   ❌ 矩阵转置运算符（前缀）解析失败"
    echo "   详细错误："
    echo 'function test(); begin A := array((1,2,3),(2,3,4)); B := `A; end;' | tree-sitter parse
fi

echo "22.2 测试矩阵列连接运算符"
echo 'function test(); begin C := A | B; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵列连接运算符解析正常"
else
    echo "   ❌ 矩阵列连接运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin C := A | B; end;' | tree-sitter parse
fi

echo "22.3 测试矩阵行连接运算符"
echo 'function test(); begin C := A union B; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵行连接运算符解析正常"
else
    echo "   ❌ 矩阵行连接运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin C := A union B; end;' | tree-sitter parse
fi

echo "22.4 测试非完全矩阵列连接运算符"
echo 'function test(); begin C := A :| B; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 非完全矩阵列连接运算符解析正常"
else
    echo "   ❌ 非完全矩阵列连接运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin C := A :| B; end;' | tree-sitter parse
fi

echo "22.5 测试矩阵求逆运算符"
echo 'function test(); begin B := !A; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵求逆运算符解析正常"
else
    echo "   ❌ 矩阵求逆运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin B := !A; end;' | tree-sitter parse
fi

echo "22.6 测试矩阵乘法运算符"
echo 'function test(); begin C := A :* B; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵乘法运算符解析正常"
else
    echo "   ❌ 矩阵乘法运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin C := A :* B; end;' | tree-sitter parse
fi

echo "22.7 测试矩阵左除运算符"
echo 'function test(); begin C := A :\ B; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵左除运算符解析正常"
else
    echo "   ❌ 矩阵左除运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin C := A :\ B; end;' | tree-sitter parse
fi

echo "22.8 测试矩阵乘方运算符"
echo 'function test(); begin C := A :^ 3; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 矩阵乘方运算符解析正常"
else
    echo "   ❌ 矩阵乘方运算符解析失败"
    echo "   详细错误："
    echo 'function test(); begin C := A :^ 3; end;' | tree-sitter parse
fi

echo "22.9 测试复杂矩阵运算组合"
echo 'function test(); begin result := `(A :* B) | (C union D); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂矩阵运算组合解析正常"
else
    echo "   ❌ 复杂矩阵运算组合解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := `(A :* B) | (C union D); end;' | tree-sitter parse
fi

echo "22.10 测试嵌套矩阵转置"
echo 'function test(); begin result := ``A; nested := `(!matrix); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 嵌套矩阵转置解析正常"
else
    echo "   ❌ 嵌套矩阵转置解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := ``A; nested := `(!matrix); end;' | tree-sitter parse
fi

# 🆕 新增步骤23：测试集合运算
echo -e "\n步骤23: 测试集合运算"

echo "23.1 测试集合并集运算"
echo 'function test(); begin result := setA union setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合并集运算解析正常"
else
    echo "   ❌ 集合并集运算解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := setA union setB; end;' | tree-sitter parse
fi

echo "23.2 测试集合并集2运算"
echo 'function test(); begin result := setA union2 setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合并集2运算解析正常"
else
    echo "   ❌ 集合并集2运算解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := setA union2 setB; end;' | tree-sitter parse
fi

echo "23.3 测试集合差集运算"
echo 'function test(); begin result := setA minus setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合差集运算解析正常"
else
    echo "   ❌ 集合差集运算解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := setA minus setB; end;' | tree-sitter parse
fi

echo "23.4 测试集合交集运算"
echo 'function test(); begin result := setA intersect setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合交集运算解析正常"
else
    echo "   ❌ 集合交集运算解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := setA intersect setB; end;' | tree-sitter parse
fi

echo "23.5 测试集合运算组合"
echo 'function test(); begin result := (setA union setB) intersect setC; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合运算组合解析正常"
else
    echo "   ❌ 集合运算组合解析失败"
    echo "   详细错误："
    echo 'function test(); begin result := (setA union setB) intersect setC; end;' | tree-sitter parse
fi

# 🆕 新增步骤24：测试复合赋值运算
echo -e "\n步骤24: 测试复合赋值运算（矩阵和集合）"

echo "24.1 测试集合并集复合赋值"
echo 'function test(); begin setA union= setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合并集复合赋值解析正常"
else
    echo "   ❌ 集合并集复合赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin setA union= setB; end;' | tree-sitter parse
fi

echo "24.2 测试集合并集2复合赋值"
echo 'function test(); begin setA union2= setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合并集2复合赋值解析正常"
else
    echo "   ❌ 集合并集2复合赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin setA union2= setB; end;' | tree-sitter parse
fi

echo "24.3 测试集合差集复合赋值"
echo 'function test(); begin setA minus= setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合差集复合赋值解析正常"
else
    echo "   ❌ 集合差集复合赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin setA minus= setB; end;' | tree-sitter parse
fi

echo "24.4 测试集合交集复合赋值"
echo 'function test(); begin setA intersect= setB; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 集合交集复合赋值解析正常"
else
    echo "   ❌ 集合交集复合赋值解析失败"
    echo "   详细错误："
    echo 'function test(); begin setA intersect= setB; end;' | tree-sitter parse
fi

echo "24.5 测试复杂复合赋值组合"
echo 'function test(); begin matrix.data union= processedData; obj.values intersect= validValues; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂复合赋值组合解析正常"
else
    echo "   ❌ 复杂复合赋值组合解析失败"
    echo "   详细错误："
    echo 'function test(); begin matrix.data union= processedData; obj.values intersect= validValues; end;' | tree-sitter parse
fi

# 🆕 新增步骤25：测试表达式语句
echo -e "\n步骤25: 测试表达式语句"

echo "25.1 测试简单表达式语句"
echo 'function test(); begin x + y; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 简单表达式语句解析正常"
else
    echo "   ❌ 简单表达式语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin x + y; end;' | tree-sitter parse
fi

echo "25.2 测试函数调用表达式语句"
echo 'function test(); begin func(); obj.method(); end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 函数调用表达式语句解析正常"
else
    echo "   ❌ 函数调用表达式语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin func(); obj.method(); end;' | tree-sitter parse
fi

echo "25.3 测试复杂表达式语句"
echo 'function test(); begin a + b * c; obj.method().value; `matrix :* other; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 复杂表达式语句解析正常"
    echo "   📝 包含：算术运算、方法链调用、前缀矩阵转置+乘法"
else
    echo "   ❌ 复杂表达式语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin a + b * c; obj.method().value; `matrix :* other; end;' | tree-sitter parse
fi

echo "25.4 测试自增自减表达式语句"
echo 'function test(); begin a++; ++b; c--; --d; obj.prop++; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 自增自减表达式语句解析正常"
else
    echo "   ❌ 自增自减表达式语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin a++; ++b; c--; --d; obj.prop++; end;' | tree-sitter parse
fi

echo "25.5 测试三元表达式语句"
echo 'function test(); begin condition ? value1 : value2; end;' | tree-sitter parse > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "   ✅ 三元表达式语句解析正常"
else
    echo "   ❌ 三元表达式语句解析失败"
    echo "   详细错误："
    echo 'function test(); begin condition ? value1 : value2; end;' | tree-sitter parse
fi

# 清理临时文件
rm -f temp_*.tsf

# 🆕 修改：更新测试完成总结
echo -e "\n=== 测试完成 ==="
echo "✅ TSF语法解析器测试完成，支持的特性包括："
echo "   - 完整的函数定义和调用"
echo "   - 复杂嵌套数组结构（array语法）"
echo "   - 解包（destructuring）赋值语法"                    
echo "   - 关键字语句（echo、raise）"                         
echo "   - \$万能连接符运算符"                              
echo "   - 判断语法（is class、in）"                          
echo "   - 矩阵运算（转置、乘法、求逆、连接等）"                # 🆕 添加
echo "   - 集合运算（并集、交集、差集等）"                     # 🆕 添加
echo "   - 类和对象系统（包括继承、访问控制）"
echo "   - Unit模块系统"
echo "   - 丰富的运算符（包括三元表达式）"
echo "   - 完整的控制流语句"
echo "   - 特殊查询语法（vselect/select/sselect）"
echo "   - 多种数字格式和注释类型"
echo "   - 数组索引和切片访问"
