Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fa224fa7b | |||
| 8a68d864e6 | |||
| 49f240ea07 | |||
| 3acbd7447a | |||
| 6f126e6daa | |||
| 2adb9d9158 | |||
| 1668dca255 | |||
| 501ff6fff4 | |||
| 3845627fe5 | |||
| cdae03bd54 | |||
| 7b501b71b5 | |||
| 913738a4d1 | |||
| 3312a38c7c | |||
| 71b57fd42c | |||
| 453afbab41 |
+19
-2
@@ -74,6 +74,10 @@
|
||||
<OutDir>$(SolutionDir)out\$(Configuration)\</OutDir>
|
||||
<IntDir>$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<OutDir>$(SolutionDir)out\$(Configuration)\</OutDir>
|
||||
<IntDir>$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -110,11 +114,25 @@
|
||||
</Link>
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>C:\Users\kayne\Desktop\Toy\source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
<PreprocessorDefinitions>
|
||||
</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
<PreprocessorDefinitions>
|
||||
</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>C:\Users\kayne\Desktop\Toy\source;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)out\$(Configuration)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Toy.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="repl\lib_about.c" />
|
||||
<ClCompile Include="repl\lib_compound.c" />
|
||||
<ClCompile Include="repl\lib_runner.c" />
|
||||
<ClCompile Include="repl\lib_standard.c" />
|
||||
<ClCompile Include="repl\repl_main.c" />
|
||||
@@ -122,7 +140,6 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="repl\lib_about.h" />
|
||||
<ClInclude Include="repl\lib_compound.h" />
|
||||
<ClInclude Include="repl\lib_runner.h" />
|
||||
<ClInclude Include="repl\lib_standard.h" />
|
||||
<ClInclude Include="repl\repl_tools.h" />
|
||||
|
||||
+12
-1
@@ -41,7 +41,7 @@
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
@@ -73,6 +73,10 @@
|
||||
<OutDir>$(SolutionDir)out\$(Configuration)\</OutDir>
|
||||
<IntDir>$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<OutDir>$(SolutionDir)out\$(Configuration)\</OutDir>
|
||||
<IntDir>$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;TOY_EXPORTS;TOY_EXPORT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -105,6 +109,13 @@
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>TOY_EXPORT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
<PreprocessorDefinitions>TOY_EXPORT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
|
||||
+2
-1
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct Toy_Runner {
|
||||
Toy_Interpreter interpreter;
|
||||
@@ -48,7 +49,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
||||
|
||||
//load and compile the bytecode
|
||||
size_t fileSize = 0;
|
||||
const char* source = Toy_readFile(filePath, &fileSize);
|
||||
const char* source = (const char*)Toy_readFile(filePath, &fileSize);
|
||||
|
||||
if (!source) {
|
||||
interpreter->errorOutput("Failed to load source file\n");
|
||||
|
||||
+4
-3
@@ -1,13 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "toy_common.h"
|
||||
#include "toy_interpreter.h"
|
||||
|
||||
int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias);
|
||||
|
||||
//file system API - these need to be set by the host
|
||||
void Toy_initDriveDictionary();
|
||||
void Toy_freeDriveDictionary();
|
||||
Toy_LiteralDictionary* Toy_getDriveDictionary();
|
||||
TOY_API void Toy_initDriveDictionary();
|
||||
TOY_API void Toy_freeDriveDictionary();
|
||||
TOY_API Toy_LiteralDictionary* Toy_getDriveDictionary();
|
||||
|
||||
#define TOY_OPAQUE_TAG_RUNNER 100
|
||||
|
||||
|
||||
+2
-1
@@ -2,9 +2,10 @@
|
||||
|
||||
#include "toy_memory.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
//no arguments
|
||||
|
||||
+3
-3
@@ -75,7 +75,7 @@ void repl(const char* initialInput) {
|
||||
|
||||
if (!error) {
|
||||
//get the bytecode dump
|
||||
int size = 0;
|
||||
size_t size = 0;
|
||||
unsigned char* tb = Toy_collateCompiler(&compiler, &size);
|
||||
|
||||
//run the bytecode
|
||||
@@ -180,7 +180,7 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
//compile and save
|
||||
size_t size = 0;
|
||||
const char* source = Toy_readFile(Toy_commandLine.compilefile, &size);
|
||||
const char* source = (const char*)Toy_readFile(Toy_commandLine.compilefile, &size);
|
||||
if (!source) {
|
||||
return 1;
|
||||
}
|
||||
@@ -220,7 +220,7 @@ int main(int argc, const char* argv[]) {
|
||||
}
|
||||
|
||||
size_t size;
|
||||
initialSource = Toy_readFile(Toy_commandLine.initialfile, &size);
|
||||
initialSource = (const char*)Toy_readFile(Toy_commandLine.initialfile, &size);
|
||||
}
|
||||
|
||||
repl(initialSource);
|
||||
|
||||
+6
-6
@@ -14,7 +14,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
//IO functions
|
||||
const char* Toy_readFile(const char* path, size_t* fileSize) {
|
||||
const unsigned char* Toy_readFile(const char* path, size_t* fileSize) {
|
||||
FILE* file = fopen(path, "rb");
|
||||
|
||||
if (file == NULL) {
|
||||
@@ -26,14 +26,14 @@ const char* Toy_readFile(const char* path, size_t* fileSize) {
|
||||
*fileSize = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
char* buffer = (char*)malloc(*fileSize + 1);
|
||||
unsigned char* buffer = (unsigned char*)malloc(*fileSize + 1);
|
||||
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "Not enough memory to read \"%s\"\n" TOY_CC_RESET, path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file);
|
||||
size_t bytesRead = fread(buffer, sizeof(unsigned char), *fileSize, file);
|
||||
|
||||
buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this
|
||||
|
||||
@@ -94,7 +94,7 @@ const unsigned char* Toy_compileString(const char* source, size_t* size) {
|
||||
}
|
||||
|
||||
//get the bytecode dump
|
||||
const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
|
||||
const unsigned char* tb = Toy_collateCompiler(&compiler, size);
|
||||
|
||||
//cleanup
|
||||
Toy_freeCompiler(&compiler);
|
||||
@@ -120,7 +120,7 @@ void Toy_runBinary(const unsigned char* tb, size_t size) {
|
||||
|
||||
void Toy_runBinaryFile(const char* fname) {
|
||||
size_t size = 0; //not used
|
||||
const unsigned char* tb = (const unsigned char*)Toy_readFile(fname, &size);
|
||||
const unsigned char* tb = Toy_readFile(fname, &size);
|
||||
if (!tb) {
|
||||
return;
|
||||
}
|
||||
@@ -140,7 +140,7 @@ void Toy_runSource(const char* source) {
|
||||
|
||||
void Toy_runSourceFile(const char* fname) {
|
||||
size_t size = 0; //not used
|
||||
const char* source = Toy_readFile(fname, &size);
|
||||
const char* source = (const char*)Toy_readFile(fname, &size);
|
||||
if (!source) {
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "toy_common.h"
|
||||
|
||||
const char* Toy_readFile(const char* path, size_t* fileSize);
|
||||
const unsigned char* Toy_readFile(const char* path, size_t* fileSize);
|
||||
int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size);
|
||||
|
||||
const unsigned char* Toy_compileString(const char* source, size_t* size);
|
||||
|
||||
@@ -17,5 +17,5 @@ fn fib(n : int) {
|
||||
|
||||
for (var i = 0; i < 40; i++) {
|
||||
var res = fib(i);
|
||||
print string i + ": " + string res + "\n";
|
||||
print string i + ": " + string res;
|
||||
}
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
//WARNING: please think twice before using this in a test
|
||||
fn fib(n : int) {
|
||||
if (n < 2) return n;
|
||||
return fib(n-1) + fib(n-2);
|
||||
@@ -5,5 +6,5 @@ fn fib(n : int) {
|
||||
|
||||
for (var i = 0; i < 20; i++) {
|
||||
var res = fib(i);
|
||||
print string i + ": " + string res + "\n";
|
||||
print string i + ": " + string res;
|
||||
}
|
||||
+2
-2
@@ -29,7 +29,7 @@ for (var i = 0; i < SIZE -1; i++) {
|
||||
prev += " ";
|
||||
}
|
||||
prev += "*"; //initial
|
||||
print prev + "\n";
|
||||
print prev;
|
||||
|
||||
//run
|
||||
for (var iteration = 0; iteration < SIZE -1; iteration++) {
|
||||
@@ -44,6 +44,6 @@ for (var iteration = 0; iteration < SIZE -1; iteration++) {
|
||||
//right
|
||||
output += (lookup[prev[SIZE-2]][prev[SIZE-1]][" "]);
|
||||
|
||||
print output + "\n";
|
||||
print output;
|
||||
prev = output;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "toy_literal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
//static math utils, copied from the interpreter
|
||||
static Toy_Literal addition(Toy_Interpreter* interpreter, Toy_Literal lhs, Toy_Literal rhs) {
|
||||
|
||||
+14
-15
@@ -15,23 +15,22 @@ STATIC_ASSERT(sizeof(unsigned char) == 1);
|
||||
STATIC_ASSERT(sizeof(unsigned short) == 2);
|
||||
STATIC_ASSERT(sizeof(unsigned int) == 4);
|
||||
|
||||
//declare the singleton
|
||||
Toy_CommandLine Toy_commandLine;
|
||||
//declare the singleton with default values
|
||||
Toy_CommandLine Toy_commandLine = {
|
||||
.error = false,
|
||||
.help = false,
|
||||
.version = false,
|
||||
.binaryfile = NULL,
|
||||
.sourcefile = NULL,
|
||||
.compilefile = NULL,
|
||||
.outfile = "out.tb",
|
||||
.source = NULL,
|
||||
.initialfile = NULL,
|
||||
.enablePrintNewline = true,
|
||||
.verbose = false
|
||||
};
|
||||
|
||||
void Toy_initCommandLine(int argc, const char* argv[]) {
|
||||
//default values
|
||||
Toy_commandLine.error = false;
|
||||
Toy_commandLine.help = false;
|
||||
Toy_commandLine.version = false;
|
||||
Toy_commandLine.binaryfile = NULL;
|
||||
Toy_commandLine.sourcefile = NULL;
|
||||
Toy_commandLine.compilefile = NULL;
|
||||
Toy_commandLine.outfile = "out.tb";
|
||||
Toy_commandLine.source = NULL;
|
||||
Toy_commandLine.initialfile = NULL;
|
||||
Toy_commandLine.enablePrintNewline = true;
|
||||
Toy_commandLine.verbose = false;
|
||||
|
||||
for (int i = 1; i < argc; i++) { //start at 1 to skip the program name
|
||||
Toy_commandLine.error = true; //error state by default, set to false by successful flags
|
||||
|
||||
|
||||
+2
-1
@@ -6,7 +6,7 @@
|
||||
|
||||
#define TOY_VERSION_MAJOR 0
|
||||
#define TOY_VERSION_MINOR 9
|
||||
#define TOY_VERSION_PATCH 0
|
||||
#define TOY_VERSION_PATCH 2
|
||||
#define TOY_VERSION_BUILD __DATE__ " " __TIME__
|
||||
|
||||
//platform/compiler-specific instructions
|
||||
@@ -43,6 +43,7 @@ typedef struct {
|
||||
bool verbose;
|
||||
} Toy_CommandLine;
|
||||
|
||||
//these are intended for the repl only, despite using the api prefix
|
||||
TOY_API Toy_CommandLine Toy_commandLine;
|
||||
|
||||
TOY_API void Toy_initCommandLine(int argc, const char* argv[]);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void Toy_initCompiler(Toy_Compiler* compiler) {
|
||||
Toy_initLiteralArray(&compiler->literalCache);
|
||||
@@ -981,6 +982,8 @@ void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node) {
|
||||
if (op != TOY_OP_EOF) {//compensate for indexing & dot notation being screwy
|
||||
compiler->bytecode[compiler->count++] = (unsigned char)op; //1 byte
|
||||
}
|
||||
|
||||
//TODO: could free up AST Nodes
|
||||
}
|
||||
|
||||
void Toy_freeCompiler(Toy_Compiler* compiler) {
|
||||
@@ -989,6 +992,7 @@ void Toy_freeCompiler(Toy_Compiler* compiler) {
|
||||
compiler->bytecode = NULL;
|
||||
compiler->capacity = 0;
|
||||
compiler->count = 0;
|
||||
compiler->panic = false;
|
||||
}
|
||||
|
||||
static void emitByte(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) {
|
||||
@@ -1036,7 +1040,7 @@ static void emitFloat(unsigned char** collationPtr, int* capacityPtr, int* count
|
||||
}
|
||||
|
||||
//return the result
|
||||
static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size, bool embedHeader) {
|
||||
static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, size_t* size, bool embedHeader) {
|
||||
if (compiler->panic) {
|
||||
fprintf(stderr, TOY_CC_ERROR "[internal] Can't collate a panicked compiler\n" TOY_CC_RESET);
|
||||
return NULL;
|
||||
@@ -1181,14 +1185,14 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size
|
||||
void* fnCompiler = TOY_AS_FUNCTION(fn).inner.bytecode; //store the compiler here for now
|
||||
|
||||
//collate the function into bytecode (without header)
|
||||
int size = 0;
|
||||
size_t size = 0;
|
||||
unsigned char* bytes = collateCompilerHeaderOpt((Toy_Compiler*)fnCompiler, &size, false);
|
||||
|
||||
//emit how long this section is, +1 for ending mark
|
||||
Toy_emitShort(&fnCollation, &fnCapacity, &fnCount, (unsigned short)size + 1);
|
||||
|
||||
//write the fn to the fn collation
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
emitByte(&fnCollation, &fnCapacity, &fnCount, bytes[i]);
|
||||
}
|
||||
|
||||
@@ -1296,6 +1300,6 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size
|
||||
}
|
||||
|
||||
//the whole point of the compiler is to alter bytecode, so leave it as non-const
|
||||
unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, int* size) {
|
||||
unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size) {
|
||||
return collateCompilerHeaderOpt(compiler, size, true);
|
||||
}
|
||||
|
||||
@@ -19,4 +19,4 @@ TOY_API void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node);
|
||||
TOY_API void Toy_freeCompiler(Toy_Compiler* compiler);
|
||||
|
||||
//embed the header, data section, code section, function section, etc.
|
||||
TOY_API unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, int* size);
|
||||
TOY_API unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size);
|
||||
|
||||
@@ -21,9 +21,7 @@ static void printWrapper(const char* output) {
|
||||
}
|
||||
|
||||
static void assertWrapper(const char* output) {
|
||||
fprintf(stderr, TOY_CC_ERROR "Assertion failure: ");
|
||||
fprintf(stderr, "%s", output);
|
||||
fprintf(stderr, "\n" TOY_CC_RESET); //default new line
|
||||
fprintf(stderr, TOY_CC_ERROR "Assertion failure: %s\n" TOY_CC_RESET, output);
|
||||
}
|
||||
|
||||
static void errorWrapper(const char* output) {
|
||||
@@ -37,8 +35,7 @@ bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_Nati
|
||||
return false;
|
||||
}
|
||||
|
||||
int identifierLength = strlen(name);
|
||||
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefStringLength(name, identifierLength));
|
||||
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(name));
|
||||
|
||||
//make sure the name isn't taken
|
||||
if (Toy_existsLiteralDictionary(&interpreter->scope->variables, identifier)) {
|
||||
@@ -1180,6 +1177,8 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
||||
bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) {
|
||||
//check for side-loaded native functions
|
||||
if (TOY_IS_FUNCTION_NATIVE(func)) {
|
||||
//TODO: parse out identifier values, see issue #64
|
||||
|
||||
//call the native function
|
||||
int returnsCount = TOY_AS_FUNCTION_NATIVE(func)(interpreter, arguments);
|
||||
|
||||
@@ -2317,7 +2316,6 @@ static void readInterpreterSections(Toy_Interpreter* interpreter) {
|
||||
|
||||
//change the type to normal
|
||||
interpreter->literalCache.literals[i] = TOY_TO_FUNCTION_LITERAL(bytes, size);
|
||||
TOY_AS_FUNCTION(interpreter->literalCache.literals[i]).scope = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@
|
||||
#include "toy_literal_dictionary.h"
|
||||
#include "toy_scope.h"
|
||||
|
||||
typedef void (*Toy_PrintFn)(const char*);
|
||||
|
||||
//the interpreter acts depending on the bytecode instructions
|
||||
typedef struct Toy_Interpreter {
|
||||
//input
|
||||
|
||||
+8
-8
@@ -117,7 +117,7 @@ static Toy_Token makeErrorToken(Toy_Lexer* lexer, char* msg) {
|
||||
#ifndef TOY_EXPORT
|
||||
if (Toy_commandLine.verbose) {
|
||||
printf("err:");
|
||||
Toy_printToken(&token);
|
||||
Toy_private_printToken(&token);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -136,7 +136,7 @@ static Toy_Token makeToken(Toy_Lexer* lexer, Toy_TokenType type) {
|
||||
//BUG #10: this shows TOKEN_EOF twice due to the overarching structure of the program - can't be fixed
|
||||
if (Toy_commandLine.verbose) {
|
||||
printf("tok:");
|
||||
Toy_printToken(&token);
|
||||
Toy_private_printToken(&token);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -168,7 +168,7 @@ static Toy_Token makeIntegerOrFloat(Toy_Lexer* lexer) {
|
||||
} else {
|
||||
printf("flt:");
|
||||
}
|
||||
Toy_printToken(&token);
|
||||
Toy_private_printToken(&token);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -221,7 +221,7 @@ static Toy_Token makeString(Toy_Lexer* lexer, char terminator) {
|
||||
#ifndef TOY_EXPORT
|
||||
if (Toy_commandLine.verbose) {
|
||||
printf("str:");
|
||||
Toy_printToken(&token);
|
||||
Toy_private_printToken(&token);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -248,7 +248,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) {
|
||||
#ifndef TOY_EXPORT
|
||||
if (Toy_commandLine.verbose) {
|
||||
printf("kwd:");
|
||||
Toy_printToken(&token);
|
||||
Toy_private_printToken(&token);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -267,7 +267,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) {
|
||||
#ifndef TOY_EXPORT
|
||||
if (Toy_commandLine.verbose) {
|
||||
printf("idf:");
|
||||
Toy_printToken(&token);
|
||||
Toy_private_printToken(&token);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -281,7 +281,7 @@ void Toy_initLexer(Toy_Lexer* lexer, const char* source) {
|
||||
lexer->source = source;
|
||||
}
|
||||
|
||||
Toy_Token Toy_scanLexer(Toy_Lexer* lexer) {
|
||||
Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer) {
|
||||
eatWhitespace(lexer);
|
||||
|
||||
lexer->start = lexer->current;
|
||||
@@ -352,7 +352,7 @@ static void trim(char** s, int* l) { //all this to remove a newline?
|
||||
}
|
||||
|
||||
//for debugging
|
||||
void Toy_printToken(Toy_Token* token) {
|
||||
void Toy_private_printToken(Toy_Token* token) {
|
||||
if (token->type == TOY_TOKEN_ERROR) {
|
||||
printf(TOY_CC_ERROR "Error\t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme);
|
||||
return;
|
||||
|
||||
+2
-2
@@ -21,9 +21,9 @@ typedef struct {
|
||||
} Toy_Token;
|
||||
|
||||
TOY_API void Toy_initLexer(Toy_Lexer* lexer, const char* source);
|
||||
TOY_API Toy_Token Toy_scanLexer(Toy_Lexer* lexer);
|
||||
TOY_API Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer);
|
||||
|
||||
//for debugging
|
||||
TOY_API void Toy_printToken(Toy_Token* token);
|
||||
TOY_API void Toy_private_printToken(Toy_Token* token);
|
||||
|
||||
TOY_API void Toy_private_setComments(Toy_Lexer* lexer, bool enabled);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
//hash util functions
|
||||
static unsigned int hashString(const char* string, int length) {
|
||||
@@ -84,11 +85,11 @@ bool Toy_private_isTruthy(Toy_Literal x) {
|
||||
}
|
||||
|
||||
Toy_Literal Toy_private_toStringLiteral(Toy_RefString* ptr) {
|
||||
return ((Toy_Literal){{ .string.ptr = ptr },TOY_LITERAL_STRING, 0});
|
||||
return ((Toy_Literal){{ .string = { .ptr = ptr }},TOY_LITERAL_STRING, 0});
|
||||
}
|
||||
|
||||
Toy_Literal Toy_private_toIdentifierLiteral(Toy_RefString* ptr) {
|
||||
return ((Toy_Literal){{ .identifier.ptr = ptr, .identifier.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, 0});
|
||||
}
|
||||
|
||||
Toy_Literal* Toy_private_typePushSubtype(Toy_Literal* lit, Toy_Literal subtype) {
|
||||
@@ -155,6 +156,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
}
|
||||
|
||||
case TOY_LITERAL_IDENTIFIER: {
|
||||
//NOTE: could optimise this by copying the hash manually, but it's a very small increase in performance
|
||||
return TOY_TO_IDENTIFIER_LITERAL(Toy_copyRefString(TOY_AS_IDENTIFIER(original)));
|
||||
}
|
||||
|
||||
@@ -404,20 +406,20 @@ int Toy_hashLiteral(Toy_Literal lit) {
|
||||
case TOY_LITERAL_FUNCTION:
|
||||
case TOY_LITERAL_FUNCTION_NATIVE:
|
||||
case TOY_LITERAL_FUNCTION_HOOK:
|
||||
return 0; //can't hash these
|
||||
return -1; //can't hash these
|
||||
|
||||
case TOY_LITERAL_IDENTIFIER:
|
||||
return TOY_HASH_I(lit); //pre-computed
|
||||
|
||||
case TOY_LITERAL_TYPE:
|
||||
return TOY_AS_TYPE(lit).typeOf; //nothing else I can do
|
||||
return -1; //not much i can really do
|
||||
|
||||
case TOY_LITERAL_OPAQUE:
|
||||
case TOY_LITERAL_ANY:
|
||||
return -1;
|
||||
|
||||
default:
|
||||
//should never bee seen
|
||||
//should never be seen
|
||||
fprintf(stderr, TOY_CC_ERROR "[internal] Unrecognized literal type in hash: %d\n" TOY_CC_RESET, lit.type);
|
||||
return 0;
|
||||
}
|
||||
@@ -453,7 +455,7 @@ void Toy_printLiteral(Toy_Literal literal) {
|
||||
Toy_printLiteralCustom(literal, stdoutWrapper);
|
||||
}
|
||||
|
||||
void Toy_printLiteralCustom(Toy_Literal literal, void (printFn)(const char*)) {
|
||||
void Toy_printLiteralCustom(Toy_Literal literal, Toy_PrintFn printFn) {
|
||||
switch(literal.type) {
|
||||
case TOY_LITERAL_NULL:
|
||||
printFn("null");
|
||||
|
||||
@@ -10,8 +10,7 @@ struct Toy_Interpreter;
|
||||
struct Toy_LiteralArray;
|
||||
typedef int (*Toy_NativeFn)(struct Toy_Interpreter* interpreter, struct Toy_LiteralArray* arguments);
|
||||
typedef int (*Toy_HookFn)(struct Toy_Interpreter* interpreter, struct Toy_Literal identifier, struct Toy_Literal alias);
|
||||
|
||||
#include <string.h>
|
||||
typedef void (*Toy_PrintFn)(const char*);
|
||||
|
||||
typedef enum {
|
||||
TOY_LITERAL_NULL,
|
||||
@@ -118,12 +117,12 @@ typedef struct Toy_Literal {
|
||||
#define TOY_TO_STRING_LITERAL(value) Toy_private_toStringLiteral(value)
|
||||
#define TOY_TO_ARRAY_LITERAL(value) ((Toy_Literal){{ .array = value }, TOY_LITERAL_ARRAY, 0})
|
||||
#define TOY_TO_DICTIONARY_LITERAL(value) ((Toy_Literal){{ .dictionary = value }, TOY_LITERAL_DICTIONARY, 0})
|
||||
#define TOY_TO_FUNCTION_LITERAL(value, l) ((Toy_Literal){{ .function.inner.bytecode = value, .function.scope = NULL }, TOY_LITERAL_FUNCTION, l})
|
||||
#define TOY_TO_FUNCTION_NATIVE_LITERAL(value) ((Toy_Literal){{ .function.inner.native = value, .function.scope = NULL }, TOY_LITERAL_FUNCTION_NATIVE, 0})
|
||||
#define TOY_TO_FUNCTION_HOOK_LITERAL(value) ((Toy_Literal){{ .function.inner.hook = value, .function.scope = NULL }, TOY_LITERAL_FUNCTION_HOOK, 0})
|
||||
#define TOY_TO_FUNCTION_LITERAL(value, l) ((Toy_Literal){{ .function = { .inner = { .bytecode = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION, l})
|
||||
#define TOY_TO_FUNCTION_NATIVE_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .native = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION_NATIVE, 0})
|
||||
#define TOY_TO_FUNCTION_HOOK_LITERAL(value) ((Toy_Literal){{ .function = { .inner = { .hook = value }, .scope = NULL }}, TOY_LITERAL_FUNCTION_HOOK, 0})
|
||||
#define TOY_TO_IDENTIFIER_LITERAL(value) Toy_private_toIdentifierLiteral(value)
|
||||
#define TOY_TO_TYPE_LITERAL(value, c) ((Toy_Literal){{ .type.typeOf = value, .type.constant = c, .type.subtypes = NULL, .type.capacity = 0, .type.count = 0 }, TOY_LITERAL_TYPE, 0})
|
||||
#define TOY_TO_OPAQUE_LITERAL(value, t) ((Toy_Literal){{ .opaque.ptr = value, .opaque.tag = t }, TOY_LITERAL_OPAQUE, 0})
|
||||
#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_OPAQUE_LITERAL(value, t) ((Toy_Literal){{ .opaque = { .ptr = value, .tag = t }}, TOY_LITERAL_OPAQUE, 0})
|
||||
|
||||
//BUGFIX: For blank indexing
|
||||
#define TOY_IS_INDEX_BLANK(value) ((value).type == TOY_LITERAL_INDEX_BLANK)
|
||||
@@ -151,5 +150,6 @@ TOY_API Toy_Literal Toy_copyLiteral(Toy_Literal original);
|
||||
TOY_API bool Toy_literalsAreEqual(Toy_Literal lhs, Toy_Literal rhs);
|
||||
TOY_API int Toy_hashLiteral(Toy_Literal lit);
|
||||
|
||||
//not thread-safe
|
||||
TOY_API void Toy_printLiteral(Toy_Literal literal);
|
||||
TOY_API void Toy_printLiteralCustom(Toy_Literal literal, void (printFn)(const char*));
|
||||
TOY_API void Toy_printLiteralCustom(Toy_Literal literal, Toy_PrintFn);
|
||||
|
||||
@@ -18,3 +18,5 @@ TOY_API bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy
|
||||
TOY_API Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index);
|
||||
|
||||
int Toy_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal);
|
||||
|
||||
//TODO: add a function to get the capacity & count
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//util functions
|
||||
static void setEntryValues(Toy_private_entry* entry, Toy_Literal key, Toy_Literal value) {
|
||||
static void setEntryValues(Toy_private_dictionary_entry* entry, Toy_Literal key, Toy_Literal value) {
|
||||
//much simpler now
|
||||
Toy_freeLiteral(entry->key);
|
||||
entry->key = Toy_copyLiteral(key);
|
||||
@@ -16,7 +16,7 @@ static void setEntryValues(Toy_private_entry* entry, Toy_Literal key, Toy_Litera
|
||||
entry->value = Toy_copyLiteral(value);
|
||||
}
|
||||
|
||||
static Toy_private_entry* getEntryArray(Toy_private_entry* array, int capacity, Toy_Literal key, unsigned int hash, bool mustExist) {
|
||||
static Toy_private_dictionary_entry* getEntryArray(Toy_private_dictionary_entry* array, int capacity, Toy_Literal key, unsigned int hash, bool mustExist) {
|
||||
//find "key", starting at index
|
||||
unsigned int index = hash % capacity;
|
||||
unsigned int start = index;
|
||||
@@ -26,7 +26,7 @@ static Toy_private_entry* getEntryArray(Toy_private_entry* array, int capacity,
|
||||
|
||||
//literal probing and collision checking
|
||||
while (index != start) { //WARNING: this is the only function allowed to retrieve an entry from the array
|
||||
Toy_private_entry* entry = &array[index];
|
||||
Toy_private_dictionary_entry* entry = &array[index];
|
||||
|
||||
if (TOY_IS_NULL(entry->key)) { //if key is empty, it's either empty or tombstone
|
||||
if (TOY_IS_NULL(entry->value) && !mustExist) {
|
||||
@@ -46,9 +46,9 @@ static Toy_private_entry* getEntryArray(Toy_private_entry* array, int capacity,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void adjustEntryCapacity(Toy_private_entry** dictionaryHandle, int oldCapacity, int capacity) {
|
||||
static void adjustEntryCapacity(Toy_private_dictionary_entry** dictionaryHandle, int oldCapacity, int capacity) {
|
||||
//new entry space
|
||||
Toy_private_entry* newEntries = TOY_ALLOCATE(Toy_private_entry, capacity);
|
||||
Toy_private_dictionary_entry* newEntries = TOY_ALLOCATE(Toy_private_dictionary_entry, capacity);
|
||||
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
newEntries[i].key = TOY_TO_NULL_LITERAL;
|
||||
@@ -62,19 +62,19 @@ static void adjustEntryCapacity(Toy_private_entry** dictionaryHandle, int oldCap
|
||||
}
|
||||
|
||||
//place the key and value in the new array (reusing string memory)
|
||||
Toy_private_entry* entry = getEntryArray(newEntries, capacity, TOY_TO_NULL_LITERAL, Toy_hashLiteral((*dictionaryHandle)[i].key), false);
|
||||
Toy_private_dictionary_entry* entry = getEntryArray(newEntries, capacity, TOY_TO_NULL_LITERAL, Toy_hashLiteral((*dictionaryHandle)[i].key), false);
|
||||
|
||||
entry->key = (*dictionaryHandle)[i].key;
|
||||
entry->value = (*dictionaryHandle)[i].value;
|
||||
}
|
||||
|
||||
//clear the old array
|
||||
TOY_FREE_ARRAY(Toy_private_entry, *dictionaryHandle, oldCapacity);
|
||||
TOY_FREE_ARRAY(Toy_private_dictionary_entry, *dictionaryHandle, oldCapacity);
|
||||
|
||||
*dictionaryHandle = newEntries;
|
||||
}
|
||||
|
||||
static bool setEntryArray(Toy_private_entry** dictionaryHandle, int* capacityPtr, int contains, Toy_Literal key, Toy_Literal value, int hash) {
|
||||
static bool setEntryArray(Toy_private_dictionary_entry** dictionaryHandle, int* capacityPtr, int contains, Toy_Literal key, Toy_Literal value, int hash) {
|
||||
//expand array if needed
|
||||
if (contains + 1 > *capacityPtr * TOY_DICTIONARY_MAX_LOAD) {
|
||||
int oldCapacity = *capacityPtr;
|
||||
@@ -82,7 +82,7 @@ static bool setEntryArray(Toy_private_entry** dictionaryHandle, int* capacityPtr
|
||||
adjustEntryCapacity(dictionaryHandle, oldCapacity, *capacityPtr); //custom rather than automatic reallocation
|
||||
}
|
||||
|
||||
Toy_private_entry* entry = getEntryArray(*dictionaryHandle, *capacityPtr, key, hash, false);
|
||||
Toy_private_dictionary_entry* entry = getEntryArray(*dictionaryHandle, *capacityPtr, key, hash, false);
|
||||
|
||||
//true = contains increase
|
||||
if (TOY_IS_NULL(entry->key)) {
|
||||
@@ -97,14 +97,14 @@ static bool setEntryArray(Toy_private_entry** dictionaryHandle, int* capacityPtr
|
||||
return false;
|
||||
}
|
||||
|
||||
static void freeEntry(Toy_private_entry* entry) {
|
||||
static void freeEntry(Toy_private_dictionary_entry* entry) {
|
||||
Toy_freeLiteral(entry->key);
|
||||
Toy_freeLiteral(entry->value);
|
||||
entry->key = TOY_TO_NULL_LITERAL;
|
||||
entry->value = TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
static void freeEntryArray(Toy_private_entry* array, int capacity) {
|
||||
static void freeEntryArray(Toy_private_dictionary_entry* array, int capacity) {
|
||||
if (array == NULL) {
|
||||
return;
|
||||
}
|
||||
@@ -115,7 +115,7 @@ static void freeEntryArray(Toy_private_entry* array, int capacity) {
|
||||
}
|
||||
}
|
||||
|
||||
TOY_FREE_ARRAY(Toy_private_entry, array, capacity);
|
||||
TOY_FREE_ARRAY(Toy_private_dictionary_entry, array, capacity);
|
||||
}
|
||||
|
||||
//exposed functions
|
||||
@@ -141,7 +141,7 @@ void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key
|
||||
}
|
||||
|
||||
//BUGFIX: Can't hash a function
|
||||
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) {
|
||||
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) {
|
||||
fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (set)\n" TOY_CC_RESET);
|
||||
return;
|
||||
}
|
||||
@@ -166,7 +166,7 @@ Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Lite
|
||||
}
|
||||
|
||||
//BUGFIX: Can't hash a function
|
||||
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) {
|
||||
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) {
|
||||
fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (get)\n" TOY_CC_RESET);
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
@@ -176,7 +176,7 @@ Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Lite
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
Toy_private_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), true);
|
||||
Toy_private_dictionary_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), true);
|
||||
|
||||
if (entry != NULL) {
|
||||
return Toy_copyLiteral(entry->value);
|
||||
@@ -193,7 +193,7 @@ void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal
|
||||
}
|
||||
|
||||
//BUGFIX: Can't hash a function
|
||||
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) {
|
||||
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) {
|
||||
fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (remove)\n" TOY_CC_RESET);
|
||||
return;
|
||||
}
|
||||
@@ -203,7 +203,7 @@ void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal
|
||||
return;
|
||||
}
|
||||
|
||||
Toy_private_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), true);
|
||||
Toy_private_dictionary_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), true);
|
||||
|
||||
if (entry != NULL) {
|
||||
freeEntry(entry);
|
||||
@@ -214,6 +214,6 @@ void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal
|
||||
|
||||
bool Toy_existsLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key) {
|
||||
//null & not tombstoned
|
||||
Toy_private_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), false);
|
||||
Toy_private_dictionary_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), false);
|
||||
return !(TOY_IS_NULL(entry->key) && TOY_IS_NULL(entry->value));
|
||||
}
|
||||
|
||||
@@ -7,13 +7,13 @@
|
||||
//TODO: benchmark this
|
||||
#define TOY_DICTIONARY_MAX_LOAD 0.75
|
||||
|
||||
typedef struct Toy_private_entry {
|
||||
typedef struct Toy_private_dictionary_entry {
|
||||
Toy_Literal key;
|
||||
Toy_Literal value;
|
||||
} Toy_private_entry;
|
||||
} Toy_private_dictionary_entry;
|
||||
|
||||
typedef struct Toy_LiteralDictionary {
|
||||
Toy_private_entry* entries;
|
||||
Toy_private_dictionary_entry* entries;
|
||||
int capacity;
|
||||
int count;
|
||||
int contains; //count + tombstones, for internal use
|
||||
|
||||
+2
-3
@@ -15,15 +15,14 @@ void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t n
|
||||
|
||||
if (newSize == 0) {
|
||||
free(pointer);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* mem = realloc(pointer, newSize);
|
||||
|
||||
if (mem == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "[internal] Memory allocation error (requested %d, replacing %d)\n" TOY_CC_RESET, (int)newSize, (int)oldSize);
|
||||
exit(-1);
|
||||
fprintf(stderr, TOY_CC_ERROR "[internal] Memory allocation error (requested %zu, replacing %zu)\n" TOY_CC_RESET, newSize, oldSize);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mem;
|
||||
|
||||
+6
-3
@@ -2,13 +2,16 @@
|
||||
|
||||
#include "toy_common.h"
|
||||
|
||||
#define TOY_ALLOCATE(type, count) ((type*)Toy_reallocate(NULL, 0, sizeof(type) * (count)))
|
||||
#define TOY_FREE(type, pointer) Toy_reallocate(pointer, sizeof(type), 0)
|
||||
#define TOY_GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2)
|
||||
#define TOY_GROW_CAPACITY_FAST(capacity) ((capacity) < 32 ? 32 : (capacity) * 2)
|
||||
|
||||
#define TOY_ALLOCATE(type, count) ((type*)Toy_reallocate(NULL, 0, sizeof(type) * (count)))
|
||||
|
||||
#define TOY_FREE(type, pointer) Toy_reallocate(pointer, sizeof(type), 0)
|
||||
#define TOY_FREE_ARRAY(type, pointer, oldCount) Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), 0)
|
||||
|
||||
#define TOY_GROW_ARRAY(type, pointer, oldCount, count) (type*)Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), sizeof(type) * (count))
|
||||
#define TOY_SHRINK_ARRAY(type, pointer, oldCount, count) (type*)Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), sizeof(type) * (count))
|
||||
#define TOY_FREE_ARRAY(type, pointer, oldCount) Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), 0)
|
||||
|
||||
//implementation details
|
||||
TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize);
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ static void error(Toy_Parser* parser, Toy_Token token, const char* message) {
|
||||
|
||||
static void advance(Toy_Parser* parser) {
|
||||
parser->previous = parser->current;
|
||||
parser->current = Toy_scanLexer(parser->lexer);
|
||||
parser->current = Toy_private_scanLexer(parser->lexer);
|
||||
|
||||
if (parser->current.type == TOY_TOKEN_ERROR) {
|
||||
error(parser, parser->current, "Toy_Lexer error");
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "toy_refstring.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
//memory allocation
|
||||
extern void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize);
|
||||
static Toy_RefStringAllocatorFn allocate = Toy_private_defaultMemoryAllocator;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "toy_common.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
//memory allocation hook
|
||||
typedef void* (*Toy_RefStringAllocatorFn)(void* pointer, size_t oldSize, size_t newSize);
|
||||
void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn);
|
||||
TOY_API void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn);
|
||||
|
||||
//the RefString structure
|
||||
typedef struct Toy_RefString {
|
||||
@@ -27,3 +26,6 @@ TOY_API Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString);
|
||||
TOY_API const char* Toy_toCString(Toy_RefString* refString);
|
||||
TOY_API bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs);
|
||||
TOY_API bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring);
|
||||
|
||||
//TODO: merge refstring memory
|
||||
|
||||
|
||||
+7
-3
@@ -126,7 +126,7 @@ static bool checkType(Toy_Literal typeLiteral, Toy_Literal original, Toy_Literal
|
||||
}
|
||||
|
||||
//find the internal child of original that matches this child of value
|
||||
Toy_private_entry* ptr = NULL;
|
||||
Toy_private_dictionary_entry* ptr = NULL;
|
||||
|
||||
for (int j = 0; j < TOY_AS_DICTIONARY(original)->capacity; j++) {
|
||||
if (Toy_literalsAreEqual(TOY_AS_DICTIONARY(original)->entries[j].key, TOY_AS_DICTIONARY(value)->entries[i].key)) {
|
||||
@@ -159,6 +159,10 @@ static bool checkType(Toy_Literal typeLiteral, Toy_Literal original, Toy_Literal
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_OPAQUE && !TOY_IS_OPAQUE(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -185,7 +189,7 @@ Toy_Scope* Toy_popScope(Toy_Scope* scope) {
|
||||
|
||||
Toy_Scope* ret = scope->ancestor;
|
||||
|
||||
//BUGFIX: when freeing a scope, free the function's scopes manually
|
||||
//BUGFIX: when freeing a scope, free the functions' scopes manually - I *think* this is related to the closure hack-in
|
||||
for (int i = 0; i < scope->variables.capacity; i++) {
|
||||
//handle keys, just in case
|
||||
if (TOY_IS_FUNCTION(scope->variables.entries[i].key)) {
|
||||
@@ -286,7 +290,7 @@ bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value,
|
||||
}
|
||||
|
||||
//actually assign
|
||||
Toy_setLiteralDictionary(&scope->variables, key, value);
|
||||
Toy_setLiteralDictionary(&scope->variables, key, value); //key & value are copied here
|
||||
|
||||
Toy_freeLiteral(typeLiteral);
|
||||
Toy_freeLiteral(original);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "toy_literal.h"
|
||||
#include "toy_literal_array.h"
|
||||
#include "toy_literal_dictionary.h"
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,12 +1,21 @@
|
||||
//memoize the fib function
|
||||
var memo: [int : int] = [:];
|
||||
|
||||
fn fib(n : int) {
|
||||
if (n < 2) {
|
||||
return n;
|
||||
}
|
||||
|
||||
return fib(n-1) + fib(n-2);
|
||||
var result = memo[n];
|
||||
if (result == null) {
|
||||
result = fib(n-1) + fib(n-2);
|
||||
memo[n] = result;
|
||||
}
|
||||
|
||||
for (var i = 0; i < 20; i++) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (var i = 0; i < 40; i++) {
|
||||
var res = fib(i);
|
||||
print string i + ": " + string res;
|
||||
}
|
||||
+26
-26
@@ -20,14 +20,14 @@ static void noPrintFn(const char* output) {
|
||||
}
|
||||
|
||||
void error(char* msg) {
|
||||
printf("%s", msg);
|
||||
printf("%s\n", msg);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
{
|
||||
size_t size = 0;
|
||||
const char* source = Toy_readFile("scripts/call-from-host.toy", &size);
|
||||
const char* source = (const char*)Toy_readFile("scripts/call-from-host.toy", &size);
|
||||
const unsigned char* tb = Toy_compileString(source, &size);
|
||||
free((void*)source);
|
||||
|
||||
@@ -41,7 +41,7 @@ int main() {
|
||||
|
||||
//test answer
|
||||
{
|
||||
interpreter.printOutput("Testing answer\n");
|
||||
interpreter.printOutput("Testing answer");
|
||||
|
||||
Toy_LiteralArray arguments;
|
||||
Toy_initLiteralArray(&arguments);
|
||||
@@ -52,15 +52,15 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments has the wrong number of members\n");
|
||||
error("Arguments has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1) {
|
||||
error("Returns has the wrong number of members\n");
|
||||
error("Returns has the wrong number of members");
|
||||
}
|
||||
|
||||
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 42) {
|
||||
error("Returned value is incorrect\n");
|
||||
error("Returned value is incorrect");
|
||||
}
|
||||
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
@@ -69,7 +69,7 @@ int main() {
|
||||
|
||||
//test identity
|
||||
{
|
||||
interpreter.printOutput("Testing identity\n");
|
||||
interpreter.printOutput("Testing identity");
|
||||
|
||||
Toy_LiteralArray arguments;
|
||||
Toy_initLiteralArray(&arguments);
|
||||
@@ -85,17 +85,17 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments has the wrong number of members\n");
|
||||
error("Arguments has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1) {
|
||||
error("Returns has the wrong number of members\n");
|
||||
error("Returns has the wrong number of members");
|
||||
}
|
||||
|
||||
float epsilon = 0.1; //because floats are evil
|
||||
|
||||
if (!TOY_IS_FLOAT(returns.literals[0]) || fabs(TOY_AS_FLOAT(returns.literals[0]) - pi) > epsilon) {
|
||||
error("Returned value is incorrect\n");
|
||||
error("Returned value is incorrect");
|
||||
}
|
||||
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
@@ -104,7 +104,7 @@ int main() {
|
||||
|
||||
//test makeCounter (closures)
|
||||
{
|
||||
interpreter.printOutput("Testing makeCounter (closures)\n");
|
||||
interpreter.printOutput("Testing makeCounter (closures)");
|
||||
|
||||
Toy_LiteralArray arguments;
|
||||
Toy_initLiteralArray(&arguments);
|
||||
@@ -115,11 +115,11 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments has the wrong number of members\n");
|
||||
error("Arguments has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1) {
|
||||
error("Returns has the wrong number of members\n");
|
||||
error("Returns has the wrong number of members");
|
||||
}
|
||||
|
||||
//grab the resulting literal
|
||||
@@ -139,15 +139,15 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments (1) has the wrong number of members\n");
|
||||
error("Arguments (1) has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1) {
|
||||
error("Returns (1) has the wrong number of members\n");
|
||||
error("Returns (1) has the wrong number of members");
|
||||
}
|
||||
|
||||
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 1) {
|
||||
error("Returned value (1) is incorrect\n");
|
||||
error("Returned value (1) is incorrect");
|
||||
}
|
||||
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
@@ -164,15 +164,15 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments (2) has the wrong number of members\n");
|
||||
error("Arguments (2) has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1) {
|
||||
error("Returns (2) has the wrong number of members\n");
|
||||
error("Returns (2) has the wrong number of members");
|
||||
}
|
||||
|
||||
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 2) {
|
||||
error("Returned value (2) is incorrect\n");
|
||||
error("Returned value (2) is incorrect");
|
||||
}
|
||||
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
@@ -189,15 +189,15 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments (3) has the wrong number of members\n");
|
||||
error("Arguments (3) has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1) {
|
||||
error("Returns (3) has the wrong number of members\n");
|
||||
error("Returns (3) has the wrong number of members");
|
||||
}
|
||||
|
||||
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 3) {
|
||||
error("Returned value (3) is incorrect\n");
|
||||
error("Returned value (3) is incorrect");
|
||||
}
|
||||
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
@@ -209,7 +209,7 @@ int main() {
|
||||
|
||||
//test assertion failure
|
||||
{
|
||||
interpreter.printOutput("Testing assertion failure\n");
|
||||
interpreter.printOutput("Testing assertion failure");
|
||||
|
||||
Toy_setInterpreterAssert(&interpreter, noPrintFn);
|
||||
|
||||
@@ -222,15 +222,15 @@ int main() {
|
||||
|
||||
//check the results
|
||||
if (arguments.count != 0) {
|
||||
error("Arguments has the wrong number of members\n");
|
||||
error("Arguments has the wrong number of members");
|
||||
}
|
||||
|
||||
if (returns.count != 1 || !TOY_IS_NULL(returns.literals[0])) {
|
||||
error("Returns has the wrong number of members\n");
|
||||
error("Returns has the wrong number of members");
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
error("Assertion gives the wrong return value\n");
|
||||
error("Assertion gives the wrong return value");
|
||||
}
|
||||
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
|
||||
@@ -39,7 +39,7 @@ int main() {
|
||||
Toy_writeCompiler(&compiler, node);
|
||||
|
||||
//collate
|
||||
int size = 0;
|
||||
size_t size = 0;
|
||||
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
||||
|
||||
//cleanup
|
||||
@@ -52,7 +52,7 @@ int main() {
|
||||
{
|
||||
//source
|
||||
size_t sourceLength = 0;
|
||||
const char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
|
||||
const char* source = (const char*)Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
|
||||
|
||||
//test basic compilation & collation
|
||||
Toy_Lexer lexer;
|
||||
@@ -78,7 +78,7 @@ int main() {
|
||||
}
|
||||
|
||||
//collate
|
||||
int size = 0;
|
||||
size_t size = 0;
|
||||
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
||||
|
||||
//cleanup
|
||||
|
||||
@@ -53,7 +53,7 @@ void runSourceCustom(const char* source) {
|
||||
|
||||
void runSourceFileCustom(const char* fname) {
|
||||
size_t size = 0; //not used
|
||||
const char* source = Toy_readFile(fname, &size);
|
||||
const char* source = (const char*)Toy_readFile(fname, &size);
|
||||
runSourceCustom(source);
|
||||
free((void*)source);
|
||||
}
|
||||
@@ -87,7 +87,7 @@ int main() {
|
||||
Toy_writeCompiler(&compiler, node);
|
||||
|
||||
//collate
|
||||
int size = 0;
|
||||
size_t size = 0;
|
||||
const unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
||||
|
||||
//NOTE: suppress print output for testing
|
||||
|
||||
+4
-4
@@ -15,10 +15,10 @@ int main() {
|
||||
Toy_initLexer(&lexer, source);
|
||||
|
||||
//get each token
|
||||
Toy_Token print = Toy_scanLexer(&lexer);
|
||||
Toy_Token null = Toy_scanLexer(&lexer);
|
||||
Toy_Token semi = Toy_scanLexer(&lexer);
|
||||
Toy_Token eof = Toy_scanLexer(&lexer);
|
||||
Toy_Token print = Toy_private_scanLexer(&lexer);
|
||||
Toy_Token null = Toy_private_scanLexer(&lexer);
|
||||
Toy_Token semi = Toy_private_scanLexer(&lexer);
|
||||
Toy_Token eof = Toy_private_scanLexer(&lexer);
|
||||
|
||||
//test each token is correct
|
||||
if (strncmp(print.lexeme, "print", print.length)) {
|
||||
|
||||
@@ -105,7 +105,7 @@ int main() {
|
||||
|
||||
//compile the source
|
||||
size_t size = 0;
|
||||
const char* source = Toy_readFile(fname, &size);
|
||||
const char* source = (const char*)Toy_readFile(fname, &size);
|
||||
if (!source) {
|
||||
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
|
||||
failedAsserts++;
|
||||
@@ -140,7 +140,7 @@ int main() {
|
||||
|
||||
//compile the source
|
||||
size_t size = 0;
|
||||
const char* source = Toy_readFile(fname, &size);
|
||||
const char* source = (const char*)Toy_readFile(fname, &size);
|
||||
if (!source) {
|
||||
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
|
||||
failedAsserts++;
|
||||
|
||||
@@ -50,7 +50,7 @@ const unsigned char* compileStringCustom(const char* source, size_t* size) {
|
||||
}
|
||||
|
||||
//get the bytecode dump
|
||||
const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
|
||||
const unsigned char* tb = Toy_collateCompiler(&compiler, size);
|
||||
|
||||
//cleanup
|
||||
Toy_freeCompiler(&compiler);
|
||||
@@ -84,7 +84,7 @@ void runSourceCustom(const char* source) {
|
||||
|
||||
void runSourceFileCustom(const char* fname) {
|
||||
size_t size = 0; //not used
|
||||
const char* source = Toy_readFile(fname, &size);
|
||||
const char* source = (const char*)Toy_readFile(fname, &size);
|
||||
runSourceCustom(source);
|
||||
free((void*)source);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ static int consume(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
int main() {
|
||||
{
|
||||
size_t size = 0;
|
||||
const char* source = Toy_readFile("scripts/opaque-data-type.toy", &size);
|
||||
const char* source = (const char*)Toy_readFile("scripts/opaque-data-type.toy", &size);
|
||||
const unsigned char* tb = Toy_compileString(source, &size);
|
||||
free((void*)source);
|
||||
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ int main() {
|
||||
{
|
||||
//get the source file
|
||||
size_t size = 0;
|
||||
const char* source = Toy_readFile("scripts/parser_sample_code.toy", &size);
|
||||
const char* source = (const char*)Toy_readFile("scripts/parser_sample_code.toy", &size);
|
||||
|
||||
//test parsing a chunk of junk (valgrind will find leaks)
|
||||
Toy_Lexer lexer;
|
||||
|
||||
@@ -39,11 +39,14 @@ static void* trackerAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
||||
return mem;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (argc <= 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//not used, except for print
|
||||
Toy_initCommandLine(argc, argv);
|
||||
|
||||
//setup for runner
|
||||
Toy_initDriveDictionary();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user