diff --git a/.notes/functions.md b/.notes/functions.md new file mode 100644 index 0000000..7dca0cb --- /dev/null +++ b/.notes/functions.md @@ -0,0 +1,82 @@ + +shared: +* Functions can be stored within Toy_Value & loaded by Toy_VM +* They have a list of parameters +* They can return a number of values + + +```toy +fn makeCounter() { + var count = 0; + + fn next() { + return ++count; + } + + return next; +} + +var tally = makeCounter(); + +print tally(); //1 +print tally(); //2 +print tally(); //3 +``` + +```toy +import standard as std; + +print std.clock(); +``` + +bytecode_functions: +* They point to a raw chunk of bytecode (the module) +* Closures are supported via scopes (they have a persistent scope) + +c_functions: +* Delegates to user code & APIs +* invoked via 'hooks' (which are called with the 'import' keyword) + +``` +typedef struct Toy_Module { + Toy_Scope* scopePtr; + + unsigned char* code; + unsigned int codeSize; + + unsigned int paramSize; + unsigned int jumpsSize; + unsigned int dataSize; + unsigned int subsSize; + + unsigned int paramAddr; + unsigned int codeAddr; + unsigned int jumpsAddr; + unsigned int dataAddr; + unsigned int subsAddr; +} +``` + +flow: + compile module from AST -> + append module to bundle -> + bind VM (takes module) -> + run VM + +Definitions: + bundles have version info (MAJOR, MINOR, PATCH, BUILD) and may have more than 1 modules. + Modules are chunks of usable memory, loadable into VMs. + VMs run the actual code. + Functions are unions, either pointers to modules, or pointers to user-defined C callbacks. + Functions are packed into values. + +API: + init bundle + verify bundle + + append bundle with module + extract module from bundle + +# Notes + +* Scopes, buckets, strings, etc. will persist until the root VM is cleared diff --git a/source/toy_routine.h b/source/toy_routine.h index 5723a72..34457b0 100644 --- a/source/toy_routine.h +++ b/source/toy_routine.h @@ -56,3 +56,7 @@ typedef struct Toy_Routine { } Toy_Routine; TOY_API void* Toy_compileRoutine(Toy_Ast* ast); + +//URGENT: Rename routines to ModuleBuilder +//URGENT: Rename bytecode to ModuleBundler +//URGENT: Compiled code is a "module" diff --git a/source/toy_vm.h b/source/toy_vm.h index 97062f0..001cf2a 100644 --- a/source/toy_vm.h +++ b/source/toy_vm.h @@ -14,7 +14,7 @@ typedef struct Toy_VM { //raw instructions to be executed - unsigned char* module; + unsigned char* module; //URGENT: rename to 'code' unsigned int moduleSize; unsigned int paramSize;