From 6a38600ce88bbf2d9fa9a3821d7df3ebd8e0d4f3 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 30 Aug 2021 19:15:05 +0300 Subject: 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 --- users/tazjin/rlox/examples/builtins.lox | 1 + users/tazjin/rlox/examples/fib.lox | 6 ++++++ users/tazjin/rlox/examples/func.lox | 5 +++++ users/tazjin/rlox/examples/hello.lox | 34 +++++++++++++++++++++++++++++++++ users/tazjin/rlox/examples/scope.lox | 19 ++++++++++++++++++ users/tazjin/rlox/examples/scope2.lox | 10 ++++++++++ users/tazjin/rlox/examples/slow.lox | 9 +++++++++ users/tazjin/rlox/examples/var.lox | 8 ++++++++ 8 files changed, 92 insertions(+) create mode 100644 users/tazjin/rlox/examples/builtins.lox create mode 100644 users/tazjin/rlox/examples/fib.lox create mode 100644 users/tazjin/rlox/examples/func.lox create mode 100644 users/tazjin/rlox/examples/hello.lox create mode 100644 users/tazjin/rlox/examples/scope.lox create mode 100644 users/tazjin/rlox/examples/scope2.lox create mode 100644 users/tazjin/rlox/examples/slow.lox create mode 100644 users/tazjin/rlox/examples/var.lox (limited to 'users') diff --git a/users/tazjin/rlox/examples/builtins.lox b/users/tazjin/rlox/examples/builtins.lox new file mode 100644 index 000000000000..39af1d73c4c9 --- /dev/null +++ b/users/tazjin/rlox/examples/builtins.lox @@ -0,0 +1 @@ +print clock(); diff --git a/users/tazjin/rlox/examples/fib.lox b/users/tazjin/rlox/examples/fib.lox new file mode 100644 index 000000000000..1b91e9db94ac --- /dev/null +++ b/users/tazjin/rlox/examples/fib.lox @@ -0,0 +1,6 @@ +fun fib(n) { + if (n <= 1) return n; + return fib(n - 2) + fib(n - 1); +} + +print fib(30); diff --git a/users/tazjin/rlox/examples/func.lox b/users/tazjin/rlox/examples/func.lox new file mode 100644 index 000000000000..d197ad11383f --- /dev/null +++ b/users/tazjin/rlox/examples/func.lox @@ -0,0 +1,5 @@ +fun foo(name) { + print("hello " + name); +} + +foo("bar"); diff --git a/users/tazjin/rlox/examples/hello.lox b/users/tazjin/rlox/examples/hello.lox new file mode 100644 index 000000000000..31752d9e2f5e --- /dev/null +++ b/users/tazjin/rlox/examples/hello.lox @@ -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"; diff --git a/users/tazjin/rlox/examples/scope.lox b/users/tazjin/rlox/examples/scope.lox new file mode 100644 index 000000000000..d563807943ba --- /dev/null +++ b/users/tazjin/rlox/examples/scope.lox @@ -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; diff --git a/users/tazjin/rlox/examples/scope2.lox b/users/tazjin/rlox/examples/scope2.lox new file mode 100644 index 000000000000..f826c8658803 --- /dev/null +++ b/users/tazjin/rlox/examples/scope2.lox @@ -0,0 +1,10 @@ +var a = "global"; +{ + fun showA() { + print a; + } + + showA(); + var a = "block"; + showA(); +} diff --git a/users/tazjin/rlox/examples/slow.lox b/users/tazjin/rlox/examples/slow.lox new file mode 100644 index 000000000000..dd6fb5e4bf4d --- /dev/null +++ b/users/tazjin/rlox/examples/slow.lox @@ -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; diff --git a/users/tazjin/rlox/examples/var.lox b/users/tazjin/rlox/examples/var.lox new file mode 100644 index 000000000000..133906ad1b2d --- /dev/null +++ b/users/tazjin/rlox/examples/var.lox @@ -0,0 +1,8 @@ +{ + +{ + var a = 5; + print a; +} + +} -- cgit 1.4.1