Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c3c46b4fc6 | |||
| 0e41b00ef4 | |||
| f6ec6a8c73 | |||
| 2157b2f540 | |||
| 1481216e69 | |||
| f25f389b4e | |||
| deff784df8 | |||
| 54e82846c3 | |||
| 67fce427eb | |||
| 8a2cb61435 | |||
| 50d03e28fc | |||
| 763581c73b | |||
| cdb2613e5d | |||
| 733df87c08 | |||
| bfd506f497 | |||
| 18b59c9e84 | |||
| d3eb31d964 | |||
| 07f4a98b95 | |||
| 0949fd6ff9 | |||
| 03e5096f10 | |||
| bb81b8c474 | |||
| cf6db57787 |
@@ -4,9 +4,11 @@
|
|||||||
|
|
||||||
# Toy
|
# Toy
|
||||||
|
|
||||||
This is the Toy programming language interpreter, written in C.
|
The Toy programming language is an imperative bytecode-intermediate embedded scripting language. It isn't intended to operate on its own, but rather as part of another program, the "host". This process is intended to allow a decent amount of easy customisation by the host's end user, by exposing logic in script files. Alternatively, binary files in a custom format can be used as well.
|
||||||
|
|
||||||
Special thanks to http://craftinginterpreters.com/ for their fantastic book that set me on this path.
|
The host will provide all of the extensions needed on a case-by-case basis. Script files have the `.toy` file extension, while binary files have the `.tb` file extension.
|
||||||
|
|
||||||
|
This is the Toy programming language interpreter, written in C.
|
||||||
|
|
||||||
# Nifty Features
|
# Nifty Features
|
||||||
|
|
||||||
@@ -14,7 +16,7 @@ Special thanks to http://craftinginterpreters.com/ for their fantastic book that
|
|||||||
* Bytecode intermediate compilation
|
* Bytecode intermediate compilation
|
||||||
* Optional, but robust type system (including `opaque` for arbitrary data)
|
* Optional, but robust type system (including `opaque` for arbitrary data)
|
||||||
* Functions and types are first-class citizens
|
* Functions and types are first-class citizens
|
||||||
* Import external libraries
|
* Import native libraries from the host
|
||||||
* Fancy slice notation for strings, arrays and dictionaries
|
* Fancy slice notation for strings, arrays and dictionaries
|
||||||
* Can re-direct output, error and assertion failure messages
|
* Can re-direct output, error and assertion failure messages
|
||||||
* Open source under the zlib license
|
* Open source under the zlib license
|
||||||
@@ -71,3 +73,4 @@ This source code is covered by the zlib license (see [LICENSE.md](LICENSE.md)).
|
|||||||
|
|
||||||
* Seth A. Robinson
|
* Seth A. Robinson
|
||||||
|
|
||||||
|
Special thanks to http://craftinginterpreters.com/ for their fantastic book that set me on this path.
|
||||||
+2
-2
@@ -115,13 +115,13 @@
|
|||||||
<ClCompile>
|
<ClCompile>
|
||||||
<AdditionalIncludeDirectories>$(SolutionDir)/source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(SolutionDir)/source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||||
<PreprocessorDefinitions>LIB_RUNNER_EXPORT</PreprocessorDefinitions>
|
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
<ClCompile>
|
<ClCompile>
|
||||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||||
<PreprocessorDefinitions>LIB_RUNNER_EXPORT</PreprocessorDefinitions>
|
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<AdditionalIncludeDirectories>$(SolutionDir)/source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(SolutionDir)/source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
<ClCompile Include="source\toy_literal_dictionary.c" />
|
<ClCompile Include="source\toy_literal_dictionary.c" />
|
||||||
<ClCompile Include="source\toy_memory.c" />
|
<ClCompile Include="source\toy_memory.c" />
|
||||||
<ClCompile Include="source\toy_parser.c" />
|
<ClCompile Include="source\toy_parser.c" />
|
||||||
|
<ClCompile Include="source\toy_reffunction.c" />
|
||||||
<ClCompile Include="source\toy_refstring.c" />
|
<ClCompile Include="source\toy_refstring.c" />
|
||||||
<ClCompile Include="source\toy_scope.c" />
|
<ClCompile Include="source\toy_scope.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -152,6 +153,7 @@
|
|||||||
<ClInclude Include="source\toy_memory.h" />
|
<ClInclude Include="source\toy_memory.h" />
|
||||||
<ClInclude Include="source\toy_opcodes.h" />
|
<ClInclude Include="source\toy_opcodes.h" />
|
||||||
<ClInclude Include="source\toy_parser.h" />
|
<ClInclude Include="source\toy_parser.h" />
|
||||||
|
<ClInclude Include="source\toy_reffunction.h" />
|
||||||
<ClInclude Include="source\toy_refstring.h" />
|
<ClInclude Include="source\toy_refstring.h" />
|
||||||
<ClInclude Include="source\toy_scope.h" />
|
<ClInclude Include="source\toy_scope.h" />
|
||||||
<ClInclude Include="source\toy_token_types.h" />
|
<ClInclude Include="source\toy_token_types.h" />
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
# Optimisation Options
|
|
||||||
# export CFLAGS+=-O2 -mtune=native -march=native
|
|
||||||
# export CFLAGS+=-fsanitize=address,undefined
|
|
||||||
|
|
||||||
export CFLAGS+=-std=c18 -pedantic -Werror
|
export CFLAGS+=-std=c18 -pedantic -Werror
|
||||||
|
|
||||||
export TOY_OUTDIR = out
|
export TOY_OUTDIR = out
|
||||||
@@ -34,6 +30,10 @@ library-release: clean $(TOY_OUTDIR)
|
|||||||
static-release: clean $(TOY_OUTDIR)
|
static-release: clean $(TOY_OUTDIR)
|
||||||
$(MAKE) -j8 -C source static-release
|
$(MAKE) -j8 -C source static-release
|
||||||
|
|
||||||
|
#distribution
|
||||||
|
dist: export CFLAGS+=-O2 -mtune=native -march=native
|
||||||
|
dist: repl-release
|
||||||
|
|
||||||
#utils
|
#utils
|
||||||
test: clean $(TOY_OUTDIR)
|
test: clean $(TOY_OUTDIR)
|
||||||
$(MAKE) -C test
|
$(MAKE) -C test
|
||||||
|
|||||||
+32
-1
@@ -1471,9 +1471,14 @@ static void recursiveLiteralQuicksortUtil(Toy_Interpreter* interpreter, Toy_Lite
|
|||||||
swapLiteralsUtil(&ptr[runner], &ptr[literalCount - 1]);
|
swapLiteralsUtil(&ptr[runner], &ptr[literalCount - 1]);
|
||||||
|
|
||||||
//recurse on each end
|
//recurse on each end
|
||||||
|
if (runner > 0) {
|
||||||
recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompareLiteral);
|
recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompareLiteral);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runner < literalCount) {
|
||||||
recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], literalCount - runner - 1, fnCompareLiteral);
|
recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], literalCount - runner - 1, fnCompareLiteral);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int nativeSort(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
static int nativeSort(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||||
//no arguments
|
//no arguments
|
||||||
@@ -1511,8 +1516,34 @@ static int nativeSort(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BUGFIX: check if the array is already sorted
|
||||||
|
bool sorted = true;
|
||||||
|
for (int checker = 0; checker < TOY_AS_ARRAY(selfLiteral)->count - 1 && sorted; checker++) {
|
||||||
|
Toy_LiteralArray arguments;
|
||||||
|
Toy_LiteralArray returns;
|
||||||
|
|
||||||
|
Toy_initLiteralArray(&arguments);
|
||||||
|
Toy_initLiteralArray(&returns);
|
||||||
|
|
||||||
|
Toy_pushLiteralArray(&arguments, TOY_AS_ARRAY(selfLiteral)->literals[checker]);
|
||||||
|
Toy_pushLiteralArray(&arguments, TOY_AS_ARRAY(selfLiteral)->literals[checker + 1]);
|
||||||
|
|
||||||
|
Toy_callLiteralFn(interpreter, fnLiteral, &arguments, &returns);
|
||||||
|
|
||||||
|
Toy_Literal lessThan = Toy_popLiteralArray(&returns);
|
||||||
|
|
||||||
|
Toy_freeLiteralArray(&arguments);
|
||||||
|
Toy_freeLiteralArray(&returns);
|
||||||
|
|
||||||
|
if (!TOY_IS_TRUTHY(lessThan)) {
|
||||||
|
sorted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Toy_freeLiteral(lessThan);
|
||||||
|
}
|
||||||
|
|
||||||
//call the quicksort util
|
//call the quicksort util
|
||||||
if (TOY_IS_ARRAY(selfLiteral)) {
|
if (!sorted) {
|
||||||
recursiveLiteralQuicksortUtil(interpreter, TOY_AS_ARRAY(selfLiteral)->literals, TOY_AS_ARRAY(selfLiteral)->count, fnLiteral);
|
recursiveLiteralQuicksortUtil(interpreter, TOY_AS_ARRAY(selfLiteral)->literals, TOY_AS_ARRAY(selfLiteral)->count, fnLiteral);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -193,8 +193,14 @@ int main(int argc, const char* argv[]) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Toy_commandLine.parseBytecodeHeader) {
|
||||||
|
//only parse the bytecode header
|
||||||
|
Toy_parseBinaryFileHeader(Toy_commandLine.binaryfile);
|
||||||
|
}
|
||||||
|
else {
|
||||||
//run the binary file
|
//run the binary file
|
||||||
Toy_runBinaryFile(Toy_commandLine.binaryfile);
|
Toy_runBinaryFile(Toy_commandLine.binaryfile);
|
||||||
|
}
|
||||||
|
|
||||||
//lib cleanup
|
//lib cleanup
|
||||||
Toy_freeDriveSystem();
|
Toy_freeDriveSystem();
|
||||||
|
|||||||
@@ -149,3 +149,59 @@ void Toy_runSourceFile(const char* fname) {
|
|||||||
Toy_runSource(source);
|
Toy_runSource(source);
|
||||||
free((void*)source);
|
free((void*)source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//utils for debugging the header
|
||||||
|
static unsigned char readByte(const unsigned char* tb, int* count) {
|
||||||
|
unsigned char ret = *(unsigned char*)(tb + *count);
|
||||||
|
*count += 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char* readString(const unsigned char* tb, int* count) {
|
||||||
|
const unsigned char* ret = tb + *count;
|
||||||
|
*count += strlen((char*)ret) + 1; //+1 for null character
|
||||||
|
return (const char*)ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Toy_parseBinaryFileHeader(const char* fname) {
|
||||||
|
size_t size = 0; //not used
|
||||||
|
const unsigned char* tb = Toy_readFile(fname, &size);
|
||||||
|
if (!tb || size < 4) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
//header section
|
||||||
|
const unsigned char major = readByte(tb, &count);
|
||||||
|
const unsigned char minor = readByte(tb, &count);
|
||||||
|
const unsigned char patch = readByte(tb, &count);
|
||||||
|
|
||||||
|
const char* build = readString(tb, &count);
|
||||||
|
|
||||||
|
printf("Toy Programming Language Interpreter Version %d.%d.%d (interpreter built on %s)\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD);
|
||||||
|
|
||||||
|
printf("Toy Programming Language Bytecode Version ");
|
||||||
|
|
||||||
|
//print the output
|
||||||
|
if (major == TOY_VERSION_MAJOR && minor == TOY_VERSION_MINOR && patch == TOY_VERSION_PATCH) {
|
||||||
|
printf("%d.%d.%d", major, minor, patch);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf(TOY_CC_FONT_YELLOW TOY_CC_BACK_BLACK "%d.%d.%d" TOY_CC_RESET, major, minor, patch);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(" (interpreter built on ");
|
||||||
|
|
||||||
|
if (strncmp(build, TOY_VERSION_BUILD, strlen(TOY_VERSION_BUILD)) == 0) {
|
||||||
|
printf("%s", build);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf(TOY_CC_FONT_YELLOW TOY_CC_BACK_BLACK "%s" TOY_CC_RESET, build);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(")\n");
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
free((void*)tb);
|
||||||
|
}
|
||||||
@@ -12,3 +12,4 @@ void Toy_runBinaryFile(const char* fname);
|
|||||||
void Toy_runSource(const char* source);
|
void Toy_runSource(const char* source);
|
||||||
void Toy_runSourceFile(const char* fname);
|
void Toy_runSourceFile(const char* fname);
|
||||||
|
|
||||||
|
void Toy_parseBinaryFileHeader(const char* fname);
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
var s = "42";
|
|
||||||
var t = "69";
|
|
||||||
|
|
||||||
print int (s + t) - 1;
|
|
||||||
+1
-1
@@ -4,7 +4,7 @@ fn fib(n : int) {
|
|||||||
return fib(n-1) + fib(n-2);
|
return fib(n-1) + fib(n-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < 20; i++) {
|
for (var i = 0; i <= 35; i++) {
|
||||||
var res = fib(i);
|
var res = fib(i);
|
||||||
print string i + ": " + string res;
|
print string i + ": " + string res;
|
||||||
}
|
}
|
||||||
+3
-1
@@ -61,8 +61,10 @@ to the same string to save memory, and you can just create a new one of these va
|
|||||||
rather than copying entirely for a speed boost. This module has it's own memory allocator system that is
|
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.
|
plugged into the main memory allocator.
|
||||||
|
|
||||||
|
`Toy_RefFunction` acts similarly to `Toy_RefString`, but instead operates on function bytecode.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "toy_scope.h"
|
#include "toy_scope.h"
|
||||||
#include "toy_refstring.h"
|
#include "toy_refstring.h"
|
||||||
|
#include "toy_reffunction.h"
|
||||||
|
|||||||
+17
-3
@@ -15,7 +15,11 @@ STATIC_ASSERT(sizeof(unsigned char) == 1);
|
|||||||
STATIC_ASSERT(sizeof(unsigned short) == 2);
|
STATIC_ASSERT(sizeof(unsigned short) == 2);
|
||||||
STATIC_ASSERT(sizeof(unsigned int) == 4);
|
STATIC_ASSERT(sizeof(unsigned int) == 4);
|
||||||
|
|
||||||
#ifndef TOY_EXPORT
|
static const char* build = __DATE__ " " __TIME__;
|
||||||
|
|
||||||
|
const char* Toy_private_version_build() {
|
||||||
|
return build;
|
||||||
|
}
|
||||||
|
|
||||||
//declare the singleton with default values
|
//declare the singleton with default values
|
||||||
Toy_CommandLine Toy_commandLine = {
|
Toy_CommandLine Toy_commandLine = {
|
||||||
@@ -29,6 +33,7 @@ Toy_CommandLine Toy_commandLine = {
|
|||||||
.source = NULL,
|
.source = NULL,
|
||||||
.initialfile = NULL,
|
.initialfile = NULL,
|
||||||
.enablePrintNewline = true,
|
.enablePrintNewline = true,
|
||||||
|
.parseBytecodeHeader = false,
|
||||||
.verbose = false
|
.verbose = false
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,6 +94,16 @@ void Toy_initCommandLine(int argc, const char* argv[]) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!strcmp(argv[i], "-p")) {
|
||||||
|
Toy_commandLine.parseBytecodeHeader = true;
|
||||||
|
|
||||||
|
if (Toy_commandLine.binaryfile) {
|
||||||
|
Toy_commandLine.error = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!strcmp(argv[i], "-n")) {
|
if (!strcmp(argv[i], "-n")) {
|
||||||
Toy_commandLine.enablePrintNewline = false;
|
Toy_commandLine.enablePrintNewline = false;
|
||||||
Toy_commandLine.error = false;
|
Toy_commandLine.error = false;
|
||||||
@@ -124,6 +139,7 @@ void Toy_helpCommandLine(int argc, const char* argv[]) {
|
|||||||
printf(" -c, --compile filename\tParse and compile the specified source file into an output file.\n");
|
printf(" -c, --compile filename\tParse and compile the specified source file into an output file.\n");
|
||||||
printf(" -o, --output outfile\t\tName of the output file built with --compile (default: out.tb).\n");
|
printf(" -o, --output outfile\t\tName of the output file built with --compile (default: out.tb).\n");
|
||||||
printf(" -t, --initial filename\tStart the repl as normal, after first running the given file.\n");
|
printf(" -t, --initial filename\tStart the repl as normal, after first running the given file.\n");
|
||||||
|
printf(" -p\t\t\t\tParse the given bytecode's header, then exit (requires file.tb).\n");
|
||||||
printf(" -n\t\t\t\tDisable the newline character at the end of the print statement.\n");
|
printf(" -n\t\t\t\tDisable the newline character at the end of the print statement.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,5 +152,3 @@ void Toy_copyrightCommandLine(int argc, const char* argv[]) {
|
|||||||
printf("2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n\n");
|
printf("2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n\n");
|
||||||
printf("3. This notice may not be removed or altered from any source distribution.\n\n");
|
printf("3. This notice may not be removed or altered from any source distribution.\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
+4
-5
@@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#define TOY_VERSION_MAJOR 1
|
#define TOY_VERSION_MAJOR 1
|
||||||
#define TOY_VERSION_MINOR 1
|
#define TOY_VERSION_MINOR 1
|
||||||
#define TOY_VERSION_PATCH 2
|
#define TOY_VERSION_PATCH 6
|
||||||
#define TOY_VERSION_BUILD __DATE__ " " __TIME__
|
#define TOY_VERSION_BUILD Toy_private_version_build()
|
||||||
|
|
||||||
//platform/compiler-specific instructions
|
//platform/compiler-specific instructions
|
||||||
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef TOY_EXPORT
|
TOY_API const char* Toy_private_version_build();
|
||||||
|
|
||||||
//for processing the command line arguments in the repl
|
//for processing the command line arguments in the repl
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -42,6 +42,7 @@ typedef struct {
|
|||||||
char* source;
|
char* source;
|
||||||
char* initialfile;
|
char* initialfile;
|
||||||
bool enablePrintNewline;
|
bool enablePrintNewline;
|
||||||
|
bool parseBytecodeHeader;
|
||||||
bool verbose;
|
bool verbose;
|
||||||
} Toy_CommandLine;
|
} Toy_CommandLine;
|
||||||
|
|
||||||
@@ -53,5 +54,3 @@ TOY_API void Toy_initCommandLine(int argc, const char* argv[]);
|
|||||||
TOY_API void Toy_usageCommandLine(int argc, const char* argv[]);
|
TOY_API void Toy_usageCommandLine(int argc, const char* argv[]);
|
||||||
TOY_API void Toy_helpCommandLine(int argc, const char* argv[]);
|
TOY_API void Toy_helpCommandLine(int argc, const char* argv[]);
|
||||||
TOY_API void Toy_copyrightCommandLine(int argc, const char* argv[]);
|
TOY_API void Toy_copyrightCommandLine(int argc, const char* argv[]);
|
||||||
|
|
||||||
#endif
|
|
||||||
+79
-4
@@ -259,6 +259,81 @@ static int writeLiteralToCompiler(Toy_Compiler* compiler, Toy_Literal literal) {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BUGFIX: check to see if this node lies within this tree
|
||||||
|
bool checkNodeInTree(Toy_ASTNode* tree, Toy_ASTNode* node) {
|
||||||
|
if (tree == node) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tree == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(tree->type) {
|
||||||
|
case TOY_AST_NODE_UNARY:
|
||||||
|
return checkNodeInTree(tree->unary.child, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_BINARY:
|
||||||
|
return checkNodeInTree(tree->binary.left, node) || checkNodeInTree(tree->binary.right, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_TERNARY:
|
||||||
|
return checkNodeInTree(tree->ternary.condition, node) || checkNodeInTree(tree->ternary.thenPath, node) || checkNodeInTree(tree->ternary.elsePath, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_GROUPING:
|
||||||
|
return checkNodeInTree(tree->grouping.child, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_BLOCK:
|
||||||
|
return checkNodeInTree(tree->block.nodes, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_COMPOUND:
|
||||||
|
return checkNodeInTree(tree->compound.nodes, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_PAIR:
|
||||||
|
return checkNodeInTree(tree->pair.left, node) || checkNodeInTree(tree->pair.right, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_INDEX:
|
||||||
|
return checkNodeInTree(tree->index.first, node) || checkNodeInTree(tree->index.second, node) || checkNodeInTree(tree->index.third, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_VAR_DECL:
|
||||||
|
return checkNodeInTree(tree->varDecl.expression, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_FN_COLLECTION:
|
||||||
|
return checkNodeInTree(tree->fnCollection.nodes, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_FN_DECL:
|
||||||
|
return checkNodeInTree(tree->fnDecl.arguments, node) || checkNodeInTree(tree->fnDecl.returns, node) || checkNodeInTree(tree->fnDecl.block, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_FN_CALL:
|
||||||
|
return checkNodeInTree(tree->fnCall.arguments, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_FN_RETURN:
|
||||||
|
return checkNodeInTree(tree->returns.returns, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_IF:
|
||||||
|
return checkNodeInTree(tree->pathIf.condition, node) || checkNodeInTree(tree->pathIf.thenPath, node) || checkNodeInTree(tree->pathIf.elsePath, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_WHILE:
|
||||||
|
return checkNodeInTree(tree->pathWhile.condition, node) || checkNodeInTree(tree->pathWhile.thenPath, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_FOR:
|
||||||
|
return checkNodeInTree(tree->pathFor.preClause, node) || checkNodeInTree(tree->pathFor.condition, node) || checkNodeInTree(tree->pathFor.postClause, node) || checkNodeInTree(tree->pathFor.thenPath, node);
|
||||||
|
|
||||||
|
case TOY_AST_NODE_ERROR:
|
||||||
|
case TOY_AST_NODE_LITERAL:
|
||||||
|
case TOY_AST_NODE_BREAK:
|
||||||
|
case TOY_AST_NODE_CONTINUE:
|
||||||
|
case TOY_AST_NODE_PREFIX_INCREMENT:
|
||||||
|
case TOY_AST_NODE_PREFIX_DECREMENT:
|
||||||
|
case TOY_AST_NODE_POSTFIX_INCREMENT:
|
||||||
|
case TOY_AST_NODE_POSTFIX_DECREMENT:
|
||||||
|
case TOY_AST_NODE_IMPORT:
|
||||||
|
case TOY_AST_NODE_PASS:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//NOTE: jumpOfsets are included, because function arg and return indexes are embedded in the code body i.e. need to include their sizes in the jump
|
//NOTE: jumpOfsets are included, because function arg and return indexes are embedded in the code body i.e. need to include their sizes in the jump
|
||||||
//NOTE: rootNode should NOT include groupings and blocks
|
//NOTE: rootNode should NOT include groupings and blocks
|
||||||
static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode* node, void* breakAddressesPtr, void* continueAddressesPtr, int jumpOffsets, Toy_ASTNode* rootNode) {
|
static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode* node, void* breakAddressesPtr, void* continueAddressesPtr, int jumpOffsets, Toy_ASTNode* rootNode) {
|
||||||
@@ -322,7 +397,8 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
|
|||||||
//return this if...
|
//return this if...
|
||||||
Toy_Opcode ret = Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
|
Toy_Opcode ret = Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
|
||||||
|
|
||||||
if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY && (rootNode->binary.opcode >= TOY_OP_VAR_ASSIGN && rootNode->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN) && rootNode->binary.right != node) { //range-based check for assignment type; make sure the index is on the left of the assignment symbol
|
//range-based check for assignment type; make sure the index is on the left of the assignment symbol
|
||||||
|
if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY && (rootNode->binary.opcode >= TOY_OP_VAR_ASSIGN && rootNode->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN) && !checkNodeInTree(rootNode->binary.right, node)) {
|
||||||
return TOY_OP_INDEX_ASSIGN_INTERMEDIATE;
|
return TOY_OP_INDEX_ASSIGN_INTERMEDIATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -518,8 +594,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
|
|||||||
}
|
}
|
||||||
|
|
||||||
//create the function in the literal cache (by storing the compiler object)
|
//create the function in the literal cache (by storing the compiler object)
|
||||||
Toy_Literal fnLiteral = TOY_TO_FUNCTION_LITERAL(fnCompiler, 0);
|
Toy_Literal fnLiteral = ((Toy_Literal){ .as = { .generic = fnCompiler }, .type = TOY_LITERAL_FUNCTION_INTERMEDIATE});
|
||||||
fnLiteral.type = TOY_LITERAL_FUNCTION_INTERMEDIATE; //NOTE: changing type
|
|
||||||
|
|
||||||
//push the name
|
//push the name
|
||||||
int identifierIndex = Toy_findLiteralIndex(&compiler->literalCache, node->fnDecl.identifier);
|
int identifierIndex = Toy_findLiteralIndex(&compiler->literalCache, node->fnDecl.identifier);
|
||||||
@@ -1210,7 +1285,7 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, size_t* s
|
|||||||
case TOY_LITERAL_FUNCTION_INTERMEDIATE: {
|
case TOY_LITERAL_FUNCTION_INTERMEDIATE: {
|
||||||
//extract the compiler
|
//extract the compiler
|
||||||
Toy_Literal fn = compiler->literalCache.literals[i];
|
Toy_Literal fn = compiler->literalCache.literals[i];
|
||||||
void* fnCompiler = TOY_AS_FUNCTION(fn).inner.bytecode; //store the compiler here for now
|
void* fnCompiler = fn.as.generic; //store the compiler here for now
|
||||||
|
|
||||||
//collate the function into bytecode (without header)
|
//collate the function into bytecode (without header)
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|||||||
@@ -1,6 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
//NOTE: you need both font AND background for these to work
|
/* toy_console_colors.h - console utility
|
||||||
|
|
||||||
|
This file provides a number of macros that can set the color of text in a console
|
||||||
|
window. These are used for convenience only. They are supposed to be dropped into
|
||||||
|
a printf()'s first argument, like so:
|
||||||
|
|
||||||
|
printf(TOY_CC_NOTICE "Hello world" TOY_CC_RESET);
|
||||||
|
|
||||||
|
NOTE: you need both font AND background for these to work
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
//platform/compiler-specific instructions
|
//platform/compiler-specific instructions
|
||||||
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
||||||
|
|||||||
+68
-184
@@ -85,55 +85,93 @@ bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_Ho
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Toy_parseCompoundToPureValues(Toy_Interpreter* interpreter, Toy_Literal* literalPtr) {
|
void Toy_parseCompoundToPureValues(Toy_Interpreter* interpreter, Toy_Literal* literalPtr) {
|
||||||
if (TOY_IS_IDENTIFIER(*literalPtr)) {
|
|
||||||
Toy_parseIdentifierToValue(interpreter, literalPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
//parse out an array
|
//parse out an array
|
||||||
if (TOY_IS_ARRAY(*literalPtr)) {
|
if (TOY_IS_ARRAY(*literalPtr)) {
|
||||||
for (int i = 0; i < TOY_AS_ARRAY(*literalPtr)->count; i++) {
|
for (int i = 0; i < TOY_AS_ARRAY(*literalPtr)->count; i++) {
|
||||||
|
//check each individual element for an identifier
|
||||||
|
if (TOY_IS_IDENTIFIER( TOY_AS_ARRAY(*literalPtr)->literals[i] )) {
|
||||||
Toy_Literal index = TOY_TO_INTEGER_LITERAL(i);
|
Toy_Literal index = TOY_TO_INTEGER_LITERAL(i);
|
||||||
Toy_Literal entry = Toy_getLiteralArray(TOY_AS_ARRAY(*literalPtr), index);
|
Toy_Literal entry = Toy_getLiteralArray(TOY_AS_ARRAY(*literalPtr), index);
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER( entry )) {
|
|
||||||
Toy_Literal idn = entry;
|
Toy_Literal idn = entry;
|
||||||
Toy_parseCompoundToPureValues(interpreter, &entry);
|
Toy_parseIdentifierToValue(interpreter, &entry);
|
||||||
|
|
||||||
Toy_setLiteralArray(TOY_AS_ARRAY(*literalPtr), index, entry);
|
Toy_setLiteralArray(TOY_AS_ARRAY(*literalPtr), index, entry);
|
||||||
|
|
||||||
Toy_freeLiteral(idn);
|
Toy_freeLiteral(idn);
|
||||||
}
|
|
||||||
|
|
||||||
Toy_freeLiteral(index);
|
Toy_freeLiteral(index);
|
||||||
Toy_freeLiteral(entry);
|
Toy_freeLiteral(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//recurse on sub-compounds
|
||||||
|
if (TOY_IS_ARRAY(TOY_AS_ARRAY(*literalPtr)->literals[i]) || TOY_IS_DICTIONARY(TOY_AS_ARRAY(*literalPtr)->literals[i])) {
|
||||||
|
Toy_parseCompoundToPureValues(interpreter, &TOY_AS_ARRAY(*literalPtr)->literals[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//parse out a dictionary
|
//parse out a dictionary
|
||||||
if (TOY_IS_DICTIONARY(*literalPtr)) {
|
else if (TOY_IS_DICTIONARY(*literalPtr)) {
|
||||||
|
//check for any identifiers
|
||||||
|
bool idnFound = false;
|
||||||
|
for (int i = 0; i < TOY_AS_DICTIONARY(*literalPtr)->capacity; i++) {
|
||||||
|
if (TOY_IS_IDENTIFIER( TOY_AS_DICTIONARY(*literalPtr)->entries[i].key ) || TOY_IS_IDENTIFIER(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value)) {
|
||||||
|
idnFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//recurse on sub-compounds
|
||||||
|
if (TOY_IS_ARRAY(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key) || TOY_IS_DICTIONARY(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key)) {
|
||||||
|
Toy_parseCompoundToPureValues(interpreter, &TOY_AS_DICTIONARY(*literalPtr)->entries[i].key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TOY_IS_ARRAY(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value) || TOY_IS_DICTIONARY(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value)) {
|
||||||
|
Toy_parseCompoundToPureValues(interpreter, &TOY_AS_DICTIONARY(*literalPtr)->entries[i].value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!idnFound) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Toy_LiteralDictionary* ret = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
|
Toy_LiteralDictionary* ret = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
|
||||||
Toy_initLiteralDictionary(ret);
|
Toy_initLiteralDictionary(ret);
|
||||||
|
|
||||||
|
//this basically copies the dict, so preallocate it manually
|
||||||
|
ret->capacity = TOY_AS_DICTIONARY(*literalPtr)->capacity;
|
||||||
|
ret->entries = TOY_ALLOCATE(Toy_private_dictionary_entry, ret->capacity);
|
||||||
|
|
||||||
|
for (int i = 0; i < ret->capacity; i++) {
|
||||||
|
ret->entries[i].key = TOY_TO_NULL_LITERAL;
|
||||||
|
ret->entries[i].value = TOY_TO_NULL_LITERAL;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < TOY_AS_DICTIONARY(*literalPtr)->capacity; i++) {
|
for (int i = 0; i < TOY_AS_DICTIONARY(*literalPtr)->capacity; i++) {
|
||||||
if ( TOY_IS_NULL(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key) ) {
|
if ( TOY_IS_NULL(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key) ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal key = TOY_TO_NULL_LITERAL;
|
Toy_Literal key = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key);
|
||||||
Toy_Literal value = TOY_TO_NULL_LITERAL;
|
Toy_Literal value = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value);
|
||||||
|
|
||||||
key = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key);
|
//check each key/value pair, and sub-compounds
|
||||||
value = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value);
|
if (TOY_IS_IDENTIFIER( key )) {
|
||||||
|
Toy_parseIdentifierToValue(interpreter, &key);
|
||||||
//
|
}
|
||||||
if (TOY_IS_IDENTIFIER( key ) || TOY_IS_IDENTIFIER(value)) {
|
if (TOY_IS_ARRAY(key) || TOY_IS_DICTIONARY(key)) {
|
||||||
Toy_parseCompoundToPureValues(interpreter, &key);
|
Toy_parseCompoundToPureValues(interpreter, &key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TOY_IS_IDENTIFIER(value)) {
|
||||||
|
Toy_parseIdentifierToValue(interpreter, &value);
|
||||||
|
}
|
||||||
|
if (TOY_IS_ARRAY(value) || TOY_IS_DICTIONARY(value)) {
|
||||||
Toy_parseCompoundToPureValues(interpreter, &value);
|
Toy_parseCompoundToPureValues(interpreter, &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_setLiteralDictionary(ret, key, value);
|
Toy_setLiteralDictionary(ret, key, value);
|
||||||
|
|
||||||
//
|
//cleanup
|
||||||
Toy_freeLiteral(key);
|
Toy_freeLiteral(key);
|
||||||
Toy_freeLiteral(value);
|
Toy_freeLiteral(value);
|
||||||
}
|
}
|
||||||
@@ -154,9 +192,9 @@ bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* liter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_ARRAY(*literalPtr) || TOY_IS_DICTIONARY(*literalPtr)) {
|
// if (TOY_IS_ARRAY(*literalPtr) || TOY_IS_DICTIONARY(*literalPtr)) {
|
||||||
Toy_parseCompoundToPureValues(interpreter, literalPtr);
|
// Toy_parseCompoundToPureValues(interpreter, literalPtr);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -236,12 +274,6 @@ static bool execAssert(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TOY_IS_STRING(rhs)) {
|
if (!TOY_IS_STRING(rhs)) {
|
||||||
interpreter->errorOutput("The assert keyword needs a string as the second argument, received: ");
|
interpreter->errorOutput("The assert keyword needs a string as the second argument, received: ");
|
||||||
Toy_printLiteralCustom(rhs, interpreter->errorOutput);
|
Toy_printLiteralCustom(rhs, interpreter->errorOutput);
|
||||||
@@ -276,11 +308,6 @@ static bool execPrint(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(idn);
|
Toy_freeLiteral(idn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lit)) {
|
|
||||||
Toy_freeLiteral(lit);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_printLiteralCustom(lit, interpreter->printOutput);
|
Toy_printLiteralCustom(lit, interpreter->printOutput);
|
||||||
|
|
||||||
Toy_freeLiteral(lit);
|
Toy_freeLiteral(lit);
|
||||||
@@ -313,11 +340,6 @@ static bool rawLiteral(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(idn);
|
Toy_freeLiteral(idn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lit)) {
|
|
||||||
Toy_freeLiteral(lit);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_pushLiteralArray(&interpreter->stack, lit);
|
Toy_pushLiteralArray(&interpreter->stack, lit);
|
||||||
Toy_freeLiteral(lit);
|
Toy_freeLiteral(lit);
|
||||||
|
|
||||||
@@ -333,11 +355,6 @@ static bool execNegate(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(idn);
|
Toy_freeLiteral(idn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lit)) {
|
|
||||||
Toy_freeLiteral(lit);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TOY_IS_INTEGER(lit)) {
|
if (TOY_IS_INTEGER(lit)) {
|
||||||
lit = TOY_TO_INTEGER_LITERAL(-TOY_AS_INTEGER(lit));
|
lit = TOY_TO_INTEGER_LITERAL(-TOY_AS_INTEGER(lit));
|
||||||
}
|
}
|
||||||
@@ -369,11 +386,6 @@ static bool execInvert(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(idn);
|
Toy_freeLiteral(idn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lit)) {
|
|
||||||
Toy_freeLiteral(lit);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TOY_IS_BOOLEAN(lit)) {
|
if (TOY_IS_BOOLEAN(lit)) {
|
||||||
lit = TOY_TO_BOOLEAN_LITERAL(!TOY_AS_BOOLEAN(lit));
|
lit = TOY_TO_BOOLEAN_LITERAL(!TOY_AS_BOOLEAN(lit));
|
||||||
}
|
}
|
||||||
@@ -407,12 +419,6 @@ static bool execArithmetic(Toy_Interpreter* interpreter, Toy_Opcode opcode) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//special case for string concatenation ONLY
|
//special case for string concatenation ONLY
|
||||||
if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs) && (opcode == TOY_OP_ADDITION || opcode == TOY_OP_VAR_ADDITION_ASSIGN)) {
|
if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs) && (opcode == TOY_OP_ADDITION || opcode == TOY_OP_VAR_ADDITION_ASSIGN)) {
|
||||||
//check for overflow
|
//check for overflow
|
||||||
@@ -545,10 +551,6 @@ static Toy_Literal parseTypeToValue(Toy_Interpreter* interpreter, Toy_Literal ty
|
|||||||
Toy_freeLiteral(typeIdn);
|
Toy_freeLiteral(typeIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(type)) {
|
|
||||||
return TOY_TO_NULL_LITERAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//if this is an array or dictionary, continue to the subtypes
|
//if this is an array or dictionary, continue to the subtypes
|
||||||
if (TOY_IS_TYPE(type) && (TOY_AS_TYPE(type).typeOf == TOY_LITERAL_ARRAY || TOY_AS_TYPE(type).typeOf == TOY_LITERAL_DICTIONARY)) {
|
if (TOY_IS_TYPE(type) && (TOY_AS_TYPE(type).typeOf == TOY_LITERAL_ARRAY || TOY_AS_TYPE(type).typeOf == TOY_LITERAL_DICTIONARY)) {
|
||||||
for (int i = 0; i < TOY_AS_TYPE(type).count; i++) {
|
for (int i = 0; i < TOY_AS_TYPE(type).count; i++) {
|
||||||
@@ -589,12 +591,6 @@ static bool execVarDecl(Toy_Interpreter* interpreter, bool lng) {
|
|||||||
Toy_freeLiteral(typeIdn);
|
Toy_freeLiteral(typeIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(type)) {
|
|
||||||
Toy_freeLiteral(identifier);
|
|
||||||
Toy_freeLiteral(type);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//BUGFIX: because identifiers are getting embedded in type definitions
|
//BUGFIX: because identifiers are getting embedded in type definitions
|
||||||
type = parseTypeToValue(interpreter, type);
|
type = parseTypeToValue(interpreter, type);
|
||||||
|
|
||||||
@@ -612,13 +608,6 @@ static bool execVarDecl(Toy_Interpreter* interpreter, bool lng) {
|
|||||||
Toy_freeLiteral(valIdn);
|
Toy_freeLiteral(valIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(val)) {
|
|
||||||
Toy_freeLiteral(identifier);
|
|
||||||
Toy_freeLiteral(type);
|
|
||||||
Toy_freeLiteral(val);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TOY_IS_ARRAY(val) || TOY_IS_DICTIONARY(val)) {
|
if (TOY_IS_ARRAY(val) || TOY_IS_DICTIONARY(val)) {
|
||||||
Toy_parseCompoundToPureValues(interpreter, &val);
|
Toy_parseCompoundToPureValues(interpreter, &val);
|
||||||
}
|
}
|
||||||
@@ -699,12 +688,6 @@ static bool execVarAssign(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(rhsIdn);
|
Toy_freeLiteral(rhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TOY_IS_ARRAY(rhs) || TOY_IS_DICTIONARY(rhs)) {
|
if (TOY_IS_ARRAY(rhs) || TOY_IS_DICTIONARY(rhs)) {
|
||||||
Toy_parseCompoundToPureValues(interpreter, &rhs);
|
Toy_parseCompoundToPureValues(interpreter, &rhs);
|
||||||
}
|
}
|
||||||
@@ -774,12 +757,6 @@ static bool execValCast(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(valueIdn);
|
Toy_freeLiteral(valueIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(value)) {
|
|
||||||
Toy_freeLiteral(type);
|
|
||||||
Toy_freeLiteral(value);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_Literal result = TOY_TO_NULL_LITERAL;
|
Toy_Literal result = TOY_TO_NULL_LITERAL;
|
||||||
|
|
||||||
if (TOY_IS_NULL(value)) {
|
if (TOY_IS_NULL(value)) {
|
||||||
@@ -914,12 +891,6 @@ static bool execCompareEqual(Toy_Interpreter* interpreter, bool invert) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool result = Toy_literalsAreEqual(lhs, rhs);
|
bool result = Toy_literalsAreEqual(lhs, rhs);
|
||||||
|
|
||||||
if (invert) {
|
if (invert) {
|
||||||
@@ -948,12 +919,6 @@ static bool execCompareLess(Toy_Interpreter* interpreter, bool invert) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//not a number, return falure
|
//not a number, return falure
|
||||||
if (!(TOY_IS_INTEGER(lhs) || TOY_IS_FLOAT(lhs))) {
|
if (!(TOY_IS_INTEGER(lhs) || TOY_IS_FLOAT(lhs))) {
|
||||||
interpreter->errorOutput("Incorrect type in comparison, value \"");
|
interpreter->errorOutput("Incorrect type in comparison, value \"");
|
||||||
@@ -1014,12 +979,6 @@ static bool execCompareLessEqual(Toy_Interpreter* interpreter, bool invert) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//not a number, return falure
|
//not a number, return falure
|
||||||
if (!(TOY_IS_INTEGER(lhs) || TOY_IS_FLOAT(lhs))) {
|
if (!(TOY_IS_INTEGER(lhs) || TOY_IS_FLOAT(lhs))) {
|
||||||
interpreter->errorOutput("Incorrect type in comparison, value \"");
|
interpreter->errorOutput("Incorrect type in comparison, value \"");
|
||||||
@@ -1081,12 +1040,6 @@ static bool execAnd(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//short-circuit support
|
//short-circuit support
|
||||||
if (!TOY_IS_TRUTHY(lhs)) {
|
if (!TOY_IS_TRUTHY(lhs)) {
|
||||||
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||||
@@ -1115,12 +1068,6 @@ static bool execOr(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(lhsIdn);
|
Toy_freeLiteral(lhsIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
|
||||||
Toy_freeLiteral(lhs);
|
|
||||||
Toy_freeLiteral(rhs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//short-circuit support
|
//short-circuit support
|
||||||
if (TOY_IS_TRUTHY(lhs)) {
|
if (TOY_IS_TRUTHY(lhs)) {
|
||||||
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||||
@@ -1165,11 +1112,6 @@ static bool execFalseJump(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(litIdn);
|
Toy_freeLiteral(litIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lit)) {
|
|
||||||
Toy_freeLiteral(lit);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TOY_IS_NULL(lit)) {
|
if (TOY_IS_NULL(lit)) {
|
||||||
interpreter->errorOutput("Null detected in comparison\n");
|
interpreter->errorOutput("Null detected in comparison\n");
|
||||||
Toy_freeLiteral(lit);
|
Toy_freeLiteral(lit);
|
||||||
@@ -1193,7 +1135,7 @@ static void readInterpreterSections(Toy_Interpreter* interpreter);
|
|||||||
//also supports identifier & arg1 to be other way around (looseFirstArgument)
|
//also supports identifier & arg1 to be other way around (looseFirstArgument)
|
||||||
static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
||||||
//BUGFIX: depth check - don't drown!
|
//BUGFIX: depth check - don't drown!
|
||||||
if (interpreter->depth >= 200) {
|
if (interpreter->depth >= 1000) {
|
||||||
interpreter->errorOutput("Infinite recursion detected - panicking\n");
|
interpreter->errorOutput("Infinite recursion detected - panicking\n");
|
||||||
interpreter->panic = true;
|
interpreter->panic = true;
|
||||||
return false;
|
return false;
|
||||||
@@ -1229,12 +1171,8 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
|||||||
|
|
||||||
//get the function literal
|
//get the function literal
|
||||||
Toy_Literal func = identifier;
|
Toy_Literal func = identifier;
|
||||||
|
if (!TOY_IS_FUNCTION(func) && Toy_parseIdentifierToValue(interpreter, &func)) {
|
||||||
if (!Toy_parseIdentifierToValue(interpreter, &func)) {
|
|
||||||
Toy_freeLiteralArray(&arguments);
|
|
||||||
Toy_freeLiteral(stackSize);
|
|
||||||
Toy_freeLiteral(identifier);
|
Toy_freeLiteral(identifier);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!TOY_IS_FUNCTION(func) && !TOY_IS_FUNCTION_NATIVE(func)) {
|
if (!TOY_IS_FUNCTION(func) && !TOY_IS_FUNCTION_NATIVE(func)) {
|
||||||
@@ -1271,7 +1209,6 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
|||||||
Toy_freeLiteralArray(&arguments);
|
Toy_freeLiteralArray(&arguments);
|
||||||
Toy_freeLiteral(func);
|
Toy_freeLiteral(func);
|
||||||
Toy_freeLiteral(stackSize);
|
Toy_freeLiteral(stackSize);
|
||||||
Toy_freeLiteral(identifier);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -1323,8 +1260,8 @@ bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_Liter
|
|||||||
//init the inner interpreter manually
|
//init the inner interpreter manually
|
||||||
Toy_initLiteralArray(&inner.literalCache);
|
Toy_initLiteralArray(&inner.literalCache);
|
||||||
inner.scope = Toy_pushScope(func.as.function.scope);
|
inner.scope = Toy_pushScope(func.as.function.scope);
|
||||||
inner.bytecode = TOY_AS_FUNCTION(func).inner.bytecode;
|
inner.bytecode = ((Toy_RefFunction*)(TOY_AS_FUNCTION(func).inner.ptr))->data;
|
||||||
inner.length = TOY_AS_FUNCTION_BYTECODE_LENGTH(func);
|
inner.length = ((Toy_RefFunction*)(TOY_AS_FUNCTION(func).inner.ptr))->length;
|
||||||
inner.count = 0;
|
inner.count = 0;
|
||||||
inner.codeStart = -1;
|
inner.codeStart = -1;
|
||||||
inner.depth = interpreter->depth + 1;
|
inner.depth = interpreter->depth + 1;
|
||||||
@@ -1390,17 +1327,6 @@ bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_Liter
|
|||||||
Toy_freeLiteral(argIdn);
|
Toy_freeLiteral(argIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(arg)) {
|
|
||||||
//free, and skip out
|
|
||||||
Toy_freeLiteral(arg);
|
|
||||||
Toy_popScope(inner.scope);
|
|
||||||
|
|
||||||
Toy_freeLiteralArray(&inner.stack);
|
|
||||||
Toy_freeLiteralArray(&inner.literalCache);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Toy_setScopeVariable(inner.scope, paramArray->literals[i], arg, false)) {
|
if (!Toy_setScopeVariable(inner.scope, paramArray->literals[i], arg, false)) {
|
||||||
interpreter->errorOutput("[internal] Could not define parameter (bad type?)\n");
|
interpreter->errorOutput("[internal] Could not define parameter (bad type?)\n");
|
||||||
|
|
||||||
@@ -1569,12 +1495,6 @@ static bool execFnReturn(Toy_Interpreter* interpreter) {
|
|||||||
Toy_freeLiteral(litIdn);
|
Toy_freeLiteral(litIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(lit)) {
|
|
||||||
Toy_freeLiteralArray(&returns);
|
|
||||||
Toy_freeLiteral(lit);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TOY_IS_ARRAY(lit) || TOY_IS_DICTIONARY(lit)) {
|
if (TOY_IS_ARRAY(lit) || TOY_IS_DICTIONARY(lit)) {
|
||||||
Toy_parseCompoundToPureValues(interpreter, &lit);
|
Toy_parseCompoundToPureValues(interpreter, &lit);
|
||||||
}
|
}
|
||||||
@@ -1641,22 +1561,11 @@ static bool execIndex(Toy_Interpreter* interpreter, bool assignIntermediate) {
|
|||||||
Toy_Literal compound = Toy_popLiteralArray(&interpreter->stack);
|
Toy_Literal compound = Toy_popLiteralArray(&interpreter->stack);
|
||||||
|
|
||||||
Toy_Literal compoundIdn = compound;
|
Toy_Literal compoundIdn = compound;
|
||||||
bool freeIdn = false;
|
bool freeIdn = false; //wtf?
|
||||||
if (TOY_IS_IDENTIFIER(compound) && Toy_parseIdentifierToValue(interpreter, &compound)) {
|
if (TOY_IS_IDENTIFIER(compound) && Toy_parseIdentifierToValue(interpreter, &compound)) {
|
||||||
freeIdn = true;
|
freeIdn = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(compound)) {
|
|
||||||
Toy_freeLiteral(third);
|
|
||||||
Toy_freeLiteral(second);
|
|
||||||
Toy_freeLiteral(first);
|
|
||||||
Toy_freeLiteral(compound);
|
|
||||||
if (freeIdn) {
|
|
||||||
Toy_freeLiteral(compoundIdn);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TOY_IS_ARRAY(compound) && !TOY_IS_DICTIONARY(compound) && !TOY_IS_STRING(compound)) {
|
if (!TOY_IS_ARRAY(compound) && !TOY_IS_DICTIONARY(compound) && !TOY_IS_STRING(compound)) {
|
||||||
interpreter->errorOutput("Unknown compound found in index notation: ");
|
interpreter->errorOutput("Unknown compound found in index notation: ");
|
||||||
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
||||||
@@ -1792,32 +1701,11 @@ static bool execIndexAssign(Toy_Interpreter* interpreter, int assignDepth) {
|
|||||||
freeIdn = true;
|
freeIdn = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(compound)) {
|
|
||||||
Toy_freeLiteral(compound);
|
|
||||||
Toy_freeLiteral(first);
|
|
||||||
Toy_freeLiteral(second);
|
|
||||||
Toy_freeLiteral(third);
|
|
||||||
Toy_freeLiteral(assign);
|
|
||||||
if (freeIdn) {
|
|
||||||
Toy_freeLiteral(compoundIdn);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_Literal assignIdn = assign;
|
Toy_Literal assignIdn = assign;
|
||||||
if (TOY_IS_IDENTIFIER(assign) && Toy_parseIdentifierToValue(interpreter, &assign)) {
|
if (TOY_IS_IDENTIFIER(assign) && Toy_parseIdentifierToValue(interpreter, &assign)) {
|
||||||
Toy_freeLiteral(assignIdn);
|
Toy_freeLiteral(assignIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_IDENTIFIER(assign)) {
|
|
||||||
Toy_freeLiteral(compound);
|
|
||||||
Toy_freeLiteral(first);
|
|
||||||
Toy_freeLiteral(second);
|
|
||||||
Toy_freeLiteral(third);
|
|
||||||
Toy_freeLiteral(assign);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TOY_IS_ARRAY(compound) && !TOY_IS_DICTIONARY(compound) && !TOY_IS_STRING(compound)) {
|
if (!TOY_IS_ARRAY(compound) && !TOY_IS_DICTIONARY(compound) && !TOY_IS_STRING(compound)) {
|
||||||
interpreter->errorOutput("Unknown compound found in index assigning notation: ");
|
interpreter->errorOutput("Unknown compound found in index assigning notation: ");
|
||||||
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
||||||
@@ -2428,20 +2316,16 @@ static void readInterpreterSections(Toy_Interpreter* interpreter) {
|
|||||||
//get the size of the function
|
//get the size of the function
|
||||||
size_t size = (size_t)readShort(interpreter->bytecode, &interpreter->count);
|
size_t size = (size_t)readShort(interpreter->bytecode, &interpreter->count);
|
||||||
|
|
||||||
//read the function code (literal cache and all)
|
|
||||||
unsigned char* bytes = TOY_ALLOCATE(unsigned char, size);
|
|
||||||
memcpy(bytes, interpreter->bytecode + interpreter->count, size); //TODO: -1 for the ending mark
|
|
||||||
interpreter->count += size;
|
|
||||||
|
|
||||||
//assert that the last memory slot is function end
|
//assert that the last memory slot is function end
|
||||||
if (bytes[size - 1] != TOY_OP_FN_END) {
|
if (interpreter->bytecode[interpreter->count + size - 1] != TOY_OP_FN_END) {
|
||||||
interpreter->errorOutput("[internal] Failed to find function end");
|
interpreter->errorOutput("[internal] Failed to find function end");
|
||||||
TOY_FREE_ARRAY(unsigned char, bytes, size);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//change the type to normal
|
//copies internally, since functions can exist independant of literalCache
|
||||||
interpreter->literalCache.literals[i] = TOY_TO_FUNCTION_LITERAL(bytes, size);
|
interpreter->literalCache.literals[i] = TOY_TO_FUNCTION_LITERAL(Toy_createRefFunction(interpreter->bytecode + interpreter->count, size));
|
||||||
|
|
||||||
|
interpreter->count += size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+24
-15
@@ -59,7 +59,7 @@ void Toy_freeLiteral(Toy_Literal literal) {
|
|||||||
if (TOY_IS_FUNCTION(literal)) {
|
if (TOY_IS_FUNCTION(literal)) {
|
||||||
Toy_popScope(TOY_AS_FUNCTION(literal).scope);
|
Toy_popScope(TOY_AS_FUNCTION(literal).scope);
|
||||||
TOY_AS_FUNCTION(literal).scope = NULL;
|
TOY_AS_FUNCTION(literal).scope = NULL;
|
||||||
TOY_FREE_ARRAY(unsigned char, TOY_AS_FUNCTION(literal).inner.bytecode, TOY_AS_FUNCTION_BYTECODE_LENGTH(literal));
|
Toy_deleteRefFunction((Toy_RefFunction*)(TOY_AS_FUNCTION(literal).inner.ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_TYPE(literal) && TOY_AS_TYPE(literal).capacity > 0) {
|
if (TOY_IS_TYPE(literal) && TOY_AS_TYPE(literal).capacity > 0) {
|
||||||
@@ -84,12 +84,8 @@ bool Toy_private_isTruthy(Toy_Literal x) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal Toy_private_toStringLiteral(Toy_RefString* ptr) {
|
|
||||||
return ((Toy_Literal){{ .string = { .ptr = ptr }},TOY_LITERAL_STRING, 0});
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr) {
|
Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr) {
|
||||||
return ((Toy_Literal){{ .identifier = { .ptr = ptr, .hash = hashString(Toy_toCString(ptr), Toy_lengthRefString(ptr)) }},TOY_LITERAL_IDENTIFIER, 0});
|
return ((Toy_Literal){{ .identifier = { .ptr = ptr, .hash = hashString(Toy_toCString(ptr), Toy_lengthRefString(ptr)) }},TOY_LITERAL_IDENTIFIER});
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype) {
|
Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype) {
|
||||||
@@ -123,6 +119,10 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
|||||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||||
Toy_initLiteralArray(array);
|
Toy_initLiteralArray(array);
|
||||||
|
|
||||||
|
//preallocate enough space
|
||||||
|
array->capacity = TOY_AS_ARRAY(original)->capacity;
|
||||||
|
array->literals = TOY_GROW_ARRAY(Toy_Literal, array->literals, 0, array->capacity);
|
||||||
|
|
||||||
//copy each element
|
//copy each element
|
||||||
for (int i = 0; i < TOY_AS_ARRAY(original)->count; i++) {
|
for (int i = 0; i < TOY_AS_ARRAY(original)->count; i++) {
|
||||||
Toy_pushLiteralArray(array, TOY_AS_ARRAY(original)->literals[i]);
|
Toy_pushLiteralArray(array, TOY_AS_ARRAY(original)->literals[i]);
|
||||||
@@ -135,6 +135,15 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
|||||||
Toy_LiteralDictionary* dictionary = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
|
Toy_LiteralDictionary* dictionary = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
|
||||||
Toy_initLiteralDictionary(dictionary);
|
Toy_initLiteralDictionary(dictionary);
|
||||||
|
|
||||||
|
//preallocate enough space
|
||||||
|
dictionary->capacity = TOY_AS_DICTIONARY(original)->capacity;
|
||||||
|
dictionary->entries = TOY_ALLOCATE(Toy_private_dictionary_entry, dictionary->capacity);
|
||||||
|
|
||||||
|
for (int i = 0; i < dictionary->capacity; i++) {
|
||||||
|
dictionary->entries[i].key = TOY_TO_NULL_LITERAL;
|
||||||
|
dictionary->entries[i].value = TOY_TO_NULL_LITERAL;
|
||||||
|
}
|
||||||
|
|
||||||
//copy each entry
|
//copy each entry
|
||||||
for (int i = 0; i < TOY_AS_DICTIONARY(original)->capacity; i++) {
|
for (int i = 0; i < TOY_AS_DICTIONARY(original)->capacity; i++) {
|
||||||
if ( !TOY_IS_NULL(TOY_AS_DICTIONARY(original)->entries[i].key) ) {
|
if ( !TOY_IS_NULL(TOY_AS_DICTIONARY(original)->entries[i].key) ) {
|
||||||
@@ -146,10 +155,8 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_FUNCTION: {
|
case TOY_LITERAL_FUNCTION: {
|
||||||
unsigned char* buffer = TOY_ALLOCATE(unsigned char, TOY_AS_FUNCTION_BYTECODE_LENGTH(original));
|
Toy_Literal literal = TOY_TO_FUNCTION_LITERAL(Toy_copyRefFunction( TOY_AS_FUNCTION(original).inner.ptr ));
|
||||||
memcpy(buffer, TOY_AS_FUNCTION(original).inner.bytecode, TOY_AS_FUNCTION_BYTECODE_LENGTH(original));
|
|
||||||
|
|
||||||
Toy_Literal literal = TOY_TO_FUNCTION_LITERAL(buffer, TOY_AS_FUNCTION_BYTECODE_LENGTH(original));
|
|
||||||
TOY_AS_FUNCTION(literal).scope = Toy_copyScope(TOY_AS_FUNCTION(original).scope);
|
TOY_AS_FUNCTION(literal).scope = Toy_copyScope(TOY_AS_FUNCTION(original).scope);
|
||||||
|
|
||||||
return literal;
|
return literal;
|
||||||
@@ -174,7 +181,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
|||||||
return original; //literally a shallow copy
|
return original; //literally a shallow copy
|
||||||
}
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_ARRAY_INTERMEDIATE: {
|
case TOY_LITERAL_ARRAY_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||||
Toy_initLiteralArray(array);
|
Toy_initLiteralArray(array);
|
||||||
|
|
||||||
@@ -190,7 +197,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_DICTIONARY_INTERMEDIATE: {
|
case TOY_LITERAL_DICTIONARY_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||||
Toy_initLiteralArray(array);
|
Toy_initLiteralArray(array);
|
||||||
|
|
||||||
@@ -206,7 +213,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_TYPE_INTERMEDIATE: {
|
case TOY_LITERAL_TYPE_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||||
Toy_initLiteralArray(array);
|
Toy_initLiteralArray(array);
|
||||||
|
|
||||||
@@ -378,8 +385,10 @@ int Toy_hashLiteral(Toy_Literal lit) {
|
|||||||
case TOY_LITERAL_INTEGER:
|
case TOY_LITERAL_INTEGER:
|
||||||
return hashUInt((unsigned int)TOY_AS_INTEGER(lit));
|
return hashUInt((unsigned int)TOY_AS_INTEGER(lit));
|
||||||
|
|
||||||
case TOY_LITERAL_FLOAT:
|
case TOY_LITERAL_FLOAT: {
|
||||||
return hashUInt(*(unsigned int*)(&TOY_AS_FLOAT(lit)));
|
unsigned int* ptr = (unsigned int*)(&TOY_AS_FLOAT(lit));
|
||||||
|
return hashUInt(*ptr);
|
||||||
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_STRING:
|
case TOY_LITERAL_STRING:
|
||||||
return hashString(Toy_toCString(TOY_AS_STRING(lit)), Toy_lengthRefString(TOY_AS_STRING(lit)));
|
return hashString(Toy_toCString(TOY_AS_STRING(lit)), Toy_lengthRefString(TOY_AS_STRING(lit)));
|
||||||
@@ -446,7 +455,7 @@ static void printToBuffer(const char* str) {
|
|||||||
globalPrintBuffer = TOY_GROW_ARRAY(char, globalPrintBuffer, oldCapacity, globalPrintCapacity);
|
globalPrintBuffer = TOY_GROW_ARRAY(char, globalPrintBuffer, oldCapacity, globalPrintCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(globalPrintBuffer + globalPrintCount, strlen(str) + 1, "%s", str);
|
snprintf(globalPrintBuffer + globalPrintCount, strlen(str) + 1, "%s", str ? str : "\0");
|
||||||
globalPrintCount += strlen(str);
|
globalPrintCount += strlen(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-18
@@ -3,6 +3,7 @@
|
|||||||
#include "toy_common.h"
|
#include "toy_common.h"
|
||||||
|
|
||||||
#include "toy_refstring.h"
|
#include "toy_refstring.h"
|
||||||
|
#include "toy_reffunction.h"
|
||||||
|
|
||||||
//forward delcare stuff
|
//forward delcare stuff
|
||||||
struct Toy_Literal;
|
struct Toy_Literal;
|
||||||
@@ -55,7 +56,7 @@ typedef struct Toy_Literal {
|
|||||||
|
|
||||||
struct {
|
struct {
|
||||||
union {
|
union {
|
||||||
void* bytecode; //8
|
Toy_RefFunction* ptr; //8
|
||||||
Toy_NativeFn native; //8
|
Toy_NativeFn native; //8
|
||||||
Toy_HookFn hook; //8
|
Toy_HookFn hook; //8
|
||||||
} inner; //8
|
} inner; //8
|
||||||
@@ -68,7 +69,6 @@ typedef struct Toy_Literal {
|
|||||||
} identifier; //16
|
} identifier; //16
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
|
||||||
struct Toy_Literal* subtypes; //8
|
struct Toy_Literal* subtypes; //8
|
||||||
Toy_LiteralType typeOf; //4
|
Toy_LiteralType typeOf; //4
|
||||||
unsigned char capacity; //1
|
unsigned char capacity; //1
|
||||||
@@ -80,10 +80,13 @@ typedef struct Toy_Literal {
|
|||||||
void* ptr; //8
|
void* ptr; //8
|
||||||
int tag; //4
|
int tag; //4
|
||||||
} opaque; //16
|
} opaque; //16
|
||||||
|
|
||||||
|
void* generic; //8
|
||||||
} as; //16
|
} as; //16
|
||||||
|
|
||||||
Toy_LiteralType type; //4
|
Toy_LiteralType type; //4
|
||||||
int bytecodeLength; //4 - shenanigans with byte alignment reduces the size of Toy_Literal
|
//4 - unused
|
||||||
|
//shenanigans with byte alignment reduces the size of Toy_Literal
|
||||||
} Toy_Literal;
|
} Toy_Literal;
|
||||||
|
|
||||||
#define TOY_IS_NULL(value) ((value).type == TOY_LITERAL_NULL)
|
#define TOY_IS_NULL(value) ((value).type == TOY_LITERAL_NULL)
|
||||||
@@ -113,29 +116,29 @@ typedef struct Toy_Literal {
|
|||||||
#define TOY_AS_TYPE(value) ((value).as.type)
|
#define TOY_AS_TYPE(value) ((value).as.type)
|
||||||
#define TOY_AS_OPAQUE(value) ((value).as.opaque.ptr)
|
#define TOY_AS_OPAQUE(value) ((value).as.opaque.ptr)
|
||||||
|
|
||||||
#define TOY_TO_NULL_LITERAL ((Toy_Literal){{ .integer = 0 }, TOY_LITERAL_NULL, 0})
|
#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, 0})
|
#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, 0})
|
#define TOY_TO_INTEGER_LITERAL(value) ((Toy_Literal){{ .integer = value }, TOY_LITERAL_INTEGER})
|
||||||
#define TOY_TO_FLOAT_LITERAL(value) ((Toy_Literal){{ .number = value }, TOY_LITERAL_FLOAT, 0})
|
#define TOY_TO_FLOAT_LITERAL(value) ((Toy_Literal){{ .number = value }, TOY_LITERAL_FLOAT})
|
||||||
#define TOY_TO_STRING_LITERAL(value) Toy_private_toStringLiteral(value)
|
#define TOY_TO_STRING_LITERAL(value) ((Toy_Literal){{ .string = { .ptr = value }},TOY_LITERAL_STRING})
|
||||||
#define TOY_TO_ARRAY_LITERAL(value) ((Toy_Literal){{ .array = value }, TOY_LITERAL_ARRAY, 0})
|
#define TOY_TO_ARRAY_LITERAL(value) ((Toy_Literal){{ .array = value }, TOY_LITERAL_ARRAY})
|
||||||
#define TOY_TO_DICTIONARY_LITERAL(value) ((Toy_Literal){{ .dictionary = value }, TOY_LITERAL_DICTIONARY, 0})
|
#define TOY_TO_DICTIONARY_LITERAL(value) ((Toy_Literal){{ .dictionary = value }, TOY_LITERAL_DICTIONARY})
|
||||||
#define TOY_TO_FUNCTION_LITERAL(value, l) ((Toy_Literal){{ .function = { .inner = { .bytecode = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION, l})
|
#define TOY_TO_FUNCTION_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .ptr = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION})
|
||||||
#define TOY_TO_FUNCTION_NATIVE_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .native = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION_NATIVE, 0})
|
#define TOY_TO_FUNCTION_NATIVE_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .native = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION_NATIVE})
|
||||||
#define TOY_TO_FUNCTION_HOOK_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .hook = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION_HOOK, 0})
|
#define TOY_TO_FUNCTION_HOOK_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .hook = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION_HOOK})
|
||||||
#define TOY_TO_IDENTIFIER_LITERAL(value) Toy_private_toIdentifierLiteral(value)
|
#define TOY_TO_IDENTIFIER_LITERAL(value) Toy_private_toIdentifierLiteral(value)
|
||||||
#define TOY_TO_TYPE_LITERAL(value, c) ((Toy_Literal){{ .type = { .typeOf = value, .constant = c, .subtypes = NULL, .capacity = 0, .count = 0 }}, TOY_LITERAL_TYPE, 0})
|
#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, 0})
|
#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
|
||||||
#define TOY_IS_INDEX_BLANK(value) ((value).type == TOY_LITERAL_INDEX_BLANK)
|
#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, 0})
|
#define TOY_TO_INDEX_BLANK_LITERAL ((Toy_Literal){{ .integer = 0 }, TOY_LITERAL_INDEX_BLANK})
|
||||||
|
|
||||||
TOY_API void Toy_freeLiteral(Toy_Literal literal);
|
TOY_API void Toy_freeLiteral(Toy_Literal literal);
|
||||||
|
|
||||||
#define TOY_IS_TRUTHY(x) Toy_private_isTruthy(x)
|
#define TOY_IS_TRUTHY(x) Toy_private_isTruthy(x)
|
||||||
|
|
||||||
#define TOY_AS_FUNCTION_BYTECODE_LENGTH(lit) ((lit).bytecodeLength)
|
#define TOY_AS_FUNCTION_BYTECODE_LENGTH(lit) (Toy_lengthRefFunction((lit).inner.ptr))
|
||||||
|
|
||||||
#define TOY_MAX_STRING_LENGTH 4096
|
#define TOY_MAX_STRING_LENGTH 4096
|
||||||
#define TOY_HASH_I(lit) ((lit).as.identifier.hash)
|
#define TOY_HASH_I(lit) ((lit).as.identifier.hash)
|
||||||
@@ -144,7 +147,6 @@ TOY_API void Toy_freeLiteral(Toy_Literal literal);
|
|||||||
|
|
||||||
//BUGFIX: macros are not functions
|
//BUGFIX: macros are not functions
|
||||||
TOY_API bool Toy_private_isTruthy(Toy_Literal x);
|
TOY_API bool Toy_private_isTruthy(Toy_Literal x);
|
||||||
TOY_API Toy_Literal Toy_private_toStringLiteral(Toy_RefString* ptr);
|
|
||||||
TOY_API Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr);
|
TOY_API Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr);
|
||||||
TOY_API Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype);
|
TOY_API Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "toy_memory.h"
|
#include "toy_memory.h"
|
||||||
#include "toy_refstring.h"
|
#include "toy_refstring.h"
|
||||||
|
#include "toy_reffunction.h"
|
||||||
|
|
||||||
#include "toy_console_colors.h"
|
#include "toy_console_colors.h"
|
||||||
|
|
||||||
@@ -49,4 +50,5 @@ void Toy_setMemoryAllocator(Toy_MemoryAllocatorFn fn) {
|
|||||||
|
|
||||||
allocator = fn;
|
allocator = fn;
|
||||||
Toy_setRefStringAllocatorFn(fn);
|
Toy_setRefStringAllocatorFn(fn);
|
||||||
|
Toy_setRefFunctionAllocatorFn(fn);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,5 +86,9 @@ typedef enum Toy_Opcode {
|
|||||||
TOY_OP_FN_END, //different from SECTION_END
|
TOY_OP_FN_END, //different from SECTION_END
|
||||||
TOY_OP_SECTION_END = 255,
|
TOY_OP_SECTION_END = 255,
|
||||||
//TODO: add more
|
//TODO: add more
|
||||||
|
|
||||||
|
//prefix & postfix signals (used internally)
|
||||||
|
TOY_OP_PREFIX,
|
||||||
|
TOY_OP_POSTFIX,
|
||||||
} Toy_Opcode;
|
} Toy_Opcode;
|
||||||
|
|
||||||
|
|||||||
+28
-6
@@ -119,6 +119,7 @@ ParseRule parseRules[];
|
|||||||
static void declaration(Toy_Parser* parser, Toy_ASTNode** nodeHandle);
|
static void declaration(Toy_Parser* parser, Toy_ASTNode** nodeHandle);
|
||||||
static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, PrecedenceRule rule);
|
static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, PrecedenceRule rule);
|
||||||
static Toy_Literal readTypeToLiteral(Toy_Parser* parser);
|
static Toy_Literal readTypeToLiteral(Toy_Parser* parser);
|
||||||
|
static void varDecl(Toy_Parser* parser, Toy_ASTNode** nodeHandle);
|
||||||
|
|
||||||
//TODO: resolve the messy order of these
|
//TODO: resolve the messy order of these
|
||||||
//the expression rules
|
//the expression rules
|
||||||
@@ -627,6 +628,14 @@ static Toy_Opcode castingPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
//BUGFIX: handle this here, and not in castingPrefix, so "any" can be recognized as a type properly
|
||||||
|
case TOY_TOKEN_ANY: {
|
||||||
|
Toy_Literal literal = TOY_TO_TYPE_LITERAL(TOY_LITERAL_ANY, false);
|
||||||
|
Toy_emitASTNodeLiteral(nodeHandle, literal);
|
||||||
|
Toy_freeLiteral(literal);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
error(parser, parser->previous, "Unexpected token passed to casting precedence rule");
|
error(parser, parser->previous, "Unexpected token passed to casting precedence rule");
|
||||||
return TOY_OP_EOF;
|
return TOY_OP_EOF;
|
||||||
@@ -684,7 +693,7 @@ static Toy_Opcode incrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle)
|
|||||||
|
|
||||||
Toy_freeASTNode(tmpNode);
|
Toy_freeASTNode(tmpNode);
|
||||||
|
|
||||||
return TOY_OP_EOF;
|
return TOY_OP_PREFIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Toy_Opcode incrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
static Toy_Opcode incrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||||
@@ -701,7 +710,7 @@ static Toy_Opcode incrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
|||||||
|
|
||||||
Toy_freeASTNode(tmpNode);
|
Toy_freeASTNode(tmpNode);
|
||||||
|
|
||||||
return TOY_OP_EOF;
|
return TOY_OP_POSTFIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Toy_Opcode decrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
static Toy_Opcode decrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||||
@@ -718,7 +727,7 @@ static Toy_Opcode decrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle)
|
|||||||
|
|
||||||
Toy_freeASTNode(tmpNode);
|
Toy_freeASTNode(tmpNode);
|
||||||
|
|
||||||
return TOY_OP_EOF;
|
return TOY_OP_PREFIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||||
@@ -735,7 +744,7 @@ static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
|||||||
|
|
||||||
Toy_freeASTNode(tmpNode);
|
Toy_freeASTNode(tmpNode);
|
||||||
|
|
||||||
return TOY_OP_EOF;
|
return TOY_OP_POSTFIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Toy_Opcode fnCall(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
static Toy_Opcode fnCall(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||||
@@ -939,7 +948,7 @@ ParseRule parseRules[] = { //must match the token types
|
|||||||
{NULL, NULL, PREC_NONE},// TOKEN_DICTIONARY,
|
{NULL, NULL, PREC_NONE},// TOKEN_DICTIONARY,
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_FUNCTION,
|
{NULL, NULL, PREC_NONE},// TOKEN_FUNCTION,
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_OPAQUE,
|
{NULL, NULL, PREC_NONE},// TOKEN_OPAQUE,
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_ANY,
|
{castingPrefix, NULL, PREC_CALL},// TOKEN_ANY,
|
||||||
|
|
||||||
//keywords and reserved words
|
//keywords and reserved words
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_AS,
|
{NULL, NULL, PREC_NONE},// TOKEN_AS,
|
||||||
@@ -1278,6 +1287,13 @@ static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, Preced
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BUGFIX: keep going, don't skip out on a postfix
|
||||||
|
if (opcode == TOY_OP_PREFIX || opcode == TOY_OP_POSTFIX) {
|
||||||
|
Toy_freeASTNode(*nodeHandle);
|
||||||
|
*nodeHandle = rhsNode;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Toy_emitASTNodeBinary(nodeHandle, rhsNode, opcode);
|
Toy_emitASTNodeBinary(nodeHandle, rhsNode, opcode);
|
||||||
|
|
||||||
//optimise away the constants
|
//optimise away the constants
|
||||||
@@ -1397,7 +1413,13 @@ static void forStmt(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
|||||||
|
|
||||||
//check the pre-clause
|
//check the pre-clause
|
||||||
if (parser->current.type != TOY_TOKEN_SEMICOLON) {
|
if (parser->current.type != TOY_TOKEN_SEMICOLON) {
|
||||||
declaration(parser, &preClause); //allow defining variables in the pre-clause
|
//allow defining variables in the pre-clause
|
||||||
|
if (match(parser, TOY_TOKEN_VAR)) {
|
||||||
|
varDecl(parser, &preClause);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
parsePrecedence(parser, &preClause, PREC_ASSIGNMENT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
consume(parser, TOY_TOKEN_SEMICOLON, "Expected ';' after empty declaration of for clause");
|
consume(parser, TOY_TOKEN_SEMICOLON, "Expected ';' after empty declaration of for clause");
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#include "toy_reffunction.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
//memory allocation
|
||||||
|
extern void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize);
|
||||||
|
static Toy_RefFunctionAllocatorFn allocate = Toy_private_defaultMemoryAllocator;
|
||||||
|
|
||||||
|
void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn allocator) {
|
||||||
|
allocate = allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
//API
|
||||||
|
Toy_RefFunction* Toy_createRefFunction(const void* data, size_t length) {
|
||||||
|
//allocate the memory area (including metadata space)
|
||||||
|
Toy_RefFunction* refFunction = allocate(NULL, 0, sizeof(size_t) + sizeof(int) + sizeof(char) * length);
|
||||||
|
|
||||||
|
if (refFunction == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//set the data
|
||||||
|
refFunction->refCount = 1;
|
||||||
|
refFunction->length = length;
|
||||||
|
memcpy(refFunction->data, data, refFunction->length);
|
||||||
|
|
||||||
|
return refFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Toy_deleteRefFunction(Toy_RefFunction* refFunction) {
|
||||||
|
//decrement, then check
|
||||||
|
refFunction->refCount--;
|
||||||
|
if (refFunction->refCount <= 0) {
|
||||||
|
allocate(refFunction, sizeof(size_t) + sizeof(int) + sizeof(char) * (refFunction->length + 1), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Toy_countRefFunction(Toy_RefFunction* refFunction) {
|
||||||
|
return refFunction->refCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction) {
|
||||||
|
return refFunction->length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Toy_RefFunction* Toy_copyRefFunction(Toy_RefFunction* refFunction) {
|
||||||
|
//Cheaty McCheater Face
|
||||||
|
refFunction->refCount++;
|
||||||
|
return refFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction) {
|
||||||
|
//create a new function, with a new refCount
|
||||||
|
return Toy_createRefFunction(refFunction->data, refFunction->length);
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "toy_common.h"
|
||||||
|
|
||||||
|
//memory allocation hook
|
||||||
|
typedef void* (*Toy_RefFunctionAllocatorFn)(void* pointer, size_t oldSize, size_t newSize);
|
||||||
|
TOY_API void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn);
|
||||||
|
|
||||||
|
//the RefFunction structure
|
||||||
|
typedef struct Toy_RefFunction {
|
||||||
|
size_t length;
|
||||||
|
int refCount;
|
||||||
|
unsigned char data[];
|
||||||
|
} Toy_RefFunction;
|
||||||
|
|
||||||
|
//API
|
||||||
|
TOY_API Toy_RefFunction* Toy_createRefFunction(const void* data, size_t length);
|
||||||
|
TOY_API void Toy_deleteRefFunction(Toy_RefFunction* refFunction);
|
||||||
|
TOY_API int Toy_countRefFunction(Toy_RefFunction* refFunction);
|
||||||
|
TOY_API size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction);
|
||||||
|
TOY_API Toy_RefFunction* Toy_copyRefFunction(Toy_RefFunction* refFunction);
|
||||||
|
TOY_API Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction);
|
||||||
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
fn a() {
|
||||||
|
fn b() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert a()() == 42, "function within function failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
fn a() {
|
||||||
|
fn b() {
|
||||||
|
fn c() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert a()()() == 42, "function within function within function failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
var a = 0;
|
||||||
|
|
||||||
|
if (a++ >= 1) {
|
||||||
|
assert false, "increment postfix bugfix failed (first check)";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (a++ >= 1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
assert a == 2, "increment postfix bugfix failed (second check)";
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -92,4 +92,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//test deep-combine example
|
||||||
|
{
|
||||||
|
fn combine(a, b, c, d) {
|
||||||
|
return [[a, b], [c, d]];
|
||||||
|
}
|
||||||
|
|
||||||
|
assert combine(1, 2, 3, 4) == [[1, 2], [3, 4]], "deep-combine example failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
print "All good";
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
var result; //result must exist to ensure assingment, rather than declaration is invoked by the comparison below
|
||||||
|
|
||||||
|
var lhs = [0];
|
||||||
|
var rhs = [0];
|
||||||
|
|
||||||
|
result = lhs[0] < rhs[0]; //make sure this doesn't invoke TOY_OP_INDEX_ASSIGN_INTERMEDIATE
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -22,4 +22,9 @@ var dict: complex = [
|
|||||||
"third array": [7, 8, 9]
|
"third array": [7, 8, 9]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
//check the any type is recognized as a type within an array
|
||||||
|
var a: [type] = [int, bool, any];
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
print "All good";
|
||||||
|
|||||||
@@ -117,9 +117,12 @@ int main() {
|
|||||||
"dot-chaining.toy",
|
"dot-chaining.toy",
|
||||||
"dot-modulo-bugfix.toy",
|
"dot-modulo-bugfix.toy",
|
||||||
"dottify-bugfix.toy",
|
"dottify-bugfix.toy",
|
||||||
|
"function-within-function-bugfix.toy",
|
||||||
"functions.toy",
|
"functions.toy",
|
||||||
|
"increment-postfix-bugfix.toy",
|
||||||
"index-arrays.toy",
|
"index-arrays.toy",
|
||||||
"index-assignment-both-bugfix.toy",
|
"index-assignment-both-bugfix.toy",
|
||||||
|
"index-assignment-intermediate-bugfix.toy",
|
||||||
"index-assignment-left-bugfix.toy",
|
"index-assignment-left-bugfix.toy",
|
||||||
"index-dictionaries.toy",
|
"index-dictionaries.toy",
|
||||||
"index-strings.toy",
|
"index-strings.toy",
|
||||||
|
|||||||
Reference in New Issue
Block a user