From cdfe17ad53fcde903f6732f9df17d121a3f9d120 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 20 Jul 2023 19:44:40 +1000 Subject: [PATCH] Added mecha-style comment docs to a bunch of headers --- .gitignore | 1 + makefile | 17 +++ source/toy_ast_node.h | 1 + source/toy_common.h | 63 ++++++++- source/toy_compiler.c | 18 +-- source/toy_compiler.h | 44 +++++- source/toy_drive_system.h | 68 ++++++++- source/toy_interpreter.h | 165 ++++++++++++++++++++-- source/toy_lexer.h | 38 ++++- source/toy_literal.h | 238 ++++++++++++++++++++++++++++++-- source/toy_literal_array.c | 2 +- source/toy_literal_array.h | 64 ++++++++- source/toy_literal_dictionary.c | 1 - source/toy_literal_dictionary.h | 67 +++++++++ source/toy_memory.h | 97 ++++++++++++- source/toy_parser.h | 85 +++++++++++- source/toy_reffunction.h | 77 ++++++++++- source/toy_refstring.h | 106 +++++++++++++- source/toy_scope.h | 68 ++++++++- tools/mecha.cpp | 4 + 20 files changed, 1165 insertions(+), 59 deletions(-) diff --git a/.gitignore b/.gitignore index 408efe48..a3e12f7d 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ bin/ *.stackdump *.tb *.filters +[Dd]ocs/ #Shell files *.bat diff --git a/makefile b/makefile index 9a4db21d..9ee51fc7 100644 --- a/makefile +++ b/makefile @@ -51,6 +51,23 @@ $(TOY_OUTDIR): install-tools: cp -rf tools/toylang.vscode-highlighting ~/.vscode/extensions +#utils +build-mecha: $(TOY_OUTDIR) + g++ -o $(TOY_OUTDIR)/mecha tools/mecha.cpp + +build-docs: build-mecha + $(TOY_OUTDIR)/mecha $(wildcard source/*.h) + +docs: + mkdir docs + +move-docs: docs + mv -u $(wildcard source/*.md) docs + +documentation: + $(MAKE) build-docs + $(MAKE) move-docs + .PHONY: clean clean: diff --git a/source/toy_ast_node.h b/source/toy_ast_node.h index 05f14b74..a9346b84 100644 --- a/source/toy_ast_node.h +++ b/source/toy_ast_node.h @@ -270,4 +270,5 @@ union Toy_private_node { Toy_NodeImport import; }; +//see toy_parser.h for more documentation on this function TOY_API void Toy_freeASTNode(Toy_ASTNode* node); diff --git a/source/toy_common.h b/source/toy_common.h index 43d4f90d..3f248625 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -1,15 +1,23 @@ #pragma once +/*! +# toy_common.h + +This file is generally included in most header files within Toy, as it is where the TOY_API macro is defined. It also has some utilities intended for use only by the repl. + +## Defined Macros +!*/ + #include #include #include -#define TOY_VERSION_MAJOR 1 -#define TOY_VERSION_MINOR 1 -#define TOY_VERSION_PATCH 6 -#define TOY_VERSION_BUILD Toy_private_version_build() +/*! +### TOY_API + +This definition of this macro is platform-dependant, and used to enable cross-platform compilation of shared and static libraries. +!*/ -//platform/compiler-specific instructions #if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__) #define TOY_API extern @@ -28,8 +36,53 @@ #endif +/*! +### TOY_VERSION_MAJOR + +The current major version of Toy. This value is embedded into the bytecode, and the interpreter will refuse to run bytecode with a major version that does not match it’s own version. + +This value MUST fit into an unsigned char. +!*/ + +#define TOY_VERSION_MAJOR 1 + +/*! +### TOY_VERSION_MINOR + +The current minor version of Toy. This value is embedded into the bytecode, and the interpreter will refuse to run bytecode with a minor version that is greater than its own minor version. + +This value MUST fit into an unsigned char. +!*/ + +#define TOY_VERSION_MINOR 1 + +/*! +### TOY_VERSION_PATCH + +The current patch version of Toy. This value is embedded into the bytecode. + +This value MUST fit into an unsigned char. +!*/ + +#define TOY_VERSION_PATCH 6 + +/*! +### TOY_VERSION_BUILD + +The current build version of Toy. This value is embedded into the bytecode. + +This evaluates to a c-string, which contains build information such as compilation date and time of the interpreter. When in verbose mode, the compiler will display a warning if the build version of the bytecode does not match the build version of the interpreter. + +This macro may also be used to store additonal information about forks of the Toy codebase. +!*/ + +#define TOY_VERSION_BUILD Toy_private_version_build() TOY_API const char* Toy_private_version_build(); +/* +The following code is intended only for use within the repl. +*/ + //for processing the command line arguments in the repl typedef struct { bool error; diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 52698936..baa3e2e3 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -48,7 +48,7 @@ static int writeLiteralTypeToCache(Toy_LiteralArray* literalCache, Toy_Literal l } //optimisation: check if exactly this literal array exists - int index = Toy_findLiteralIndex(literalCache, literal); + int index = Toy_private_findLiteralIndex(literalCache, literal); if (index < 0) { index = Toy_pushLiteralArray(literalCache, literal); } @@ -74,7 +74,7 @@ static int writeNodeCompoundToCache(Toy_Compiler* compiler, Toy_ASTNode* node) { switch(node->compound.nodes[i].pair.left->type) { case TOY_AST_NODE_LITERAL: { //keys are literals - int key = Toy_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].pair.left->atomic.literal); + int key = Toy_private_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].pair.left->atomic.literal); if (key < 0) { key = Toy_pushLiteralArray(&compiler->literalCache, node->compound.nodes[i].pair.left->atomic.literal); } @@ -103,7 +103,7 @@ static int writeNodeCompoundToCache(Toy_Compiler* compiler, Toy_ASTNode* node) { switch(node->compound.nodes[i].pair.right->type) { case TOY_AST_NODE_LITERAL: { //values are literals - int val = Toy_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].pair.right->atomic.literal); + int val = Toy_private_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].pair.right->atomic.literal); if (val < 0) { val = Toy_pushLiteralArray(&compiler->literalCache, node->compound.nodes[i].pair.right->atomic.literal); } @@ -142,7 +142,7 @@ static int writeNodeCompoundToCache(Toy_Compiler* compiler, Toy_ASTNode* node) { switch(node->compound.nodes[i].type) { case TOY_AST_NODE_LITERAL: { //values - int val = Toy_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].atomic.literal); + int val = Toy_private_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].atomic.literal); if (val < 0) { val = Toy_pushLiteralArray(&compiler->literalCache, node->compound.nodes[i].atomic.literal); } @@ -230,7 +230,7 @@ static int writeNodeCollectionToCache(Toy_Compiler* compiler, Toy_ASTNode* node) static int writeLiteralToCompiler(Toy_Compiler* compiler, Toy_Literal literal) { //get the index - int index = Toy_findLiteralIndex(&compiler->literalCache, literal); + int index = Toy_private_findLiteralIndex(&compiler->literalCache, literal); if (index < 0) { if (TOY_IS_TYPE(literal)) { @@ -550,7 +550,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode } //write each piece of the declaration to the bytecode - int identifierIndex = Toy_findLiteralIndex(&compiler->literalCache, node->varDecl.identifier); + int identifierIndex = Toy_private_findLiteralIndex(&compiler->literalCache, node->varDecl.identifier); if (identifierIndex < 0) { identifierIndex = Toy_pushLiteralArray(&compiler->literalCache, node->varDecl.identifier); } @@ -597,7 +597,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode Toy_Literal fnLiteral = ((Toy_Literal){ .as = { .generic = fnCompiler }, .type = TOY_LITERAL_FUNCTION_INTERMEDIATE}); //push the name - int identifierIndex = Toy_findLiteralIndex(&compiler->literalCache, node->fnDecl.identifier); + int identifierIndex = Toy_private_findLiteralIndex(&compiler->literalCache, node->fnDecl.identifier); if (identifierIndex < 0) { identifierIndex = Toy_pushLiteralArray(&compiler->literalCache, node->fnDecl.identifier); } @@ -653,7 +653,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode } //write each argument to the bytecode - int argumentsIndex = Toy_findLiteralIndex(&compiler->literalCache, node->fnCall.arguments->fnCollection.nodes[i].atomic.literal); + int argumentsIndex = Toy_private_findLiteralIndex(&compiler->literalCache, node->fnCall.arguments->fnCollection.nodes[i].atomic.literal); if (argumentsIndex < 0) { argumentsIndex = Toy_pushLiteralArray(&compiler->literalCache, node->fnCall.arguments->fnCollection.nodes[i].atomic.literal); } @@ -675,7 +675,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode //push the argument COUNT to the top of the stack Toy_Literal argumentsCountLiteral = TOY_TO_INTEGER_LITERAL(node->fnCall.argumentCount); //argumentCount is set elsewhere to support dot operator - int argumentsCountIndex = Toy_findLiteralIndex(&compiler->literalCache, argumentsCountLiteral); + int argumentsCountIndex = Toy_private_findLiteralIndex(&compiler->literalCache, argumentsCountLiteral); if (argumentsCountIndex < 0) { argumentsCountIndex = Toy_pushLiteralArray(&compiler->literalCache, argumentsCountLiteral); } diff --git a/source/toy_compiler.h b/source/toy_compiler.h index 58f986c9..b6c30253 100644 --- a/source/toy_compiler.h +++ b/source/toy_compiler.h @@ -1,11 +1,20 @@ #pragma once +/*! +# toy_compiler.h + +This header defines the compiler structure, which is used to transform abstract syntax trees into usable intermediate bytecode. There are two steps to generating bytecode - the writing step, and the collation step. + +During the writing step, the core of the program is generated, along with a series of literals representing the values within the program; these values are compressed and flattened into semi-unrecognizable forms. If the same literal is used multiple times in a program, such as a variable name, the name itself is replaced by a reference to the flattened literals within the cache. + +During the collation step, everything from the core program’s execution instructions, the flattened literals, the functions (which have their own sections and protocols within the bytecode) and version information (such as the macros defined in toy_common.h) are all combined into a single buffer of bytes, known as bytecode. This bytecode can then be safely saved to a file or immediately executed. +!*/ + #include "toy_common.h" #include "toy_opcodes.h" #include "toy_ast_node.h" #include "toy_literal_array.h" -//the compiler takes the nodes, and turns them into sequential chunks of bytecode, saving literals to an external array typedef struct Toy_Compiler { Toy_LiteralArray literalCache; unsigned char* bytecode; @@ -14,9 +23,38 @@ typedef struct Toy_Compiler { bool panic; } Toy_Compiler; +/*! +## Define Functions + +Executing the following functions out-of-order causes undefiend behaviour. +!*/ + +/*! +### void Toy_initCompiler(Toy_Compiler* compiler) + +This function initializes the given compiler. +!*/ TOY_API void Toy_initCompiler(Toy_Compiler* compiler); + +/*! +### void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node) + +This function writes the given `node` argument to the compiler. During the writing step, this function may be called repeatedly, with a stream of results from `Toy_scanParser()`, until `Toy_scanParser()` returns `NULL`. +!*/ TOY_API void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node); -TOY_API void Toy_freeCompiler(Toy_Compiler* compiler); -//embed the header, data section, code section, function section, etc. +/*! +### unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size) + +This function returns a buffer of bytes, known as "bytecode", created from the given compiler; it also stores the size of the bytecode in the variable pointed to by `size`. + +Calling `Toy_collateCompiler()` multiple times on the same compiler will produce undefined behaviour. +!*/ TOY_API unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size); + +/*! +### void Toy_freeCompiler(Toy_Compiler* compiler) + +This function frees a compiler. Calling this on a compiler which has not been collated will free that compiler as expected - anything written to it will be lost. +!*/ +TOY_API void Toy_freeCompiler(Toy_Compiler* compiler); diff --git a/source/toy_drive_system.h b/source/toy_drive_system.h index 2f20f228..3864c4ad 100644 --- a/source/toy_drive_system.h +++ b/source/toy_drive_system.h @@ -1,12 +1,76 @@ +#pragma once + +/*! +# toy_drive_system.h + +When accessing the file system through toy (such as with the runner library), it's best practice to utilize Toy's built-in drive system - this system (tries to) prevent malicious accessing of files outside of the designated folders. It does this by causing an error when a script tries to access a parent directory. + +To use the drive system, first you must designate specific folders which can be accessed, like so: + +```c +#include "toy_drive_system.h" + +int main(int argc, char* argv[]) { + //the drive system uses a LiteralDictionary, which must be initialized with this + Toy_initDriveSystem(); + + Toy_setDrivePath("scripts", "assets/scripts"); + Toy_setDrivePath("sprites", "assets/sprites"); + Toy_setDrivePath("fonts", "assets/fonts"); + + //TODO: do you stuff here + + //clean up the drive dictionary when you're done + Toy_freeDriveSystem(); + + return 0; +} +``` + +This utility is intended mainly for libraries to use - as such, the core of Toy does not utilize it. + +### Implementation Details + +The drive system uses a Toy's Dictionary structure to store the mappings between keys and values - this dictionary object is a static global which persists for the lifetime of the program. +!*/ + #include "toy_common.h" #include "toy_literal.h" #include "toy_interpreter.h" -//file system API - these need to be set by the host +/*! +## Defined Functions +!*/ + +/*! +### void Toy_initDriveSystem() + +This function initializes the drive system. +!*/ TOY_API void Toy_initDriveSystem(); + +/*! +### void Toy_freeDriveSystem() + +This function cleans up after the drive system is no longer needed. +!*/ TOY_API void Toy_freeDriveSystem(); -//file system API - for use with libs +/*! +### void Toy_setDrivePath(char* drive, char* path) + +This function sets a key-value pair in the drive system. It uses C strings, since its intended to be called directly from `main()`. +!*/ TOY_API void Toy_setDrivePath(char* drive, char* path); + +/*! +### Toy_Literal Toy_getDrivePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral) + +This function, when given a string literal of the correct format, will return a new string literal containing the relative filepath to a specified file. + +The correct format is `drive:/path/to/filename`, where `drive` is a drive that was specified with `Toy_setDrivePath()`. + +On failure, this function returns a null literal. +!*/ TOY_API Toy_Literal Toy_getDrivePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral); diff --git a/source/toy_interpreter.h b/source/toy_interpreter.h index 8c8940e0..41ed0ba4 100644 --- a/source/toy_interpreter.h +++ b/source/toy_interpreter.h @@ -1,5 +1,41 @@ #pragma once +/*! +# toy_interpreter.h + +This header defines the interpreter structure, which is the beating heart of Toy. + +`Toy_Interpreter` is a stack-based, bytecode-driven interpreter with a number of customisation options, including "hooks"; native C functions wrapped in `Toy_Literal` instances, injected into the interpreter in order to give the Toy scripts access to libraries via the `import` keyword. The hooks, when invoked this way, can then inject further native functions into the interpreter's current scope. Exactly which hooks are made available varies by host program, but `standard` is the most commonly included one. + +Another useful customisation feature is the ability to redicrect output from the `print` and `assert` keywords, as well as any internal errors that occur. This can allow you to add in a logging system, or even hook the `print` statement up to some kind of HUD. + +## Defined Interfaces + +Note: These interfaces are *actually* defined in [toy_literal.h](toy_literal_h.md) but are documented here, because this is where it matters most. + +### typedef void (*Toy_PrintFn)(const char*) + +This is the interface used by "print functions" - that is, functions used to print messages from the `print` and `assert` keywords, as well as internal interpreter errors. + +### typedef int (*Toy_NativeFn)(struct Toy_Interpreter* interpreter, struct Toy_LiteralArray* arguments) + +This is the interface used by "native functions" - that is, functions written in C which can be called directly by Toy scripts. + +The arguments to the function are passed in as a `Toy_LiteralArray`. + +### typedef int (*Toy_HookFn)(struct Toy_Interpreter* interpreter, struct Toy_Literal identifier, struct Toy_Literal alias) + +This is the interface used by "hook functions" - that is, functions written in C which are invoked by using the `import` keyword, and are intended to inject other native functions into the current scope. While hook functions are capable of doing other things, this is greatly discouraged. + +The identifier of the library (its name) is passed in as a `Toy_Literal`, as is any given alias; if no alias is given, then `alias` will be a null literal. Here, the identifier is `standard`, while the alias is `std`. + +``` +import standard as std; +``` + +Conventionally, when an alias is given, all of the functions should instead be inserted into a `Toy_LiteralDictionary` which is then inserted into the scope with the alias as its identifier. +!*/ + #include "toy_common.h" #include "toy_literal.h" #include "toy_literal_array.h" @@ -31,21 +67,134 @@ typedef struct Toy_Interpreter { bool panic; } Toy_Interpreter; -//native API +/*! +## Defined Functions +!*/ + +/*! +### void Toy_initInterpreter(Toy_Interpreter* interpreter) + +This function initializes the interpreter. It allocates memory for internal systems such as the stack, and zeroes-out systems that have yet to be invoked. Internally, it also invokes `Toy_resetInterpreter` to initialize the environment. +!*/ +TOY_API void Toy_initInterpreter(Toy_Interpreter* interpreter); //start of program + +/*! +### void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, size_t length) + +This function takes a `Toy_Interpreter` and `bytecode` (as well as the `length` of the bytecode), checks its version information, parses and un-flattens the literal cache, and executes the compiled program stored in the bytecode. This function also consumes the bytecode, so the `bytecode` argument is no longer valid after calls. + +If the given bytecode's embedded version is not compatible with the current interpreter, then this function will refuse to execute. + +Re-using a `Toy_Interpreter` instance without first resetting it is possible (that's how the repl works), however doing so may have unintended consequences if the scripts are not intended to be used in such a way. Any variables declared will persist. +!*/ +TOY_API void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, size_t length); + +/*! +### void Toy_resetInterpreter(Toy_Interpreter* interpreter) + +This function frees any scopes that the scripts have built up, and generates a new one. It also injects several globally available functions: + +* set +* get +* push +* pop +* length +* clear +!*/ +TOY_API void Toy_resetInterpreter(Toy_Interpreter* interpreter); + +/*! +### void Toy_freeInterpreter(Toy_Interpreter* interpreter) + +This function frees a `Toy_Interpreter`, clearing all of the memory used within. That interpreter is no longer valid for use, and must be re-initialized. +!*/ +TOY_API void Toy_freeInterpreter(Toy_Interpreter* interpreter); + +/*! +### bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_NativeFn func) + +This function will inject the given native function `func` into the `Toy_Interpreter`'s current scope, with the identifer as `name`. Both the name and function will be converted into literals internally before being stored. It will return true on success, otherwise it will return false. + +The primary use of this function is within hooks. +!*/ TOY_API bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_NativeFn func); + +/*! +### bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_HookFn hook) + +This function will inject the given native function `hook` into the `Toy_Interpreter`'s hook cache, with the identifier as `name`. Both the name and the function will be converted into literals internally before being stored. It will return true on success, otherwise it will return false. + +Hooks are invoked with the `import` keyword within Toy's scripts. +!*/ TOY_API bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_HookFn hook); +/*! +### bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) + +This function calls a `Toy_Literal` which contains a function, with the arguments to that function passed in as `arguments` and the results stored in `returns`. It returns true on success, otherwise it returns false. + +The literal `func` can be either a native function or a Toy function, but it won't execute a hook. +!*/ TOY_API bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns); + +/*! +### bool Toy_callFn(Toy_Interpreter* interpreter, const char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) + +This utility function will find a `Toy_literal` within the `Toy_Interpreter`'s scope with an identifier that matches `name`, and will invoke it using `Toy_callLiteralFn` (passing in `arguments` and `returns` as expected). +!*/ TOY_API bool Toy_callFn(Toy_Interpreter* interpreter, const char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns); -//utilities for the host program +/*! +### bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* literalPtr) + +Sometimes, native functions will receive `Toy_Literal` identifiers instead of the values - the correct values can be retreived from the given interpreter's scope using the following pattern: + +```c +Toy_Literal foobarIdn = foobar; +if (TOY_IS_IDENTIFIER(foobar) && Toy_parseIdentifierToValue(interpreter, &foobar)) { + freeLiteral(foobarIdn); //remember to free the identifier +} +``` +!*/ TOY_API bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* literalPtr); + +/*! +### void Toy_setInterpreterPrint(Toy_Interpreter* interpreter, Toy_PrintFn printOutput) + +This function sets the function called by the `print` keyword. By default, the following wrapper is used: + +```c +static void printWrapper(const char* output) { + printf("%s\n", output); +} +``` + +Note: The above is a very minor lie - in reality there are some preprocessor directives to allow the repl's `-n` flag to work. +!*/ TOY_API void Toy_setInterpreterPrint(Toy_Interpreter* interpreter, Toy_PrintFn printOutput); + +/*! +### void Toy_setInterpreterAssert(Toy_Interpreter* interpreter, Toy_PrintFn assertOutput) + +This function sets the function called by the `assert` keyword on failure. By default, the following wrapper is used: + +```c +static void assertWrapper(const char* output) { + fprintf(stderr, "Assertion failure: %s\n", output); +} +``` +!*/ TOY_API void Toy_setInterpreterAssert(Toy_Interpreter* interpreter, Toy_PrintFn assertOutput); -TOY_API void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn errorOutput); -//main access -TOY_API void Toy_initInterpreter(Toy_Interpreter* interpreter); //start of program -TOY_API void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, size_t length); //run the code -TOY_API void Toy_resetInterpreter(Toy_Interpreter* interpreter); //use this to reset the interpreter's environment between runs -TOY_API void Toy_freeInterpreter(Toy_Interpreter* interpreter); //end of program +/*! +### void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn errorOutput) + +This function sets the function called when an error occurs within the interpreter. By default, the following wrapper is used: + +```c +static void errorWrapper(const char* output) { + fprintf(stderr, "%s", output); //no newline +} +``` +!*/ +TOY_API void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn errorOutput); diff --git a/source/toy_lexer.h b/source/toy_lexer.h index c45f2374..4d97ed5f 100644 --- a/source/toy_lexer.h +++ b/source/toy_lexer.h @@ -1,5 +1,11 @@ #pragma once +/*! +# toy_lexer.h + +This header defines the lexer and token structures, which can be bound to a piece of source code, and used to tokenize it within a parser. +!*/ + #include "toy_common.h" #include "toy_token_types.h" @@ -20,10 +26,40 @@ typedef struct { int line; } Toy_Token; +/*! +## Defined Functions +!*/ + +/*! +### void Toy_initLexer(Toy_Lexer* lexer, const char* source) + +This function initializes a lexer, binding it to the `source` parameter; the lexer is now ready to be passed to the parser. +!*/ TOY_API void Toy_initLexer(Toy_Lexer* lexer, const char* source); + +/*! +### Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer) + +This function "scans" the lexer, returning a token to the parser. + +Private functions are not intended for general use. +!*/ TOY_API Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer); -//for debugging +/*! +### void Toy_private_printToken(Toy_Token* token) + +This function prints a given token to stdout. + +Private functions are not intended for general use. +!*/ TOY_API void Toy_private_printToken(Toy_Token* token); +/*! +### void Toy_private_setComments(Toy_Lexer* lexer, bool enabled) + +This function sets whether comments are allowed within source code. By default, comments are allowed, and are only disabled in the repl. + +Private functions are not intended for general use. +!*/ TOY_API void Toy_private_setComments(Toy_Lexer* lexer, bool enabled); diff --git a/source/toy_literal.h b/source/toy_literal.h index c730e39a..07752059 100644 --- a/source/toy_literal.h +++ b/source/toy_literal.h @@ -1,5 +1,15 @@ #pragma once +/*! +# toy_literal.h + +This header defines the literal structure, which is used extensively throughout Toy to represent values of some kind. + +The main way of interacting with literals is to use a macro of some kind, as the exact implementation of `Toy_Literal` has and will change based on the needs of Toy. + +User data can be passed around within Toy as an opaque type - use the tag value for determining what kind of opaque it is, or leave it as 0. +!*/ + #include "toy_common.h" #include "toy_refstring.h" @@ -15,6 +25,31 @@ typedef int (*Toy_NativeFn)(struct Toy_Interpreter* interpreter, struct Toy_Lite typedef int (*Toy_HookFn)(struct Toy_Interpreter* interpreter, struct Toy_Literal identifier, struct Toy_Literal alias); typedef void (*Toy_PrintFn)(const char*); +/*! +## Defined Enums + +### Toy_LiteralType + +* `TOY_LITERAL_NULL` +* `TOY_LITERAL_BOOLEAN` +* `TOY_LITERAL_INTEGER` +* `TOY_LITERAL_FLOAT` +* `TOY_LITERAL_STRING` +* `TOY_LITERAL_ARRAY` +* `TOY_LITERAL_DICTIONARY` +* `TOY_LITERAL_FUNCTION` +* `TOY_LITERAL_FUNCTION_NATIVE` +* `TOY_LITERAL_FUNCTION_HOOK` +* `TOY_LITERAL_IDENTIFIER` +* `TOY_LITERAL_TYPE` +* `TOY_LITERAL_OPAQUE` +* `TOY_LITERAL_ANY` + +These are the main values of `Toy_LiteralType`, each of which represents a potential state of the `Toy_Literal` structure. Do not interact with a literal without determining its type with the `IS_*` macros first. + +Other type values are possible, but are only used internally. +!*/ + typedef enum { TOY_LITERAL_NULL, TOY_LITERAL_BOOLEAN, @@ -89,6 +124,28 @@ typedef struct Toy_Literal { //shenanigans with byte alignment reduces the size of Toy_Literal } Toy_Literal; +/*! +## Defined Macros +!*/ + +/*! +The following macros are used to determine if a given literal, passed in as `value`, is of a specific type. It should be noted that `TOY_IS_FUNCTION` will return false for native and hook functions. + +* `TOY_IS_NULL(value)` +* `TOY_IS_BOOLEAN(value)` +* `TOY_IS_INTEGER(value)` +* `TOY_IS_FLOAT(value)` +* `TOY_IS_STRING(value)` +* `TOY_IS_ARRAY(value)` +* `TOY_IS_DICTIONARY(value)` +* `TOY_IS_FUNCTION(value)` +* `TOY_IS_FUNCTION_NATIVE(value)` +* `TOY_IS_FUNCTION_HOOK(value)` +* `TOY_IS_IDENTIFIER(value)` +* `TOY_IS_TYPE(value)` +* `TOY_IS_OPAQUE(value)` +!*/ + #define TOY_IS_NULL(value) ((value).type == TOY_LITERAL_NULL) #define TOY_IS_BOOLEAN(value) ((value).type == TOY_LITERAL_BOOLEAN) #define TOY_IS_INTEGER(value) ((value).type == TOY_LITERAL_INTEGER) @@ -103,6 +160,23 @@ typedef struct Toy_Literal { #define TOY_IS_TYPE(value) ((value).type == TOY_LITERAL_TYPE) #define TOY_IS_OPAQUE(value) ((value).type == TOY_LITERAL_OPAQUE) +/*! +The following macros are used to cast a literal to a specific C type to be used. + +* `TOY_AS_BOOLEAN(value)` +* `TOY_AS_INTEGER(value)` +* `TOY_AS_FLOAT(value)` +* `TOY_AS_STRING(value)` +* `TOY_AS_ARRAY(value)` +* `TOY_AS_DICTIONARY(value)` +* `TOY_AS_FUNCTION(value)` +* `TOY_AS_FUNCTION_NATIVE(value)` +* `TOY_AS_FUNCTION_HOOK(value)` +* `TOY_AS_IDENTIFIER(value)` +* `TOY_AS_TYPE(value)` +* `TOY_AS_OPAQUE(value)` +!*/ + #define TOY_AS_BOOLEAN(value) ((value).as.boolean) #define TOY_AS_INTEGER(value) ((value).as.integer) #define TOY_AS_FLOAT(value) ((value).as.number) @@ -116,6 +190,24 @@ typedef struct Toy_Literal { #define TOY_AS_TYPE(value) ((value).as.type) #define TOY_AS_OPAQUE(value) ((value).as.opaque.ptr) +/*! +The following macros are used to create a new literal, with the given `value` as it's internal value. + +* `TOY_TO_NULL_LITERAL` - does not need parantheses +* `TOY_TO_BOOLEAN_LITERAL(value)` +* `TOY_TO_INTEGER_LITERAL(value)` +* `TOY_TO_FLOAT_LITERAL(value)` +* `TOY_TO_STRING_LITERAL(value)` +* `TOY_TO_ARRAY_LITERAL(value)` +* `TOY_TO_DICTIONARY_LITERAL(value)` +* `TOY_TO_FUNCTION_LITERAL(value, l)` - `l` represents the length of the bytecode passed as `value` +* `TOY_TO_FUNCTION_NATIVE_LITERAL(value)` +* `TOY_TO_FUNCTION_HOOK_LITERAL(value)` +* `TOY_TO_IDENTIFIER_LITERAL(value)` +* `TOY_TO_TYPE_LITERAL(value, c)` - `c` is the true of the type should be const +* `TOY_TO_OPAQUE_LITERAL(value, t)` - `t` is the integer tag +!*/ + #define TOY_TO_NULL_LITERAL ((Toy_Literal){{ .integer = 0 }, TOY_LITERAL_NULL}) #define TOY_TO_BOOLEAN_LITERAL(value) ((Toy_Literal){{ .boolean = value }, TOY_LITERAL_BOOLEAN}) #define TOY_TO_INTEGER_LITERAL(value) ((Toy_Literal){{ .integer = value }, TOY_LITERAL_INTEGER}) @@ -130,31 +222,159 @@ typedef struct Toy_Literal { #define TOY_TO_TYPE_LITERAL(value, c) ((Toy_Literal){{ .type = { .typeOf = value, .constant = c, .subtypes = NULL, .capacity = 0, .count = 0 }}, TOY_LITERAL_TYPE}) #define TOY_TO_OPAQUE_LITERAL(value, t) ((Toy_Literal){{ .opaque = { .ptr = value, .tag = t }}, TOY_LITERAL_OPAQUE}) -//BUGFIX: For blank indexing +//BUGFIX: For blank indexing - not for general use #define TOY_IS_INDEX_BLANK(value) ((value).type == TOY_LITERAL_INDEX_BLANK) #define TOY_TO_INDEX_BLANK_LITERAL ((Toy_Literal){{ .integer = 0 }, TOY_LITERAL_INDEX_BLANK}) -TOY_API void Toy_freeLiteral(Toy_Literal literal); +/*! +## More Defined Macros -#define TOY_IS_TRUTHY(x) Toy_private_isTruthy(x) +The following macros are utilities used throughout Toy's internals, and are available for the user as well. +!*/ +/*! +### TOY_IS_TRUTHY(x) + +Returns true of the literal `x` is truthy, otherwise it returns false. + +Currently, every value is considered truthy except `false`, which is falsy and `null`, which is neither true or false. +!*/ + +#define TOY_IS_TRUTHY(x) Toy_private_isTruthy(x) + +/*! +### TOY_AS_FUNCTION_BYTECODE_LENGTH(lit) + +Returns the length of a Toy function's bytecode. + +This macro is only valid on `TOY_LITERAL_FUNCTION`. +!*/ #define TOY_AS_FUNCTION_BYTECODE_LENGTH(lit) (Toy_lengthRefFunction((lit).inner.ptr)) +/*! +### TOY_MAX_STRING_LENGTH + +The maximum length of a string in Toy, which is 4096 bytes by default. This can be changed at compile time, but the results of doing so are not officially supported. +!*/ #define TOY_MAX_STRING_LENGTH 4096 + +/*! +### TOY_HASH_I(lit) + +Identifiers are the names of values within Toy; to speed up execution, their "hash value" is computed at compile time and stored within them. Use this to access it, if needed. + +This macro is only valid on `TOY_LITERAL_IDENTIFIER`. +!*/ #define TOY_HASH_I(lit) ((lit).as.identifier.hash) + +/*! +### TOY_TYPE_PUSH_SUBTYPE(lit, subtype) + +When building a complex type, such as the type of an array or dictionary, you may need to specify inner types. Use this to push a `subtype`. calling `Toy_freeLiteral()` on the outermost type should clean up all inner types, as expected. + +This macro returns the index of the newly pushed value within it's parent. + +This macro is only valid on `TOY_LITERAL_TYPE`, for both `type` and `subtype`. +!*/ #define TOY_TYPE_PUSH_SUBTYPE(lit, subtype) Toy_private_typePushSubtype(lit, subtype) + +/*! +### TOY_GET_OPAQUE_TAG(o) + +Returns the value of the opaque `o`'s tag. + +This macro is only valid on `TOY_LITERAL_OPAQUE`. +!*/ #define TOY_GET_OPAQUE_TAG(o) o.as.opaque.tag -//BUGFIX: macros are not functions -TOY_API bool Toy_private_isTruthy(Toy_Literal x); -TOY_API Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr); -TOY_API Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype); +/*! +## Defined Functions +!*/ + +/*! +### void Toy_freeLiteral(Toy_Literal literal) + +This function frees the given literal's memory. Any internal pointers are now invalid. -//utils +This function should be called on EVERY literal when it is no longer needed, regardless of type. +!*/ +TOY_API void Toy_freeLiteral(Toy_Literal literal); + +/*! +### Toy_Literal Toy_copyLiteral(Toy_Literal original) + +This function returns a copy of the given literal. Literals should never be copied without this function, as it handles a lot of internal memory allocations. +!*/ TOY_API Toy_Literal Toy_copyLiteral(Toy_Literal original); + +/*! +### bool Toy_literalsAreEqual(Toy_Literal lhs, Toy_Literal rhs) + +This checks to see if two given literals are equal. + +When an integer and a float are compared, the integer is cooerced into a float for the duration of the call. + +Arrays or dictionaries are equal only if their keys and values all equal. Likewise, types only equal if all subtypes are equal, in order. + +Functions and opaques are never equal to anything, while values with the type `TOY_LITERAL_ANY` are always equal. +!*/ TOY_API bool Toy_literalsAreEqual(Toy_Literal lhs, Toy_Literal rhs); + +/*! +### int Toy_hashLiteral(Toy_Literal lit) + +This finds the hash of a literal, for various purposes. Different hashing algorithms are used for different types, and some types can't be hashed at all. + +types that can't be hashed are + +* all kinds of functions +* type +* opaque +* any + +In the case of identifiers, their hashes are precomputed on creation and are stored within the literal. +!*/ TOY_API int Toy_hashLiteral(Toy_Literal lit); -//not thread-safe +/*! +### void Toy_printLiteral(Toy_Literal literal) + +This wraps a call to `Toy_printLiteralCustom`, with a printf-stdout wrapper as `printFn`. +!*/ TOY_API void Toy_printLiteral(Toy_Literal literal); + +/*! +### void Toy_printLiteralCustom(Toy_Literal literal, PrintFn printFn) + +This function passes the string representation of `literal` to `printFn`. + +This function is not thread safe - due to the loopy and recursive nature of printing compound values, this function uses some globally persistent variables. +!*/ TOY_API void Toy_printLiteralCustom(Toy_Literal literal, Toy_PrintFn); + +/*! +### bool Toy_private_isTruthy(Toy_Literal x) + +Utilized by the `TOY_IS_TRUTHY` macro. + +Private functions are not intended for general use. +!*/ +TOY_API bool Toy_private_isTruthy(Toy_Literal x); + +/*! +### bool Toy_private_toIdentifierLiteral(Toy_RefString* ptr) + +Utilized by the `TOY_TO_IDENTIFIER_LITERAL` macro. + +Private functions are not intended for general use. +!*/ +TOY_API Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr); + +/*! +### bool Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype) + +Utilized by the `TOY_TYPE_PUSH_SUBTYPE` macro. + +Private functions are not intended for general use. +!*/ +TOY_API Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype); diff --git a/source/toy_literal_array.c b/source/toy_literal_array.c index 6de2d38f..88c34438 100644 --- a/source/toy_literal_array.c +++ b/source/toy_literal_array.c @@ -52,7 +52,7 @@ Toy_Literal Toy_popLiteralArray(Toy_LiteralArray* array) { } //find a literal in the array that matches the "literal" argument -int Toy_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal) { +int Toy_private_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal) { for (int i = 0; i < array->count; i++) { //not the same type if (array->literals[i].type != literal.type) { diff --git a/source/toy_literal_array.h b/source/toy_literal_array.h index 5e5004ee..1769bbf4 100644 --- a/source/toy_literal_array.h +++ b/source/toy_literal_array.h @@ -1,5 +1,13 @@ #pragma once +/*! +# literal_array.h + +This header defines the array structure, which manages a series of `Toy_Literal` instances in sequential memory. The array does not take ownership of given literals, instead it makes an internal copy. + +The array type is one of two fundemental data structures used throughout Toy - the other is the dictionary. +!*/ + #include "toy_common.h" #include "toy_literal.h" @@ -10,13 +18,65 @@ typedef struct Toy_LiteralArray { int count; } Toy_LiteralArray; +/*! +## Defined Functions +!*/ + +/* +### void Toy_initLiteralArray(Toy_LiteralArray* array) + +This function initializes a `Toy_LiteralArray` pointed to by `array`. +*/ TOY_API void Toy_initLiteralArray(Toy_LiteralArray* array); + +/*! +### void Toy_freeLiteralArray(Toy_LiteralArray* array) + +This function frees a `Toy_LiteralArray` pointed to by `array`. Every literal within is passed to `Toy_freeLiteral()` before its memory is released. +!*/ TOY_API void Toy_freeLiteralArray(Toy_LiteralArray* array); + +/*! +### int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal) + +This function adds a new `literal` to the end of the `array`, growing the array's internal buffer if needed. + +This function returns the index of the inserted value. +!*/ TOY_API int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal); + +/*! +### Toy_Literal Toy_popLiteralArray(Toy_LiteralArray* array) + +This function removes the literal at the end of the `array`, and returns it. +!*/ TOY_API Toy_Literal Toy_popLiteralArray(Toy_LiteralArray* array); + +/*! +### bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy_Literal value) + +This function frees the literal at the position represented by the integer literal `index`, and stores `value` in its place. + +This function returns true on success, otherwise it returns false. +!*/ TOY_API bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy_Literal value); + +/*! +### Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index) + +This function returns the literal at the position represented by the integer literal `index`, or returns a null literal if none is found. + +If `index` is not an integer literal or is out of bounds, this function returns a null literal. +!*/ TOY_API Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index); -int Toy_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal); +/*! +### int Toy_private_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal) + +This function scans through the array, and returns the index of the first element that matches the given `literal`, otherwise it returns -1. + +Private functions are not intended for general use. +!*/ +int Toy_private_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal); -//TODO: add a function to get the capacity & count \ No newline at end of file +//TODO: add a function to get the capacity & count diff --git a/source/toy_literal_dictionary.c b/source/toy_literal_dictionary.c index 4c6e94a3..612b3c68 100644 --- a/source/toy_literal_dictionary.c +++ b/source/toy_literal_dictionary.c @@ -131,7 +131,6 @@ static void freeEntryArray(Toy_private_dictionary_entry* array, int capacity) { //exposed functions void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary) { - //HACK: because modulo by 0 is undefined, set the capacity to a non-zero value (and allocate the arrays) dictionary->entries = NULL; dictionary->capacity = 0; dictionary->contains = 0; diff --git a/source/toy_literal_dictionary.h b/source/toy_literal_dictionary.h index 7b594bf0..b261e3d7 100644 --- a/source/toy_literal_dictionary.h +++ b/source/toy_literal_dictionary.h @@ -1,9 +1,31 @@ #pragma once +/*! +# toy_literal_dictionary.h + +This header defines the dictionary structure (as well as the private entry structure), which manages a series of `Toy_Literal` instances stored in a key-value hash map. The dictionary does not take ownership of given literals, instead it makes an internal copy. + +The dictionary type is one of two fundemental data structures used throughout Toy - the other is the array. +!*/ + #include "toy_common.h" #include "toy_literal.h" +/*! +## Defined Macros +!*/ + +/*! +### TOY_DICTIONARY_MAX_LOAD + +If the contents of a dictionary exceeds this percentage of it's capacity, then a new buffer is created, the old contents are copied over one-by-one, and the original buffer is freed. + +Since this process can be memory and time intensive, a configurable macro is used to allow for fine-grained control across the lang. + +The current default value is `0.75`, representing 75% capacity. +!*/ + //TODO: benchmark this #define TOY_DICTIONARY_MAX_LOAD 0.75 @@ -19,11 +41,56 @@ typedef struct Toy_LiteralDictionary { int contains; //count + tombstones, for internal use } Toy_LiteralDictionary; +/*! +## Defined Functions +!*/ + +/*! +### void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary) + +This function initializes the `Toy_LiteralDictionary` pointed to by `dictionary`. +!*/ TOY_API void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary); + +/*! +### void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary) + +This function frees a `Toy_LiteralDictionary` pointed to by `dictionary`. Every literal within is passed to `Toy_freeLiteral()` before its memory is released. +!*/ TOY_API void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary); +/*! +### void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value) + +This function inserts the given key-value pair of literals into `dictionary`, creating it if it doesn't exist, or freeing and overwriting it if `key` is already present. This function may also expand the memory buffer if needed. + +When expanding the memory buffer, a full copy of the existing dictionary's contents is created - this can be memory intensive. + +Literal functions and opaques cannot be used as keys. +!*/ TOY_API void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value); + +/*! +### Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key) + +This function returns the value of the literal within `dictionary` identified by `key`, or a null literal if it doesn't exist. + +Literal functions and opaques cannot be used as keys. +!*/ TOY_API Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key); + +/*! +### void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key) + +This function removes the key-value pair of literals from `dictionary` identified by `key`, if it exists. + +Literal functions and opaques cannot be used as keys. +!*/ TOY_API void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key); +/*! +### bool Toy_existsLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key) + +This function returns true if the key-value pair identified by `key` exists within `dictionary`, otherwise it returns false. +!*/ TOY_API bool Toy_existsLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key); diff --git a/source/toy_memory.h b/source/toy_memory.h index 191ecc24..a08cdb14 100644 --- a/source/toy_memory.h +++ b/source/toy_memory.h @@ -1,21 +1,112 @@ #pragma once +/*! +# toy_memory.h + +This header defines all of the memory management utilities. Any and all heap-based memory management goes through these utilities. + +A default memory allocator function is used internally, but it can be overwritten for diagnostic and platform related purposes. +!*/ + #include "toy_common.h" +/*! +## Defined Macros +!*/ + +/*! +### TOY_GROW_CAPACITY(capacity) + +This macro calculates, in place, what size of memory should be allocated based on the previous size. +!*/ #define TOY_GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2) + +/*! +### TOY_GROW_CAPACITY_FAST(capacity) + +This macro calculates, in place, what size of memory should be allocated based on the previous size. It grows faster than `TOY_GROW_CAPACITY`. +!*/ #define TOY_GROW_CAPACITY_FAST(capacity) ((capacity) < 32 ? 32 : (capacity) * 2) +/* +### TOY_ALLOCATE(type, count) + +This macro wraps `Toy_reallocate()`, which itself calls the allocator function. `type` is the type that will be allocated, and `count` is the number which will be needed (usually calculated with `TOY_GROW_CAPACITY`). + +This returns a pointer of `type`. +*/ #define TOY_ALLOCATE(type, count) ((type*)Toy_reallocate(NULL, 0, sizeof(type) * (count))) +/*! +### TOY_FREE(type, pointer) + +This macro wraps `Toy_reallocate()`, which itself calls the allocator function. `type` is the type that will be freed, and `pointer` is to what is being freed. This should only be used when a single element has been allocated, as opposed to an array. +!*/ #define TOY_FREE(type, pointer) Toy_reallocate(pointer, sizeof(type), 0) + +/*! +### TOY_FREE_ARRAY(type, pointer, oldCount) + +This macro wraps `Toy_reallocate()`, which itself calls the allocator function. `type` is the type that will be freed, `pointer` is a reference to what is being freed, and `oldCount` is the size of the array being freed. This should only be used when an array has been allocated, as opposed to a single element. +!*/ #define TOY_FREE_ARRAY(type, pointer, oldCount) Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), 0) +/*! +### TOY_GROW_ARRAY(type, pointer, oldCount, count) + +This macro wraps `Toy_reallocate()`, which itself calls the allocator function. `type` is the type that is being operated on, `pointer` is what is being resized, `oldCount` is the previous size of the array and `count` is the new size of the array (usually calculated with `TOY_GROW_CAPACITY`). + +This returns a pointer of `type`. +!*/ #define TOY_GROW_ARRAY(type, pointer, oldCount, count) (type*)Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), sizeof(type) * (count)) + +/*! +### TOY_SHRINK_ARRAY(type, pointer, oldCount, count) + +This macro wraps `Toy_reallocate()`, which itself calls the allocator function. `type` is the type that is being operated on, `pointer` is what is being resized, `oldCount` is the previous size of the array and `count` is the new size of the array. + +This returns a pointer of `type`. +!*/ #define TOY_SHRINK_ARRAY(type, pointer, oldCount, count) (type*)Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), sizeof(type) * (count)) -//implementation details -TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize); +/*! +## Defined Interfaces +!*/ + +/*! +### typedef void* (*Toy_MemoryAllocatorFn)(void* pointer, size_t oldSize, size_t newSize) + +This function interface is used for defining any memory allocator functions. + +Any and all memory allocator functions should: + +* Take a `pointer` to a previously allocated block of memory, or `NULL` +* Take the `oldSize`, which is the previous size of the `pointer` allocated, in bytes (`oldSize` can be 0) +* Take the `newSize`, which is the new size of the buffer to be allocaated, in bytes (`newSize` can be 0) +* Return the newly allocated buffer, or `NULL` if `newSize` is zero +* Return `NULL` on error +!*/ -//assign the memory allocator typedef void* (*Toy_MemoryAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); + +/*! +## Defined Functions +!*/ + +/*! +### TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize) + +This function shouldn't be called directly. Instead, use one of the given macros. + +This function wraps a call to the internal assigned memory allocator. +!*/ +TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize); + +/*! +### void Toy_setMemoryAllocator(Toy_MemoryAllocatorFn) + +This function sets the memory allocator, replacing the default memory allocator. + +This function also overwrites any given refstring and reffunction memory allocators, see [toy_refstring.h](toy_refstring_h.md). +!*/ TOY_API void Toy_setMemoryAllocator(Toy_MemoryAllocatorFn); diff --git a/source/toy_parser.h b/source/toy_parser.h index 314d6f77..f38e344d 100644 --- a/source/toy_parser.h +++ b/source/toy_parser.h @@ -1,10 +1,62 @@ #pragma once +/*! +# toy_parser.h + +This header defines the parser structure which, after being initialized with a lexer produces a series of abstract syntax trees to be passed to the compiler. The following is a utility function provided by [repl_tools.h](repl_tools_h.md), demonstrating how to use the parser. + +```c +//generate bytecode from a given string +const unsigned char* Toy_compileString(const char* source, size_t* size) { + //declare the relevant instances + Toy_Lexer lexer; + Toy_Parser parser; + Toy_Compiler compiler; + + //initialize each of them + Toy_initLexer(&lexer, source); + Toy_initParser(&parser, &lexer); + Toy_initCompiler(&compiler); + + //when the parser returns NULL, it is finished + Toy_ASTNode* node = Toy_scanParser(&parser); + while(node != NULL) { + //if the parser returns an error node, clean up and exit gracefully + if (node->type == TOY_AST_NODE_ERROR) { + Toy_freeASTNode(node); + Toy_freeCompiler(&compiler); + Toy_freeParser(&parser); + //no need to clean the lexer + return NULL; + } + + //write the node to the compiler + Toy_writeCompiler(&compiler, node); + Toy_freeASTNode(node); + + //grab the next node + node = Toy_scanParser(&parser); + } + + //get the bytecode to be returned + const unsigned char* tb = Toy_collateCompiler(&compiler, size); + + //cleanup + Toy_freeCompiler(&compiler); + Toy_freeParser(&parser); + //no need to clean the lexer + + //finally + return tb; +} +``` +!*/ + #include "toy_common.h" #include "toy_lexer.h" #include "toy_ast_node.h" -//DOCS: parsers are bound to a lexer, and turn the outputted tokens into AST nodes +//Parsers are bound to a lexer, and turn the outputted tokens into AST nodes typedef struct { Toy_Lexer* lexer; bool error; //I've had an error @@ -15,6 +67,37 @@ typedef struct { Toy_Token previous; } Toy_Parser; +/*! +## Defined Functions +!*/ + +/*! +### void Toy_initParser(Toy_Parser* parser, Toy_Lexer* lexer) + +This function initializes a `Toy_Parser`, binding the given `Toy_Lexer` to it. +!*/ TOY_API void Toy_initParser(Toy_Parser* parser, Toy_Lexer* lexer); + +/*! +### void Toy_freeParser(Toy_Parser* parser) + +This function frees a `Toy_Parser` once its task is completed. +!*/ TOY_API void Toy_freeParser(Toy_Parser* parser); + +/*! +### Toy_ASTNode* Toy_scanParser(Toy_Parser* parser) + +This function returns an abstract syntax tree representing part of the program, or an error node. The abstract syntax tree must be passed to `Toy_writeCompiler()` and/or `Toy_freeASTNode()`. + +This function should be called repeatedly until it returns `NULL`, indicating the end of the program. +!*/ TOY_API Toy_ASTNode* Toy_scanParser(Toy_Parser* parser); + +/*! +### void Toy_freeASTNode(Toy_ASTNode* node) + +This function cleans up any valid instance of `Toy_ASTNode` pointer passed to it. It is most commonly used to clean up the values returned by `Toy_scanParser`, after they have been passsed to `Toy_writeCompiler`, or when the node is an error node. + +Note: this function is *actually* defined in toy_ast_node.h, but documented here, because this is where it matters most. +!*/ diff --git a/source/toy_reffunction.h b/source/toy_reffunction.h index b51b8760..ec7637a8 100644 --- a/source/toy_reffunction.h +++ b/source/toy_reffunction.h @@ -1,10 +1,16 @@ #pragma once -#include "toy_common.h" +/*! +# toy_reffunction.h -//memory allocation hook -typedef void* (*Toy_RefFunctionAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); -TOY_API void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn); +This header defines the Toy_RefFunction structure, as well as all of the related utilities. + +See [Toy_RefString](toy_refstring_h.md) for more information about the reference pattern. + +This module reserves the right to instead preform a deep copy when it sees fit (this is for future debugging purposes). +!*/ + +#include "toy_common.h" //the RefFunction structure typedef struct Toy_RefFunction { @@ -13,11 +19,70 @@ typedef struct Toy_RefFunction { unsigned char data[]; } Toy_RefFunction; -//API +/*! +## Defined Interfaces +!*/ + +/*! +### typedef void* (*Toy_RefFunctionAllocatorFn)(void* pointer, size_t oldSize, size_t newSize) + +This interface conforms to Toy's memory API, and generally shouldn't be used without a good reason. +!*/ +typedef void* (*Toy_RefFunctionAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); + +/*! +## Defined Functions +!*/ + +/*! +### void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn) + +This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used without a good reason. +!*/ +TOY_API void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn); + +/*! +### Toy_RefFunction* Toy_createRefFunction(const void* data, size_t length) + +This function returns a new `Toy_RefFunction`, containing a copy of `data`, or `NULL` on error. + +This function also sets the returned `refFunction`'s reference counter to 1. +!*/ TOY_API Toy_RefFunction* Toy_createRefFunction(const void* data, size_t length); + +/*! +### void Toy_deleteRefFunction(Toy_RefFunction* refFunction) + +This function reduces the `refFunction`'s reference counter by 1 and, if it reaches 0, frees the memory. +!*/ TOY_API void Toy_deleteRefFunction(Toy_RefFunction* refFunction); + +/*! +### int Toy_countRefFunction(Toy_RefFunction* refFunction) + +This function returns the total number of references to `refFunction`, for debugging. +!*/ TOY_API int Toy_countRefFunction(Toy_RefFunction* refFunction); + +/*! +### size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction) + +This function returns the length of the underlying bytecode of `refFunction`. +!*/ TOY_API size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction); + +/*! +### Toy_RefFunction* Toy_copyRefFunction(Toy_RefFunction* refFunction) + +This function increases the reference counter of `refFunction` by 1, before returning the given pointer. + +This function reserves the right to create a deep copy where needed. +!*/ TOY_API Toy_RefFunction* Toy_copyRefFunction(Toy_RefFunction* refFunction); -TOY_API Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction); +/*! +### Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction) + +This function behaves identically to `Toy_copyRefFunction`, except that it explicitly forces a deep copy of the internal memory. Using this function should be done carefully, as it incurs a performance penalty that negates the benefit of this module. +!*/ +TOY_API Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction); diff --git a/source/toy_refstring.h b/source/toy_refstring.h index 2928b6ed..c0df7389 100644 --- a/source/toy_refstring.h +++ b/source/toy_refstring.h @@ -1,13 +1,24 @@ #pragma once +/*! +# toy_refstring.h + +This header defines the structure `Toy_RefString`, as well as all of the related utilities. + +[refstring](https://github.com/Ratstail91/refstring) is a stand-alone utility written to reduce the amount of memory manipulation used within Toy. It was independantly written and tested, before being incorporated into Toy proper. As such it has it's own memory management API, which by default is tied into Toy's [core memory API](toy_memory_h.md). + +Instances of `Toy_RefString` are reference counted - that is, rather than copying an existing string in memory, a pointer to the refstring is returned, and the internal reference counter is increased by 1. When the pointer is no longer needed, `Toy_DeleteRefString` can be called; this will decrement the internal reference counter by 1, and only free it when it reaches 0. This has multiple benefits, when used correctly: + +* Reduced memory usage +* Faster program execution + +This module reserves the right to instead preform a deep copy when it sees fit (this is for future debugging purposes). +!*/ + #include "toy_common.h" #include -//memory allocation hook -typedef void* (*Toy_RefStringAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); -TOY_API void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn); - //the RefString structure typedef struct Toy_RefString { size_t length; @@ -15,17 +26,100 @@ typedef struct Toy_RefString { char data[]; } Toy_RefString; -//API +/*! +## Defined Interfaces +!*/ + +/*! +### typedef void* (*Toy_RefStringAllocatorFn)(void* pointer, size_t oldSize, size_t newSize) + +This interface conforms to Toy's memory API, and generally shouldn't be used without a good reason. +!*/ +typedef void* (*Toy_RefStringAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); + +/*! +## Defined Functions +!*/ + +/*! +### void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn) + +This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used without a good reason. +!*/ +TOY_API void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn); + +/*! +### Toy_RefString* Toy_createRefString(const char* cstring) + +This function wraps `Toy_CreateRefStringLength`, by determining the length of the given `cstring` and passing it to the other function. +!*/ TOY_API Toy_RefString* Toy_createRefString(const char* cstring); + +/*! +### Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length) + +This function returns a new `Toy_RefString`, containing a copy of `cstring`, or `NULL` on error. + +This function also sets the returned refstring's reference counter to 1. +!*/ TOY_API Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length); + +/*! +### void Toy_deleteRefString(Toy_RefString* refString) + +This function reduces the `refString`'s reference counter by 1 and, if it reaches 0, frees the memory. +!*/ TOY_API void Toy_deleteRefString(Toy_RefString* refString); + +/*! +### int Toy_countRefString(Toy_RefString* refString) + +This function returns the total number of references to `refString`, for debugging. +!*/ TOY_API int Toy_countRefString(Toy_RefString* refString); + +/*! +### size_t Toy_lengthRefString(Toy_RefString* refString) + +This function returns the length of the underlying cstring of `refString`. +!*/ TOY_API size_t Toy_lengthRefString(Toy_RefString* refString); + +/*! +### Toy_RefString* Toy_copyRefString(Toy_RefString* refString) + +This function increases the reference counter of `refString` by 1, before returning the given pointer. + +This function reserves the right to create a deep copy where needed. +!*/ TOY_API Toy_RefString* Toy_copyRefString(Toy_RefString* refString); + +/*! +### Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString) + +This function behaves identically to `Toy_copyRefString`, except that it explicitly forces a deep copy of the internal memory. Using this function should be done carefully, as it incurs a performance penalty that negates the benefit of this module. +!*/ TOY_API Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString); + +/*! +### const char* Toy_toCString(Toy_RefString* refString) + +This function exposes the interal cstring of `refString`. Only use this function when dealing with external APIs. +!*/ TOY_API const char* Toy_toCString(Toy_RefString* refString); + +/*! +### bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs) + +This function returns true when the two refstrings are either the same refstring, or contain the same value. Otherwise it returns false. +!*/ TOY_API bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs); + +/*! +### bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring) + +This function returns true when the `refString` contains the same value as the `cstring`. Otherwise it returns false. +!*/ TOY_API bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring); //TODO: merge refstring memory - diff --git a/source/toy_scope.h b/source/toy_scope.h index 8f22504c..b6e3f3ff 100644 --- a/source/toy_scope.h +++ b/source/toy_scope.h @@ -1,5 +1,15 @@ #pragma once +/*! +# toy_scope.h + +This header defines the scope structure, which stores all of the variables used within a given block of code. + +Scopes are arranged into a linked list of ancestors, each of which is reference counted. When a scope is popped off the end of the chain, every ancestor scope has it's reference counter reduced by 1 and, if any reach 0, they are freed. + +This is also where Toy's type system lives. +!*/ + #include "toy_literal.h" #include "toy_literal_array.h" #include "toy_literal_dictionary.h" @@ -11,16 +21,70 @@ typedef struct Toy_Scope { int references; //how many scopes point here } Toy_Scope; +/*! +## Defined Functions +!*/ + +/*! +### Toy_Scope* Toy_pushScope(Toy_Scope* scope) + +This function creates a new `Toy_scope` with `scope` as it's ancestor, and returns it. +!*/ TOY_API Toy_Scope* Toy_pushScope(Toy_Scope* scope); + +/*! +### Toy_Scope* Toy_popScope(Toy_Scope* scope) + +This function frees the given `scope`, and returns it's ancestor. +!*/ TOY_API Toy_Scope* Toy_popScope(Toy_Scope* scope); + +/*! +### Toy_Scope* Toy_copyScope(Toy_Scope* original) + +This function copies an existing scope, and returns the copy. + +This copies the internal dictionaries, so it can be memory intensive. +!*/ TOY_API Toy_Scope* Toy_copyScope(Toy_Scope* original); -//returns false if error +/*! +### bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal type) + +This function declares a new variable `key` within `scope`, giving it the type of `type`. + +This function returns true on success, otherwise it returns failure (such as if the given key already exists). +!*/ TOY_API bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal type); + +/*! +### bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key) + +This function checks to see if a given variable with the name `key` has been previously declared. +!*/ TOY_API bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key); -//return false if undefined +/*! +### bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value, bool constCheck) + +This function sets an existing variable named `key` to the value of `value`. This function fails if `constCheck` is true and the given key's type has the constaant flag set. It also fails if the given key doesn't exist. + +This function returns true on success, otherwise it returns false. +!*/ TOY_API bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value, bool constCheck); + +/*! +### bool Toy_getScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal* value) + +This function sets the literal pointed to by `value` to equal the variable named `key`. + +This function returns true on success, otherwise it returns false. +!*/ TOY_API bool Toy_getScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal* value); +/*! +### Toy_Literal Toy_getScopeType(Toy_Scope* scope, Toy_Literal key) + +This function returns a new `Toy_Literal` representing the type of the variable named `key`. +!*/ TOY_API Toy_Literal Toy_getScopeType(Toy_Scope* scope, Toy_Literal key); diff --git a/tools/mecha.cpp b/tools/mecha.cpp index 04fe95cd..4200b1c2 100644 --- a/tools/mecha.cpp +++ b/tools/mecha.cpp @@ -81,6 +81,10 @@ int main(int argc, char** argv) { //finally is.close(); + if (buffer.length() == 0) { + continue; + } + std::ofstream os; std::string ofname = argv[fileCounter];