diff --git a/makefile b/makefile index 9ee51fc..42b8f50 100644 --- a/makefile +++ b/makefile @@ -57,12 +57,14 @@ build-mecha: $(TOY_OUTDIR) build-docs: build-mecha $(TOY_OUTDIR)/mecha $(wildcard source/*.h) + $(TOY_OUTDIR)/mecha $(wildcard repl/*.h) docs: mkdir docs move-docs: docs mv -u $(wildcard source/*.md) docs + mv -u $(wildcard repl/*.md) docs documentation: $(MAKE) build-docs diff --git a/repl/lib_about.h b/repl/lib_about.h index 26b1d4a..c7ba214 100644 --- a/repl/lib_about.h +++ b/repl/lib_about.h @@ -3,4 +3,3 @@ #include "toy_interpreter.h" int Toy_hookAbout(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias); - diff --git a/repl/lib_random.h b/repl/lib_random.h index 2dfae4d..9614506 100644 --- a/repl/lib_random.h +++ b/repl/lib_random.h @@ -2,6 +2,6 @@ #include "toy_interpreter.h" -int Toy_hookRandom(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias); - #define TOY_OPAQUE_TAG_RANDOM 200 + +int Toy_hookRandom(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias); diff --git a/repl/lib_runner.h b/repl/lib_runner.h index 549c2ac..57123a4 100644 --- a/repl/lib_runner.h +++ b/repl/lib_runner.h @@ -2,7 +2,8 @@ #include "toy_interpreter.h" -int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias); - #define TOY_OPAQUE_TAG_RUNNER 100 +int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias); + + diff --git a/repl/lib_standard.h b/repl/lib_standard.h index d1b9816..70c73f6 100644 --- a/repl/lib_standard.h +++ b/repl/lib_standard.h @@ -3,4 +3,3 @@ #include "toy_interpreter.h" int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias); - diff --git a/repl/repl_tools.h b/repl/repl_tools.h index 5fda23e..8226476 100644 --- a/repl/repl_tools.h +++ b/repl/repl_tools.h @@ -1,15 +1,80 @@ #pragma once +/*! +# repl_tools.h + +This header provides a number of tools for compiling and running Toy, and is used primarily by the repl. However, it can also be modified and used by any host program with a little effort. + +This is not a core part of Toy or a library, and as such `repl_tools.h` and `repl_tools.c` can both be found in the `repl/` folder. +!*/ + #include "toy_common.h" +/*! +### const char* Toy_readFile(const char* path, size_t* fileSize) + +This function reads in a file, and returns it as a constant buffer. It also sets the variable pointed to by `fileSize` to the size of the given buffer. + +On error, this function returns `NULL`. +!*/ const unsigned char* Toy_readFile(const char* path, size_t* fileSize); + +/*! +### int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size) + +This function writes the buffer pointed to by `bytes` to a file specified by `path`. The buffer's size should be specified by `size`. + +On error, this function returns a non-zero value. +!*/ int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size); +/*! +### const unsigned char* Toy_compileString(const char* source, size_t* size) + +This function takes a cstring of Toy source code, and returns a compiled buffer based on that source code. The variable pointed to by `size` is set to the size of the bytecode. + +On error, this function returns `NULL`. +!*/ const unsigned char* Toy_compileString(const char* source, size_t* size); +/*! +### void Toy_runBinary(const unsigned char* tb, size_t size) + +This function takes a bytecode array of `size` size, and executes it. The libraries available to the code are currently: + +* lib_about +* lib_standard +* lib_random +* lib_runner +!*/ void Toy_runBinary(const unsigned char* tb, size_t size); + +/*! +### void Toy_runBinaryFile(const char* fname) + +This function loads in the binary file specified by `fname`, and passes it to `Toy_runBinary()`. +!*/ void Toy_runBinaryFile(const char* fname); + +/*! +### void Toy_runSource(const char* source) + +This function compiles the source with `Toy_compileString()`, and passes it to `Toy_runBinary()`. +!*/ void Toy_runSource(const char* source); + +/*! +### void Toy_runSourceFile(const char* fname) + +This function loads in the file specified by `fname`, compiles it, and passes it to `Toy_runBinary()`. +!*/ void Toy_runSourceFile(const char* fname); +/*! +### void Toy_parseBinaryFileHeader(const char* fname) + +This function parses the header information stored within the bytecode file `fname`. + +This is only used for debugging and validation purposes. +!*/ void Toy_parseBinaryFileHeader(const char* fname); \ No newline at end of file diff --git a/source/toy.h b/source/toy.h index 7b11af2..cdbf01e 100644 --- a/source/toy.h +++ b/source/toy.h @@ -1,69 +1,88 @@ #pragma once -/* toy.h - A Toy Programming Language +/*! +# toy.h - A Toy Programming Language If you're looking how to use Toy directly, try https://toylang.com/ -Otherwise, these headers may help learn how Toy works internally. +Otherwise, this header may help learn how Toy works internally. +!*/ -*/ +/*! +## Utilities -/* utilities - these define a bunch of useful macros based on platform. +These headers define a bunch of useful macros, based on what platform you build for. -The most important one is `TOY_API`, which highlights functions intended for the end user. +The most important macro is `TOY_API`, which specifies functions intended for the end user. -*/ +* [toy_common.h](toy_common_h.md) +* [toy_console_colors.h](toy_console_colors_h.md) +* [toy_memory.h](toy_memory_h.md) +* [toy_drive_system.h](toy_drive_system_h.md) +!*/ #include "toy_common.h" #include "toy_console_colors.h" #include "toy_memory.h" #include "toy_drive_system.h" -/* core pipeline - from source to execution +/*! +## Core Pipeline -Each step is as follows: +From source to execution, each step is as follows: +``` source -> lexer -> token token -> parser -> AST AST -> compiler -> bytecode bytecode -> interpreter -> result +``` -I should note that the parser -> compiler phase is actually made up of two steps - the write step -and the collate step. See `Toy_compileString()` in `repl/repl_tools.c` for an example of how to compile -properly. +I should note that the parser -> compiler phase is actually made up of two steps - the write step and the collate step. See `Toy_compileString()` in `repl/repl_tools.c` for an example of how to compile properly. -*/ +* [toy_lexer.h](toy_lexer_h.md) +* [toy_parser.h](toy_parser_h.md) +* [toy_compiler.h](toy_compiler_h.md) +* [toy_interpreter.h](toy_interpreter_h.md) +!*/ #include "toy_lexer.h" #include "toy_parser.h" #include "toy_compiler.h" #include "toy_interpreter.h" -/* building block structures - the basic units of operation +/*! +## Building Block Structures Literals represent any value within the language, including some internal ones that you never see. -Literal Arrays are literally arrays within memory, and are the most heavily used structure in Toy. -Literal Dictionaries are unordered key-value hashmaps, that use a running strategy for collisions. -*/ +Literal arrays are contiguous arrays within memory, and are the most heavily used structure in Toy. + +Literal dictionaries are unordered key-value hashmaps, that use a running strategy for collisions. + +* [toy_literal.h](toy_literal_h.md) +* [toy_literal_array.h](toy_literal_array_h.md) +* [toy_literal_dictionary.h](toy_literal_dictionary_h.md) +!*/ #include "toy_literal.h" #include "toy_literal_array.h" #include "toy_literal_dictionary.h" -/* other components - you probably won't use these directly, but they're a good learning opportunity. +/*! +## Other Components -`Toy_Scope` holds the variables of a specific scope within Toy - be it a script, a function, a block, etc. -Scopes are also where the type system lives at runtime. They use identifier literals as keys, exclusively. +You probably won't use these directly, but they're a good learning opportunity. -`Toy_RefString` is a utility class that wraps traditional C strings, making them less memory intensive and -faster to copy and move. In reality, since strings are considered immutable, multiple variables can point -to the same string to save memory, and you can just create a new one of these vars pointing to the original -rather than copying entirely for a speed boost. This module has it's own memory allocator system that is -plugged into the main memory allocator. +`Toy_Scope` holds the variables of a specific scope within Toy - be it a script, a function, a block, etc. Scopes are also where the type system lives at runtime. They use identifier literals as keys, exclusively. + +`Toy_RefString` is a utility class that wraps traditional C strings, making them less memory intensive and faster to copy and move. In reality, since strings are considered immutable, multiple variables can point to the same string to save memory, and you can just create a new one of these vars pointing to the original rather than copying entirely for a speed boost. This module has it's own memory allocator system that is plugged into the main memory allocator. `Toy_RefFunction` acts similarly to `Toy_RefString`, but instead operates on function bytecode. -*/ +* [toy_scope.h](toy_scope_h.md) +* [toy_refstring.h](toy_refstring_h.md) +* [toy_reffunction.h](toy_reffunction_h.md) +!*/ #include "toy_scope.h" #include "toy_refstring.h"