Docs now based on comments

This commit is contained in:
2023-07-21 02:52:29 +10:00
parent 0e4347c103
commit 2a5ffe6eb6
13 changed files with 852 additions and 792 deletions

View File

@@ -1,36 +1,36 @@
# toy_common.h
# 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; as such, they won't be documented here.
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
## Defined Macros
### TOY_API
### TOY_API
This definition of this macro is platform-dependant, and used to enable cross-platform compilation of shared and static libraries.
This definition of this macro is platform-dependant, and used to enable cross-platform compilation of shared and static libraries.
### TOY_VERSION_MAJOR
### 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.
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 its own version.
This value MUST fit into an unsigned char.
This value MUST fit into an unsigned char.
### TOY_VERSION_MINOR
### 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.
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.
This value MUST fit into an unsigned char.
### TOY_VERSION_PATCH
### TOY_VERSION_PATCH
The current patch version of Toy. This value is embedded into the bytecode.
The current patch version of Toy. This value is embedded into the bytecode.
This value MUST fit into an unsigned char.
This value MUST fit into an unsigned char.
### TOY_VERSION_BUILD
### TOY_VERSION_BUILD
The current build version of Toy. This value is embedded into the bytecode.
The current build version of Toy. This value is embedded into the bytecode.
This value is a 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 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 information about forks of the Toy codebase.
This macro may also be used to store additonal information about forks of the Toy codebase.

View File

@@ -1,32 +1,30 @@
# toy_compiler.h
# toy_compiler.h
This header defines the structure `Toy_Compiler`, which is used to transform abstract syntax trees into usable intermediate bytecode.
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.
There are two steps to generating intermediate 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 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 programs 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.
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](toy_common_h.md)) 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.
## Define Functions
Executing these functions out-of-order causes undefiend behaviour.
Executing the following functions out-of-order causes undefiend behaviour.
## Defined Functions
### void Toy_initCompiler(Toy_Compiler* compiler)
### void Toy_initCompiler(Toy_Compiler* compiler)
This function initializes the given compiler.
This function initializes the given `Toy_Compiler`.
### void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node)
### 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`.
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`.
### unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size)
### void Toy_freeCompiler(Toy_Compiler* compiler)
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`.
This function frees a `Toy_Compiler`. Calling this on a compiler which has not been collated will free that compiler as expected - anything written to it will be lost.
Calling `Toy_collateCompiler()` multiple times on the same compiler will produce undefined behaviour.
### unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size)
### void Toy_freeCompiler(Toy_Compiler* compiler)
This function returns a buffer of bytes, known as "bytecode", created from the given `Toy_Compiler`; it also stores the size of the bytecode in the variable pointed to by `size`.
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.
Calling `Toy_collateCompiler` multiple times on the same compiler will produce undefined behaviour.

View File

@@ -1,50 +1,54 @@
# toy_drive_system.h
# toy_drive_system.h
When accessing the file system through toy (such as with lib runner), 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.
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:
To use the drive system, first you must designate specific folders which can be accessed, like so:
```c
#include "toy_drive_system.h" ```c
#include "toy_drive_system.h"
int main(int argc, char* argv[]) {
//the drive system uses a LiteralDictionary, which must be initialized with this int main(int argc, char* argv[]) {
Toy_initDriveSystem(); //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("scripts", "assets/scripts");
Toy_setDrivePath("fonts", "assets/fonts"); Toy_setDrivePath("sprites", "assets/sprites");
Toy_setDrivePath("fonts", "assets/fonts");
//TODO: do you stuff here
//TODO: do you stuff here
//clean up the drive dictionary when you're done
Toy_freeDriveSystem(); //clean up the drive dictionary when you're done
Toy_freeDriveSystem();
return 0;
} return 0;
``` }
```
This utility is intended mainly for libraries to use - as such, the core of Toy does not utilize it.
This utility is intended mainly for libraries to use - as such, the core of Toy does not utilize it.
## Defined Functions
### Implementation Details
### void Toy_initDriveSystem()
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.
This function initializes the drive system.
## Defined Functions
### void Toy_freeDriveSystem()
### void Toy_initDriveSystem()
This function cleans up after the drive system is no longer needed.
This function initializes the drive system.
### void Toy_setDrivePath(char* drive, char* path)
### void Toy_freeDriveSystem()
This function sets a key-value pair in the drive system. It uses C strings, since its intended to be called directly from `main()`.
This function cleans up after the drive system is no longer needed.
### Toy_Literal Toy_getDrivePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral)
### void Toy_setDrivePath(char* drive, char* path)
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.
This function sets a key-value pair in the drive system. It uses C strings, since its intended to be called directly from `main()`.
The correct format is `drive:/path/to/filename`, where `drive` is a drive given to `Toy_setDrivePath()`.
### Toy_Literal Toy_getDrivePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral)
On failure, this function returns a null literal.
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.

