diff options
Diffstat (limited to 'test/class.ns')
-rw-r--r-- | test/class.ns | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/test/class.ns b/test/class.ns new file mode 100644 index 0000000..babe7ba --- /dev/null +++ b/test/class.ns @@ -0,0 +1,264 @@ +import math for Constant, math; +import io for io; // class io {...} +import gl; +import util; // util.qs or import "util.qs" +import module.common; // module/common/init.qs +import system; +import stm; + +// 不会被实例化的class倾向于用小写,用static class标记 +// static class io { ... } +// 用static 标记的class,里面的方法都是静态方法,不需要用static声明函数 + +var print = io.print; + +var glv = gl.version ; + +// 支持宏 +#if TEST + +#endif + +/* +* 16进制hex 0x +* 8进制oct 0c +* 2进制bin 0b +*/ +var n = 0x0f_ff_ff_ff; +n = 0c11_11_11_11; +n = 0b01_11_01_00; + +// false values: false, null, 0 + +class Test { + + function init(n) { + if( typeof(n) == "number") { + + } + self.name = ""; + self.test = n; + self.test2 = 0; + self._test = 0; + self._private = 10; + self._func = function () { + + } + } + + function foo() { + + } + + static function foo2() { + + } + + static foo3 = function() { + if(true){ + + } elif(false) { + + } else {} + } + + // release native resources here + function finalize() { + + } + + function + (right) { + return self.test + right.test + } + + function + () {} + + function - (right) { + if(right == null) { + // -test; + + } else { + // test - right + + } + } + + // ++和--只有后置一种 + function ++ (){ + self._test += 1; + } + + function -- () { + + } + + function () (param) { } + + function tostring() {} +} + +// 对于无参数函数,可以省去括号。这是为了实现 +// Test.instance.foo(); + +// 继承用 extends +class Test2 extends Test{ + static _instance = null; + static foo = 2; + + function init() { + base.init(); + } + + static function getInstance() { + if(_instance == null) + _instance = new Test2(); + return _instance; + } + + function finalize() { + base.finalize(); + } +} + +var Test2_ = Test2; // 类也是第一类值 + +var test = new Test2(); +print(typeof(test)); // Test2 +print(instanceof(test, Test1)); // true + +enum Color +{ + White = 0x1, + Red = 0x2, +}; + +//最多32个 +mask ColorMask +{ + White, + Red, +}; + +// event +// proxy + +var Say = new event(); +Say += function(content) { + IO.Print(content); +} + +var Say = new event(); + +var a = 10; +Say(a); // fire +Say = null; + +// no global variable + +class State { + static _foo = 10; +} + +// 三种集合 +// map \ list \ hash_set + +// map\list op +// + 并集 +// - 差集 +// * 交集 + +// map +var m = {}; +m = { + "asd" : 1, + "qwe" : null, + "embed" : { + "kll" : 2, + }, + 1 : 10, +}; + +map.add(m, "wwe", 19); +m["asd"] == m.asd; +m[1]; +var l = map.size(m); +map.join(); // m.join(); +map.intersect(); // m.intersect(); +map.differ(); // m.differ(); +map.has(m, "asd"); // true +map.contains(m, 1); // true + +foreach(var v in m) { + var key = v.key; + var value = v.value; +} + +// list +var a = []; +a = ["sdsd", 123]; +for(var i = 0; i < a.size(); ++i) { + var v = a[i]; +} +foreach(var v in a) { + var index = v.index; + var value = v.value; +} + +list.push(a, 10); // a.push(10); +list.pop(a); // a.pop(); +list.removeAt(a, 1); // a.removeAt(1); + +// hashset +var b = (); +b = (1, 2, 3, 4); +hashset.put(b, 5); + + +// raw string +//var str = r"asdasdasd"+"\"ok\""+ r"asdasd"; +var str = @" "@; +var str = @' '@; +var str = @| |@; + +// main entry + +function main() { + +} + +main(); + + +// bool +// int +// double + +// 默认全局的类型 +// class +// enum +// mask + +// 用internal将class\enum\mask内部化,只有本文件内可访问 + +internal class CFoo { + +} + +internal enum Type { + +} + +internal mask TypeMask { + +} + + +// upvalue +function foo() { + var a = 10; // upvalue + return function() { + return a++; + } +} +var f = foo(); +f(); |