mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 06:44:07 +10:00
Also fixed a minor bug with printing, and removed the ability to configure the parser. Added and updated QUICKSTART.md as a quick way to get people started. There's some broken scripts under 'scripts/' that require functions to work properly.
161 lines
2.8 KiB
Plaintext
161 lines
2.8 KiB
Plaintext
//declare a variable with an initial value
|
|
var answer = 42;
|
|
|
|
//declare a variable without an initial value
|
|
var empty;
|
|
|
|
//assign a previously existing variable
|
|
answer = 6 * 9;
|
|
|
|
//access a variable
|
|
answer = answer + 1;
|
|
|
|
//compound assignments
|
|
answer += 5;
|
|
answer -= 5;
|
|
answer *= 9;
|
|
answer /= 2;
|
|
answer %= 10;
|
|
|
|
//equality checks
|
|
print 1 == 1; //true
|
|
print 1 != 1; //false
|
|
|
|
//comparison checks
|
|
print 1 < 2; //true
|
|
|
|
print "foo" > "bar"; //true
|
|
|
|
print 1 < 2; //true
|
|
print 1 > 2; //false
|
|
|
|
print 2 <= 2; //true
|
|
print 2 >= 2; //true
|
|
|
|
print 1 <= 2; //true
|
|
print 1 >= 2; //false
|
|
|
|
//logical checks
|
|
print true && true; //true
|
|
print true && false; //false
|
|
print false && true; //false
|
|
print false && false; //false
|
|
|
|
print true || true; //true
|
|
print true || false; //true
|
|
print false || true; //true
|
|
print false || false; //false
|
|
|
|
print !true; //false
|
|
print !false; //true
|
|
|
|
//logical AND short-circuits and chained assignments
|
|
{
|
|
var a = 1;
|
|
var b = 2;
|
|
var c = a + 1 && b + 2;
|
|
|
|
assert a == 1, "short circuit 1.1";
|
|
assert b == 2, "short circuit 1.2";
|
|
assert c == 4, "short circuit 1.3";
|
|
}
|
|
|
|
{
|
|
var a = 1;
|
|
var b = 2;
|
|
var c = a = (a + 1) && b + 2;
|
|
|
|
assert a == 4, "short circuit 2.1";
|
|
assert b == 2, "short circuit 2.2";
|
|
assert c == 4, "short circuit 2.3";
|
|
}
|
|
|
|
{
|
|
var a = 1;
|
|
var b = 2;
|
|
var c = a = a + 1 && b + 2;
|
|
|
|
assert a == 4, "short circuit 3.1";
|
|
assert b == 2, "short circuit 3.2";
|
|
assert c == 4, "short circuit 3.3";
|
|
}
|
|
|
|
//logical OR short-circuits and chained assignments
|
|
{
|
|
var a = 1;
|
|
var b = 2;
|
|
var c = a + 1 || b + 2;
|
|
|
|
assert a == 1, "short circuit 4.1";
|
|
assert b == 2, "short circuit 4.2";
|
|
assert c == 2, "short circuit 4.3";
|
|
}
|
|
|
|
{
|
|
var a = 1;
|
|
var b = 2;
|
|
var c = a = (a + 1) || b + 2;
|
|
|
|
assert a == 2, "short circuit 5.1";
|
|
assert b == 2, "short circuit 5.2";
|
|
assert c == 2, "short circuit 5.3";
|
|
}
|
|
|
|
{
|
|
var a = 1;
|
|
var b = 2;
|
|
var c = a = a + 1 || b + 2;
|
|
|
|
assert a == 2, "short circuit 6.1";
|
|
assert b == 2, "short circuit 6.2";
|
|
assert c == 2, "short circuit 6.3";
|
|
}
|
|
|
|
//types
|
|
{
|
|
var a: int;
|
|
var b: int = 42;
|
|
|
|
a = 69;
|
|
b = 8891;
|
|
|
|
print a;
|
|
print b;
|
|
}
|
|
|
|
//constants
|
|
{
|
|
var c: int const = 42;
|
|
print c;
|
|
}
|
|
|
|
//indexing
|
|
{
|
|
var s = "Hello" .. "world!";
|
|
print s[3, 3];
|
|
}
|
|
|
|
//increment & decrement (prefix)
|
|
{
|
|
var a = 42;
|
|
assert a == 42, "prefix increment & decrement 1.1";
|
|
assert ++a == 43, "prefix increment & decrement 1.2";
|
|
assert a == 43, "prefix increment & decrement 1.3";
|
|
assert --a == 42, "prefix increment & decrement 1.4";
|
|
assert a == 42, "prefix increment & decrement 1.5";
|
|
}
|
|
|
|
//increment & decrement (postfix)
|
|
{
|
|
var a = 42;
|
|
assert a == 42, "postfix increment & decrement 1.1";
|
|
assert a++ == 42, "postfix increment & decrement 1.2";
|
|
assert a == 43, "postfix increment & decrement 1.3";
|
|
assert a-- == 43, "postfix increment & decrement 1.4";
|
|
assert a == 42, "postfix increment & decrement 1.5";
|
|
|
|
print a;
|
|
}
|
|
|
|
//TODO: type casting
|