View File

@@ -1,124 +1,128 @@
# toy_interpreter.h
# toy_interpreter.h
This header defines the structure `Toy_Interpreter`, which is the beating heart of Toy.
This header defines the interpreter structure, which is the beating heart of Toy.
The `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 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.
`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.
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
## 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.
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*)
### 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.
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)
### 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.
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`.
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)
### 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 whihc 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.
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`.
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; ```
``` 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.
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.
## Defined Functions
## Defined Functions
### void Toy_initInterpreter(Toy_Interpreter* interpreter)
### void Toy_initInterpreter(Toy_Interpreter* interpreter)
This function initializes the `Toy_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.
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.
### void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, size_t length)
### 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.
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.
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.
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.
### void Toy_resetInterpreter(Toy_Interpreter* interpreter)
### void Toy_resetInterpreter(Toy_Interpreter* interpreter)
This function frees any environment that the scripts have built up, and generates a new one. It also injects several globally available functions:
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 * set
* push * get
* pop * push
* length * pop
* clear * length
* clear
### void Toy_freeInterpreter(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.
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.
### bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_NativeFn func)
### 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 name passed 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.
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.
The primary use of this function is within hooks.
### bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_HookFn hook)
### 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 name passed in 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.
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.
### bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns)
Hooks are invoked with the `import` keyword within Toy's scripts.
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.
### bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns)
The literal `func` can be either a native function or a Toy function, but it won't execute a hook.
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.
### bool Toy_callFn(Toy_Interpreter* interpreter, const char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns)
The literal `func` can be either a native function or a Toy function, but it won't execute a hook.
This utility function will find a `Toy_literal` within the `Toy_Interpreter`'s scope with a name that matches `name`, and will invoke it using `Toy_callLiteralFn` (passing in `arguments` and `returns` as expected).
### bool Toy_callFn(Toy_Interpreter* interpreter, const char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns)
### bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* literalPtr)
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).
Sometimes, native functions will receive `Toy_Literal` identifiers instead of the values - the values can be retreived from the given interpreter's scope using the following pattern:
### bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* literalPtr)
```c
Toy_Literal foobarIdn = foobar; 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:
if (TOY_IS_IDENTIFIER(foobar) && Toy_parseIdentifierToValue(interpreter, &foobar)) {
freeLiteral(foobarIdn); //remember to free the identifier ```c
} Toy_Literal foobarIdn = foobar;
``` if (TOY_IS_IDENTIFIER(foobar) && Toy_parseIdentifierToValue(interpreter, &foobar)) {
freeLiteral(foobarIdn); //remember to free the identifier
### 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:
### void Toy_setInterpreterPrint(Toy_Interpreter* interpreter, Toy_PrintFn printOutput)
```c
static void printWrapper(const char* output) { This function sets the function called by the `print` keyword. By default, the following wrapper is used:
printf("%s\n", output);
} ```c
``` static void printWrapper(const char* output) {
printf("%s\n", output);
### 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:
Note: The above is a very minor lie - in reality there are some preprocessor directives to allow the repl's `-n` flag to work.
```c
static void assertWrapper(const char* output) { ### void Toy_setInterpreterAssert(Toy_Interpreter* interpreter, Toy_PrintFn assertOutput)
fprintf(stderr, "Assertion failure: %s\n", output);
} This function sets the function called by the `assert` keyword on failure. By default, the following wrapper is used:
```
```c
### void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn errorOutput) static void assertWrapper(const char* output) {
fprintf(stderr, "Assertion failure: %s\n", output);
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) { ### void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn errorOutput)
fprintf(stderr, "%s", output); //no newline
} 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
}
```

View File

@@ -1,10 +1,28 @@
# toy_lexer.h
# toy_lexer.h
This header defines the structure `Toy_Lexer`, which can be bound to a piece of source code, and used to tokenize it within a parser.
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.
## Defined Functions
## Defined Functions
### void Toy_initLexer(Toy_Lexer* lexer, const char* source)
### 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.
This function initializes a lexer, binding it to the `source` parameter; the lexer is now ready to be passed to the parser.
### 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.
### void Toy_private_printToken(Toy_Token* token)
This function prints a given token to stdout.
Private functions are not intended for general use.
### 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.

View File

@@ -1,38 +1,40 @@
# literal_array.h
# literal_array.h
This header defines the structure `Toy_LiteralArray`, 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.
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.
The array type is one of two fundemental data structures used throughout Toy - the other is the dictionary.
## Defined Functions
## Defined Functions
### void Toy_initLiteralArray(Toy_LiteralArray* array)
### void Toy_freeLiteralArray(Toy_LiteralArray* array)
This function initializes a `Toy_LiteralArray` pointed to by `array`.
This function frees a `Toy_LiteralArray` pointed to by `array`. Every literal within is passed to `Toy_freeLiteral()` before its memory is released.
### void Toy_freeLiteralArray(Toy_LiteralArray* array)
### int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal)
This function frees a `Toy_LiteralArray` pointed to by `array`. Every literal within is passed to `Toy_freeLiteral` before its memory is released.
This function adds a new `literal` to the end of the `array`, growing the array's internal buffer if needed.
### int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal)
This function returns the index of the inserted value.
This function adds a new `literal` to the end of the `array`, growing the array's internal buffer if needed.
### Toy_Literal Toy_popLiteralArray(Toy_LiteralArray* array)
This function returns the index of the inserted value.
This function removes the literal at the end of the `array`, and returns it.
### Toy_Literal Toy_popLiteralArray(Toy_LiteralArray* array)
### bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy_Literal value)
This function removes the literal at the end of the `array`, and returns it.
This function frees the literal at the position represented by the integer literal `index`, and stores `value` in its place.
### bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy_Literal value)
This function returns true on success, otherwise it returns false.
This function frees the literal at the position represented by the integer literal `index`, and stores `value` in its place.
### Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index)
This function returns true on success, otherwise it returns false.
This function returns the literal at the position represented by the integer literal `index`, or returns a null literal if none is found.
### Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index)
If `index` is not an integer literal or is out of bounds, this function returns a null literal.
This function returns the literal at the position represented by the integer literal `index`, or returns a null literal if none is found.
### int Toy_private_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal)
If `index` is not an integer literal or is out of bounds, this function returns a null 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.

View File

@@ -1,38 +1,50 @@
# toy_literal_dictionary.h
# toy_literal_dictionary.h
This header defines the structure `Toy_LiteralDictionary`, 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.
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.
The dictionary type is one of two fundemental data structures used throughout Toy - the other is the array.
## Defined Functions
## Defined Macros
### void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary);
### TOY_DICTIONARY_MAX_LOAD
This function initializes the `Toy_LiteralDictionary` pointed to by `dictionary`.
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.
### void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary);
Since this process can be memory and time intensive, a configurable macro is used to allow for fine-grained control across the lang.
This function frees a `Toy_LiteralDictionary` pointed to by `dictionary`. Every literal within is passed to `Toy_freeLiteral` before its memory is released.
The current default value is `0.75`, representing 75% capacity.
### void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value);
## Defined Functions
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.
### void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary)
Literal functions and opaques cannot be used as keys.
This function initializes the `Toy_LiteralDictionary` pointed to by `dictionary`.
### Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key);
### void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary)
This function returns the value of the literal within `dictionary` identified by `key`, or a null literal if it doesn't exist.
This function frees a `Toy_LiteralDictionary` pointed to by `dictionary`. Every literal within is passed to `Toy_freeLiteral()` before its memory is released.
Literal functions and opaques cannot be used as keys.
### void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value)
### void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key);
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.
This function removes the key-value pair of literals from `dictionary` identified by `key`, if it exists.
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.
Literal functions and opaques cannot be used as keys.
### bool Toy_existsLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key);
### Toy_Literal Toy_getLiteralDictionary(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.
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.
### 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.
### 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.

View File

@@ -1,163 +1,183 @@
# toy_literal.h
# toy_literal.h
This header defines the structure `Toy_Literal`, which is used extensively throughout Toy to represent values of some kind.
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.
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.
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.
## Defined Enums
## Defined Enums
### Toy_LiteralType
### Toy_LiteralType
* `TOY_LITERAL_NULL`
* `TOY_LITERAL_BOOLEAN` * `TOY_LITERAL_NULL`
* `TOY_LITERAL_INTEGER` * `TOY_LITERAL_BOOLEAN`
* `TOY_LITERAL_FLOAT` * `TOY_LITERAL_INTEGER`
* `TOY_LITERAL_STRING` * `TOY_LITERAL_FLOAT`
* `TOY_LITERAL_ARRAY` * `TOY_LITERAL_STRING`
* `TOY_LITERAL_DICTIONARY` * `TOY_LITERAL_ARRAY`
* `TOY_LITERAL_FUNCTION` * `TOY_LITERAL_DICTIONARY`
* `TOY_LITERAL_FUNCTION_NATIVE` * `TOY_LITERAL_FUNCTION`
* `TOY_LITERAL_FUNCTION_HOOK` * `TOY_LITERAL_FUNCTION_NATIVE`
* `TOY_LITERAL_IDENTIFIER` * `TOY_LITERAL_FUNCTION_HOOK`
* `TOY_LITERAL_TYPE` * `TOY_LITERAL_IDENTIFIER`
* `TOY_LITERAL_OPAQUE` * `TOY_LITERAL_TYPE`
* `TOY_LITERAL_ANY` * `TOY_LITERAL_OPAQUE`
* `TOY_LITERAL_ANY`
These are the main possible 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.
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.
## Defined Macros
Other type values are possible, but are only used internally.
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.
## Defined Macros
* `TOY_IS_NULL(value)`
* `TOY_IS_BOOLEAN(value)` 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_INTEGER(value)`
* `TOY_IS_FLOAT(value)` * `TOY_IS_NULL(value)`
* `TOY_IS_STRING(value)` * `TOY_IS_BOOLEAN(value)`
* `TOY_IS_ARRAY(value)` * `TOY_IS_INTEGER(value)`
* `TOY_IS_DICTIONARY(value)` * `TOY_IS_FLOAT(value)`
* `TOY_IS_FUNCTION(value)` * `TOY_IS_STRING(value)`
* `TOY_IS_FUNCTION_NATIVE(value)` * `TOY_IS_ARRAY(value)`
* `TOY_IS_FUNCTION_HOOK(value)` * `TOY_IS_DICTIONARY(value)`
* `TOY_IS_IDENTIFIER(value)` * `TOY_IS_FUNCTION(value)`
* `TOY_IS_TYPE(value)` * `TOY_IS_FUNCTION_NATIVE(value)`
* `TOY_IS_OPAQUE(value)` * `TOY_IS_FUNCTION_HOOK(value)`
* `TOY_IS_IDENTIFIER(value)`
The following macros are used to cast a literal to a specific type to be used. * `TOY_IS_TYPE(value)`
* `TOY_IS_OPAQUE(value)`
* `TOY_AS_BOOLEAN(value)`
* `TOY_AS_INTEGER(value)` The following macros are used to cast a literal to a specific C type to be used.
* `TOY_AS_FLOAT(value)`
* `TOY_AS_STRING(value)` * `TOY_AS_BOOLEAN(value)`
* `TOY_AS_ARRAY(value)` * `TOY_AS_INTEGER(value)`
* `TOY_AS_DICTIONARY(value)` * `TOY_AS_FLOAT(value)`
* `TOY_AS_FUNCTION(value)` * `TOY_AS_STRING(value)`
* `TOY_AS_FUNCTION_NATIVE(value)` * `TOY_AS_ARRAY(value)`
* `TOY_AS_FUNCTION_HOOK(value)` * `TOY_AS_DICTIONARY(value)`
* `TOY_AS_IDENTIFIER(value)` * `TOY_AS_FUNCTION(value)`
* `TOY_AS_TYPE(value)` * `TOY_AS_FUNCTION_NATIVE(value)`
* `TOY_AS_OPAQUE(value)` * `TOY_AS_FUNCTION_HOOK(value)`
* `TOY_AS_IDENTIFIER(value)`
The following macros are used to create a new literal, with the given `value` as it's internal value. * `TOY_AS_TYPE(value)`
* `TOY_AS_OPAQUE(value)`
* `TOY_TO_NULL_LITERAL` - does not need parantheses
* `TOY_TO_BOOLEAN_LITERAL(value)` The following macros are used to create a new literal, with the given `value` as it's internal value.
* `TOY_TO_INTEGER_LITERAL(value)`
* `TOY_TO_FLOAT_LITERAL(value)` * `TOY_TO_NULL_LITERAL` - does not need parantheses
* `TOY_TO_STRING_LITERAL(value)` * `TOY_TO_BOOLEAN_LITERAL(value)`
* `TOY_TO_ARRAY_LITERAL(value)` * `TOY_TO_INTEGER_LITERAL(value)`
* `TOY_TO_DICTIONARY_LITERAL(value)` * `TOY_TO_FLOAT_LITERAL(value)`
* `TOY_TO_FUNCTION_LITERAL(value, l)` - `l` represents the length of the bytecode passed as `value` * `TOY_TO_STRING_LITERAL(value)`
* `TOY_TO_FUNCTION_NATIVE_LITERAL(value)` * `TOY_TO_ARRAY_LITERAL(value)`
* `TOY_TO_FUNCTION_HOOK_LITERAL(value)` * `TOY_TO_DICTIONARY_LITERAL(value)`
* `TOY_TO_IDENTIFIER_LITERAL(value)` * `TOY_TO_FUNCTION_LITERAL(value, l)` - `l` represents the length of the bytecode passed as `value`
* `TOY_TO_TYPE_LITERAL(value, c)` - `c` is the true of the type should be const * `TOY_TO_FUNCTION_NATIVE_LITERAL(value)`
* `TOY_TO_OPAQUE_LITERAL(value, t)` - `t` is the integer tag * `TOY_TO_FUNCTION_HOOK_LITERAL(value)`
* `TOY_TO_IDENTIFIER_LITERAL(value)`
## More Defined Macros * `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
The following macros are utilities used throughout Toy's internals, and are available for the user as well.
## More Defined Macros
### TOY_IS_TRUTHY(x)
The following macros are utilities used throughout Toy's internals, and are available for the user as well.
Returns true of the literal `x` is truthy, otherwise it returns false.
### TOY_IS_TRUTHY(x)
Currently, every value is considered truthy except `false`, which is falsy and `null`, which is neither true or false.
Returns true of the literal `x` is truthy, otherwise it returns false.
### TOY_AS_FUNCTION_BYTECODE_LENGTH(lit)
Currently, every value is considered truthy except `false`, which is falsy and `null`, which is neither true or false.
Returns the length of a Toy function's bytecode.
### TOY_AS_FUNCTION_BYTECODE_LENGTH(lit)
This macro is only valid on `TOY_LITERAL_FUNCTION`.
Returns the length of a Toy function's bytecode.
### TOY_MAX_STRING_LENGTH
This macro is only valid on `TOY_LITERAL_FUNCTION`.
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.
### TOY_MAX_STRING_LENGTH
### TOY_HASH_I(lit)
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.
Identifiers are the names of 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.
### TOY_HASH_I(lit)
This macro is only valid on `TOY_LITERAL_IDENTIFIER`.
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.
### TOY_TYPE_PUSH_SUBTYPE(lit, subtype)
This macro is only valid on `TOY_LITERAL_IDENTIFIER`.
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.
### TOY_TYPE_PUSH_SUBTYPE(lit, subtype)
This macro returns the index of the newly pushed value within it's parent.
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 is only valid on `TOY_LITERAL_TYPE`, for both `type` and `subtype`.
This macro returns the index of the newly pushed value within it's parent.
### TOY_GET_OPAQUE_TAG(o)
This macro is only valid on `TOY_LITERAL_TYPE`, for both `type` and `subtype`.
Returns the value of the opaque `o`'s tag.
### TOY_GET_OPAQUE_TAG(o)
This macro is only valid on `TOY_LITERAL_OPAQUE`.
Returns the value of the opaque `o`'s tag.
## Defined Functions
This macro is only valid on `TOY_LITERAL_OPAQUE`.
### void Toy_freeLiteral(Toy_Literal literal)
## Defined Functions
This function frees the given literal's memory. Any internal pointers are now invalid.
### void Toy_freeLiteral(Toy_Literal literal)
This function should be called on EVERY literal when it is no longer needed, regardless of type.
This function frees the given literal's memory. Any internal pointers are now invalid.
### Toy_Literal Toy_copyLiteral(Toy_Literal original)
This function should be called on EVERY literal when it is no longer needed, regardless of type.
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_Literal Toy_copyLiteral(Toy_Literal original)
### bool Toy_literalsAreEqual(Toy_Literal lhs, Toy_Literal rhs)
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.
This checks to see if two given literals are equal.
### bool Toy_literalsAreEqual(Toy_Literal lhs, Toy_Literal rhs)
When an integer and a float are compared, the integer is cooerced into a float for the duration of the call.
This checks to see if two given literals are equal.
Arrays and dictionaries are equal only if their keys and values all equal. Likewise, types only equal if all subtypes are equal, in order.
When an integer and a float are compared, the integer is cooerced into a float for the duration of the call.
Functions and opaques are never equal to anything, while values with the type `TOY_LITERAL_ANY` are always equal.
Arrays or dictionaries are equal only if their keys and values all equal. Likewise, types only equal if all subtypes are equal, in order.
### int Toy_hashLiteral(Toy_Literal lit)
Functions and opaques are never equal to anything, while values with the type `TOY_LITERAL_ANY` are always equal.
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.
### int Toy_hashLiteral(Toy_Literal lit)
types that can't be hashed are
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.
* all kinds of functions
* type types that can't be hashed are
* opaque
* any * all kinds of functions
* type
In the case of identifiers, their hashes are precomputed on creation and are stored within the literal. * opaque
* any
### void Toy_printLiteral(Toy_Literal literal)
In the case of identifiers, their hashes are precomputed on creation and are stored within the literal.
This wraps a call to `Toy_printLiteralCustom`, with a printf-stdout wrapper as `printFn`.
### void Toy_printLiteral(Toy_Literal literal)
### void Toy_printLiteralCustom(Toy_Literal literal, PrintFn printFn)
This wraps a call to `Toy_printLiteralCustom`, with a printf-stdout wrapper as `printFn`.
This function passes the string representation of `literal` to `printFn`.
### void Toy_printLiteralCustom(Toy_Literal literal, PrintFn 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.
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.
### bool Toy_private_isTruthy(Toy_Literal x)
Utilized by the `TOY_IS_TRUTHY` macro.
Private functions are not intended for general use.
### bool Toy_private_toIdentifierLiteral(Toy_RefString* ptr)
Utilized by the `TOY_TO_IDENTIFIER_LITERAL` macro.
Private functions are not intended for general use.
### 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.

View File

@@ -1,70 +1,64 @@
# toy_memory.h
# toy_memory.h
This header defines all of the memory management utilities. Any and all heap-based memory management goes through these utilities.
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.
A default memory allocator function is used internally, but it can be overwritten for diagnostic and platform related purposes.
## Defined Macros
## Defined Macros
### TOY_GROW_CAPACITY(capacity)
### TOY_GROW_CAPACITY(capacity)
This macro calculates, in place, what size of memory should be allocated based on the previous size.
This macro calculates, in place, what size of memory should be allocated based on the previous size.
### TOY_GROW_CAPACITY_FAST(capacity)
### 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`.
This macro calculates, in place, what size of memory should be allocated based on the previous size. It grows faster than `TOY_GROW_CAPACITY`.
### TOY_ALLOCATE(type, count)
### TOY_FREE(type, pointer)
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 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.
This returns a pointer of `type`.
### TOY_FREE_ARRAY(type, pointer, oldCount)
### TOY_FREE(type, pointer)
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.
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.
### TOY_GROW_ARRAY(type, pointer, oldCount, count)
### TOY_FREE_ARRAY(type, pointer, oldCount)
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 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.
This returns a pointer of `type`.
### TOY_GROW_ARRAY(type, pointer, oldCount, 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 (usually calculated with `TOY_GROW_CAPACITY`).
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`.
This returns a pointer of `type`.
### TOY_SHRINK_ARRAY(type, pointer, oldCount, count)
## Defined Interfaces
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.
### typedef void* (*Toy_MemoryAllocatorFn)(void* pointer, size_t oldSize, size_t newSize)
This returns a pointer of `type`.
This function interface is used for defining any memory allocator functions.
## Defined Interfaces
Any and all memory allocator functions should:
### typedef void* (*Toy_MemoryAllocatorFn)(void* pointer, size_t oldSize, size_t newSize)
* Take a `pointer` to a previously allocated block of memory, or `NULL`
This function interface is used for defining any memory allocator functions. * 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)
Any and all memory allocator functions should: * Return the newly allocated buffer, or `NULL` if `newSize` is zero
* Return `NULL` on error
* 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) ## Defined Functions
* 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 ### TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize)
* Return `NULL` on error
This function shouldn't be called directly. Instead, use one of the given macros.
## Defined Functions
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 shouldn't be called directly. Instead, use one of the given macros.
This function sets the memory allocator, replacing the default memory allocator.
This function wraps a call to the internal assigned memory allocator.
This function also overwrites any given refstring and reffunction memory allocators, see [toy_refstring.h](toy_refstring_h.md).
### void Toy_setMemoryAllocator(Toy_MemoryAllocatorFn)
This function sets the memory allocator, replacing the default memory allocator.
This function also overwrites any given refstring memory allocator, see [toy_refstring.h](toy_refstring_h.md).

View File

@@ -1,72 +1,72 @@
# toy_parser.h
# toy_parser.h
This header defines the structure `Toy_Parser` which, after being initialized with a `Toy_Lexer` produces a series of abstract syntax trees to be passed to the `Toy_Compiler`. The following is a utility function provided by [repl_tools.h](repl_tools_h.md), demonstrating how to use the parser.
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 ```c
const unsigned char* Toy_compileString(const char* source, size_t* size) { //generate bytecode from a given string
//declare the relevant instances const unsigned char* Toy_compileString(const char* source, size_t* size) {
Toy_Lexer lexer; //declare the relevant instances
Toy_Parser parser; Toy_Lexer lexer;
Toy_Compiler compiler; Toy_Parser parser;
Toy_Compiler compiler;
//initialize each of them
Toy_initLexer(&lexer, source); //initialize each of them
Toy_initParser(&parser, &lexer); Toy_initLexer(&lexer, source);
Toy_initCompiler(&compiler); Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
//when the parser returns NULL, it is finished
Toy_ASTNode* node = Toy_scanParser(&parser); //when the parser returns NULL, it is finished
while(node != NULL) { Toy_ASTNode* node = Toy_scanParser(&parser);
//if the parser returns an error node, clean up and exit gracefully while(node != NULL) {
if (node->type == TOY_AST_NODE_ERROR) { //if the parser returns an error node, clean up and exit gracefully
Toy_freeASTNode(node); if (node->type == TOY_AST_NODE_ERROR) {
Toy_freeCompiler(&compiler); Toy_freeASTNode(node);
Toy_freeParser(&parser); Toy_freeCompiler(&compiler);
//no need to clean the lexer Toy_freeParser(&parser);
return NULL; //no need to clean the lexer
} return NULL;
}
//write the node to the compiler
Toy_writeCompiler(&compiler, node); //write the node to the compiler
Toy_freeASTNode(node); Toy_writeCompiler(&compiler, node);
Toy_freeASTNode(node);
//grab the next node
node = Toy_scanParser(&parser); //grab the next node
} node = Toy_scanParser(&parser);
}
//get the bytecode to be returned
const unsigned char* tb = Toy_collateCompiler(&compiler, size); //get the bytecode to be returned
const unsigned char* tb = Toy_collateCompiler(&compiler, size);
//cleanup
Toy_freeCompiler(&compiler); //cleanup
Toy_freeParser(&parser); Toy_freeCompiler(&compiler);
//no need to clean the lexer Toy_freeParser(&parser);
//no need to clean the lexer
//finally
return tb; //finally
} return tb;
``` }
```
## Defined Functions
## Defined Functions
### void Toy_initParser(Toy_Parser* parser, Toy_Lexer* lexer)
### void Toy_initParser(Toy_Parser* parser, Toy_Lexer* lexer)
This function initializes a `Toy_Parser`, binding the given `Toy_Lexer` to it.
This function initializes a `Toy_Parser`, binding the given `Toy_Lexer` to it.
### void Toy_freeParser(Toy_Parser* parser)
### void Toy_freeParser(Toy_Parser* parser)
This function frees a `Toy_Parser` once its task is completed.
This function frees a `Toy_Parser` once its task is completed.
### Toy_ASTNode* Toy_scanParser(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 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.
This function should be called repeatedly until it returns `NULL`, indicating the end of the program.
### void Toy_freeASTNode(Toy_ASTNode* node)
### 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.
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.
Note: this function is *actually* defined in toy_ast_node.h, but documented here, because this is where it matters most.

