/* 基于栈的虚拟机 lls本身并不提供除了 * include() * typeof() 的函数,全部需要用户手动绑定。官方提供std包,包含一些常用的功能 数据类型: real | 实数 | 值 boolean | 布尔值 | 值 --------+--------+---------- string | 字符串 | 地址 set | 集合 | 地址 array | 数组 | 地址 --------+--------+---------- function| 函数 | 地址(仅) new关键字会拷贝内存,如果没有new则会使用地址赋值。 引用计数=0时会被标记可回收。在GC时回收内存。 local s = "hello"; local a = s; a[0] = '1'; io.print(s); // 1ello local b = new s; b[0] = '1'; io.print(s); // hello 包含其他文件,使用include函数: 加载文件时会判断文件是否加载过,加载过则不加载. local something = include("another.lls") 运算符: 关系 == != > < >= <= 逻辑 ! && || 赋值 = 算数 + - * / % += -= *= /= %= 位 & 按位与 | 按位或 ~ 按位取反 ^ 按位异或 &= |= ~= ^= >> << >>= <<= 取set\array\string长度 # 关键字 null true false local global 声明一个变量时必须制定作用域,否则是赋值 new 数字表示方式 可以加任意个_方便阅读 十进制 [1~9][0~9]...[0~9] 12306 二进制 0 0_00_00_00_00 十六进制 0x 0xFA 原生字符串 local rawString = "" this is a raw string test ""; */ /* 可以成为语句: 赋值、 不可成为语句: ; */ local too; local foo = null; local a = 10; global b = 12; /* 类(首字母大写的set)*/ global Monster = { health = 100, strength = 0.4, speed = 20, name = null, attack = func(self, other) { other.health -= self.strength; }, ['escape'] = func(self) { speed -= 10; }, ["init"] = func(self, _health, _speed, _strength) { self.speed = _speed; self.health = _health; }, }; /* 继承 */ Bat = new Monster; Bat += { fly = 20, wings = true, create = func(_name, _speed, _wings) { local a = new Bat; a:init(_name, _speed, _wings); return a; } }; /* 或者下面这样,可以实现多重继承 Bat = Monster + { fly = 20, wings = true, } + Foo; Bat = Monster + Foo + { fly = 20, wings = true, init = func(self, _name, _speed, _wings) { //... } }; */ bat = new Bat; // 语法糖,把调用者作为第一个参数压入栈 bat:init(1, 2, 3); func newBat(_name, _speed, _wings) { local a = new Bat; a:init(_name, _speed, _wings); return a; } local bat1 = newBat(1, 2, 3); local bat2 = Bat.create(1, 2, 3); func test() { a = 13; c = { c_1 = 12, c_2 = 13, c_3 = func() { }, ['c_4'] = func(obj){ obj.c_1 = 12 }, ["c_5"] = func(msg){ print(msg) } }; d = new c; e = c; d["c_4"] = "d_c_4"; d["c_5"]("function test"); d.c_5("function test"); /* 数组,默认无key的set */ /* 如果第一个数据没有key,则set成为array */ /* array从0开始索引 */ arr = {1, 2, 2, 3, 5, 2}; /* set 和 array 不可以用+拼接 */ /* 字符串拼接 */ str = "hello " + "world"; f = {}; } func main() { local image = gl.createTexture(); global canvas = gl.createCanvas(); } main() func instanceof(Class, obj) { return obj._class == Class._class } Object = { _class = {"Object"}, instanceof = func(self, Class) { return array.contain(self._class, Class._class); //return string.contain(self._class, Class._class); //return self._class == Class._class; } }; Box = Object + { _class = {"Box"}, }; box = new Box; if(box:instanceof(Object)) { } // 为了解决重复拷贝函数的问题,采用一个set封装这些函数 // set\array变量赋值的时候不会拷贝,而是引用同一个地址 Bird = { _method = { fly = func(self) { // .... } } } bird = new Bird; bird._method.fly(bird); // 考虑引入unique关键字,声明变量的时候,不会拷贝副本,而是拷贝地址 Bird = { unique fly = func(self){ // .... } } bird = new Bird; bird:fly(); // 或者限制函数只能拷贝地址 // 枚举和mask会在编译时被替换为数字 // 枚举 local /* global */enum { STAT_BOY = 01, STAT_MAN = 010, STAT_WOMAN = 0100, STAT_TALL = 01000, STAT_SMART = 010000, } // 等价于下面的 // 掩码 local /* global */mask { STAT_BOY , STAT_MAN , STAT_WOMAN , STAT_TALL , STAT_SMART , }