Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 17f0e4476b | |||
| 1095e1a885 | |||
| 2edfbbe3ef | |||
| 4b83f1f0d6 | |||
| e2fa1cf2e8 | |||
| a04d2c4816 | |||
| f2f8aed23a | |||
| 68ed52b347 | |||
| 88dac53ae0 | |||
| f84cdff883 | |||
| f869c9425a | |||
| 76ddd5703e | |||
| 669808730e | |||
| e6d9809da5 | |||
| 502032e514 | |||
| 6e9d42f892 | |||
| 70ca27486e | |||
| 12fa434e0f | |||
| efc1e764d2 | |||
| c5c0122243 | |||
| 348b7b8c24 | |||
| e243ad949a | |||
| 9b673f23ad | |||
| 624a0c80ba |
@@ -123,6 +123,7 @@
|
||||
<ClCompile Include="source\toy_builtin.c" />
|
||||
<ClCompile Include="source\toy_common.c" />
|
||||
<ClCompile Include="source\toy_compiler.c" />
|
||||
<ClCompile Include="source\toy_drive_system.c" />
|
||||
<ClCompile Include="source\toy_interpreter.c" />
|
||||
<ClCompile Include="source\toy_keyword_types.c" />
|
||||
<ClCompile Include="source\toy_lexer.c" />
|
||||
@@ -135,11 +136,13 @@
|
||||
<ClCompile Include="source\toy_scope.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="source\toy.h" />
|
||||
<ClInclude Include="source\toy_ast_node.h" />
|
||||
<ClInclude Include="source\toy_builtin.h" />
|
||||
<ClInclude Include="source\toy_common.h" />
|
||||
<ClInclude Include="source\toy_compiler.h" />
|
||||
<ClInclude Include="source\toy_console_colors.h" />
|
||||
<ClInclude Include="source\toy_drive_system.h" />
|
||||
<ClInclude Include="source\toy_interpreter.h" />
|
||||
<ClInclude Include="source\toy_keyword_types.h" />
|
||||
<ClInclude Include="source\toy_lexer.h" />
|
||||
|
||||
@@ -28,10 +28,10 @@ library: $(TOY_OUTDIR)
|
||||
static: $(TOY_OUTDIR)
|
||||
$(MAKE) -j8 -C source static
|
||||
|
||||
library-release: $(TOY_OUTDIR)
|
||||
library-release: clean $(TOY_OUTDIR)
|
||||
$(MAKE) -j8 -C source library-release
|
||||
|
||||
static-release: $(TOY_OUTDIR)
|
||||
static-release: clean $(TOY_OUTDIR)
|
||||
$(MAKE) -j8 -C source static-release
|
||||
|
||||
#utils
|
||||
|
||||
+10
-145
@@ -1,13 +1,12 @@
|
||||
#include "lib_runner.h"
|
||||
|
||||
#include "toy_memory.h"
|
||||
#include "toy_drive_system.h"
|
||||
#include "toy_interpreter.h"
|
||||
|
||||
#include "repl_tools.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct Toy_Runner {
|
||||
Toy_Interpreter interpreter;
|
||||
@@ -38,7 +37,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral);
|
||||
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||
|
||||
if (TOY_IS_NULL(filePathLiteral)) {
|
||||
Toy_freeLiteral(filePathLiteral);
|
||||
@@ -113,70 +112,19 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(drivePathLiteral));
|
||||
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||
|
||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
||||
size_t driveLength = 0;
|
||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
||||
interpreter->errorOutput("Incorrect drive path format given to loadScriptBytecode\n");
|
||||
Toy_deleteRefString(drivePath);
|
||||
Toy_freeLiteral(drivePathLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
driveLength++;
|
||||
}
|
||||
|
||||
Toy_RefString* drive = Toy_createRefStringLength(Toy_toCString(drivePath), driveLength);
|
||||
Toy_RefString* path = Toy_createRefStringLength( &Toy_toCString(drivePath)[driveLength + 1], Toy_lengthRefString(drivePath) - driveLength );
|
||||
|
||||
//get the real drive file path
|
||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(drive); //NOTE: driveLiteral takes ownership of the refString
|
||||
Toy_Literal realDriveLiteral = Toy_getLiteralDictionary(Toy_getDriveDictionary(), driveLiteral);
|
||||
|
||||
if (!TOY_IS_STRING(realDriveLiteral)) {
|
||||
interpreter->errorOutput("Incorrect literal type found for drive: ");
|
||||
Toy_printLiteralCustom(realDriveLiteral, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\n");
|
||||
Toy_freeLiteral(realDriveLiteral);
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_deleteRefString(path);
|
||||
Toy_deleteRefString(drivePath);
|
||||
if (TOY_IS_NULL(filePathLiteral)) {
|
||||
Toy_freeLiteral(filePathLiteral);
|
||||
Toy_freeLiteral(drivePathLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the final real file path (concat) TODO: move this concat to refstring library
|
||||
Toy_RefString* realDrive = Toy_copyRefString(TOY_AS_STRING(realDriveLiteral));
|
||||
size_t realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path);
|
||||
|
||||
char* filePath = TOY_ALLOCATE(char, realLength + 1); //+1 for null
|
||||
snprintf(filePath, realLength, "%s%s", Toy_toCString(realDrive), Toy_toCString(path));
|
||||
|
||||
//clean up the drivepath stuff
|
||||
Toy_deleteRefString(realDrive);
|
||||
Toy_freeLiteral(realDriveLiteral);
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_deleteRefString(path);
|
||||
Toy_deleteRefString(drivePath);
|
||||
Toy_freeLiteral(drivePathLiteral);
|
||||
|
||||
//check for file extensions
|
||||
if (!(filePath[realLength - 4] == '.' && filePath[realLength - 3] == 't' && filePath[realLength - 2] == 'b')) {
|
||||
interpreter->errorOutput("Bad binary file extension (expected .tb)\n");
|
||||
TOY_FREE_ARRAY(char, filePath, realLength);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//check for break-out attempts
|
||||
for (size_t i = 0; i < realLength - 1; i++) {
|
||||
if (filePath[i] == '.' && filePath[i + 1] == '.') {
|
||||
interpreter->errorOutput("Parent directory access not allowed\n");
|
||||
TOY_FREE_ARRAY(char, filePath, realLength);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
//use raw types - easier
|
||||
const char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral));
|
||||
size_t filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral));
|
||||
|
||||
//load the bytecode
|
||||
size_t fileSize = 0;
|
||||
@@ -203,7 +151,8 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr
|
||||
Toy_Literal runnerLiteral = TOY_TO_OPAQUE_LITERAL(runner, TOY_OPAQUE_TAG_RUNNER);
|
||||
Toy_pushLiteralArray(&interpreter->stack, runnerLiteral);
|
||||
|
||||
TOY_FREE_ARRAY(char, filePath, realLength);
|
||||
//free the drive path
|
||||
Toy_freeLiteral(filePathLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -602,87 +551,3 @@ int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Lit
|
||||
return 0;
|
||||
}
|
||||
|
||||
//file system API
|
||||
static Toy_LiteralDictionary Toy_driveDictionary;
|
||||
|
||||
void Toy_initDriveDictionary() {
|
||||
Toy_initLiteralDictionary(&Toy_driveDictionary);
|
||||
}
|
||||
|
||||
void Toy_freeDriveDictionary() {
|
||||
Toy_freeLiteralDictionary(&Toy_driveDictionary);
|
||||
}
|
||||
|
||||
Toy_LiteralDictionary* Toy_getDriveDictionary() {
|
||||
return &Toy_driveDictionary;
|
||||
}
|
||||
|
||||
Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral) {
|
||||
//check argument types
|
||||
if (!TOY_IS_STRING(*drivePathLiteral)) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to Toy_getFilePathLiteral\n");
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(*drivePathLiteral));
|
||||
|
||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
||||
size_t driveLength = 0;
|
||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
||||
interpreter->errorOutput("Incorrect drive path format given to Toy_getFilePathLiteral\n");
|
||||
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
driveLength++;
|
||||
}
|
||||
|
||||
Toy_RefString* drive = Toy_createRefStringLength(Toy_toCString(drivePath), driveLength);
|
||||
Toy_RefString* path = Toy_createRefStringLength( &Toy_toCString(drivePath)[driveLength + 1], Toy_lengthRefString(drivePath) - driveLength );
|
||||
|
||||
//get the real drive file path
|
||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(drive); //NOTE: driveLiteral takes ownership of the refString
|
||||
Toy_Literal realDriveLiteral = Toy_getLiteralDictionary(Toy_getDriveDictionary(), driveLiteral);
|
||||
|
||||
if (!TOY_IS_STRING(realDriveLiteral)) {
|
||||
interpreter->errorOutput("Incorrect literal type found for drive: ");
|
||||
Toy_printLiteralCustom(realDriveLiteral, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\n");
|
||||
Toy_freeLiteral(realDriveLiteral);
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_deleteRefString(path);
|
||||
Toy_deleteRefString(drivePath);
|
||||
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
//get the final real file path (concat) TODO: move this concat to refstring library
|
||||
Toy_RefString* realDrive = Toy_copyRefString(TOY_AS_STRING(realDriveLiteral));
|
||||
size_t realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path);
|
||||
|
||||
char* filePath = TOY_ALLOCATE(char, realLength + 1); //+1 for null
|
||||
snprintf(filePath, realLength, "%s%s", Toy_toCString(realDrive), Toy_toCString(path));
|
||||
|
||||
//clean up the drivepath stuff
|
||||
Toy_deleteRefString(realDrive);
|
||||
Toy_freeLiteral(realDriveLiteral);
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_deleteRefString(path);
|
||||
Toy_deleteRefString(drivePath);
|
||||
|
||||
//check for break-out attempts
|
||||
for (size_t i = 0; i < realLength - 1; i++) {
|
||||
if (filePath[i] == '.' && filePath[i + 1] == '.') {
|
||||
interpreter->errorOutput("Parent directory access not allowed\n");
|
||||
TOY_FREE_ARRAY(char, filePath, realLength + 1);
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
}
|
||||
|
||||
Toy_Literal result = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(filePath, realLength));
|
||||
|
||||
TOY_FREE_ARRAY(char, filePath, realLength + 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1,35 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "toy_common.h"
|
||||
#include "toy_interpreter.h"
|
||||
|
||||
int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias);
|
||||
|
||||
#define TOY_OPAQUE_TAG_RUNNER 100
|
||||
|
||||
//platform/compiler-specific instructions - because MSVC + Box Engine are dumber than a bag of rocks
|
||||
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
||||
|
||||
#define LIB_RUNNER_API extern
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
|
||||
#ifndef LIB_RUNNER_EXPORT
|
||||
#define LIB_RUNNER_API __declspec(dllimport)
|
||||
#else
|
||||
#define LIB_RUNNER_API __declspec(dllexport)
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define LIB_RUNNER_API extern
|
||||
|
||||
#endif
|
||||
|
||||
//file system API - these need to be set by the host
|
||||
LIB_RUNNER_API void Toy_initDriveDictionary();
|
||||
LIB_RUNNER_API void Toy_freeDriveDictionary();
|
||||
LIB_RUNNER_API Toy_LiteralDictionary* Toy_getDriveDictionary();
|
||||
|
||||
//file system API - for use with other libs
|
||||
LIB_RUNNER_API Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral);
|
||||
|
||||
+317
-47
@@ -7,6 +7,61 @@
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
//no arguments
|
||||
if (arguments->count != 0) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to clock\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the time from C (what a pain)
|
||||
time_t rawtime = time(NULL);
|
||||
struct tm* timeinfo = localtime( &rawtime );
|
||||
char* timestr = asctime(timeinfo);
|
||||
|
||||
//push to the stack
|
||||
size_t len = strlen(timestr) - 1; //-1 for the newline
|
||||
Toy_Literal timeLiteral = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(timestr, len));
|
||||
|
||||
//push to the stack
|
||||
Toy_pushLiteralArray(&interpreter->stack, timeLiteral);
|
||||
|
||||
//cleanup
|
||||
Toy_freeLiteral(timeLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeHash(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to hash\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_Literal result = TOY_TO_INTEGER_LITERAL(Toy_hashLiteral(selfLiteral));
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, result);
|
||||
|
||||
Toy_freeLiteral(result);
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeAbs(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to abs\n");
|
||||
@@ -50,27 +105,262 @@ static int nativeAbs(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
//no arguments
|
||||
if (arguments->count != 0) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to clock\n");
|
||||
static int nativeCeil(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to ceil\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the time from C (what a pain)
|
||||
time_t rawtime = time(NULL);
|
||||
struct tm* timeinfo = localtime( &rawtime );
|
||||
char* timestr = asctime(timeinfo);
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//push to the stack
|
||||
size_t len = strlen(timestr) - 1; //-1 for the newline
|
||||
Toy_Literal timeLiteral = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(timestr, len));
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
//push to the stack
|
||||
Toy_pushLiteralArray(&interpreter->stack, timeLiteral);
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
Toy_freeLiteral(timeLiteral);
|
||||
if (!(TOY_IS_INTEGER(selfLiteral) || TOY_IS_FLOAT(selfLiteral))) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to ceil\n");
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_Literal result;
|
||||
|
||||
if (TOY_IS_INTEGER(selfLiteral)) {
|
||||
//NO-OP
|
||||
result = Toy_copyLiteral(selfLiteral);
|
||||
}
|
||||
if (TOY_IS_FLOAT(selfLiteral)) {
|
||||
result = TOY_TO_INTEGER_LITERAL( (int)TOY_AS_FLOAT(selfLiteral) - TOY_AS_FLOAT(selfLiteral) == 0 ? (int)TOY_AS_FLOAT(selfLiteral) : (int)TOY_AS_FLOAT(selfLiteral) + 1 );
|
||||
}
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, result);
|
||||
|
||||
Toy_freeLiteral(result);
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeFloor(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to floor\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(TOY_IS_INTEGER(selfLiteral) || TOY_IS_FLOAT(selfLiteral))) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to floor\n");
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_Literal result;
|
||||
|
||||
if (TOY_IS_INTEGER(selfLiteral)) {
|
||||
//NO-OP
|
||||
result = Toy_copyLiteral(selfLiteral);
|
||||
}
|
||||
if (TOY_IS_FLOAT(selfLiteral)) {
|
||||
result = TOY_TO_INTEGER_LITERAL( (int)TOY_AS_FLOAT(selfLiteral) );
|
||||
}
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, result);
|
||||
|
||||
Toy_freeLiteral(result);
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeMax(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
//return value
|
||||
Toy_Literal resultLiteral = TOY_TO_NULL_LITERAL;
|
||||
|
||||
//iterate over all arguments
|
||||
do {
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(TOY_IS_INTEGER(selfLiteral) || TOY_IS_FLOAT(selfLiteral))) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to max\n");
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//if not comparing yet...
|
||||
if (TOY_IS_NULL(resultLiteral)) {
|
||||
resultLiteral = selfLiteral;
|
||||
continue;
|
||||
}
|
||||
|
||||
//cooerce if needed
|
||||
if (TOY_IS_INTEGER(resultLiteral) && TOY_IS_FLOAT(selfLiteral)) {
|
||||
resultLiteral = TOY_TO_FLOAT_LITERAL( TOY_AS_INTEGER(resultLiteral) );
|
||||
}
|
||||
|
||||
if (TOY_IS_FLOAT(resultLiteral) && TOY_IS_INTEGER(selfLiteral)) {
|
||||
selfLiteral = TOY_TO_FLOAT_LITERAL( TOY_AS_INTEGER(selfLiteral) );
|
||||
}
|
||||
|
||||
//compare
|
||||
if (TOY_IS_INTEGER(resultLiteral) && TOY_AS_INTEGER(resultLiteral) < TOY_AS_INTEGER(selfLiteral)) {
|
||||
//NOTE: just ints, don't free
|
||||
resultLiteral = selfLiteral;
|
||||
}
|
||||
|
||||
else if (TOY_IS_FLOAT(resultLiteral) && TOY_AS_FLOAT(resultLiteral) < TOY_AS_FLOAT(selfLiteral)) {
|
||||
//NOTE: just floats, don't free
|
||||
resultLiteral = selfLiteral;
|
||||
}
|
||||
}
|
||||
while (arguments->count > 0);
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
||||
|
||||
Toy_freeLiteral(resultLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeMin(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
//return value
|
||||
Toy_Literal resultLiteral = TOY_TO_NULL_LITERAL;
|
||||
|
||||
//iterate over all arguments
|
||||
do {
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(TOY_IS_INTEGER(selfLiteral) || TOY_IS_FLOAT(selfLiteral))) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to min\n");
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//if not comparing yet...
|
||||
if (TOY_IS_NULL(resultLiteral)) {
|
||||
resultLiteral = selfLiteral;
|
||||
continue;
|
||||
}
|
||||
|
||||
//cooerce if needed
|
||||
if (TOY_IS_INTEGER(resultLiteral) && TOY_IS_FLOAT(selfLiteral)) {
|
||||
resultLiteral = TOY_TO_FLOAT_LITERAL( TOY_AS_INTEGER(resultLiteral) );
|
||||
}
|
||||
|
||||
if (TOY_IS_FLOAT(resultLiteral) && TOY_IS_INTEGER(selfLiteral)) {
|
||||
selfLiteral = TOY_TO_FLOAT_LITERAL( TOY_AS_INTEGER(selfLiteral) );
|
||||
}
|
||||
|
||||
//compare
|
||||
if (TOY_IS_INTEGER(resultLiteral) && TOY_AS_INTEGER(resultLiteral) > TOY_AS_INTEGER(selfLiteral)) {
|
||||
//NOTE: just ints, don't free
|
||||
resultLiteral = selfLiteral;
|
||||
}
|
||||
|
||||
else if (TOY_IS_FLOAT(resultLiteral) && TOY_AS_FLOAT(resultLiteral) > TOY_AS_FLOAT(selfLiteral)) {
|
||||
//NOTE: just floats, don't free
|
||||
resultLiteral = selfLiteral;
|
||||
}
|
||||
}
|
||||
while (arguments->count > 0);
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
||||
|
||||
Toy_freeLiteral(resultLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeRound(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to round\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(TOY_IS_INTEGER(selfLiteral) || TOY_IS_FLOAT(selfLiteral))) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to round\n");
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_Literal result;
|
||||
|
||||
if (TOY_IS_INTEGER(selfLiteral)) {
|
||||
//NO-OP
|
||||
result = Toy_copyLiteral(selfLiteral);
|
||||
}
|
||||
if (TOY_IS_FLOAT(selfLiteral)) {
|
||||
//catch the already-rounded case
|
||||
if (TOY_AS_FLOAT(selfLiteral) == (int)TOY_AS_FLOAT(selfLiteral)) {
|
||||
result = TOY_TO_INTEGER_LITERAL((int)TOY_AS_FLOAT(selfLiteral));
|
||||
}
|
||||
else {
|
||||
result = TOY_TO_INTEGER_LITERAL( TOY_AS_FLOAT(selfLiteral) - (int)TOY_AS_FLOAT(selfLiteral) < 0.5 ? (int)TOY_AS_FLOAT(selfLiteral) : (int)TOY_AS_FLOAT(selfLiteral) + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, result);
|
||||
|
||||
Toy_freeLiteral(result);
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -745,36 +1035,6 @@ static int nativeGetValues(Toy_Interpreter* interpreter, Toy_LiteralArray* argum
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeHash(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments to hash\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get the self
|
||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||
|
||||
//parse to value if needed
|
||||
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteralIdn);
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(selfLiteral)) {
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_Literal result = TOY_TO_INTEGER_LITERAL(Toy_hashLiteral(selfLiteral));
|
||||
|
||||
Toy_pushLiteralArray(&interpreter->stack, result);
|
||||
|
||||
Toy_freeLiteral(result);
|
||||
Toy_freeLiteral(selfLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeIndexOf(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||
//no arguments
|
||||
if (arguments->count != 2) {
|
||||
@@ -1740,8 +2000,19 @@ typedef struct Natives {
|
||||
int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias) {
|
||||
//build the natives list
|
||||
Natives natives[] = {
|
||||
{"abs", nativeAbs},
|
||||
//misc. utils
|
||||
{"clock", nativeClock},
|
||||
{"hash", nativeHash},
|
||||
|
||||
//math utils
|
||||
{"abs", nativeAbs},
|
||||
{"ceil", nativeCeil},
|
||||
{"floor", nativeFloor},
|
||||
{"max", nativeMax},
|
||||
{"min", nativeMin},
|
||||
{"round", nativeRound},
|
||||
|
||||
//compound utils
|
||||
{"concat", nativeConcat}, //array, dictionary, string
|
||||
{"containsKey", nativeContainsKey}, //dictionary
|
||||
{"containsValue", nativeContainsValue}, //array, dictionary
|
||||
@@ -1750,7 +2021,6 @@ int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
|
||||
{"forEach", nativeForEach}, //array, dictionary
|
||||
{"getKeys", nativeGetKeys}, //dictionary
|
||||
{"getValues", nativeGetValues}, //dictionary
|
||||
{"hash", nativeHash},
|
||||
{"indexOf", nativeIndexOf}, //array
|
||||
{"map", nativeMap}, //array, dictionary
|
||||
{"reduce", nativeReduce}, //array, dictionary
|
||||
|
||||
+8
-18
@@ -6,10 +6,7 @@
|
||||
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include "toy_lexer.h"
|
||||
#include "toy_parser.h"
|
||||
#include "toy_compiler.h"
|
||||
#include "toy_interpreter.h"
|
||||
#include "toy.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -106,16 +103,9 @@ void repl(const char* initialInput) {
|
||||
int main(int argc, const char* argv[]) {
|
||||
Toy_initCommandLine(argc, argv);
|
||||
|
||||
//lib setup (hacky - only really for this program)
|
||||
Toy_initDriveDictionary();
|
||||
|
||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
||||
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
||||
|
||||
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
|
||||
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_freeLiteral(pathLiteral);
|
||||
//setup the drive system (for filesystem access)
|
||||
Toy_initDriveSystem();
|
||||
Toy_setDrivePath("scripts", "scripts");
|
||||
|
||||
//command line specific actions
|
||||
if (Toy_commandLine.error) {
|
||||
@@ -151,7 +141,7 @@ int main(int argc, const char* argv[]) {
|
||||
Toy_runSourceFile(Toy_commandLine.sourcefile);
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveDictionary();
|
||||
Toy_freeDriveSystem();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -161,7 +151,7 @@ int main(int argc, const char* argv[]) {
|
||||
Toy_runSource(Toy_commandLine.source);
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveDictionary();
|
||||
Toy_freeDriveSystem();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -207,7 +197,7 @@ int main(int argc, const char* argv[]) {
|
||||
Toy_runBinaryFile(Toy_commandLine.binaryfile);
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveDictionary();
|
||||
Toy_freeDriveSystem();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -228,7 +218,7 @@ int main(int argc, const char* argv[]) {
|
||||
repl(initialSource);
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveDictionary();
|
||||
Toy_freeDriveSystem();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+3
-3
@@ -78,10 +78,10 @@ const unsigned char* Toy_compileString(const char* source, size_t* size) {
|
||||
Toy_initParser(&parser, &lexer);
|
||||
Toy_initCompiler(&compiler);
|
||||
|
||||
//run the parser until the end of the source
|
||||
//step 1 - run the parser until the end of the source
|
||||
Toy_ASTNode* node = Toy_scanParser(&parser);
|
||||
while(node != NULL) {
|
||||
//pack up and leave
|
||||
//on error, pack up and leave
|
||||
if (node->type == TOY_AST_NODE_ERROR) {
|
||||
Toy_freeASTNode(node);
|
||||
Toy_freeCompiler(&compiler);
|
||||
@@ -94,7 +94,7 @@ const unsigned char* Toy_compileString(const char* source, size_t* size) {
|
||||
node = Toy_scanParser(&parser);
|
||||
}
|
||||
|
||||
//get the bytecode dump
|
||||
//step 2 - get the bytecode dump
|
||||
const unsigned char* tb = Toy_collateCompiler(&compiler, size);
|
||||
|
||||
//cleanup
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
var s = "42";
|
||||
var t = "69";
|
||||
|
||||
print int (s + t) - 1;
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
/* toy.h - A Toy Programming Language
|
||||
|
||||
If you're looking how to use Toy directly, try https://toylang.com/
|
||||
Otherwise, these headers may help learn how Toy works internally.
|
||||
|
||||
*/
|
||||
|
||||
/* utilities - these define a bunch of useful macros based on platform.
|
||||
|
||||
The most important one is `TOY_API`, which highlights functions intended for the end user.
|
||||
|
||||
*/
|
||||
|
||||
#include "toy_common.h"
|
||||
#include "toy_console_colors.h"
|
||||
#include "toy_memory.h"
|
||||
#include "toy_drive_system.h"
|
||||
|
||||
/* core pipeline - from source to execution
|
||||
|
||||
Each step is as follows:
|
||||
|
||||
source -> lexer -> token
|
||||
token -> parser -> AST
|
||||
AST -> compiler -> bytecode
|
||||
bytecode -> interpreter -> result
|
||||
|
||||
I should note that the parser -> compiler phase is actually made up of two steps - the write step
|
||||
and the collate step. See `Toy_compileString()` in `repl/repl_tools.c` for an example of how to compile
|
||||
properly.
|
||||
|
||||
*/
|
||||
|
||||
#include "toy_lexer.h"
|
||||
#include "toy_parser.h"
|
||||
#include "toy_compiler.h"
|
||||
#include "toy_interpreter.h"
|
||||
|
||||
/* building block structures - the basic units of operation
|
||||
|
||||
Literals represent any value within the language, including some internal ones that you never see.
|
||||
Literal Arrays are literally arrays within memory, and are the most heavily used structure in Toy.
|
||||
Literal Dictionaries are unordered key-value hashmaps, that use a running strategy for collisions.
|
||||
|
||||
*/
|
||||
|
||||
#include "toy_literal.h"
|
||||
#include "toy_literal_array.h"
|
||||
#include "toy_literal_dictionary.h"
|
||||
|
||||
/* other components - you probably won't use these directly, but they're a good learning opportunity.
|
||||
|
||||
`Toy_Scope` holds the variables of a specific scope within Toy - be it a script, a function, a block, etc.
|
||||
Scopes are also where the type system lives at runtime. They use identifier literals as keys, exclusively.
|
||||
|
||||
`Toy_RefString` is a utility class that wraps traditional C strings, making them less memory intensive and
|
||||
faster to copy and move. In reality, since strings are considered immutable, multiple variables can point
|
||||
to the same string to save memory, and you can just create a new one of these vars pointing to the original
|
||||
rather than copying entirely for a speed boost. This module has it's own memory allocator system that is
|
||||
plugged into the main memory allocator.
|
||||
|
||||
*/
|
||||
|
||||
#include "toy_scope.h"
|
||||
#include "toy_refstring.h"
|
||||
|
||||
+15
-9
@@ -40,17 +40,21 @@ static void freeASTNodeCustom(Toy_ASTNode* node, bool freeSelf) {
|
||||
break;
|
||||
|
||||
case TOY_AST_NODE_BLOCK:
|
||||
for (int i = 0; i < node->block.count; i++) {
|
||||
freeASTNodeCustom(node->block.nodes + i, false);
|
||||
if (node->block.capacity > 0) {
|
||||
for (int i = 0; i < node->block.count; i++) {
|
||||
freeASTNodeCustom(node->block.nodes + i, false);
|
||||
}
|
||||
TOY_FREE_ARRAY(Toy_ASTNode, node->block.nodes, node->block.capacity);
|
||||
}
|
||||
TOY_FREE_ARRAY(Toy_ASTNode, node->block.nodes, node->block.capacity);
|
||||
break;
|
||||
|
||||
case TOY_AST_NODE_COMPOUND:
|
||||
for (int i = 0; i < node->compound.count; i++) {
|
||||
freeASTNodeCustom(node->compound.nodes + i, false);
|
||||
if (node->compound.capacity > 0) {
|
||||
for (int i = 0; i < node->compound.count; i++) {
|
||||
freeASTNodeCustom(node->compound.nodes + i, false);
|
||||
}
|
||||
TOY_FREE_ARRAY(Toy_ASTNode, node->compound.nodes, node->compound.capacity);
|
||||
}
|
||||
TOY_FREE_ARRAY(Toy_ASTNode, node->compound.nodes, node->compound.capacity);
|
||||
break;
|
||||
|
||||
case TOY_AST_NODE_PAIR:
|
||||
@@ -71,10 +75,12 @@ static void freeASTNodeCustom(Toy_ASTNode* node, bool freeSelf) {
|
||||
break;
|
||||
|
||||
case TOY_AST_NODE_FN_COLLECTION:
|
||||
for (int i = 0; i < node->fnCollection.count; i++) {
|
||||
freeASTNodeCustom(node->fnCollection.nodes + i, false);
|
||||
if (node->fnCollection.capacity > 0) {
|
||||
for (int i = 0; i < node->fnCollection.count; i++) {
|
||||
freeASTNodeCustom(node->fnCollection.nodes + i, false);
|
||||
}
|
||||
TOY_FREE_ARRAY(Toy_ASTNode, node->fnCollection.nodes, node->fnCollection.capacity);
|
||||
}
|
||||
TOY_FREE_ARRAY(Toy_ASTNode, node->fnCollection.nodes, node->fnCollection.capacity);
|
||||
break;
|
||||
|
||||
case TOY_AST_NODE_FN_DECL:
|
||||
|
||||
@@ -621,7 +621,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
|
||||
//simple indexing assignment if second is null
|
||||
if (TOY_IS_NULL(second)) {
|
||||
bool ret = -1;
|
||||
int ret = -1;
|
||||
|
||||
if (!Toy_setLiteralArray(TOY_AS_ARRAY(compound), first, assign)) {
|
||||
interpreter->errorOutput("Array index out of bounds in assignment");
|
||||
@@ -629,6 +629,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
}
|
||||
else {
|
||||
Toy_pushLiteralArray(&interpreter->stack, compound); //leave the array on the stack
|
||||
//...
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
@@ -1385,7 +1386,7 @@ int Toy_private_push(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
||||
Toy_Literal subtypeLiteral = ((Toy_Literal*)(TOY_AS_TYPE(typeLiteral).subtypes))[0];
|
||||
|
||||
if (TOY_AS_TYPE(subtypeLiteral).typeOf != TOY_LITERAL_ANY && TOY_AS_TYPE(subtypeLiteral).typeOf != val.type) {
|
||||
interpreter->errorOutput("Bad argument type in push");
|
||||
interpreter->errorOutput("Bad argument type in push\n");
|
||||
Toy_freeLiteral(typeLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
+5
-1
@@ -4,7 +4,7 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
//test variable sizes based on platform
|
||||
//test variable sizes based on platform - see issue #35
|
||||
#define STATIC_ASSERT(test_for_true) static_assert((test_for_true), "(" #test_for_true ") failed")
|
||||
|
||||
STATIC_ASSERT(sizeof(char) == 1);
|
||||
@@ -15,6 +15,8 @@ STATIC_ASSERT(sizeof(unsigned char) == 1);
|
||||
STATIC_ASSERT(sizeof(unsigned short) == 2);
|
||||
STATIC_ASSERT(sizeof(unsigned int) == 4);
|
||||
|
||||
#ifndef TOY_EXPORT
|
||||
|
||||
//declare the singleton with default values
|
||||
Toy_CommandLine Toy_commandLine = {
|
||||
.error = false,
|
||||
@@ -134,3 +136,5 @@ 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
|
||||
|
||||
+6
-2
@@ -6,7 +6,7 @@
|
||||
|
||||
#define TOY_VERSION_MAJOR 1
|
||||
#define TOY_VERSION_MINOR 1
|
||||
#define TOY_VERSION_PATCH 0
|
||||
#define TOY_VERSION_PATCH 2
|
||||
#define TOY_VERSION_BUILD __DATE__ " " __TIME__
|
||||
|
||||
//platform/compiler-specific instructions
|
||||
@@ -28,7 +28,9 @@
|
||||
|
||||
#endif
|
||||
|
||||
//for processing the command line arguments
|
||||
#ifndef TOY_EXPORT
|
||||
|
||||
//for processing the command line arguments in the repl
|
||||
typedef struct {
|
||||
bool error;
|
||||
bool help;
|
||||
@@ -51,3 +53,5 @@ 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
|
||||
+32
-4
@@ -130,7 +130,7 @@ static int writeNodeCompoundToCache(Toy_Compiler* compiler, Toy_ASTNode* node) {
|
||||
}
|
||||
|
||||
//push the store to the cache, with instructions about how pack it
|
||||
Toy_Literal literal = TOY_TO_DICTIONARY_LITERAL(store);
|
||||
Toy_Literal literal = TOY_TO_DICTIONARY_LITERAL((Toy_LiteralDictionary*)store); //cast from array to dict, because it's intermediate
|
||||
literal.type = TOY_LITERAL_DICTIONARY_INTERMEDIATE; //god damn it - nested in a dictionary
|
||||
index = Toy_pushLiteralArray(&compiler->literalCache, literal);
|
||||
Toy_freeLiteral(literal);
|
||||
@@ -331,9 +331,37 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
|
||||
return node->binary.opcode;
|
||||
}
|
||||
|
||||
if (ret != TOY_OP_EOF && (node->binary.opcode == TOY_OP_VAR_ASSIGN || node->binary.opcode == TOY_OP_AND || node->binary.opcode == TOY_OP_OR || (node->binary.opcode >= TOY_OP_COMPARE_EQUAL && node->binary.opcode <= TOY_OP_INVERT))) {
|
||||
compiler->bytecode[compiler->count++] = (unsigned char)ret; //1 byte
|
||||
ret = TOY_OP_EOF; //untangle in this case
|
||||
//untangle in these cases - (WTF, are you serious?)
|
||||
if (ret != TOY_OP_EOF) {
|
||||
switch(node->binary.opcode) {
|
||||
case TOY_OP_NEGATE:
|
||||
case TOY_OP_ADDITION:
|
||||
case TOY_OP_SUBTRACTION:
|
||||
case TOY_OP_MULTIPLICATION:
|
||||
case TOY_OP_DIVISION:
|
||||
case TOY_OP_MODULO:
|
||||
case TOY_OP_VAR_ASSIGN:
|
||||
case TOY_OP_VAR_ADDITION_ASSIGN:
|
||||
case TOY_OP_VAR_SUBTRACTION_ASSIGN:
|
||||
case TOY_OP_VAR_MULTIPLICATION_ASSIGN:
|
||||
case TOY_OP_VAR_DIVISION_ASSIGN:
|
||||
case TOY_OP_VAR_MODULO_ASSIGN:
|
||||
case TOY_OP_COMPARE_EQUAL:
|
||||
case TOY_OP_COMPARE_NOT_EQUAL:
|
||||
case TOY_OP_COMPARE_LESS:
|
||||
case TOY_OP_COMPARE_LESS_EQUAL:
|
||||
case TOY_OP_COMPARE_GREATER:
|
||||
case TOY_OP_COMPARE_GREATER_EQUAL:
|
||||
case TOY_OP_INVERT:
|
||||
case TOY_OP_AND:
|
||||
case TOY_OP_OR:
|
||||
//place the rhs result before the outer instruction
|
||||
compiler->bytecode[compiler->count++] = (unsigned char)ret; //1 byte
|
||||
ret = TOY_OP_EOF;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
compiler->bytecode[compiler->count++] = (unsigned char)node->binary.opcode; //1 byte
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "toy_drive_system.h"
|
||||
|
||||
#include "toy_memory.h"
|
||||
#include "toy_literal_dictionary.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//file system API
|
||||
static Toy_LiteralDictionary driveDictionary;
|
||||
|
||||
void Toy_initDriveSystem() {
|
||||
Toy_initLiteralDictionary(&driveDictionary);
|
||||
}
|
||||
|
||||
void Toy_freeDriveSystem() {
|
||||
Toy_freeLiteralDictionary(&driveDictionary);
|
||||
}
|
||||
|
||||
TOY_API void Toy_setDrivePath(char* drive, char* path) {
|
||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString(drive));
|
||||
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString(path));
|
||||
|
||||
Toy_setLiteralDictionary(&driveDictionary, driveLiteral, pathLiteral);
|
||||
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_freeLiteral(pathLiteral);
|
||||
}
|
||||
|
||||
Toy_Literal Toy_getDrivePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral) {
|
||||
//check argument types
|
||||
if (!TOY_IS_STRING(*drivePathLiteral)) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to Toy_getDrivePathLiteral\n");
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(*drivePathLiteral));
|
||||
|
||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
||||
size_t driveLength = 0;
|
||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
||||
interpreter->errorOutput("Incorrect drive path format given to Toy_getDrivePathLiteral\n");
|
||||
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
driveLength++;
|
||||
}
|
||||
|
||||
Toy_RefString* drive = Toy_createRefStringLength(Toy_toCString(drivePath), driveLength);
|
||||
Toy_RefString* filePath = Toy_createRefStringLength( &Toy_toCString(drivePath)[driveLength + 1], Toy_lengthRefString(drivePath) - driveLength );
|
||||
|
||||
//get the real drive file path
|
||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(drive); //NOTE: driveLiteral takes ownership of the refString
|
||||
Toy_Literal pathLiteral = Toy_getLiteralDictionary(&driveDictionary, driveLiteral);
|
||||
|
||||
if (!TOY_IS_STRING(pathLiteral)) {
|
||||
interpreter->errorOutput("Incorrect literal type found for drive: ");
|
||||
Toy_printLiteralCustom(pathLiteral, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\n");
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_freeLiteral(pathLiteral);
|
||||
Toy_deleteRefString(filePath);
|
||||
Toy_deleteRefString(drivePath);
|
||||
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
//get the final real file path (concat) TODO: move this concat to refstring library
|
||||
Toy_RefString* path = Toy_copyRefString(TOY_AS_STRING(pathLiteral));
|
||||
size_t fileLength = Toy_lengthRefString(path) + Toy_lengthRefString(filePath);
|
||||
|
||||
char* file = TOY_ALLOCATE(char, fileLength + 1); //+1 for null
|
||||
snprintf(file, fileLength, "%s%s", Toy_toCString(path), Toy_toCString(filePath));
|
||||
|
||||
//clean up the drive/path stuff
|
||||
Toy_deleteRefString(drivePath);
|
||||
Toy_deleteRefString(filePath);
|
||||
Toy_deleteRefString(path);
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_freeLiteral(pathLiteral);
|
||||
|
||||
//check for break-out attempts
|
||||
for (size_t i = 0; i < fileLength - 1; i++) {
|
||||
if (file[i] == '.' && file[i + 1] == '.') {
|
||||
interpreter->errorOutput("Parent directory access not allowed\n");
|
||||
TOY_FREE_ARRAY(char, file, fileLength + 1);
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
}
|
||||
|
||||
Toy_Literal result = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(file, fileLength));
|
||||
|
||||
TOY_FREE_ARRAY(char, file, fileLength + 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "toy_common.h"
|
||||
|
||||
#include "toy_literal.h"
|
||||
#include "toy_interpreter.h"
|
||||
|
||||
//file system API - these need to be set by the host
|
||||
TOY_API void Toy_initDriveSystem();
|
||||
TOY_API void Toy_freeDriveSystem();
|
||||
|
||||
//file system API - for use with libs
|
||||
TOY_API void Toy_setDrivePath(char* drive, char* path);
|
||||
TOY_API Toy_Literal Toy_getDrivePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral);
|
||||
+140
-159
@@ -12,12 +12,17 @@
|
||||
#include <string.h>
|
||||
|
||||
static void printWrapper(const char* output) {
|
||||
//allow for disabling of newlines in the repl
|
||||
#ifndef TOY_EXPORT
|
||||
if (Toy_commandLine.enablePrintNewline) {
|
||||
printf("%s\n", output);
|
||||
}
|
||||
else {
|
||||
printf("%s", output);
|
||||
}
|
||||
#else
|
||||
printf("%s\n", output);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void assertWrapper(const char* output) {
|
||||
@@ -1082,11 +1087,12 @@ static bool execAnd(Toy_Interpreter* interpreter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TOY_IS_TRUTHY(lhs) && TOY_IS_TRUTHY(rhs)) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(true));
|
||||
//short-circuit support
|
||||
if (!TOY_IS_TRUTHY(lhs)) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||
}
|
||||
else {
|
||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(false));
|
||||
Toy_pushLiteralArray(&interpreter->stack, rhs);
|
||||
}
|
||||
|
||||
Toy_freeLiteral(lhs);
|
||||
@@ -1115,11 +1121,12 @@ static bool execOr(Toy_Interpreter* interpreter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TOY_IS_TRUTHY(lhs) || TOY_IS_TRUTHY(rhs)) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(true));
|
||||
//short-circuit support
|
||||
if (TOY_IS_TRUTHY(lhs)) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||
}
|
||||
else {
|
||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(false));
|
||||
Toy_pushLiteralArray(&interpreter->stack, rhs);
|
||||
}
|
||||
|
||||
Toy_freeLiteral(lhs);
|
||||
@@ -1651,7 +1658,7 @@ static bool execIndex(Toy_Interpreter* interpreter, bool assignIntermediate) {
|
||||
}
|
||||
|
||||
if (!TOY_IS_ARRAY(compound) && !TOY_IS_DICTIONARY(compound) && !TOY_IS_STRING(compound)) {
|
||||
interpreter->errorOutput("Unknown compound found in indexing notation: ");
|
||||
interpreter->errorOutput("Unknown compound found in index notation: ");
|
||||
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\n");
|
||||
|
||||
@@ -1689,7 +1696,7 @@ static bool execIndex(Toy_Interpreter* interpreter, bool assignIntermediate) {
|
||||
Toy_pushLiteralArray(&interpreter->stack, third);
|
||||
}
|
||||
|
||||
//call the _index function
|
||||
//call the index function
|
||||
if (Toy_private_index(interpreter, &arguments) < 0) {
|
||||
interpreter->errorOutput("Something went wrong while indexing (simple index): ");
|
||||
Toy_printLiteralCustom(compoundIdn, interpreter->errorOutput);
|
||||
@@ -1720,85 +1727,101 @@ static bool execIndex(Toy_Interpreter* interpreter, bool assignIntermediate) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool execIndexAssign(Toy_Interpreter* interpreter) {
|
||||
static bool execIndexAssign(Toy_Interpreter* interpreter, int assignDepth) {
|
||||
//assume -> compound, first, second, third, assign are all on the stack
|
||||
|
||||
Toy_Literal assign = Toy_popLiteralArray(&interpreter->stack);
|
||||
Toy_Literal third = Toy_popLiteralArray(&interpreter->stack);
|
||||
Toy_Literal second = Toy_popLiteralArray(&interpreter->stack);
|
||||
Toy_Literal first = Toy_popLiteralArray(&interpreter->stack);
|
||||
Toy_Literal compound = Toy_popLiteralArray(&interpreter->stack);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Toy_Literal compoundIdn = compound;
|
||||
Toy_Literal assign = TOY_TO_NULL_LITERAL, third = TOY_TO_NULL_LITERAL, second = TOY_TO_NULL_LITERAL, first = TOY_TO_NULL_LITERAL, compound = TOY_TO_NULL_LITERAL, result = TOY_TO_NULL_LITERAL;
|
||||
Toy_Literal compoundIdn = TOY_TO_NULL_LITERAL;
|
||||
bool freeIdn = false;
|
||||
if (TOY_IS_IDENTIFIER(compound) && Toy_parseIdentifierToValue(interpreter, &compound)) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (!TOY_IS_ARRAY(compound) && !TOY_IS_DICTIONARY(compound) && !TOY_IS_STRING(compound)) {
|
||||
interpreter->errorOutput("Unknown compound found in index assigning notation\n");
|
||||
Toy_freeLiteral(assign);
|
||||
Toy_freeLiteral(third);
|
||||
Toy_freeLiteral(second);
|
||||
Toy_freeLiteral(first);
|
||||
Toy_freeLiteral(compound);
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//build the opcode
|
||||
unsigned char opcode = readByte(interpreter->bytecode, &interpreter->count);
|
||||
char* opStr = "";
|
||||
switch(opcode) {
|
||||
case TOY_OP_VAR_ASSIGN:
|
||||
opStr = "=";
|
||||
switch (opcode) {
|
||||
case TOY_OP_VAR_ASSIGN:
|
||||
opStr = "=";
|
||||
break;
|
||||
case TOY_OP_VAR_ADDITION_ASSIGN:
|
||||
opStr = "+=";
|
||||
case TOY_OP_VAR_ADDITION_ASSIGN:
|
||||
opStr = "+=";
|
||||
break;
|
||||
case TOY_OP_VAR_SUBTRACTION_ASSIGN:
|
||||
opStr = "-=";
|
||||
case TOY_OP_VAR_SUBTRACTION_ASSIGN:
|
||||
opStr = "-=";
|
||||
break;
|
||||
case TOY_OP_VAR_MULTIPLICATION_ASSIGN:
|
||||
opStr = "*=";
|
||||
case TOY_OP_VAR_MULTIPLICATION_ASSIGN:
|
||||
opStr = "*=";
|
||||
break;
|
||||
case TOY_OP_VAR_DIVISION_ASSIGN:
|
||||
opStr = "/=";
|
||||
case TOY_OP_VAR_DIVISION_ASSIGN:
|
||||
opStr = "/=";
|
||||
break;
|
||||
case TOY_OP_VAR_MODULO_ASSIGN:
|
||||
opStr = "%=";
|
||||
case TOY_OP_VAR_MODULO_ASSIGN:
|
||||
opStr = "%=";
|
||||
break;
|
||||
|
||||
default:
|
||||
interpreter->errorOutput("bad opcode in index assigning notation\n");
|
||||
default:
|
||||
interpreter->errorOutput("bad opcode in index assigning notation\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
//iterate...
|
||||
while(assignDepth-- >= 0) {
|
||||
Toy_freeLiteral(assign);
|
||||
Toy_freeLiteral(third);
|
||||
Toy_freeLiteral(second);
|
||||
Toy_freeLiteral(first);
|
||||
Toy_freeLiteral(compound);
|
||||
|
||||
if (TOY_IS_NULL(result)) {
|
||||
assign = Toy_popLiteralArray(&interpreter->stack);
|
||||
}
|
||||
else {
|
||||
assign = result;
|
||||
}
|
||||
|
||||
third = Toy_popLiteralArray(&interpreter->stack);
|
||||
second = Toy_popLiteralArray(&interpreter->stack);
|
||||
first = Toy_popLiteralArray(&interpreter->stack);
|
||||
compound = Toy_popLiteralArray(&interpreter->stack);
|
||||
|
||||
if (TOY_IS_IDENTIFIER(compound)) {
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
|
||||
compoundIdn = compound;
|
||||
Toy_parseIdentifierToValue(interpreter, &compound);
|
||||
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);
|
||||
interpreter->errorOutput("\n");
|
||||
Toy_freeLiteral(assign);
|
||||
Toy_freeLiteral(third);
|
||||
Toy_freeLiteral(second);
|
||||
@@ -1808,103 +1831,60 @@ static bool execIndexAssign(Toy_Interpreter* interpreter) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int opLength = strlen(opStr);
|
||||
Toy_Literal op = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(opStr, opLength)); //TODO: static reference optimisation?
|
||||
|
||||
//build the argument list
|
||||
Toy_LiteralArray arguments;
|
||||
Toy_initLiteralArray(&arguments);
|
||||
|
||||
Toy_pushLiteralArray(&arguments, compound);
|
||||
Toy_pushLiteralArray(&arguments, first);
|
||||
Toy_pushLiteralArray(&arguments, second);
|
||||
Toy_pushLiteralArray(&arguments, third);
|
||||
Toy_pushLiteralArray(&arguments, assign); //it expects an assignment command
|
||||
Toy_pushLiteralArray(&arguments, op); //it expects an assignment "opcode"
|
||||
|
||||
//call the _index function
|
||||
if (Toy_private_index(interpreter, &arguments) < 0) {
|
||||
//clean up
|
||||
Toy_freeLiteral(assign);
|
||||
Toy_freeLiteral(third);
|
||||
Toy_freeLiteral(second);
|
||||
Toy_freeLiteral(first);
|
||||
Toy_freeLiteral(compound);
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
Toy_freeLiteral(op);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
|
||||
return false;
|
||||
}
|
||||
int opLength = strlen(opStr);
|
||||
Toy_Literal op = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(opStr, opLength)); //TODO: static reference optimisation?
|
||||
|
||||
//save the result (assume top of the interpreter stack is the new compound value)
|
||||
Toy_Literal result = Toy_popLiteralArray(&interpreter->stack);
|
||||
//build the argument list
|
||||
Toy_LiteralArray arguments;
|
||||
Toy_initLiteralArray(&arguments);
|
||||
|
||||
//deep
|
||||
if (!freeIdn) {
|
||||
while (interpreter->stack.count > 1) {
|
||||
//read the new values
|
||||
Toy_freeLiteral(compound);
|
||||
Toy_pushLiteralArray(&arguments, compound);
|
||||
Toy_pushLiteralArray(&arguments, first);
|
||||
Toy_pushLiteralArray(&arguments, second);
|
||||
Toy_pushLiteralArray(&arguments, third);
|
||||
Toy_pushLiteralArray(&arguments, assign); //it expects an assignment command
|
||||
Toy_pushLiteralArray(&arguments, op); //it expects an assignment "opcode"
|
||||
|
||||
//call the index function
|
||||
if (Toy_private_index(interpreter, &arguments) < 0) {
|
||||
//clean up
|
||||
Toy_freeLiteral(assign);
|
||||
Toy_freeLiteral(third);
|
||||
Toy_freeLiteral(second);
|
||||
Toy_freeLiteral(first);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
Toy_initLiteralArray(&arguments);
|
||||
Toy_freeLiteral(op);
|
||||
|
||||
//reuse these like an idiot
|
||||
third = Toy_popLiteralArray(&interpreter->stack);
|
||||
second = Toy_popLiteralArray(&interpreter->stack);
|
||||
first = Toy_popLiteralArray(&interpreter->stack);
|
||||
compound = Toy_popLiteralArray(&interpreter->stack);
|
||||
|
||||
char* opStr = "="; //shadow, but force assignment
|
||||
int opLength = strlen(opStr);
|
||||
op = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(opStr, opLength)); //TODO: static reference optimisation?
|
||||
|
||||
//assign to the idn / compound - with _index
|
||||
Toy_pushLiteralArray(&arguments, compound); //
|
||||
Toy_pushLiteralArray(&arguments, first);
|
||||
Toy_pushLiteralArray(&arguments, second);
|
||||
Toy_pushLiteralArray(&arguments, third);
|
||||
Toy_pushLiteralArray(&arguments, result);
|
||||
Toy_pushLiteralArray(&arguments, op);
|
||||
|
||||
if (Toy_private_index(interpreter, &arguments) < 0) {
|
||||
interpreter->errorOutput("Something went wrong while indexing (index assign): ");
|
||||
Toy_printLiteralCustom(compound, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\n");
|
||||
|
||||
//clean up
|
||||
Toy_freeLiteral(assign);
|
||||
Toy_freeLiteral(third);
|
||||
Toy_freeLiteral(second);
|
||||
Toy_freeLiteral(first);
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
Toy_freeLiteral(op);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
return false;
|
||||
Toy_freeLiteral(compound);
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
Toy_freeLiteral(op);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
|
||||
Toy_freeLiteral(result);
|
||||
result = Toy_popLiteralArray(&interpreter->stack);
|
||||
return false;
|
||||
}
|
||||
|
||||
Toy_freeLiteral(compound);
|
||||
compound = Toy_popLiteralArray(&interpreter->stack);
|
||||
compoundIdn = compound;
|
||||
freeIdn = false;
|
||||
//save the result (assume top of the interpreter stack is the new compound value)
|
||||
result = Toy_popLiteralArray(&interpreter->stack);
|
||||
|
||||
Toy_freeLiteral(op);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
|
||||
//if we loop, then we need to be assigning
|
||||
opStr = "=";
|
||||
}
|
||||
|
||||
//BUGFIX: make sure the compound name can be assigned
|
||||
if (TOY_IS_NULL(compoundIdn)) {
|
||||
compoundIdn = Toy_popLiteralArray(&interpreter->stack);
|
||||
freeIdn = true;
|
||||
}
|
||||
|
||||
if (TOY_IS_IDENTIFIER(compoundIdn) && !Toy_setScopeVariable(interpreter->scope, compoundIdn, result, true)) {
|
||||
interpreter->errorOutput("Incorrect type assigned to compound member ");
|
||||
Toy_printLiteralCustom(compoundIdn, interpreter->errorOutput);
|
||||
interpreter->errorOutput(", value: ");
|
||||
Toy_printLiteralCustom(result, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\n");
|
||||
|
||||
//clean up
|
||||
@@ -1916,8 +1896,6 @@ static bool execIndexAssign(Toy_Interpreter* interpreter) {
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
Toy_freeLiteral(op);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
Toy_freeLiteral(result);
|
||||
return false;
|
||||
}
|
||||
@@ -1931,8 +1909,6 @@ static bool execIndexAssign(Toy_Interpreter* interpreter) {
|
||||
if (freeIdn) {
|
||||
Toy_freeLiteral(compoundIdn);
|
||||
}
|
||||
Toy_freeLiteral(op);
|
||||
Toy_freeLiteralArray(&arguments);
|
||||
Toy_freeLiteral(result);
|
||||
|
||||
return true;
|
||||
@@ -1945,6 +1921,9 @@ static void execInterpreter(Toy_Interpreter* interpreter) {
|
||||
interpreter->codeStart = interpreter->count;
|
||||
}
|
||||
|
||||
//BUGFIX
|
||||
int intermediateAssignDepth = 0;
|
||||
|
||||
unsigned char opcode = readByte(interpreter->bytecode, &interpreter->count);
|
||||
|
||||
while(opcode != TOY_OP_EOF && opcode != TOY_OP_SECTION_END && !interpreter->panic) {
|
||||
@@ -2159,12 +2138,14 @@ static void execInterpreter(Toy_Interpreter* interpreter) {
|
||||
if (!execIndex(interpreter, true)) {
|
||||
return;
|
||||
}
|
||||
intermediateAssignDepth++;
|
||||
break;
|
||||
|
||||
case TOY_OP_INDEX_ASSIGN:
|
||||
if (!execIndexAssign(interpreter)) {
|
||||
if (!execIndexAssign(interpreter, intermediateAssignDepth)) {
|
||||
return;
|
||||
}
|
||||
intermediateAssignDepth = 0;
|
||||
break;
|
||||
|
||||
case TOY_OP_POP_STACK:
|
||||
|
||||
@@ -62,7 +62,7 @@ void Toy_freeLiteral(Toy_Literal literal) {
|
||||
TOY_FREE_ARRAY(unsigned char, TOY_AS_FUNCTION(literal).inner.bytecode, TOY_AS_FUNCTION_BYTECODE_LENGTH(literal));
|
||||
}
|
||||
|
||||
if (TOY_IS_TYPE(literal)) {
|
||||
if (TOY_IS_TYPE(literal) && TOY_AS_TYPE(literal).capacity > 0) {
|
||||
for (int i = 0; i < TOY_AS_TYPE(literal).count; i++) {
|
||||
Toy_freeLiteral(((Toy_Literal*)(TOY_AS_TYPE(literal).subtypes))[i]);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
struct Toy_Literal;
|
||||
struct Toy_Interpreter;
|
||||
struct Toy_LiteralArray;
|
||||
struct Toy_LiteralDictionary;
|
||||
struct Toy_Scope;
|
||||
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);
|
||||
@@ -49,8 +50,8 @@ typedef struct Toy_Literal {
|
||||
//string hash?
|
||||
} string; //8
|
||||
|
||||
void* array; //8
|
||||
void* dictionary; //8
|
||||
struct Toy_LiteralArray* array; //8
|
||||
struct Toy_LiteralDictionary* dictionary; //8
|
||||
|
||||
struct {
|
||||
union {
|
||||
@@ -67,7 +68,8 @@ typedef struct Toy_Literal {
|
||||
} identifier; //16
|
||||
|
||||
struct {
|
||||
void* subtypes; //8
|
||||
|
||||
struct Toy_Literal* subtypes; //8
|
||||
Toy_LiteralType typeOf; //4
|
||||
unsigned char capacity; //1
|
||||
unsigned char count; //1
|
||||
|
||||
@@ -18,8 +18,10 @@ void Toy_freeLiteralArray(Toy_LiteralArray* array) {
|
||||
Toy_freeLiteral(array->literals[i]);
|
||||
}
|
||||
|
||||
TOY_FREE_ARRAY(Toy_Literal, array->literals, array->capacity);
|
||||
Toy_initLiteralArray(array);
|
||||
if (array->capacity > 0) {
|
||||
TOY_FREE_ARRAY(Toy_Literal, array->literals, array->capacity);
|
||||
Toy_initLiteralArray(array);
|
||||
}
|
||||
}
|
||||
|
||||
int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal) {
|
||||
|
||||
@@ -22,11 +22,13 @@ static Toy_private_dictionary_entry* getEntryArray(Toy_private_dictionary_entry*
|
||||
}
|
||||
|
||||
//find "key", starting at index
|
||||
unsigned int index = hash % capacity;
|
||||
unsigned int start = index;
|
||||
int index = hash % capacity;
|
||||
int start = index;
|
||||
|
||||
//increment once, so it can't equal start
|
||||
index = (index + 1) % capacity;
|
||||
if (++index >= capacity) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
//literal probing and collision checking
|
||||
while (index != start) { //WARNING: this is the only function allowed to retrieve an entry from the array
|
||||
@@ -44,7 +46,10 @@ static Toy_private_dictionary_entry* getEntryArray(Toy_private_dictionary_entry*
|
||||
}
|
||||
}
|
||||
|
||||
index = (index + 1) % capacity;
|
||||
if (++index >= capacity) {
|
||||
index = 0;
|
||||
}
|
||||
//index = (index + 1) % capacity;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -73,7 +78,9 @@ static void adjustEntryCapacity(Toy_private_dictionary_entry** dictionaryHandle,
|
||||
}
|
||||
|
||||
//clear the old array
|
||||
TOY_FREE_ARRAY(Toy_private_dictionary_entry, *dictionaryHandle, oldCapacity);
|
||||
if (oldCapacity > 0) {
|
||||
TOY_FREE_ARRAY(Toy_private_dictionary_entry, *dictionaryHandle, oldCapacity);
|
||||
}
|
||||
|
||||
*dictionaryHandle = newEntries;
|
||||
}
|
||||
@@ -133,9 +140,11 @@ void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary) {
|
||||
}
|
||||
|
||||
void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary) {
|
||||
freeEntryArray(dictionary->entries, dictionary->capacity);
|
||||
dictionary->capacity = 0;
|
||||
dictionary->contains = 0;
|
||||
if (dictionary->capacity > 0) {
|
||||
freeEntryArray(dictionary->entries, dictionary->capacity);
|
||||
dictionary->capacity = 0;
|
||||
dictionary->contains = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value) {
|
||||
|
||||
+4
-4
@@ -8,10 +8,10 @@
|
||||
|
||||
//default allocator
|
||||
void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
||||
if (newSize == 0 && oldSize == 0) {
|
||||
//causes issues, so just skip out with a NO-OP
|
||||
return NULL;
|
||||
}
|
||||
//causes issues, so just skip out with a NO-OP (DISABLED for performance reasons)
|
||||
// if (newSize == 0 && oldSize == 0) {
|
||||
// return NULL;
|
||||
// }
|
||||
|
||||
if (newSize == 0) {
|
||||
free(pointer);
|
||||
|
||||
+42
-21
@@ -140,7 +140,7 @@ static Toy_Opcode asType(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
|
||||
static Toy_Opcode typeOf(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
Toy_ASTNode* rhs = NULL;
|
||||
parsePrecedence(parser, &rhs, PREC_TERNARY);
|
||||
parsePrecedence(parser, &rhs, PREC_CALL);
|
||||
Toy_emitASTNodeUnary(nodeHandle, TOY_OP_TYPE_OF, rhs);
|
||||
return TOY_OP_EOF;
|
||||
}
|
||||
@@ -341,99 +341,99 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
switch(parser->previous.type) {
|
||||
//arithmetic
|
||||
case TOY_TOKEN_PLUS: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM);
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM + 1);
|
||||
return TOY_OP_ADDITION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MINUS: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM);
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM + 1);
|
||||
return TOY_OP_SUBTRACTION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MULTIPLY: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
|
||||
return TOY_OP_MULTIPLICATION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_DIVIDE: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
|
||||
return TOY_OP_DIVISION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MODULO: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
|
||||
return TOY_OP_MODULO;
|
||||
}
|
||||
|
||||
//assignment
|
||||
case TOY_TOKEN_ASSIGN: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT);
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT + 1);
|
||||
return TOY_OP_VAR_ASSIGN;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_PLUS_ASSIGN: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT);
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT + 1);
|
||||
return TOY_OP_VAR_ADDITION_ASSIGN;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MINUS_ASSIGN: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT);
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT + 1);
|
||||
return TOY_OP_VAR_SUBTRACTION_ASSIGN;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MULTIPLY_ASSIGN: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT);
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT + 1);
|
||||
return TOY_OP_VAR_MULTIPLICATION_ASSIGN;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_DIVIDE_ASSIGN: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT);
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT + 1);
|
||||
return TOY_OP_VAR_DIVISION_ASSIGN;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MODULO_ASSIGN: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT);
|
||||
parsePrecedence(parser, nodeHandle, PREC_ASSIGNMENT + 1);
|
||||
return TOY_OP_VAR_MODULO_ASSIGN;
|
||||
}
|
||||
|
||||
//comparison
|
||||
case TOY_TOKEN_EQUAL: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON + 1);
|
||||
return TOY_OP_COMPARE_EQUAL;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_NOT_EQUAL: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON + 1);
|
||||
return TOY_OP_COMPARE_NOT_EQUAL;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_LESS: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON + 1);
|
||||
return TOY_OP_COMPARE_LESS;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_LESS_EQUAL: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON + 1);
|
||||
return TOY_OP_COMPARE_LESS_EQUAL;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_GREATER: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON + 1);
|
||||
return TOY_OP_COMPARE_GREATER;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_GREATER_EQUAL: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON + 1);
|
||||
return TOY_OP_COMPARE_GREATER_EQUAL;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_AND: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_AND);
|
||||
parsePrecedence(parser, nodeHandle, PREC_AND + 1);
|
||||
return TOY_OP_AND;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_OR: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_OR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_OR + 1);
|
||||
return TOY_OP_OR;
|
||||
}
|
||||
|
||||
@@ -658,7 +658,7 @@ static Toy_Opcode castingInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
break;
|
||||
|
||||
case TOY_TOKEN_LITERAL_STRING:
|
||||
atomic(parser, nodeHandle);
|
||||
string(parser, nodeHandle);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -739,6 +739,27 @@ static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
}
|
||||
|
||||
static Toy_Opcode fnCall(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
//wait - is the previous token a type? this should be casting instead
|
||||
if (parser->previous.type >= TOY_TOKEN_NULL && parser->previous.type <= TOY_TOKEN_ANY) {
|
||||
//casting type
|
||||
Toy_ASTNode* lhsNode = NULL;
|
||||
castingPrefix(parser, &lhsNode);
|
||||
advance(parser);
|
||||
|
||||
//casting value
|
||||
Toy_ASTNode* rhsNode = NULL;
|
||||
grouping(parser, &rhsNode);
|
||||
|
||||
//emit the cast node
|
||||
|
||||
Toy_emitASTNodeBinary(&lhsNode, rhsNode, TOY_OP_TYPE_CAST);
|
||||
|
||||
//pass it off to the caller
|
||||
*nodeHandle = lhsNode;
|
||||
|
||||
return TOY_OP_GROUPING_BEGIN; //dummy value
|
||||
}
|
||||
|
||||
advance(parser); //skip the left paren
|
||||
|
||||
//binary() is an infix rule - so only get the RHS of the operator
|
||||
|
||||
+54
-60
@@ -4,21 +4,19 @@
|
||||
|
||||
//run up the ancestor chain, freeing anything with 0 references left
|
||||
static void freeAncestorChain(Toy_Scope* scope) {
|
||||
scope->references--;
|
||||
while (scope != NULL) {
|
||||
Toy_Scope* next = scope->ancestor;
|
||||
|
||||
//free scope chain
|
||||
if (scope->ancestor != NULL) {
|
||||
freeAncestorChain(scope->ancestor);
|
||||
scope->references--;
|
||||
|
||||
if (scope->references <= 0) {
|
||||
Toy_freeLiteralDictionary(&scope->variables);
|
||||
Toy_freeLiteralDictionary(&scope->types);
|
||||
TOY_FREE(Toy_Scope, scope);
|
||||
}
|
||||
|
||||
scope = next;
|
||||
}
|
||||
|
||||
if (scope->references > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Toy_freeLiteralDictionary(&scope->variables);
|
||||
Toy_freeLiteralDictionary(&scope->types);
|
||||
|
||||
TOY_FREE(Toy_Scope, scope);
|
||||
}
|
||||
|
||||
//return false if invalid type
|
||||
@@ -259,74 +257,70 @@ bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal typ
|
||||
}
|
||||
|
||||
bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key) {
|
||||
if (scope == NULL) {
|
||||
return false;
|
||||
while (scope != NULL) {
|
||||
if (Toy_existsLiteralDictionary(&scope->variables, key)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
scope = scope->ancestor;
|
||||
}
|
||||
|
||||
//if it's not in this scope, keep searching up the chain
|
||||
if (!Toy_existsLiteralDictionary(&scope->variables, key)) {
|
||||
return Toy_isDelcaredScopeVariable(scope->ancestor, key);
|
||||
}
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
//return false if undefined, or can't be assigned
|
||||
bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value, bool constCheck) {
|
||||
//dead end
|
||||
if (scope == NULL) {
|
||||
return false;
|
||||
}
|
||||
while (scope != NULL) {
|
||||
//if it's not in this scope, keep searching up the chain
|
||||
if (!Toy_existsLiteralDictionary(&scope->variables, key)) {
|
||||
scope = scope->ancestor;
|
||||
continue;
|
||||
}
|
||||
|
||||
//if it's not in this scope, keep searching up the chain
|
||||
if (!Toy_existsLiteralDictionary(&scope->variables, key)) {
|
||||
return Toy_setScopeVariable(scope->ancestor, key, value, constCheck);
|
||||
}
|
||||
//type checking
|
||||
Toy_Literal typeLiteral = Toy_getLiteralDictionary(&scope->types, key);
|
||||
Toy_Literal original = Toy_getLiteralDictionary(&scope->variables, key);
|
||||
|
||||
//type checking
|
||||
Toy_Literal typeLiteral = Toy_getLiteralDictionary(&scope->types, key);
|
||||
Toy_Literal original = Toy_getLiteralDictionary(&scope->variables, key);
|
||||
if (!checkType(typeLiteral, original, value, constCheck)) {
|
||||
Toy_freeLiteral(typeLiteral);
|
||||
Toy_freeLiteral(original);
|
||||
return false;
|
||||
}
|
||||
|
||||
//actually assign
|
||||
Toy_setLiteralDictionary(&scope->variables, key, value); //key & value are copied here
|
||||
|
||||
if (!checkType(typeLiteral, original, value, constCheck)) {
|
||||
Toy_freeLiteral(typeLiteral);
|
||||
Toy_freeLiteral(original);
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//actually assign
|
||||
Toy_setLiteralDictionary(&scope->variables, key, value); //key & value are copied here
|
||||
|
||||
Toy_freeLiteral(typeLiteral);
|
||||
Toy_freeLiteral(original);
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Toy_getScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal* valueHandle) {
|
||||
//dead end
|
||||
if (scope == NULL) {
|
||||
return false;
|
||||
//optimized to reduce call stack
|
||||
while (scope != NULL) {
|
||||
if (Toy_existsLiteralDictionary(&scope->variables, key)) {
|
||||
*valueHandle = Toy_getLiteralDictionary(&scope->variables, key);
|
||||
return true;
|
||||
}
|
||||
|
||||
scope = scope->ancestor;
|
||||
}
|
||||
|
||||
//if it's not in this scope, keep searching up the chain
|
||||
if (!Toy_existsLiteralDictionary(&scope->variables, key)) {
|
||||
return Toy_getScopeVariable(scope->ancestor, key, valueHandle);
|
||||
}
|
||||
|
||||
*valueHandle = Toy_getLiteralDictionary(&scope->variables, key);
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
Toy_Literal Toy_getScopeType(Toy_Scope* scope, Toy_Literal key) {
|
||||
//dead end
|
||||
if (scope == NULL) {
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
while (scope != NULL) {
|
||||
if (Toy_existsLiteralDictionary(&scope->types, key)) {
|
||||
return Toy_getLiteralDictionary(&scope->types, key);
|
||||
}
|
||||
|
||||
scope = scope->ancestor;
|
||||
}
|
||||
|
||||
//if it's not in this scope, keep searching up the chain
|
||||
if (!Toy_existsLiteralDictionary(&scope->types, key)) {
|
||||
return Toy_getScopeType(scope->ancestor, key);
|
||||
}
|
||||
|
||||
return Toy_getLiteralDictionary(&scope->types, key);
|
||||
return TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
@@ -38,5 +38,12 @@ s += "bar";
|
||||
|
||||
assert s == "foobar", "string addition failed (wasn't sticky enough)";
|
||||
|
||||
//check order of operations
|
||||
assert 30 / 3 * 2 == 20, "Order of operations failed (raw numbers)";
|
||||
var x = 30;
|
||||
var y = 3;
|
||||
var z = 2;
|
||||
assert x / y * z == 20, "Order of operations failed (variables)";
|
||||
|
||||
|
||||
print "All good";
|
||||
@@ -0,0 +1,7 @@
|
||||
var s = "42";
|
||||
var t = "69";
|
||||
|
||||
assert int (s + t) - 1 == 4268, "casting parentheses failed";
|
||||
|
||||
|
||||
print "All good";
|
||||
@@ -23,5 +23,8 @@ assert !false, "!false";
|
||||
var c = false;
|
||||
assert !c, "!c";
|
||||
|
||||
//test multiple comparisons
|
||||
assert 1 == 2 == false, "Left-accociative equality failed";
|
||||
|
||||
|
||||
print "All good";
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
var days = [
|
||||
"sunday",
|
||||
"monday",
|
||||
"tuesday",
|
||||
"wednesday",
|
||||
"thursday",
|
||||
"friday",
|
||||
"saturday"
|
||||
];
|
||||
|
||||
var rng = 10; //for chosen at random
|
||||
|
||||
|
||||
var index = rng % days.length();
|
||||
|
||||
assert index == 3, "dot modulo bugfix failed";
|
||||
|
||||
rng %= days.length();
|
||||
|
||||
assert rng == 3, "dot modulo assign bugfix failed";
|
||||
|
||||
|
||||
print "All good";
|
||||
@@ -1,5 +1,19 @@
|
||||
import standard;
|
||||
|
||||
//test clock
|
||||
{
|
||||
//this depends on external factors, so only check the length
|
||||
assert clock().length() == 24, "clock().length() failed";
|
||||
}
|
||||
|
||||
|
||||
//test hash
|
||||
{
|
||||
assert typeof "Hello world".hash() == int, "typeof \"Hello world\".hash() failed";
|
||||
assert "Hello world".hash() == 994097935, "\"Hello world\".hash() failed"; //NOTE: specific value based on algorithm
|
||||
}
|
||||
|
||||
|
||||
//test abs
|
||||
{
|
||||
assert abs(-5) == 5, "abs(-integer) failed";
|
||||
@@ -13,10 +27,77 @@ import standard;
|
||||
}
|
||||
|
||||
|
||||
//test clock
|
||||
//test ceil
|
||||
{
|
||||
//this depends on external factors, so only check the length
|
||||
assert clock().length() == 24, "clock().length() failed";
|
||||
assert ceil(4) == 4, "ceil(int) failed";
|
||||
assert ceil(4.0) == 4, "ceil(float) failed";
|
||||
assert ceil(4.1) == 5, "ceil() failed";
|
||||
|
||||
var x = 4.1;
|
||||
|
||||
assert x.ceil() == 5, "var.ceil() failed";
|
||||
}
|
||||
|
||||
|
||||
//test floor
|
||||
{
|
||||
assert floor(4) == 4, "floor(int) failed";
|
||||
assert floor(4.0) == 4, "floor(float) failed";
|
||||
assert floor(4.1) == 4, "floor() failed";
|
||||
|
||||
var x = 4.1;
|
||||
|
||||
assert x.floor() == 4, "var.floor() failed";
|
||||
}
|
||||
|
||||
|
||||
//test max
|
||||
{
|
||||
assert max(1, 2, 3) == 3, "max() failed";
|
||||
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var c = 3;
|
||||
|
||||
assert max(a, b, c) == 3, "var.max() failed";
|
||||
|
||||
assert max(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) == 9, "max() with many args failed";
|
||||
|
||||
assert typeof max(1, 2, 3) == int, "typeof max() == int failed";
|
||||
assert typeof max(1, 2, 3.4) == float, "typeof max() == float failed";
|
||||
}
|
||||
|
||||
|
||||
//test min
|
||||
{
|
||||
assert min(1, 2, 3) == 1, "min() failed";
|
||||
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var c = 3;
|
||||
|
||||
assert min(a, b, c) == 1, "var.min() failed";
|
||||
|
||||
assert min(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) == 0, "min() with many args failed";
|
||||
|
||||
assert typeof min(1, 2, 3) == int, "typeof min() == int failed";
|
||||
assert typeof min(1, 2, 3.4) == float, "typeof min() == float failed";
|
||||
}
|
||||
|
||||
|
||||
//test round
|
||||
{
|
||||
assert round(4) == 4, "round(int) failed";
|
||||
assert round(4.0) == 4, "round(float) failed";
|
||||
assert round(4.1) == 4, "round(less than half) failed";
|
||||
assert round(4.9) == 5, "round(greater than half) failed";
|
||||
assert round(4.5) == 5, "round(exactly half) failed";
|
||||
|
||||
var x = 4.1;
|
||||
|
||||
assert x.round() == 4, "var.round() failed";
|
||||
|
||||
assert typeof round(1.0) == int, "typeof round() == int failed";
|
||||
}
|
||||
|
||||
|
||||
@@ -175,13 +256,6 @@ import standard;
|
||||
}
|
||||
|
||||
|
||||
//test hash
|
||||
{
|
||||
assert typeof "Hello world".hash() == int, "typeof \"Hello world\".hash() failed";
|
||||
assert "Hello world".hash() == 994097935, "\"Hello world\".hash() failed"; //NOTE: specific value based on algorithm
|
||||
}
|
||||
|
||||
|
||||
//test indexOf
|
||||
{
|
||||
var a = [1, 2, 42, 3];
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
//explicitly support && and || short circuits
|
||||
|
||||
assert 1 && 2 == 2, "&& short-circuit failed";
|
||||
|
||||
assert 1 || 2 == 1, "|| short-circuit failed";
|
||||
|
||||
|
||||
print "All good";
|
||||
|
||||
@@ -108,12 +108,14 @@ int main() {
|
||||
//run each file in tests/scripts/
|
||||
const char* filenames[] = {
|
||||
"arithmetic.toy",
|
||||
"casting-parentheses-bugfix.toy",
|
||||
"casting.toy",
|
||||
"coercions.toy",
|
||||
"comparisons.toy",
|
||||
"dot-and-matrix.toy",
|
||||
"dot-assignments-bugfix.toy",
|
||||
"dot-chaining.toy",
|
||||
"dot-modulo-bugfix.toy",
|
||||
"dottify-bugfix.toy",
|
||||
"functions.toy",
|
||||
"index-arrays.toy",
|
||||
@@ -132,6 +134,7 @@ int main() {
|
||||
"panic-within-functions.toy",
|
||||
"polyfill-insert.toy",
|
||||
"polyfill-remove.toy",
|
||||
"short-circuiting-support.toy",
|
||||
"ternary-expressions.toy",
|
||||
"types.toy",
|
||||
NULL
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include "toy_memory.h"
|
||||
#include "toy_drive_system.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -63,15 +64,9 @@ typedef struct Payload {
|
||||
|
||||
int main() {
|
||||
//setup the runner filesystem (hacky)
|
||||
Toy_initDriveDictionary();
|
||||
Toy_initDriveSystem();
|
||||
|
||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
||||
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
||||
|
||||
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
|
||||
|
||||
Toy_freeLiteral(driveLiteral);
|
||||
Toy_freeLiteral(pathLiteral);
|
||||
Toy_setDrivePath("scripts", "scripts");
|
||||
|
||||
{
|
||||
//run each file in test/scripts
|
||||
@@ -113,7 +108,7 @@ int main() {
|
||||
}
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveDictionary();
|
||||
Toy_freeDriveSystem();
|
||||
|
||||
if (!failedAsserts) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
|
||||
+11
-4
@@ -10,25 +10,32 @@
|
||||
int currentMemoryUsed = 0;
|
||||
int maxMemoryUsed = 0;
|
||||
int memoryAllocCalls = 0;
|
||||
int memoryAllocFree = 0;
|
||||
int memoryAllocRealloc = 0;
|
||||
|
||||
static void* trackerAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
||||
//the number of raw calls
|
||||
memoryAllocCalls++;
|
||||
|
||||
//causes issues, so just skip out with a NO-OP
|
||||
if (newSize == 0 && oldSize == 0) {
|
||||
//causes issues, so just skip out with a NO-OP
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memoryAllocCalls++;
|
||||
|
||||
//track the changes
|
||||
currentMemoryUsed = currentMemoryUsed - oldSize + newSize;
|
||||
maxMemoryUsed = currentMemoryUsed > maxMemoryUsed ? currentMemoryUsed : maxMemoryUsed;
|
||||
|
||||
if (newSize == 0) {
|
||||
//the number of frees
|
||||
memoryAllocFree++;
|
||||
free(pointer);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//the number of reallocations
|
||||
memoryAllocRealloc++;
|
||||
void* mem = realloc(pointer, newSize);
|
||||
|
||||
if (mem == NULL) {
|
||||
@@ -69,7 +76,7 @@ int main(int argc, const char* argv[]) {
|
||||
Toy_freeDriveDictionary();
|
||||
|
||||
//report output
|
||||
printf("Memory report: %d max bytes, %d calls\n", maxMemoryUsed, memoryAllocCalls);
|
||||
printf("Heap Memory Report:\n\t%d max bytes\n\t%d calls to the allocator\n\t%d calls to realloc()\n\t%d calls to free()\n\t%d discrepancies\n", maxMemoryUsed, memoryAllocCalls, memoryAllocRealloc, memoryAllocFree, memoryAllocCalls - memoryAllocRealloc - memoryAllocFree);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user