View File

@@ -1,46 +1,48 @@
# toy_reffunction.h
# toy_reffunction.h
This header defines the structure `Toy_RefFunction`, as well as all of the related utilities.
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.
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).
This module reserves the right to instead preform a deep copy when it sees fit (this is for future debugging purposes).
## Defined Interfaces
## Defined Interfaces
### typedef void* (*Toy_RefFunctionAllocatorFn)(void* pointer, size_t oldSize, size_t newSize)
### 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.
This interface conforms to Toy's memory API, and generally shouldn't be used without a good reason.
## Defined Functions
## Defined Functions
### void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn)
### void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn)
This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used.
This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used without a good reason.
### Toy_RefFunction* Toy_createRefFunction(const void* data, size_t length)
### 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 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.
This function also sets the returned `refFunction`'s reference counter to 1.
### void Toy_deleteRefFunction(Toy_RefFunction* refFunction)
### void Toy_deleteRefFunction(Toy_RefFunction* refFunction)
This function reduces the `refFunction`'s reference counter by 1 and, if it reaches 0, frees the memory.
This function reduces the `refFunction`'s reference counter by 1 and, if it reaches 0, frees the memory.
### int Toy_countRefFunction(Toy_RefFunction* refFunction)
### int Toy_countRefFunction(Toy_RefFunction* refFunction)
This function returns the total number of references to `refFunction`, for debugging.
This function returns the total number of references to `refFunction`, for debugging.
### size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction)
### size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction)
This function returns the length of the underlying bytecode of `refFunction`.
This function returns the length of the underlying bytecode of `refFunction`.
### Toy_RefFunction* Toy_copyRefFunction(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 increases the reference counter of `refFunction` by 1, before returning the given pointer.
### Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction)
This function reserves the right to create a deep copy where needed.
This function behaves identically to `Toy_copyRefFunction`, except that it explicitly preforms 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_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.

