Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c3c46b4fc6 | |||
| 0e41b00ef4 | |||
| f6ec6a8c73 | |||
| 2157b2f540 | |||
| 1481216e69 | |||
| f25f389b4e | |||
| deff784df8 | |||
| 54e82846c3 | |||
| 67fce427eb | |||
| 8a2cb61435 | |||
| 50d03e28fc | |||
| 763581c73b | |||
| cdb2613e5d | |||
| 733df87c08 | |||
| bfd506f497 |
@@ -4,9 +4,11 @@
|
||||
|
||||
# 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
|
||||
|
||||
@@ -14,7 +16,7 @@ Special thanks to http://craftinginterpreters.com/ for their fantastic book that
|
||||
* Bytecode intermediate compilation
|
||||
* Optional, but robust type system (including `opaque` for arbitrary data)
|
||||
* Functions and types are first-class citizens
|
||||
* Import external libraries
|
||||
* Import native libraries from the host
|
||||
* Fancy slice notation for strings, arrays and dictionaries
|
||||
* Can re-direct output, error and assertion failure messages
|
||||
* 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
|
||||
|
||||
Special thanks to http://craftinginterpreters.com/ for their fantastic book that set me on this path.
|
||||
+2
-4
@@ -115,15 +115,13 @@
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)/source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
<PreprocessorDefinitions>
|
||||
</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
<PreprocessorDefinitions>
|
||||
</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)/source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
|
||||
@@ -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 TOY_OUTDIR = out
|
||||
@@ -34,6 +30,10 @@ library-release: clean $(TOY_OUTDIR)
|
||||
static-release: clean $(TOY_OUTDIR)
|
||||
$(MAKE) -j8 -C source static-release
|
||||
|
||||
#distribution
|
||||
dist: export CFLAGS+=-O2 -mtune=native -march=native
|
||||
dist: repl-release
|
||||
|
||||
#utils
|
||||
test: clean $(TOY_OUTDIR)
|
||||
$(MAKE) -C test
|
||||
|
||||
+34
-3
@@ -1471,8 +1471,13 @@ static void recursiveLiteralQuicksortUtil(Toy_Interpreter* interpreter, Toy_Lite
|
||||
swapLiteralsUtil(&ptr[runner], &ptr[literalCount - 1]);
|
||||
|
||||
//recurse on each end
|
||||
recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompareLiteral);
|
||||
recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], literalCount - runner - 1, fnCompareLiteral);
|
||||
if (runner > 0) {
|
||||
recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompareLiteral);
|
||||
}
|
||||
|
||||
if (runner < literalCount) {
|
||||
recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], literalCount - runner - 1, fnCompareLiteral);
|
||||
}
|
||||
}
|
||||
|
||||
static int nativeSort(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
@@ -1511,8 +1516,34 @@ static int nativeSort(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
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
|
||||
if (TOY_IS_ARRAY(selfLiteral)) {
|
||||
if (!sorted) {
|
||||
recursiveLiteralQuicksortUtil(interpreter, TOY_AS_ARRAY(selfLiteral)->literals, TOY_AS_ARRAY(selfLiteral)->count, fnLiteral);
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -193,8 +193,14 @@ int main(int argc, const char* argv[]) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//run the binary file
|
||||
Toy_runBinaryFile(Toy_commandLine.binaryfile);
|
||||
if (Toy_commandLine.parseBytecodeHeader) {
|
||||
//only parse the bytecode header
|
||||
Toy_parseBinaryFileHeader(Toy_commandLine.binaryfile);
|
||||
}
|
||||
else {
|
||||
//run the binary file
|
||||
Toy_runBinaryFile(Toy_commandLine.binaryfile);
|
||||
}
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveSystem();
|
||||
|
||||
@@ -149,3 +149,59 @@ void Toy_runSourceFile(const char* fname) {
|
||||
Toy_runSource(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_runSourceFile(const char* fname);
|
||||
|
||||
void Toy_parseBinaryFileHeader(const char* fname);
|
||||
+1
-1
@@ -4,7 +4,7 @@ fn fib(n : int) {
|
||||
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);
|
||||
print string i + ": " + string res;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
C 0.002s 1
|
||||
JS 0.064s 32
|
||||
Py3 0.070s 35
|
||||
Toy 0.430s 215
|
||||
@@ -1,22 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//functions be local in C
|
||||
int sum(int n) {
|
||||
if (n < 2) {
|
||||
return n;
|
||||
}
|
||||
|
||||
return n + sum(n - 1);
|
||||
}
|
||||
|
||||
//the test case (C)
|
||||
void test_sum(int key, int val) {
|
||||
const int result = sum(val);
|
||||
printf("%d: %d\n", key, result);
|
||||
}
|
||||
|
||||
int main() {
|
||||
for (int i = 0; i <= 10; i++) {
|
||||
test_sum(i, i * 1000);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
//the test case (js)
|
||||
function test_sum(key, val) {
|
||||
function sum(n) {
|
||||
if (n < 2) {
|
||||
return n;
|
||||
}
|
||||
|
||||
return n + sum(n - 1);
|
||||
}
|
||||
|
||||
const result = sum(val);
|
||||
console.log(`${key}: ${result}`);
|
||||
}
|
||||
|
||||
for (let i = 0; i <= 10; i++) {
|
||||
test_sum(i, i * 1000);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import sys
|
||||
sys.setrecursionlimit(11000)
|
||||
|
||||
#the test case (python)
|
||||
def test_sum(key: int, val: int):
|
||||
def sum(n: int):
|
||||
if n < 2:
|
||||
return n
|
||||
|
||||
return n + sum(n - 1)
|
||||
|
||||
result: int = sum(val)
|
||||
print(str(key) + ": " + str(result))
|
||||
|
||||
for i in range(0, 10):
|
||||
test_sum(i, i * 1000)
|
||||
@@ -1,17 +0,0 @@
|
||||
//the test case (toy)
|
||||
fn test_sum(key: int, val: int) {
|
||||
fn sum(n: int) {
|
||||
if (n < 2) {
|
||||
return n;
|
||||
}
|
||||
|
||||
return n + sum(n - 1);
|
||||
}
|
||||
|
||||
var result: int const = sum(val);
|
||||
print string key + ": " + string result;
|
||||
}
|
||||
|
||||
for (var i: int = 0; i <= 10; i++) {
|
||||
test_sum(i, i * 1000);
|
||||
}
|
||||
+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
|
||||
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_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 int) == 4);
|
||||
|
||||
#ifndef TOY_DISABLE_REPL
|
||||
static const char* build = __DATE__ " " __TIME__;
|
||||
|
||||
const char* Toy_private_version_build() {
|
||||
return build;
|
||||
}
|
||||
|
||||
//declare the singleton with default values
|
||||
Toy_CommandLine Toy_commandLine = {
|
||||
@@ -29,6 +33,7 @@ Toy_CommandLine Toy_commandLine = {
|
||||
.source = NULL,
|
||||
.initialfile = NULL,
|
||||
.enablePrintNewline = true,
|
||||
.parseBytecodeHeader = false,
|
||||
.verbose = false
|
||||
};
|
||||
|
||||
@@ -89,6 +94,16 @@ void Toy_initCommandLine(int argc, const char* argv[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "-p")) {
|
||||
Toy_commandLine.parseBytecodeHeader = true;
|
||||
|
||||
if (Toy_commandLine.binaryfile) {
|
||||
Toy_commandLine.error = false;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "-n")) {
|
||||
Toy_commandLine.enablePrintNewline = 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(" -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(" -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");
|
||||
}
|
||||
|
||||
@@ -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("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_MINOR 1
|
||||
#define TOY_VERSION_PATCH 3
|
||||
#define TOY_VERSION_BUILD __DATE__ " " __TIME__
|
||||
#define TOY_VERSION_PATCH 6
|
||||
#define TOY_VERSION_BUILD Toy_private_version_build()
|
||||
|
||||
//platform/compiler-specific instructions
|
||||
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef TOY_DISABLE_REPL
|
||||
TOY_API const char* Toy_private_version_build();
|
||||
|
||||
//for processing the command line arguments in the repl
|
||||
typedef struct {
|
||||
@@ -42,6 +42,7 @@ typedef struct {
|
||||
char* source;
|
||||
char* initialfile;
|
||||
bool enablePrintNewline;
|
||||
bool parseBytecodeHeader;
|
||||
bool verbose;
|
||||
} 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_helpCommandLine(int argc, const char* argv[]);
|
||||
TOY_API void Toy_copyrightCommandLine(int argc, const char* argv[]);
|
||||
|
||||
#endif
|
||||
+77
-1
@@ -259,6 +259,81 @@ static int writeLiteralToCompiler(Toy_Compiler* compiler, Toy_Literal literal) {
|
||||
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: 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) {
|
||||
@@ -322,7 +397,8 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
|
||||
//return this if...
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
#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
|
||||
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
||||
|
||||
+63
-175
@@ -85,55 +85,93 @@ bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_Ho
|
||||
}
|
||||
|
||||
void Toy_parseCompoundToPureValues(Toy_Interpreter* interpreter, Toy_Literal* literalPtr) {
|
||||
if (TOY_IS_IDENTIFIER(*literalPtr)) {
|
||||
Toy_parseIdentifierToValue(interpreter, literalPtr);
|
||||
}
|
||||
|
||||
//parse out an array
|
||||
if (TOY_IS_ARRAY(*literalPtr)) {
|
||||
for (int i = 0; i < TOY_AS_ARRAY(*literalPtr)->count; i++) {
|
||||
Toy_Literal index = TOY_TO_INTEGER_LITERAL(i);
|
||||
Toy_Literal entry = Toy_getLiteralArray(TOY_AS_ARRAY(*literalPtr), index);
|
||||
//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 entry = Toy_getLiteralArray(TOY_AS_ARRAY(*literalPtr), index);
|
||||
|
||||
if (TOY_IS_IDENTIFIER( entry )) {
|
||||
Toy_Literal idn = entry;
|
||||
Toy_parseCompoundToPureValues(interpreter, &entry);
|
||||
Toy_parseIdentifierToValue(interpreter, &entry);
|
||||
|
||||
Toy_setLiteralArray(TOY_AS_ARRAY(*literalPtr), index, entry);
|
||||
|
||||
Toy_freeLiteral(idn);
|
||||
Toy_freeLiteral(index);
|
||||
Toy_freeLiteral(entry);
|
||||
}
|
||||
|
||||
Toy_freeLiteral(index);
|
||||
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
|
||||
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_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++) {
|
||||
if ( TOY_IS_NULL(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Toy_Literal key = TOY_TO_NULL_LITERAL;
|
||||
Toy_Literal value = TOY_TO_NULL_LITERAL;
|
||||
Toy_Literal key = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key);
|
||||
Toy_Literal value = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value);
|
||||
|
||||
key = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].key);
|
||||
value = Toy_copyLiteral(TOY_AS_DICTIONARY(*literalPtr)->entries[i].value);
|
||||
|
||||
//
|
||||
if (TOY_IS_IDENTIFIER( key ) || TOY_IS_IDENTIFIER(value)) {
|
||||
//check each key/value pair, and sub-compounds
|
||||
if (TOY_IS_IDENTIFIER( key )) {
|
||||
Toy_parseIdentifierToValue(interpreter, &key);
|
||||
}
|
||||
if (TOY_IS_ARRAY(key) || TOY_IS_DICTIONARY(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_setLiteralDictionary(ret, key, value);
|
||||
|
||||
//
|
||||
//cleanup
|
||||
Toy_freeLiteral(key);
|
||||
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)) {
|
||||
Toy_parseCompoundToPureValues(interpreter, literalPtr);
|
||||
}
|
||||
// if (TOY_IS_ARRAY(*literalPtr) || TOY_IS_DICTIONARY(*literalPtr)) {
|
||||
// Toy_parseCompoundToPureValues(interpreter, literalPtr);
|
||||
// }
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -236,12 +274,6 @@ static bool execAssert(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(lhsIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lhs)) {
|
||||
Toy_freeLiteral(lhs);
|
||||
Toy_freeLiteral(rhs);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TOY_IS_STRING(rhs)) {
|
||||
interpreter->errorOutput("The assert keyword needs a string as the second argument, received: ");
|
||||
Toy_printLiteralCustom(rhs, interpreter->errorOutput);
|
||||
@@ -276,11 +308,6 @@ static bool execPrint(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lit)) {
|
||||
Toy_freeLiteral(lit);
|
||||
return false;
|
||||
}
|
||||
|
||||
Toy_printLiteralCustom(lit, interpreter->printOutput);
|
||||
|
||||
Toy_freeLiteral(lit);
|
||||
@@ -313,11 +340,6 @@ static bool rawLiteral(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lit)) {
|
||||
Toy_freeLiteral(lit);
|
||||
return false;
|
||||
}
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, lit);
|
||||
Toy_freeLiteral(lit);
|
||||
|
||||
@@ -333,11 +355,6 @@ static bool execNegate(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lit)) {
|
||||
Toy_freeLiteral(lit);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TOY_IS_INTEGER(lit)) {
|
||||
lit = TOY_TO_INTEGER_LITERAL(-TOY_AS_INTEGER(lit));
|
||||
}
|
||||
@@ -369,11 +386,6 @@ static bool execInvert(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lit)) {
|
||||
Toy_freeLiteral(lit);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TOY_IS_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);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
||||
Toy_freeLiteral(lhs);
|
||||
Toy_freeLiteral(rhs);
|
||||
return false;
|
||||
}
|
||||
|
||||
//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)) {
|
||||
//check for overflow
|
||||
@@ -545,10 +551,6 @@ static Toy_Literal parseTypeToValue(Toy_Interpreter* interpreter, Toy_Literal ty
|
||||
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 (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++) {
|
||||
@@ -589,12 +591,6 @@ static bool execVarDecl(Toy_Interpreter* interpreter, bool lng) {
|
||||
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
|
||||
type = parseTypeToValue(interpreter, type);
|
||||
|
||||
@@ -612,13 +608,6 @@ static bool execVarDecl(Toy_Interpreter* interpreter, bool lng) {
|
||||
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)) {
|
||||
Toy_parseCompoundToPureValues(interpreter, &val);
|
||||
}
|
||||
@@ -699,12 +688,6 @@ static bool execVarAssign(Toy_Interpreter* interpreter) {
|
||||
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)) {
|
||||
Toy_parseCompoundToPureValues(interpreter, &rhs);
|
||||
}
|
||||
@@ -774,12 +757,6 @@ static bool execValCast(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(valueIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(value)) {
|
||||
Toy_freeLiteral(type);
|
||||
Toy_freeLiteral(value);
|
||||
return false;
|
||||
}
|
||||
|
||||
Toy_Literal result = TOY_TO_NULL_LITERAL;
|
||||
|
||||
if (TOY_IS_NULL(value)) {
|
||||
@@ -914,12 +891,6 @@ static bool execCompareEqual(Toy_Interpreter* interpreter, bool invert) {
|
||||
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);
|
||||
|
||||
if (invert) {
|
||||
@@ -948,12 +919,6 @@ static bool execCompareLess(Toy_Interpreter* interpreter, bool invert) {
|
||||
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
|
||||
if (!(TOY_IS_INTEGER(lhs) || TOY_IS_FLOAT(lhs))) {
|
||||
interpreter->errorOutput("Incorrect type in comparison, value \"");
|
||||
@@ -1014,12 +979,6 @@ static bool execCompareLessEqual(Toy_Interpreter* interpreter, bool invert) {
|
||||
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
|
||||
if (!(TOY_IS_INTEGER(lhs) || TOY_IS_FLOAT(lhs))) {
|
||||
interpreter->errorOutput("Incorrect type in comparison, value \"");
|
||||
@@ -1081,12 +1040,6 @@ static bool execAnd(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(lhsIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
||||
Toy_freeLiteral(lhs);
|
||||
Toy_freeLiteral(rhs);
|
||||
return false;
|
||||
}
|
||||
|
||||
//short-circuit support
|
||||
if (!TOY_IS_TRUTHY(lhs)) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||
@@ -1115,12 +1068,6 @@ static bool execOr(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(lhsIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lhs) || TOY_IS_IDENTIFIER(rhs)) {
|
||||
Toy_freeLiteral(lhs);
|
||||
Toy_freeLiteral(rhs);
|
||||
return false;
|
||||
}
|
||||
|
||||
//short-circuit support
|
||||
if (TOY_IS_TRUTHY(lhs)) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||
@@ -1165,11 +1112,6 @@ static bool execFalseJump(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(litIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(lit)) {
|
||||
Toy_freeLiteral(lit);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TOY_IS_NULL(lit)) {
|
||||
interpreter->errorOutput("Null detected in comparison\n");
|
||||
Toy_freeLiteral(lit);
|
||||
@@ -1193,7 +1135,7 @@ static void readInterpreterSections(Toy_Interpreter* interpreter);
|
||||
//also supports identifier & arg1 to be other way around (looseFirstArgument)
|
||||
static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
||||
//BUGFIX: depth check - don't drown!
|
||||
if (interpreter->depth >= 1000 * 10) {
|
||||
if (interpreter->depth >= 1000) {
|
||||
interpreter->errorOutput("Infinite recursion detected - panicking\n");
|
||||
interpreter->panic = true;
|
||||
return false;
|
||||
@@ -1229,12 +1171,8 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
||||
|
||||
//get the function literal
|
||||
Toy_Literal func = identifier;
|
||||
|
||||
if (!Toy_parseIdentifierToValue(interpreter, &func)) {
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
Toy_freeLiteral(stackSize);
|
||||
if (!TOY_IS_FUNCTION(func) && Toy_parseIdentifierToValue(interpreter, &func)) {
|
||||
Toy_freeLiteral(identifier);
|
||||
return false;
|
||||
}
|
||||
|
||||
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_freeLiteral(func);
|
||||
Toy_freeLiteral(stackSize);
|
||||
Toy_freeLiteral(identifier);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1390,17 +1327,6 @@ bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_Liter
|
||||
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)) {
|
||||
interpreter->errorOutput("[internal] Could not define parameter (bad type?)\n");
|
||||
|
||||
@@ -1569,12 +1495,6 @@ static bool execFnReturn(Toy_Interpreter* interpreter) {
|
||||
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)) {
|
||||
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 compoundIdn = compound;
|
||||
bool freeIdn = false;
|
||||
bool freeIdn = false; //wtf?
|
||||
if (TOY_IS_IDENTIFIER(compound) && Toy_parseIdentifierToValue(interpreter, &compound)) {
|
||||
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)) {
|
||||
interpreter->errorOutput("Unknown compound found in index notation: ");
|
||||
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
||||
@@ -1792,32 +1701,11 @@ static bool execIndexAssign(Toy_Interpreter* interpreter, int assignDepth) {
|
||||
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;
|
||||
if (TOY_IS_IDENTIFIER(assign) && Toy_parseIdentifierToValue(interpreter, &assign)) {
|
||||
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)) {
|
||||
interpreter->errorOutput("Unknown compound found in index assigning notation: ");
|
||||
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
||||
|
||||
+21
-6
@@ -119,6 +119,10 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
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
|
||||
for (int i = 0; i < TOY_AS_ARRAY(original)->count; i++) {
|
||||
Toy_pushLiteralArray(array, TOY_AS_ARRAY(original)->literals[i]);
|
||||
@@ -131,6 +135,15 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
Toy_LiteralDictionary* dictionary = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
|
||||
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
|
||||
for (int i = 0; i < TOY_AS_DICTIONARY(original)->capacity; i++) {
|
||||
if ( !TOY_IS_NULL(TOY_AS_DICTIONARY(original)->entries[i].key) ) {
|
||||
@@ -168,7 +181,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
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_initLiteralArray(array);
|
||||
|
||||
@@ -184,7 +197,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
case TOY_LITERAL_DICTIONARY_INTERMEDIATE: {
|
||||
case TOY_LITERAL_DICTIONARY_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
Toy_initLiteralArray(array);
|
||||
|
||||
@@ -200,7 +213,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
case TOY_LITERAL_TYPE_INTERMEDIATE: {
|
||||
case TOY_LITERAL_TYPE_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
Toy_initLiteralArray(array);
|
||||
|
||||
@@ -372,8 +385,10 @@ int Toy_hashLiteral(Toy_Literal lit) {
|
||||
case TOY_LITERAL_INTEGER:
|
||||
return hashUInt((unsigned int)TOY_AS_INTEGER(lit));
|
||||
|
||||
case TOY_LITERAL_FLOAT:
|
||||
return hashUInt(*(unsigned int*)(&TOY_AS_FLOAT(lit)));
|
||||
case TOY_LITERAL_FLOAT: {
|
||||
unsigned int* ptr = (unsigned int*)(&TOY_AS_FLOAT(lit));
|
||||
return hashUInt(*ptr);
|
||||
}
|
||||
|
||||
case TOY_LITERAL_STRING:
|
||||
return hashString(Toy_toCString(TOY_AS_STRING(lit)), Toy_lengthRefString(TOY_AS_STRING(lit)));
|
||||
@@ -440,7 +455,7 @@ static void printToBuffer(const char* str) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "toy_memory.h"
|
||||
#include "toy_refstring.h"
|
||||
#include "toy_reffunction.h"
|
||||
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
@@ -49,4 +50,5 @@ void Toy_setMemoryAllocator(Toy_MemoryAllocatorFn fn) {
|
||||
|
||||
allocator = 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_SECTION_END = 255,
|
||||
//TODO: add more
|
||||
|
||||
//prefix & postfix signals (used internally)
|
||||
TOY_OP_PREFIX,
|
||||
TOY_OP_POSTFIX,
|
||||
} Toy_Opcode;
|
||||
|
||||
|
||||
+28
-6
@@ -119,6 +119,7 @@ ParseRule parseRules[];
|
||||
static void declaration(Toy_Parser* parser, Toy_ASTNode** nodeHandle);
|
||||
static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, PrecedenceRule rule);
|
||||
static Toy_Literal readTypeToLiteral(Toy_Parser* parser);
|
||||
static void varDecl(Toy_Parser* parser, Toy_ASTNode** nodeHandle);
|
||||
|
||||
//TODO: resolve the messy order of these
|
||||
//the expression rules
|
||||
@@ -627,6 +628,14 @@ static Toy_Opcode castingPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
}
|
||||
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:
|
||||
error(parser, parser->previous, "Unexpected token passed to casting precedence rule");
|
||||
return TOY_OP_EOF;
|
||||
@@ -684,7 +693,7 @@ static Toy_Opcode incrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle)
|
||||
|
||||
Toy_freeASTNode(tmpNode);
|
||||
|
||||
return TOY_OP_EOF;
|
||||
return TOY_OP_PREFIX;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return TOY_OP_EOF;
|
||||
return TOY_OP_POSTFIX;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return TOY_OP_EOF;
|
||||
return TOY_OP_PREFIX;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return TOY_OP_EOF;
|
||||
return TOY_OP_POSTFIX;
|
||||
}
|
||||
|
||||
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_FUNCTION,
|
||||
{NULL, NULL, PREC_NONE},// TOKEN_OPAQUE,
|
||||
{NULL, NULL, PREC_NONE},// TOKEN_ANY,
|
||||
{castingPrefix, NULL, PREC_CALL},// TOKEN_ANY,
|
||||
|
||||
//keywords and reserved words
|
||||
{NULL, NULL, PREC_NONE},// TOKEN_AS,
|
||||
@@ -1278,6 +1287,13 @@ static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, Preced
|
||||
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);
|
||||
|
||||
//optimise away the constants
|
||||
@@ -1397,7 +1413,13 @@ static void forStmt(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
|
||||
//check the pre-clause
|
||||
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 {
|
||||
consume(parser, TOY_TOKEN_SEMICOLON, "Expected ';' after empty declaration of for clause");
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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]
|
||||
];
|
||||
|
||||
|
||||
//check the any type is recognized as a type within an array
|
||||
var a: [type] = [int, bool, any];
|
||||
|
||||
|
||||
print "All good";
|
||||
|
||||
@@ -117,9 +117,12 @@ int main() {
|
||||
"dot-chaining.toy",
|
||||
"dot-modulo-bugfix.toy",
|
||||
"dottify-bugfix.toy",
|
||||
"function-within-function-bugfix.toy",
|
||||
"functions.toy",
|
||||
"increment-postfix-bugfix.toy",
|
||||
"index-arrays.toy",
|
||||
"index-assignment-both-bugfix.toy",
|
||||
"index-assignment-intermediate-bugfix.toy",
|
||||
"index-assignment-left-bugfix.toy",
|
||||
"index-dictionaries.toy",
|
||||
"index-strings.toy",
|
||||
|
||||
Reference in New Issue
Block a user