summaryrefslogtreecommitdiff
path: root/test/hello.lls
blob: 105aa0aa1dc2d3a7661189da4a224dfb36b34726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
基于栈的虚拟机
lls本身并不提供除了
  * include(<filepath>)
  * typeof(<variable>)
  
的函数,全部需要用户手动绑定。官方提供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 
	""; 
	
*/
/*
可以成为语句:
	赋值、

不可成为语句:
	<variable>; 
*/
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 ,
}