// EXPRESSION assert 1 > 0 assert system.true assert 1 + 2 * 2 == 5 assert 10 % 3 == 1 assert 10 / 3 == 3 assert 'o' | 'ne' | ' tw' | 'o' == 'one two' assert 'three' | ' four' == 'three four' assert not true == false assert not 1 == -2 assert not (1 == 1 and 2 == 1) assert 1 == 1 and 2 == 2 and 3 == 2 + 1 and 'a' == 'a' assert true or false assert not (false or false) assert (1 xor 3) == 2 assert (1 or 2) == 3 // DEFINITIONS def type nums = (one, two, three) // error: def nums badnum = 5 as nums def dow = (Mon, Tue, Wed, Thu, Fri, Sat, Sun) def intvec = int *[] def numvec = int *[nums] def charset = void *^[char] def matrix0 = int *[str]^[int] def type charset2 = void *[char] def CharIntMap = int *[char] def charfifo = char *<> def sign = -1..1 // CONSTANTS def n0 = two def s0 = 'abc' def s1 = '' def i0 = 2 * 3 + 4 def v1 = 'a' | 'b' def v2 = 'abc' | 'def' | 'g' def v3 = 3 | 4 def v4 = [] def v5 = [5] def v5a = v5 def v6 = [5, 6] def vnull = [] def int *[] v7 = [] def v8 = v7 | 10 | 11 def v9 = 12 | v4 | 13 | vnull def sign b0 = -1 def t0 = {1} def t1 = {1, 5, 6, 7, 10} def t3 = {} def identChars = {'A'..'Z', 'a'..'z', '0'..'9', '_'} def t4 = {(5 as byte)..15, 17} def t5 = {two..three} def d0 = {'one' = 1, 'two' = 2} def d1 = {'a' = 'Letter A', 'b' = 'Letter B'} def byte cbv[] = [0, 1] | 2 | 3 | [] | [4] assert true ; assert s0 == 'abc' assert i0 == 10 assert v5 == v5a assert v1 == 'ab' and len(v1) == 2 assert v2[0] == 'a' and v2[1] == 'b' and v2[3] == 'd' and v2[6] == 'g' assert v2.len() == 7 assert -len(v2) == -7 assert len(v4) == 0 and vnull.len() == 0 assert v6[0] == 5 and 6 == v6[1] and len(v6) == 2 assert v8[0] == 10 and v8[1] == 11 assert v9[0] == 12 and v9[1] == 13 assert d0['one'] == 1 and d0['two'] == 2 assert d1['a'] == 'Letter A' and d1['b'] == 'Letter B' assert i0? and v5? and n0? and not one? and not false? and not s1? and v2? assert 'a' in identChars and not '?' in identChars assert (int *[] *[str]) == (intvec *[str]) assert lo(v1) == 0 and v1.hi() == 1 assert lo(char) == 0 and char.hi() == 255 assert [].hi() == -1 def cc1 = 1 def cc2 = __result.cc1 assert cc2 == 1 // ASSIGNMENTS, DEL, INS var a = 2 var int b = 1 assert a == 2 and b == 1 var char ch1 = 'z' var c = 'abcd' var d = [1, 2, 3] var (byte *[][]) e = [[], [1, 2, 3], [4, 5]] var int *[][] er = [[6, 7], [8, 9, 10], []] var byte ei[] = [0, 1] | 2 | 3 | [] | [4] var int*^ r1 = 2 var r2 = @3 var str r3^ = 'abc' var typeof r1 r4 = 10 a = r1 assert a == 2 r1 = 12 assert r1 == 12 @r2 = r1 r1 = 1 assert r2 == 1 assert len(d) == 3 del d[1] assert len(d) == 2 and d[0] == 1 and d[1] == 3 // error: del d[2] del r3[0] assert r3 == 'bc' def int r5^ = 4 // error: r5 = 5 // error: @r5 = 5 var str s3 = 'XYZxyz' del s3[0..1] assert s3 == 'Zxyz' del s3[2..] assert s3 == 'Zx' ins s3[1] = 'a' assert s3 == 'Zax' ins s3[3] = 'bc' assert s3 == 'Zaxbc' ins s3[1..1] = 'so' assert s3 == 'Zsoxbc' ins s3[2..3] = 'p' assert s3 == 'Zspbc' ins s3[3..] = 'def' assert s3 == 'Zspdef' ins s3[2..4] = '' assert s3 == 'Zsf' a = 3 a += 1 assert a == 4 a -= 2 assert a == 2 a *= 6 assert a == 12 a /= 2 assert a == 6 a %= 4 assert a == 2 var inpcat = 'ab' inpcat |= 'c' assert inpcat == 'abc' inpcat |= 'de' assert inpcat == 'abcde' var inpcat2 = [1] inpcat2 |= 2 assert len(inpcat2) == 2 and inpcat2[1] == 2 inpcat2 |= [3, 4] assert len(inpcat2) == 4 and inpcat2[3] == 4 var v11 = ['abc', 'def', 'ghi', 'jkl', 'mno'] del v11[1..2] assert len(v11) == 3 and v11[0] == 'abc' and v11[1] == 'jkl' and v11[2] == 'mno' del v11[1..] assert len(v11) == 1 and v11[0] == 'abc' ins v11[0] = ['pqr', 'stu'] assert len(v11) == 3 and v11[0] == 'pqr' and v11[1] == 'stu' and v11[2] == 'abc' ins v11[len(v11)] = 'vwx' assert len(v11) == 4 and v11[0] == 'pqr' and v11[1] == 'stu' and v11[2] == 'abc' \ and v11[3] == 'vwx' ins v11[1..] = ['def', 'ghi'] assert len(v11) == 3 and v11[0] == 'pqr' and v11[1] == 'def' \ and v11[2] == 'ghi' ins v11[2..2] = [] assert len(v11) == 2 and v11[0] == 'pqr' and v11[1] == 'def' assert typeof c == str and typeof d == (int *[]) and typeof e == byte *[][] \ and typeof er == int *[][] var void words[str] = {'as', 'is'} var void chars1[char] = {'a'..'z'} var dic1 = {'one' = 1, 'two' = 0, 'three' = 3} var byte dic2[str] = {'o' = 1, 'two' = 0, 'three' = 3} var int ints1[]^[] = [[], [10, 11, 12], @[13, 14], @[]] var int dics1[][str] = [dic1, {}] var int dics2[]^[str] = [dic1, {'e' = 0}, {}] var int dic3[char] = {'a' = 10, 'b' = 20} // Should give range check errors: var char dic4[0..2] = {0 = 'a', 2 = 'c', 255 = 'd'} var void set2[0..2] = {-1, 0..2, 3, 4, 10} assert typeof dic1 == int *[str] and typeof dic1['www'] == int and \ typeof ints1[100] == int *[] and \ typeof ints1[1 + 2 * 3][4 + 5 * 6] == int a = 10 + b assert a == 11 assert len(e[0]) == 0 and e[1][1] == 2 and e[2][0] == 4 assert len(ei) == 5 and ei[1] == 1 assert dic1['one'] == 1 and dic1['two'] == 0 system.__program_result = 'OK' assert __program_result == 'OK' __program_result = null c[1] = ':' assert c == 'a:cd' (c[1 + 1]) = '$' assert c == 'a:$d' var didx = 3 d[12 - didx * 4] = 4 assert d[0] == 4 // error: e[0][1] = 6 // error: 1 = 2 // error: s0 = '' assert dic1['one'] == 1 and dic1['two'] == 0 dic1['two'] = 2 assert dic1['one'] == 1 and dic1['two'] == 2 ints1[1][2] = 111 assert ints1[1][2] == 111 dics2[0]['three'] = 33 assert dics2[0]['three'] == 33 assert 'as' in words and not 'kuku' in words assert dic2['o'] == 1 and dic2['two'] == 0 and dic2['three'] == 3 dic2['two'] = 2 assert 'two' in dic2 and dic2['two'] == 2 del dic2['two'] assert not 'two' in dic2 assert not 'z' in dic3 and 'a' in dic3 and dic3['a'] == 10 dic3['z'] = 30 assert 'z' in dic3 and dic3['z'] == 30 and dic3['b'] == 20 del dic3['z'] del dic3['a'] assert not 'a' in dic3 and not 'z' in dic3 assert 'as' in words and 'is' in words del words['is'] assert not 'is' in words assert typeof words['as'] == void assert 'a' in chars1 del chars1['a'] assert not 'a' in chars1 assert 2 in nums and 96 in char and not 256 in char assert 1 in 0..2 and ints1[1][2] in 110..111 and 10 in 0..a and not 12 in 0..a assert if(true, true, false) and if(a in 10..1000, 'abc', 'def') == 'abc' var rng1 = [0..999] var rng2 = rng1 assert 1 in rng1 and not -1 in rng1 and not 1000 in rng1 assert 1 in 0..10 and 1 in [0..10] assert not -1 in 0..10 and not -1 in [0..10] assert rng1 == [0..999] assert 1 in rng2 assert rng1.hi() == 999 and lo(rng2) == 0 var char rng3[..] = ['A'..'Z'] assert 'B' in rng3 def rng5 = [1..7] assert 1 in rng5 var str1 = 'abc' assert str1[0..1] == 'ab' and str1[0..2] == 'abc' and str1[2..2] == 'c' assert str1[0..] == 'abc' and str1[1..] == 'bc' and str1[3..] == '' var vec1 = ['abc', 'def', 'ghi'] assert len(vec1[0..1]) == 2 and vec1[0..1][0] == 'abc' and vec1[0..1][1] == 'def' assert len(vec1[1..]) == 2 and vec1[1..][1] == 'ghi' assert vec1[0..] == vec1 var void ncont1()... = {} assert 1._str() == '1' assert {'A'..'Z'}._str() == '{\'A\'..\'Z\'}' assert rng1._str() == '[0..999]' var char rng4[..] = [] assert not 'a' in rng4 // exception: dump rng4.lo() var char ref4^ = [] assert (ref4 as int) == 0 var (void *()...) funcp4 = [] // exception: funcp4() // TYPECASTS begin: assert (1 as char) == '\x01' begin: assert ('a' as int) == 97 var any v10 = 0 begin // Hm { assert (v10 as nums) == one assert (1 is system.int) and ('abc' is str) assert not (1 is str) and not ('abc' is int) assert (v10 is int) and not (v10 is str) assert v10 is any assert (v10 as nums) is nums begin { assert ('a' as str) == 'a' and (ch1 as str) == 'z' } var any anyv = [0, 1, 2] var int intv[] = anyv as int*[] assert intv[1] == 1 def enumv = int *[(hoo, haa, hee)] def enumv enumvv = {hoo = 1, haa = 0, hoo = 3} assert typeof enumvv[hoo] == int } // exit 'Hmmmmmm' if true: ; else: assert false // LOCAL BLOCKS var b1 = 0 begin { var b1 = 1 assert b1 == 1 } begin { var b1 = 2 var b2 = 3 def s0 = 'xyz' def nums = (zero, one, two, three, four) assert b1 == 2 and b2 == 3 and s0 == 'xyz' and one == 1 as nums } assert b1 == 0 and s0 == 'abc' and one == 0 as nums // BRANCHING if b1 == 0: assert true if b1 != 0: assert false if true { var b1 = 4 assert b1 == 4 } assert b1 == 0 if b1 == 0: // Huh? assert true else: assert false if b1 != 0 { assert false } // Hmmmm else { // Hm? assert true } if b1 == 0: assert true elif b1 == 1: assert false elif b1 == 2 { // Hm... assert false; } else: assert false if b1 == 1: assert false elif b1 == 0: assert true elif b1 == 2: assert false else: assert false if b1 == 1: assert false elif b1 == 2: assert false elif b1 == 0: assert true else: assert false if b1 == 1: assert false elif b1 == 2: assert false elif b1 == 3: assert false else: assert true if b1 == 1: assert false elif b1 == 0: assert true elif b1 == 3: assert false switch b1 { case 1: assert false case 0 { assert true } default { assert false } } switch b1 { case 1 { assert false } case 0, 2: assert true default: assert false } switch b1 { case 0..2, 5: assert true case 1: assert false } switch b1 { case 1: assert false case 2 { assert false } default: assert true } switch s0 { case 'abc': assert true case 'xyz': assert false } switch typeof s0 { case str: assert true case int: assert false default { assert false } } // WHILE LOOP var wi = 0 while wi < 3 { wi = wi + 1 continue assert false } assert wi == 3 while wi < 3: assert false while wi < 6 { var wl1 = 'asd' begin { var wl2 = wi wi = wi + 1 continue } assert false } assert wi == 6 while wi < 100 { var wl1 = 'zxc' if wi == 8 { var wl2 = 'qwe' break assert false } wi = wi + 1 } assert wi == 8 // FOR LOOP var fori = 10 for i = 10..20 { assert fori == i fori += 1 } assert fori == 21 for i = 1..1: fori += 1 assert fori == 22 for i = 0..-1: assert false for i = one..three: fori += 1 assert fori == 25 for i = 'A'..'Z': fori += 1 assert fori == 51 var fors = 'ABCDEF' for i = fors { if i == 4: assert fors[i] == 'E' elif i == 5: assert fors[i] == 'F' fori += 1 } assert fori == 57 for i = '': assert false for i = []: assert false for i, j = 'GHIJKL' { // TODO: collect the string and compare if i == 4 { assert j == 'K' } fori += 1 } assert fori == 63 for i, j = '': assert false for i, j = [10, 20, 30] { if i == 1 { assert j == 20 } fori += 1 } assert fori == 66 var int fornullvec[] = [] for i, j = fornullvec: assert false for i, j = []: assert false for i = {'A'..'C'} { fori += 1 } assert fori == 69 for i = {}: assert false for i, j = {} { assert false; dump typeof j } for i = {one..three} { fori += 1 } assert fori == 72 for i = {'A' = 10, 'B' = 20} { fori += 1 } assert fori == 74 for i, j = {'A' = 10, 'B' = 20} { fori += 1 if i == 'B': assert j == 20 } assert fori == 76 for i = {1000, 2000, 3000} { fori += 1 } assert fori == 79 for i = {'one' = one, 'two' = two, 'three' = three} { fori += 1 } assert fori == 82 for i, j = {'one' = one, 'two' = two, 'three' = three} { if i == 'two': assert j == two fori += 1 } // STATES def proto1 = int *(int a, int b) ... var inc_call_count = 0 def inc = int *(int i) { __result = i + 1 inc_call_count = inc_call_count + 1 } var incv = int *(int i) { inc_call_count = inc_call_count + 1 return i + 1 // error: i = 0 } var rr1 = inc(5) assert rr1 == 6 assert inc_call_count == 1 inc(9) assert inc_call_count == 2 def void nested_inc_test() { var t = inc(10) assert t == 11 assert inc_call_count == 3 } nested_inc_test() incv(10) assert inc_call_count == 4 def confusing_proto = int *[]() { } var str2 = '' def void vfunc(str s) { str2 = s } def int avg(int a, int b, str s) { var t = wi assert t == wi str2 = s return (a + b) / 2 } vfunc('Hey') assert str2 == 'Hey' var rr2 = avg(5, 10, 'Um') assert rr2 == 7 assert str2 == 'Um' var loc2 = 1 begin { var loc1 = 0 def void vfunc() { } def void vfunc2() { assert this.loc2 == 1 assert loc2 == 1 // error: loc1 = 1 } vfunc2() loc2 = inc(loc2) } assert loc2 == 2 var int loc3^ = 3 def void vinc(int i^) { i = i + 1 } vinc(loc3) assert loc3 == 4 var statv = 1 def int nonstatf(): return statv assert nonstatf() == 1 def void shouldnt_be_statf(): assert nonstatf() == 1 shouldnt_be_statf() // Nested functions var vn1 = 1 def void nested1(int vn2) { var vn6 = __program_result var vn3 = 3 def void nested2(int vn4) { var vn5 = vn1 assert vn5 == 1 vn1 = 2 inc(1) } nested2(5) // this.vfunc(); } nested1(4) assert vn1 == 2 // Return def char rettest1() { var x = 1 begin { var y = 2 return 'a' } if x == 1: return } // Default args def void defarg1(int i, int j = 1, str k = 'hoohoo') { if j == 1: assert k == 'hoohoo' else: assert k == 'moomoo' } defarg1(0, 0, 'moomoo') defarg1(0, ,) defarg1(0, 1) defarg1(0) // Argument reclamation var argrec1 = 0 def void argrec(int a) { var a argrec1 = a } argrec(149) assert argrec1 == 149 // Function pointers var fpstr = '' def intfunc = int *(int, int)... def voidfunc = void *(str)... def int min(int a, int b) { return if(a < b, a, b) } def int max(int a, int b): __result = if(a > b, a, b) def void assignstr(str s) { fpstr = s } assert min(2, 1) == 1 var intfp = min var intfunc intfp1 = max assert intfp(20, 10) == 10 assert intfp1(20, 10) == 20 assignstr('klm') assert fpstr == 'klm' var voidfp = assignstr voidfp('nop') assert fpstr == 'nop' var cpint = 0 class intclass(int i) { var i cpint = i } def classptr = intclass *(int)... var classptr cp = intclass assert cpint == 0 var intclass ic = cp(1) assert cpint == 1 def incfunc = int *(int)... def incd = int *(int i) { return i + 1 } var incvar = int *(int i) { __result = i + 1 } assert incvar(100) == 101 assert incvar is incfunc // static call via an object assert __result.incd(2) == 3 // static call at compile time def ctcall = max(10, 20) assert ctcall == 20 // var args def void varf2(var int a): a = 2 var varfv2 = 1 varf2(varfv2) assert(varfv2 == 2) def void varf1(str a, var str b, str c) { b = a | c } var varf1v = 'd?' varf1('aa', varf1v, 'bb') assert(varf1v == 'aabb') // OOP var pst = 2 class point(int x, int y) { def cpt = 50 var x var y begin { var x = 0 } def void move(int dx, int dy) { x += dx y += dy } def void move2(int dx, int dy) { def int double(int i) { return i * pst; } move(double(dx), double(dy)) } def int pstatic(int): return 10 } var p = point(10, 20) var point p2 = point(100, 200) var point p3 = __result.point(1000, 2000) assert p.x == 10 and p.y == 20 assert p2.x == 100 and p2.y == 200 assert p3.x == 1000 and p3.y == 2000 assert p.cpt == 50 and p2.cpt == 50 def cpt = point.cpt assert cpt == 50 // error: var pmove = point.move // error: point.move(0, 1) var point pnull = {} assert not pnull? and p? p.move(5, 10) assert p.x == 15 and p.y == 30 // point.move(5, 5) p.move2(5, 10) assert p.x == 25 and p.y == 50 def static_point = point(1, 5) // error: assert static_point.x == 1 and static_point.y == 5 // error: static_point.move(1, 2) assert point.pstatic(0) == 10 assert p.pstatic(0) == 10 // FIFOs var char chfz<> = <> var chf = <'a', 'b', 'c'> // dump chfz, chf, sio assert not chfz? and chf? assert chf.deq() == 'a' and deq(chf) == 'b' chf.enq('d').enq('e') assert chf.deq() == 'c' and deq(chf) == 'd' and chf.deq() == 'e' assert not chf? chf << 'fgh' << 'i' << 'jk' assert chf.deq() == 'f' // "fifo empty" error: var deqv = chfz.deq() var strf = <'one', 'two', 'three'> assert strf.deq() == 'one' and strf.deq() == 'two' var chf2 = strfifo('Hello, FIFO!\nWell, hello, Hovik.\nHey! Howdy man?\nLike a FIFO') assert chf2.deq() == 'H' assert chf2.token({'a'..'z'}) == 'ello' chf2.skip({',', ' '}) assert chf2.deq() == 'F' assert chf2.token({'A'..'Z', '!'}) == 'IFO!' assert chf2.eol() chf2.skipln() assert chf2.line() == 'Well, hello, Hovik.' assert chf2.deq() == 'H' chf2.skipln() assert chf2.look() == 'L' assert chf2.line() == 'Like a FIFO' assert not chf2? var numf = assert numf.deq() == one and numf.deq() == three var numft = numf.token({three}) assert numft.len() == 1 and numft[0] == three dump system.__program_result