Renamed 'scripts' directory to 'examples'
Also allowed assignment within conditionals
This commit is contained in:
@@ -33,10 +33,9 @@ fn makeCounter() {
|
|||||||
|
|
||||||
var tally = makeCounter();
|
var tally = makeCounter();
|
||||||
|
|
||||||
while (true) {
|
var result = 0;
|
||||||
var result = tally();
|
while (result = tally()) {
|
||||||
|
print result;
|
||||||
print result; //prints 1 to 10
|
|
||||||
|
|
||||||
if (result >= 10) {
|
if (result >= 10) {
|
||||||
break;
|
break;
|
||||||
|
|||||||
+3
-4
@@ -29,10 +29,9 @@ fn makeCounter() {
|
|||||||
|
|
||||||
var tally = makeCounter();
|
var tally = makeCounter();
|
||||||
|
|
||||||
while (true) {
|
var result = 0;
|
||||||
var result = tally();
|
while (result = tally()) {
|
||||||
|
print result;
|
||||||
print result; //prints 1 to 10
|
|
||||||
|
|
||||||
if (result >= 10) {
|
if (result >= 10) {
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
//tentatively functional
|
|
||||||
|
|
||||||
//fibonacci sequence
|
//fibonacci sequence
|
||||||
fn fib(n) {
|
fn fib(n) {
|
||||||
if (n < 2) return n;
|
if (n < 2) return n;
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
//the semicolon is super important
|
||||||
|
print "Hello world!";
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
//find the leap years
|
//is 'n' a leap year
|
||||||
fn isLeapYear(n: Int) {
|
fn isLeapYear(n: Int) {
|
||||||
if (n % 400 == 0) return true;
|
if (n % 400 == 0) return true;
|
||||||
if (n % 100 == 0) return false;
|
if (n % 100 == 0) return false;
|
||||||
return n % 4 == 0;
|
return n % 4 == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check for string reuse
|
|
||||||
{
|
{
|
||||||
print isLeapYear(1999);
|
print isLeapYear(1999);
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
@@ -8,11 +8,11 @@ fn makeCounter() {
|
|||||||
return increment;
|
return increment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//'tally' becomes a closure
|
||||||
var tally = makeCounter();
|
var tally = makeCounter();
|
||||||
|
|
||||||
while (true) {
|
var result = 0;
|
||||||
var result = tally();
|
while (result = tally()) {
|
||||||
|
|
||||||
print result;
|
print result;
|
||||||
|
|
||||||
if (result >= 10) {
|
if (result >= 10) {
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
|
|
||||||
fn output(arg) {
|
|
||||||
print arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
var array = ["alpha", "bravo", "charlie"];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
array.forEach(echo);
|
|
||||||
array.forEach(output);
|
|
||||||
@@ -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());
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
var randi: Int = 69420;
|
|
||||||
fn rand() {
|
|
||||||
return randi = randi * 1664525 + 1013904223;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var a = rand();
|
|
||||||
|
|
||||||
|
|
||||||
@@ -632,6 +632,12 @@ static unsigned int writeInstructionIfThenElse(Toy_Bytecode** mb, Toy_AstIfThenE
|
|||||||
//cond-branch
|
//cond-branch
|
||||||
writeBytecodeFromAst(mb, ast.condBranch);
|
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 the jump word (opcode, type, condition, padding)
|
||||||
EMIT_BYTE(mb, code, TOY_OPCODE_JUMP);
|
EMIT_BYTE(mb, code, TOY_OPCODE_JUMP);
|
||||||
EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);
|
EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);
|
||||||
@@ -677,6 +683,12 @@ static unsigned int writeInstructionWhileThen(Toy_Bytecode** mb, Toy_AstWhileThe
|
|||||||
//cond-branch
|
//cond-branch
|
||||||
writeBytecodeFromAst(mb, ast.condBranch);
|
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 the jump word (opcode, type, condition, padding)
|
||||||
EMIT_BYTE(mb, code, TOY_OPCODE_JUMP);
|
EMIT_BYTE(mb, code, TOY_OPCODE_JUMP);
|
||||||
EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);
|
EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);
|
||||||
|
|||||||
Reference in New Issue
Block a user