1 // This is a one line comment. 2 /* an inner comment */ 3 4 /* nested /* comments */ */ 5 6 /* 7 /* 8 Multi-line. 9 */ 10 */ 11 12// Binary blob escape. 13//"some text \B(3)("\") ouhyeah" == "\"\\\""; 14"some text \B(3)("\") ouhyeah" == "\"\\\""; 15'some text \B(3)('\') ouhyeah' == '\'\\\''; 16 17//"\B(4)()"'()"; 18"\B(4)()"'()"; 19'\B(4)()'"()'; 20 21//blob size limits 22"hey ! \B(0)() oh !" 23 24//blob format is wrong 25"hey ! \B(2)(aaa) oh !" 26"hey ! \B(100)(aaa) oh !" 27 28//multiple blob in a string 29"hey ! \B(3)(aaa) hey ! \B(3)(aaa) oh !" 30 31// multiple digits blob size 32"hey ! \B(10)(aaaaaaaaaa) !" 33"hey ! \B(10)(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) !" 34"hey ! \B(100)(a) !" 35 36// multiple digits blob size 37"hey ! \B(007)(aaaaaaa) !" 38"hey ! \B(007)(aa) !" 39"hey ! \B(007)(aaaaaaaaaaaaaaaaaa) !" 40 41// deprecated and restricted keyworks 42emit Event.new; 43static int main(); 44 45loopn (2) {echo("a");}; 46 47foreach (var i : [1,2,3,4]) { 48 echo(i); 49}; 50 51function() {}; 52 53var 'if'; 54var this.'else'; 55 56var '%x'; 57var '1 2 3'; 58var this.'[]'; 59 60// angles 61pi == 180deg; 62pi == 200grad; 63 64// Dictionary 65[ => ]; // The empty dictionary 66 67// duration 681d == 24h; 690.5d == 12h; 701h == 60min; 711min == 60s; 721s == 1000ms; 73 741s == 1; 751s 2s 3s == 6; 761s 1ms == 1.001; 771ms 1s == 1.001; 78 79 80 1 == 1; 81 1 == 1.0; 82 1.2 == 1.2000; 83 1.234e6 == 1234000; 84 1e+11 == 1E+11; 85 1e10 == 10000000000; 86 1e30 == 1e10 * 1e10 * 1e10; 87 88 890.000001; 90 910.0000001; 92 930.00000000001; 94 951e+3; 96 971E-5; 98 99 1001.; 101// [00004701:error] !!! syntax error: unexpected ; 102 103 0x2a == 42; 104 0x2A == 42; 105 0xabcdef == 11259375; 106 0xABCDEF == 11259375; 1070xFFFFFFFF == 4294967295; 108 109 110//123foo; 111//[00005658:error] !!! syntax error: invalid token: '123foo' 112//12.3foo; 113//[00018827:error] !!! syntax error: invalid token: '12.3foo' 1140xabcdef; 115//[00060432] 11259375 116//0xabcdefg; 117//[00061848:error] !!! syntax error: invalid token: '0xabcdefg' 118 119 120[]; // The empty list 121[1, 2, 3]; 122 123// Special characters. 124"\"" == "\""; 125"\\" == "\\"; 126 127// ASCII characters. 128"\a" == "\007"; "\a" == "\x07"; 129"\b" == "\010"; "\b" == "\x08"; 130"\f" == "\014"; "\f" == "\x0c"; 131"\n" == "\012"; "\n" == "\x0a"; 132"\r" == "\015"; "\r" == "\x0d"; 133"\t" == "\011"; "\t" == "\x09"; 134"\v" == "\013"; "\v" == "\x0b"; 135 136// Octal escapes. 137"\0" == "\00"; "\0" == "\000"; 138"\0000" == "\0""0"; 139"\062\063" == "23"; 140 141// Hexadecimal escapes. 142"\x00" == "\0"; 143"\x32\x33" == "23"; 144 145 146 147"foo" "bar" "baz" == "foobarbaz"; 148 149// Tuples 150(); 151[00000000] () 152(1,); 153[00000000] (1,) 154(1, 2); 155[00000000] (1, 2) 156(1, 2, 3, 4,); 157[00000000] (1, 2, 3, 4) 158 159function Global.verboseId(var x) 160{ 161 echo(x) | x 162}|; 163class verboseId(Global).math : verboseId(Math) 164{ 165}; 166 167{ 168 for (3) 169 { 170 sleep(1s); 171 echo("ping"); 172 }, 173 sleep(0.5s); 174 for (3) 175 { 176 sleep(1s); 177 echo("pong"); 178 }, 179}; 180 181 1 + 1 == 2; 182 1 - 2 == -1; 183 2 * 3 == 6; 184 10 / 2 == 5; 185 2 ** 10 == 1024; 186 -(1 + 2) == -3; 187 1 + 2 * 3 == 7; 188 (1 + 2) * 3 == 9; 189 -2 ** 2 == -4; 190 - - - - 1 == 1; 191 192a = b 193a += b 194a -= b 195a *= b 196a /= b 197a %= b 198a ^= b 199 200 201var value = 0|; 202var valueAlias = value|; 203value += 10; 204valueAlias; 205var myList = []|; 206var myList.specialFeature = 42|; 207myList += [1, 2, 3]; 208myList.specialFeature; 209var myOtherList = myList + [4, 5]; 210myOtherList.specialFeature; 211var something = []|; 212var somethingElse = something|; 213something += [1, 2]; 214somethingElse += [3, 4]; 215something; 216 217 218class Counter 219{ 220 var count = 0; 221 function init (n) { var this.count = n }; 222 // Display the value, and the identity. 223 function asString() { "%s @ %s" % [count, uid ] }; 224 function '+'(var n) { new(count + n) }; 225 function '-'(var n) { new(count - n) }; 226}|; 227 228 229class ImmutableCounter : Counter 230{ 231 function '+='(var n) { this + n }; 232 function '-='(var n) { this - n }; 233}|; 234 235var ic1 = ImmutableCounter.new(0); 236var ic2 = ic1; 237 238ic1 += 1; 239ic1; 240ic2; 241 242 243a << b 244a >> b 245a ^ b 246 2474 << 2 == 16; 2484 >> 2 == 1; 249 250!a 251a && b 252a || b 253 254true && true; 255true || false; 256!true == false; 257true || (1 / 0); 258(false && (1 / 0)) == false; 259 260a == b 261a != b 262a === b 263a !== b 264a ~= b 265a =~= b 266a < b 267a <= b 268a > b 269a >= b 270 271assert{ 272 ! (0 < 0); 273 0 <= 0; 274 0 == 0; 275 0 !== 0; 276}; 277 278a in b 279a not in b 280a[args] 281a[args] = v 282 2831 in [0, 1, 2]; 2843 not in [0, 1, 2]; 285 286"one" in ["zero" => 0, "one" => 1, "two" => 2]; 287"three" not in ["zero" => 0, "one" => 1, "two" => 2]; 288 289a.b 290a.b(args) 291a->b 292a->b = v 293a.&b 294 295var obj = Object.new|; 296function obj.f() { 24 }|; 297 298 299var f = function(a, b) { 300 echo(b + a); 301}| 302f(1, 0); 303 304 305function g3() 306{ 307 return; // Stop execution at this point and return void 308 echo(0); // This is not executed 309}| 310 311Object.setProperty, to define/set a property. 312Object.getProperty, to get a property. 313Object.removeProperty, to delete a property. 314Object.hasProperty, to test for the existence of a property. 315Object.properties, to get all the properties of a slot. 316 317enum Suit 318{ 319 hearts, 320 diamonds, 321 clubs, 322 spades, // Last comma is optional 323}; 324 325for (var suit in Suit) 326 echo("%s the ace of %s." % [find_ace(suit), suit]); 327 328switch ( ("foo", [1, 2]) ) 329{ 330 // The pattern does not match the values of the list. 331 case ("foo", [2, 1]): 332 echo("fail"); 333 334 // The pattern does not match the tuple. 335 case ["foo", [1, 2]]: 336 echo("fail"); 337 338 // The pattern matches and binds the variable "l" 339 // but the condition is not verified. 340 case ("foo", var l) if l.size == 0: 341 echo("fail"); 342 343 // The pattern matches. 344 case ("foo", [var a, var b]): 345 echo("foo(%s, %s)" % [a, b]); 346}; 347//[00000000] *** foo(1, 2) 348 349{ 350 ["b" => var b, "a" => var a] = ["a" => 1, "b" => 2, "c" => 3]; 351 echo("a = %d, b = %d" % [a, b]); 352}; 353//[00000000] *** a = 1, b = 2 354 355 356switch (["speed" => 2, "time" => 6s]) 357{ 358 case ["speed" => var s] if s > 3: 359 echo("Too fast"); 360 case ["speed" => var s, "time" => var t] if s * t > 10: 361 echo("Too far"); 362}; 363//[00000000] *** Too far 364 365 366try 367{ 368 throw ("message", 0) 369} 370catch (var e if e.isA(Exception)) 371{ 372 echo(e.message) 373} 374catch ((var msg, var value) if value.isA(Float)) 375{ 376 echo("%s: %d" % [msg, value]) 377}; 378//[00000000] *** message: 0 379 380 381{ 382 var e = Event.new; 383 at (e?(var msg, var value) if value % 2 == 0) 384 echo("%s: %d" % [msg, value]); 385 386 // Does not trigger the "at" because the guard is not verified. 387 e!("message", 1); 388 389 // Trigger the "at". 390 e!("message", 2); 391}; 392//[00000000] *** message: 2 393 394for (var i = 0; i < 8; i++) 395{ 396 if (i % 2 != 0) 397 continue; 398 echo(i); 399}; 400 401do (1024) 402{ 403 assert(this == 1024); 404 assert(sqrt == 32); 405 setSlot("y", 23); 406}.y; 407 408{ 409 var n = 10|; 410 var res = []|; 411 loop;{ 412 n--; 413 res << n; 414 if (n == 0) 415 break 416 }; 417 res 418} 419 420 421{ 422 var n = 10|; 423 var res = []|; 424 loop|{ 425 n--; 426 res << n; 427 if (n == 0) 428 break 429 }; 430 res 431} 432 433 434var j = 3| 435while (0 < j) 436{ 437 echo(j); 438 j--; 439}; 440 441 442{ 443 var i = 4| 444 while| (true) 445 { 446 i -= 1; 447 echo ("in: " + i); 448 if (i == 1) 449 break 450 else if (i == 2) 451 continue; 452 echo ("out: " + i); 453 }; 454}; 455 456 457 458function test(e) 459{ 460 try 461 { throw e; } 462 catch (0) 463 { echo("zero") } 464 catch ([var x, var y]) 465 { echo(x + y) } 466} | {}; 467 468try { echo("try") } 469catch { echo("catch")} 470else { echo("else")}; 471 472 473try 474{ 475 echo("inside"); 476} 477finally 478{ 479 echo("finally"); 480}; 481//[00000001] *** inside 482//[00000002] *** finally 483 484at (e?(var start) ~ 1s) 485 echo("in : %s" % (time - start).round) 486onleave 487 echo("out: %s" % (time - start).round); 488 489// This emission is too short to trigger the at. 490e!(time); 491 492// This one is long enough. 493// The body triggers 1s after the emission started. 494e!(time) ~ 2s; 495//[00001000] *** in : 1 496//[00002000] *** out: 2 497 498 499timeout (2.1s) 500 every (1s) 501 echo("Are you still there?"); 502//[00000000] *** Are you still there? 503//[00001000] *** Are you still there? 504//[00002000] *** Are you still there? 505 506 every| (1s) 507 { 508 echo("aba"); 509 }; 510 511for, (var i = 3; 0 < i; i -= 1) 512{ 513 echo (i); 514}; 515 516 517for& (var i: [0, 1, 2]) 518{ 519 echo (i * i); 520}; 521 522loop,{ 523}; 524 525 526waituntil (e?(1, var b)); 527 528whenever (e?("arg", var arg) if arg % 2) 529 echo("e (%s) on" % arg) 530else 531 echo("e off"); 532 533 534 while, (i) 535 { 536 var j = i -= 1; 537 }| 538 539 540var y = 0; 541{ 542 sleep(0.5s); 543 y = 100 smooth:3s, 544}, 545 546 547 548