diff --git a/docs/src/quickstart.md b/docs/src/quickstart.md index 2fee909..16ce3b3 100644 --- a/docs/src/quickstart.md +++ b/docs/src/quickstart.md @@ -101,18 +101,35 @@ while (true) { } ``` -*Note: The `for` loop is underway, and will allow for iteration over an arrays, tables, and maybe functions.* +Alternatively, when iterating over an array or table, you can use the `for` keyword like this. While inside a for-loop, the iterable is inaccessible to prevent run-time modification (it's an ad-hoc bugfix, and will likely be improved later). ``` -//arrays do currently work with for-loops +//Iterate on an array with a for-loop var array = ["foo", "bar", "buzz", "fizz"]; for (var i in array) { - if (i == "buzz") break; //supports break or continue + if (i == "buzz") break; //supports break and continue print i; } ``` +``` +//Also works when printing the values in a table +//However, the order of the values is undefined +var table = [ + "Alpha": 1, + "Bravo": 2, + "Charlie": 3, + "Delta": 4, + "Echo": 5, +]; + +for (var i in table) { + print i; //this will print a number + print table; //this will print null +} +``` + ## Arrays and Tables Arrays are defined with a pair of brackets, and can contain a list of comma-separated values. diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 39667cc..a21c22d 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -763,6 +763,7 @@ static unsigned int writeInstructionForThen(Toy_Bytecode** mb, Toy_AstForThen as emitString(mb, ast.condBranch->iterable.left->varDeclare.name); + //TODO: use a different approach //BUGFIX: shadow the iterable's name EMIT_BYTE(mb, code, TOY_OPCODE_READ); EMIT_BYTE(mb, code, TOY_VALUE_NULL); diff --git a/source/toy_vm.c b/source/toy_vm.c index fad5bf8..ed395d4 100644 --- a/source/toy_vm.c +++ b/source/toy_vm.c @@ -482,7 +482,6 @@ static void processEliminate(Toy_VM* vm) { } static void processIterate(Toy_VM* vm) { - //URGENT: finish this //ITERATE on [-2] based on type, with [-1] as counter //then delegate to processJump @@ -545,6 +544,7 @@ static void processIterate(Toy_VM* vm) { processJump(vm); } } + //TODO: support closures as a parameter else { fprintf(stderr, TOY_CC_ERROR "ERROR: Unknown iterable type '%s' found in for loop, exiting\n" TOY_CC_RESET, Toy_getValueTypeAsCString(Toy_unwrapValue(compound).type)); exit(-1);