chore(tazjin/rlox): Add some old code example files
Change-Id: I484b11069286ea2277e9e158fa5c3bd34f84c89e Reviewed-on: https://cl.tvl.fyi/c/depot/+/3464 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
parent
edd8680e87
commit
6a38600ce8
8 changed files with 92 additions and 0 deletions
1
users/tazjin/rlox/examples/builtins.lox
Normal file
1
users/tazjin/rlox/examples/builtins.lox
Normal file
|
@ -0,0 +1 @@
|
||||||
|
print clock();
|
6
users/tazjin/rlox/examples/fib.lox
Normal file
6
users/tazjin/rlox/examples/fib.lox
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
fun fib(n) {
|
||||||
|
if (n <= 1) return n;
|
||||||
|
return fib(n - 2) + fib(n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
print fib(30);
|
5
users/tazjin/rlox/examples/func.lox
Normal file
5
users/tazjin/rlox/examples/func.lox
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
fun foo(name) {
|
||||||
|
print("hello " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
foo("bar");
|
34
users/tazjin/rlox/examples/hello.lox
Normal file
34
users/tazjin/rlox/examples/hello.lox
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
var a = 12;
|
||||||
|
var b = a * 2;
|
||||||
|
|
||||||
|
{
|
||||||
|
var b = a * 3;
|
||||||
|
a = 42;
|
||||||
|
print b;
|
||||||
|
}
|
||||||
|
|
||||||
|
print a;
|
||||||
|
print b;
|
||||||
|
|
||||||
|
if (5 > 4)
|
||||||
|
print "it's true";
|
||||||
|
else
|
||||||
|
print "it's false";
|
||||||
|
|
||||||
|
if (false)
|
||||||
|
print "it's not true";
|
||||||
|
|
||||||
|
if (true and false)
|
||||||
|
print "won't happen";
|
||||||
|
|
||||||
|
if (true or false)
|
||||||
|
print "will happen";
|
||||||
|
|
||||||
|
var n = 5;
|
||||||
|
while (n > 0) {
|
||||||
|
print "counting down";
|
||||||
|
n = n - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(var i = 0; i < 10; i = i + 1)
|
||||||
|
print "bla";
|
19
users/tazjin/rlox/examples/scope.lox
Normal file
19
users/tazjin/rlox/examples/scope.lox
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
var a = "global a";
|
||||||
|
var b = "global b";
|
||||||
|
var c = "global c";
|
||||||
|
{
|
||||||
|
var a = "outer a";
|
||||||
|
var b = "outer b";
|
||||||
|
{
|
||||||
|
var a = "inner a";
|
||||||
|
print a;
|
||||||
|
print b;
|
||||||
|
print c;
|
||||||
|
}
|
||||||
|
print a;
|
||||||
|
print b;
|
||||||
|
print c;
|
||||||
|
}
|
||||||
|
print a;
|
||||||
|
print b;
|
||||||
|
print c;
|
10
users/tazjin/rlox/examples/scope2.lox
Normal file
10
users/tazjin/rlox/examples/scope2.lox
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
var a = "global";
|
||||||
|
{
|
||||||
|
fun showA() {
|
||||||
|
print a;
|
||||||
|
}
|
||||||
|
|
||||||
|
showA();
|
||||||
|
var a = "block";
|
||||||
|
showA();
|
||||||
|
}
|
9
users/tazjin/rlox/examples/slow.lox
Normal file
9
users/tazjin/rlox/examples/slow.lox
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
fun fib(n) {
|
||||||
|
if (n < 2) return n;
|
||||||
|
return fib(n - 1) + fib(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
var before = clock();
|
||||||
|
print fib(40);
|
||||||
|
var after = clock();
|
||||||
|
print after - before;
|
8
users/tazjin/rlox/examples/var.lox
Normal file
8
users/tazjin/rlox/examples/var.lox
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
|
||||||
|
{
|
||||||
|
var a = 5;
|
||||||
|
print a;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue