// TSF 变量声明测试代码示例 // 用于测试 var, static, global, const 语法 //================================================================================ // 1. var 声明测试 //================================================================================ // 保留关键字 do := 13; type := "1234"; in := f2(); class := defff; delete := +inf; // 基本声明 var simple; var withType: integer; var withInit := 42; var complete: real := 3.14; // 多变量声明 var a, b, c; var x, y, z: integer; var m, n := 100; var p, q, r: string := "default"; // 数组类型 var intArray: array of integer; var stringArray: array of string := null; var matrix: array of real; var tensor: array of float; // 复杂类型名 var list: System.Collections.List; var dict: System.Collections.Generic.Dictionary_string_int; var custom: MyNamespace.MyClass.NestedType; // 复杂初始化器 var fromFunc := getValue(); var fromExpr := (a + b) * c; var fromArray := array(1, 2, 3, 4, 5); var fromCall := createObject("param1", 123, true); //================================================================================ // 2. static 声明测试 //================================================================================ // 基本声明 static counter; static typed: integer; static initialized := 0; static full: string := "static"; // 多变量声明 static s1, s2, s3; static sx, sy: real; static sa, sb := 999; static sp, sq, sr: boolean := false; // 静态数组 static cache: array of object; static buffer: array of byte := null; static lookupTable: array of integer := createTable(); // 复杂类型和初始化 static singleton: MyClass := MyClass.getInstance(); static config: dictionary := loadConfig("app.config"); static handler: EventHandler := lambda(e) { handleEvent(e); }; //================================================================================ // 3. global 声明测试 //================================================================================ // 基本声明 global appConfig; global settings: object; global database := null; global connection: DBConnection := connect(); // 多变量声明 global g1, g2; global gx, gy, gz: any; global ga, gb := "global"; global gp, gq: integer := 0; // 全局数组和集合 global users: array of User; global cache: dictionary := createDictionary(); global eventQueue: array of Event := array(); // 复杂全局变量 global logger: ILogger := createLogger("app.log"); global threadPool: ThreadPool := ThreadPool(4); global serviceLocator: ServiceLocator := ServiceLocator.default; //================================================================================ // 4. const 声明测试 //================================================================================ // 数值常量 const PI = 3.14159265359; const E = 2.71828; const MAX_INT: integer = 2147483647; const MIN_INT: integer = -2147483648; // 字符串常量 const VERSION = "1.0.0"; const APP_NAME: string = "MyApplication"; const PATH_SEPARATOR = "/"; const EMPTY = ""; // 布尔常量 const DEBUG = true; const RELEASE: boolean = false; const ENABLED = true; // 表达式常量 const BUFFER_SIZE = 1024 * 8; const TIMEOUT = 60 * 1000; const MASK = 0xFF; const COMBINED = "Hello" $ " " $ "World"; // 特殊值常量 const NULL_VALUE = null; const DEFAULT_PORT = 8080; const MAX_RETRIES: integer = 3; //================================================================================ // 5. 混合声明测试 //================================================================================ // 同一作用域内的多种声明 var localVar: integer; static sharedStatic := 0; global globalResource: Resource; const CONFIG_FILE = "config.json"; // 连续声明 var v1; var v2; var v3; static s1; static s2; global g1; global g2; const C1 = 1; const C2 = 2; //================================================================================ // 6. 特殊字符和命名测试 //================================================================================ // 下划线开头 var _private; static __internal: integer; const _CONSTANT = 100; // 数字结尾 var var1, var2, var3; static counter_2024; global data_123: array of integer; const VERSION_2 = "2.0"; // Unicode标识符(如果支持) var 变量: string; static 静态变量 := 0; global 全局设置: dictionary; const 常量值 = "中文"; //================================================================================ // 7. 边界和错误情况(这些应该导致解析错误) //================================================================================ // 以下是故意的错误示例,用于测试错误处理 // var x = 10; // 错误:应该使用 := // const C := 5; // 错误:const应该使用 = // const EMPTY_CONST; // 错误:const必须有值 // var ; // 错误:缺少标识符 // var x: integer // 错误:缺少分号 //================================================================================ // 8. 格式化和空白测试 //================================================================================ // 紧凑格式 var a,b,c:integer:=0; static x,y:real; global m:string:="test"; const K=999; // 宽松格式 var spaced , names : type := value ; static wide : integer ; global lots , of , spaces ; const SPACEY = "value" ; // 多行格式 var multiline, declaration, test: integer := 0; //================================================================================ // 测试结束 //================================================================================