View File

@@ -1,67 +1,69 @@
# toy_refstring.h
# toy_refstring.h
This header defines the structure `Toy_RefString`, as well as all of the related utilities.
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).
[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:
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 * 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).
This module reserves the right to instead preform a deep copy when it sees fit (this is for future debugging purposes).
## Defined Interfaces
## Defined Interfaces
### typedef void* (*Toy_RefStringAllocatorFn)(void* pointer, size_t oldSize, size_t newSize)
### 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.
This interface conforms to Toy's memory API, and generally shouldn't be used without a good reason.
## Defined Functions
## Defined Functions
### void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn)
### void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn)
This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used.
This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used without a good reason.
### Toy_RefString* Toy_createRefString(const char* cstring)
### 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.
This function wraps `Toy_CreateRefStringLength`, by determining the length of the given `cstring` and passing it to the other function.
### Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length)
### 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 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.
This function also sets the returned refstring's reference counter to 1.
### void Toy_deleteRefString(Toy_RefString* refString)
### void Toy_deleteRefString(Toy_RefString* refString)
This function reduces the `refString`'s reference counter by 1 and, if it reaches 0, frees the memory.
This function reduces the `refString`'s reference counter by 1 and, if it reaches 0, frees the memory.
### int Toy_countRefString(Toy_RefString* refString)
### int Toy_countRefString(Toy_RefString* refString)
This function returns the total number of references to `refString`, for debugging.
This function returns the total number of references to `refString`, for debugging.
### size_t Toy_lengthRefString(Toy_RefString* refString)
### size_t Toy_lengthRefString(Toy_RefString* refString)
This function returns the length of the underlying cstring of `refString`.
This function returns the length of the underlying cstring of `refString`.
### Toy_RefString* Toy_copyRefString(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 increases the reference counter of `refString` by 1, before returning the given pointer.
### Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString)
This function reserves the right to create a deep copy where needed.
This function behaves identically to `Toy_copyRefString`, except that it explicitly preforms 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_RefString* Toy_deepCopyRefString(Toy_RefString* refString)
### const char* Toy_toCString(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.
This function exposes the interal cstring of `refString`. Only use this function when dealing with external APIs.
### const char* Toy_toCString(Toy_RefString* refString)
### bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs)
This function exposes the interal cstring of `refString`. Only use this function when dealing with external APIs.
This function returns true when the two refstrings are either the same refstring, or contain the same value. Otherwise it returns false.
### bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs)
### bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring)
This function returns true when the two refstrings are either the same refstring, or contain the same value. Otherwise it returns false.
This function returns true when the `refString` contains the same value as the `cstring`. Otherwise it returns false.
### 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.

