Implemented array.forEach(fn)

This commit is contained in:
2026-04-26 11:59:15 +10:00
parent efc9fe1406
commit af30246e0c
5 changed files with 105 additions and 31 deletions
+12 -1
View File
@@ -8,6 +8,9 @@
#include "toy_compiler.h"
#include "toy_vm.h"
//NOTE: for testing
#include "standard_library.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -367,7 +370,14 @@ int repl(const char* filepath, bool verbose) {
inspect_bytecode(bytecode);
}
Toy_bindVM(&vm, bytecode, NULL);
//WARN: Hacky debugging
if (vm.scope == NULL) {
Toy_bindVM(&vm, bytecode, NULL);
initStandardLibrary(&vm);
}
else {
Toy_bindVM(&vm, bytecode, NULL);
}
//run
Toy_runVM(&vm);
@@ -474,6 +484,7 @@ int main(int argc, const char* argv[]) {
Toy_VM vm;
Toy_initVM(&vm);
Toy_bindVM(&vm, bytecode, NULL);
initStandardLibrary(&vm); //WARN: Hacky debugging
Toy_runVM(&vm);
+21 -6
View File
@@ -15,20 +15,35 @@ typedef struct CallbackPairs {
} CallbackPairs;
//example callbacks
void hello(Toy_VM* vm) {
(void)vm;
Toy_print("Hello world!");
}
void debug(Toy_VM* vm) {
(void)vm;
Toy_print("This function returns the integer '42' to the calling scope.");
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_INTEGER(42));
}
void identity(Toy_VM* vm) {
//does nothing, but any arguements are left on the stack as results
(void)vm;
}
void echo(Toy_VM* vm) {
//pops one argument, and prints it
Toy_Value value = Toy_popStack(&vm->stack);
Toy_String* string = Toy_stringifyValue(&vm->memoryBucket, value);
char* cstr = Toy_getStringRaw(string);
Toy_print(cstr);
free(cstr);
Toy_freeString(string);
Toy_freeValue(value);
}
CallbackPairs callbackPairs[] = {
{"hello", hello},
{"debug", debug},
{"identity", identity},
{"echo", echo},
{NULL, NULL},
};