From 92e4a41662daa46ab0412e7c7663b5f1b2ea02fc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 26 May 2026 23:40:18 +1000 Subject: [PATCH] Renamed 'scripts' directory to 'examples' Also allowed assignment within conditionals --- README.md | 7 ++--- docs/src/README.md | 7 ++--- scripts/fib.toy => examples/fibonacci.toy | 2 -- {scripts => examples}/fizzbuzz.toy | 0 examples/hello_world.toy | 2 ++ {scripts => examples}/leapyear.toy | 3 +- examples/rng.toy | 10 +++++++ {scripts => examples}/tally.toy | 6 ++-- scripts/benchpress.toy | 16 ----------- scripts/blockhead.toy | 11 ------- scripts/for-theory.toy | 35 ----------------------- scripts/hello_world.toy | 21 -------------- scripts/randi.toy | 12 -------- source/toy_compiler.c | 12 ++++++++ 14 files changed, 34 insertions(+), 110 deletions(-) rename scripts/fib.toy => examples/fibonacci.toy (92%) rename {scripts => examples}/fizzbuzz.toy (100%) create mode 100644 examples/hello_world.toy rename {scripts => examples}/leapyear.toy (81%) create mode 100644 examples/rng.toy rename {scripts => examples}/tally.toy (72%) delete mode 100644 scripts/benchpress.toy delete mode 100644 scripts/blockhead.toy delete mode 100644 scripts/for-theory.toy delete mode 100644 scripts/hello_world.toy delete mode 100644 scripts/randi.toy diff --git a/README.md b/README.md index ee5e52e..29b47eb 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,9 @@ fn makeCounter() { var tally = makeCounter(); -while (true) { - var result = tally(); - - print result; //prints 1 to 10 +var result = 0; +while (result = tally()) { + print result; if (result >= 10) { break; diff --git a/docs/src/README.md b/docs/src/README.md index 5f47650..533f0c8 100644 --- a/docs/src/README.md +++ b/docs/src/README.md @@ -29,10 +29,9 @@ fn makeCounter() { var tally = makeCounter(); -while (true) { - var result = tally(); - - print result; //prints 1 to 10 +var result = 0; +while (result = tally()) { + print result; if (result >= 10) { break; diff --git a/scripts/fib.toy b/examples/fibonacci.toy similarity index 92% rename from scripts/fib.toy rename to examples/fibonacci.toy index 79e290d..c3b3f44 100644 --- a/scripts/fib.toy +++ b/examples/fibonacci.toy @@ -1,5 +1,3 @@ -//tentatively functional - //fibonacci sequence fn fib(n) { if (n < 2) return n; diff --git a/scripts/fizzbuzz.toy b/examples/fizzbuzz.toy similarity index 100% rename from scripts/fizzbuzz.toy rename to examples/fizzbuzz.toy diff --git a/examples/hello_world.toy b/examples/hello_world.toy new file mode 100644 index 0000000..7389fcd --- /dev/null +++ b/examples/hello_world.toy @@ -0,0 +1,2 @@ +//the semicolon is super important +print "Hello world!"; \ No newline at end of file diff --git a/scripts/leapyear.toy b/examples/leapyear.toy similarity index 81% rename from scripts/leapyear.toy rename to examples/leapyear.toy index 832d61e..30b0ad1 100644 --- a/scripts/leapyear.toy +++ b/examples/leapyear.toy @@ -1,11 +1,10 @@ -//find the leap years +//is 'n' a leap year fn isLeapYear(n: Int) { if (n % 400 == 0) return true; if (n % 100 == 0) return false; return n % 4 == 0; } -//check for string reuse { print isLeapYear(1999); } diff --git a/examples/rng.toy b/examples/rng.toy new file mode 100644 index 0000000..7773fba --- /dev/null +++ b/examples/rng.toy @@ -0,0 +1,10 @@ + +var randi: Int = 69420; +fn rand() { + //a quick and dirty random number generator + return randi = randi * 1664525 + 1013904223; +} + +print rand(); +print rand(); +print rand(); diff --git a/scripts/tally.toy b/examples/tally.toy similarity index 72% rename from scripts/tally.toy rename to examples/tally.toy index 7bf03b8..084a316 100644 --- a/scripts/tally.toy +++ b/examples/tally.toy @@ -8,11 +8,11 @@ fn makeCounter() { return increment; } +//'tally' becomes a closure var tally = makeCounter(); -while (true) { - var result = tally(); - +var result = 0; +while (result = tally()) { print result; if (result >= 10) { diff --git a/scripts/benchpress.toy b/scripts/benchpress.toy deleted file mode 100644 index 45c4b85..0000000 --- a/scripts/benchpress.toy +++ /dev/null @@ -1,16 +0,0 @@ -//calculate the nth fibonacci number, and print it - -var counter: Int = 0; - -var first: Int = 1; -var second: Int = 0; - -while (counter < 100_000) { - var third: Int = first + second; - first = second; - second = third; - - print third; - - ++counter; -} \ No newline at end of file diff --git a/scripts/blockhead.toy b/scripts/blockhead.toy deleted file mode 100644 index a0cecdb..0000000 --- a/scripts/blockhead.toy +++ /dev/null @@ -1,11 +0,0 @@ - -fn output(arg) { - print arg; -} - -var array = ["alpha", "bravo", "charlie"]; - - - -array.forEach(echo); -array.forEach(output); \ No newline at end of file diff --git a/scripts/for-theory.toy b/scripts/for-theory.toy deleted file mode 100644 index 94d494c..0000000 --- a/scripts/for-theory.toy +++ /dev/null @@ -1,35 +0,0 @@ -//WARN: This is just a scratch pad, don't use it -//TODO: table.hasValue or table.getKeyFromValue? - - -//for (var i in array) print i; -//for (var i in table) print i; -//for (var i in range(10)) print i; -//for (range(10)) print "ha"; - - -//example of a `range`-like function -fn range(limit: Int) { - var counter: Int = 0; - - fn next() { - if (counter >= limit) { - return null; - } - else return counter++; - } - - return next; -} - -var next = range(10); - - -fn log(x) { - if (x == null) return; - print x; -} - -while (true) { - log(next()); -} \ No newline at end of file diff --git a/scripts/hello_world.toy b/scripts/hello_world.toy deleted file mode 100644 index 296d960..0000000 --- a/scripts/hello_world.toy +++ /dev/null @@ -1,21 +0,0 @@ - - -var table = [ - "Alpha": 1, - "Bravo": 2, - "Charlie": 3, - "Delta": 4, - "Echo": 5, - "Foxtrot": 6, - "Golf": 7, - "Hotel": 8, - "India": 9, - "Juliett": 10, - "Kilo": 11, - "Lima": 12, - "Mike": 13, -]; - -for (var i in table) { - print i; -} \ No newline at end of file diff --git a/scripts/randi.toy b/scripts/randi.toy deleted file mode 100644 index 9014b34..0000000 --- a/scripts/randi.toy +++ /dev/null @@ -1,12 +0,0 @@ - - - -var randi: Int = 69420; -fn rand() { - return randi = randi * 1664525 + 1013904223; -} - - -var a = rand(); - - diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 21f76ca..af6079a 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -632,6 +632,12 @@ static unsigned int writeInstructionIfThenElse(Toy_Bytecode** mb, Toy_AstIfThenE //cond-branch writeBytecodeFromAst(mb, ast.condBranch); + //leave the assigned value on the stack when inside a condition + if (checkForChainedAssign(ast.condBranch)) { + Toy_AstVarAccess access = { .type = TOY_AST_VAR_ACCESS, .child = ast.condBranch->varAssign.target }; + writeInstructionAccess(mb, access); + } + //emit the jump word (opcode, type, condition, padding) EMIT_BYTE(mb, code, TOY_OPCODE_JUMP); EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE); @@ -677,6 +683,12 @@ static unsigned int writeInstructionWhileThen(Toy_Bytecode** mb, Toy_AstWhileThe //cond-branch writeBytecodeFromAst(mb, ast.condBranch); + //leave the assigned value on the stack when inside a condition + if (checkForChainedAssign(ast.condBranch)) { + Toy_AstVarAccess access = { .type = TOY_AST_VAR_ACCESS, .child = ast.condBranch->varAssign.target }; + writeInstructionAccess(mb, access); + } + //emit the jump word (opcode, type, condition, padding) EMIT_BYTE(mb, code, TOY_OPCODE_JUMP); EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);