View File

@@ -1,46 +1,50 @@
# toy_scope.h
# toy_scope.h
This header defines the `Toy_Scope` structure, which stores all of the variables used within a given block of code.
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.
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.
## Defined Functions
This is also where Toy's type system lives.
### Toy_Scope* Toy_pushScope(Toy_Scope* scope)
## Defined Functions
This function creates a new `Toy_scope` with `scope` as it's ancestor, and returns it.
### Toy_Scope* Toy_pushScope(Toy_Scope* scope)
### Toy_Scope* Toy_popScope(Toy_Scope* scope)
This function creates a new `Toy_scope` with `scope` as it's ancestor, and returns it.
This function frees the given `scope`, and returns it's ancestor.
### Toy_Scope* Toy_popScope(Toy_Scope* scope)
### Toy_Scope* Toy_copyScope(Toy_Scope* original)
This function frees the given `scope`, and returns it's ancestor.
This function copies an existing scope, and returns the copy.
### Toy_Scope* Toy_copyScope(Toy_Scope* original)
### bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal type)
This function copies an existing scope, and returns the copy.
This function declares a new variable `key` within `scope`, giving it the type of `type`.
This copies the internal dictionaries, so it can be memory intensive.
This function returns true on success, otherwise it returns failure (such as if the given key already exists).
### bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal type)
### bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key)
This function declares a new variable `key` within `scope`, giving it the type of `type`.
This function checks to see if a given variable with the name `key` has been previously declared.
This function returns true on success, otherwise it returns failure (such as if the given key already exists).
### bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value, bool constCheck)
### bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key)
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 checks to see if a given variable with the name `key` has been previously declared.
This function returns true on success, otherwise it returns false.
### 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 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 sets the literal pointed to by `value` to equal the variable named `key`.
This function returns true on success, otherwise it returns false.
This function returns true on success, otherwise it returns false.
### bool Toy_getScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal* value)
### Toy_Literal Toy_getScopeType(Toy_Scope* scope, Toy_Literal key)
This function sets the literal pointed to by `value` to equal the variable named `key`.
This function returns a new `Toy_Literal` representing the type of the variable named `key`.
This function returns true on success, otherwise it returns false.
### 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`.