Began working on runner library

This commit is contained in:
2023-01-20 13:42:45 +00:00
parent 390f60e0ce
commit e6e24ca19f
18 changed files with 435 additions and 426 deletions

275
repl/lib_runner.c Normal file
View File

@@ -0,0 +1,275 @@
#include "lib_runner.h"
#include "memory.h"
#include "interpreter.h"
#include "repl_tools.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct Runner {
Interpreter interpreter;
unsigned char* bytecode;
size_t size;
//
} Runner;
//Toy native functions
static int nativeLoadScript(Interpreter* interpreter, LiteralArray* arguments) {
//arguments
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments to loadScript\n");
return -1;
}
//get the argument
Literal drivePathLiteral = popLiteralArray(arguments);
RefString* drivePath = copyRefString(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
int driveLength = 0;
while (toCString(drivePath)[driveLength] != ':') {
if (driveLength >= lengthRefString(drivePath)) {
interpreter->errorOutput("Incorrect drive path format given to loadScript\n");
deleteRefString(drivePath);
freeLiteral(drivePathLiteral);
return -1;
}
driveLength++;
}
RefString* drive = createRefStringLength(toCString(drivePath), driveLength);
RefString* path = createRefStringLength( &toCString(drivePath)[driveLength + 1], lengthRefString(drivePath) - driveLength );
//get the real drive file path
Literal driveLiteral = TO_STRING_LITERAL(drive);
Literal realDriveLiteral = getLiteralDictionary(getDriveDictionary(), driveLiteral);
if (!IS_STRING(realDriveLiteral)) {
interpreter->errorOutput("Incorrect literal type found for drive: ");
printLiteralCustom(realDriveLiteral, interpreter->errorOutput);
interpreter->errorOutput("\n");
freeLiteral(realDriveLiteral);
freeLiteral(driveLiteral);
deleteRefString(drive);
deleteRefString(path);
deleteRefString(drivePath);
freeLiteral(drivePathLiteral);
return -1;
}
//get the final real file path (concat) TODO: move this concat to refstring library
RefString* realDrive = copyRefString(AS_STRING(realDriveLiteral));
int realLength = lengthRefString(realDrive) + lengthRefString(path);
char* filePath = ALLOCATE(char, realLength) +1 + 1; //+1 for null
snprintf(filePath, realLength, "%s%s", toCString(realDrive), toCString(path));
//clean up the drivepath stuff
FREE_ARRAY(char, filePath, realLength);
deleteRefString(realDrive);
freeLiteral(realDriveLiteral);
freeLiteral(driveLiteral);
deleteRefString(drive);
deleteRefString(path);
deleteRefString(drivePath);
freeLiteral(drivePathLiteral);
//load and compile the bytecode
size_t fileSize = 0;
char* source = readFile(filePath, &fileSize);
if (!source) {
interpreter->errorOutput("Failed to load source file\n");
return -1;
}
unsigned char* bytecode = compileString(source, &fileSize);
free((void*)source);
if (!bytecode) {
interpreter->errorOutput("Failed to compile source file\n");
return -1;
}
//build the runner object
Runner* runner = ALLOCATE(Runner, 1);
initInterpreter(&runner->interpreter);
runner->bytecode = bytecode;
runner->size = fileSize;
//build the opaque object, and push it to the stack
Literal runnerLiteral = TO_OPAQUE_LITERAL(runner, OPAQUE_TAG_RUNNER);
pushLiteralArray(&interpreter->stack, runnerLiteral);
return 1;
}
static int nativeRunScript(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments
if (arguments->count != 0) {
interpreter->errorOutput("Incorrect number of arguments to _runScript\n");
return -1;
}
//
return 1;
}
static int nativeGetScriptVar(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments
if (arguments->count != 0) {
interpreter->errorOutput("Incorrect number of arguments to _getScriptVar\n");
return -1;
}
//
return 1;
}
static int nativeCallScriptFn(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments
if (arguments->count != 0) {
interpreter->errorOutput("Incorrect number of arguments to _callScriptFn\n");
return -1;
}
//
return 1;
}
static int nativeResetScript(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments
if (arguments->count != 0) {
interpreter->errorOutput("Incorrect number of arguments to _resetScript\n");
return -1;
}
//
return 0;
}
static int nativeFreeScript(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments to _freeScript\n");
return -1;
}
//get the runner object
Literal runnerLiteral = popLiteralArray(arguments);
if (IS_IDENTIFIER(runnerLiteral)) {
Literal idn = runnerLiteral;
parseIdentifierToValue(interpreter, &runnerLiteral);
freeLiteral(idn);
}
if (OPAQUE_TAG(runnerLiteral) != OPAQUE_TAG_RUNNER) {
interpreter->errorOutput("Unrecognized opaque literal in _freeScript\n");
return -1;
}
Runner* runner = AS_OPAQUE(runnerLiteral);
//clear out the runner object
freeInterpreter(&runner->interpreter);
FREE_ARRAY(unsigned char, runner->bytecode, runner->size);
FREE(Runner, runner);
freeLiteral(runnerLiteral);
return 0;
}
//call the hook
typedef struct Natives {
char* name;
NativeFn fn;
} Natives;
int hookRunner(Interpreter* interpreter, Literal identifier, Literal alias) {
//build the natives list
Natives natives[] = {
{"loadScript", nativeLoadScript},
{"_runScript", nativeRunScript},
{"_getScriptVar", nativeGetScriptVar},
{"_callScriptFn", nativeCallScriptFn},
{"_resetScript", nativeResetScript},
{"_freeScript", nativeFreeScript},
{NULL, NULL}
};
//store the library in an aliased dictionary
if (!IS_NULL(alias)) {
//make sure the name isn't taken
if (isDelcaredScopeVariable(interpreter->scope, alias)) {
interpreter->errorOutput("Can't override an existing variable\n");
freeLiteral(alias);
return false;
}
//create the dictionary to load up with functions
LiteralDictionary* dictionary = ALLOCATE(LiteralDictionary, 1);
initLiteralDictionary(dictionary);
//load the dict with functions
for (int i = 0; natives[i].name; i++) {
Literal name = TO_STRING_LITERAL(createRefString(natives[i].name));
Literal func = TO_FUNCTION_LITERAL((void*)natives[i].fn, 0);
func.type = LITERAL_FUNCTION_NATIVE;
setLiteralDictionary(dictionary, name, func);
freeLiteral(name);
freeLiteral(func);
}
//build the type
Literal type = TO_TYPE_LITERAL(LITERAL_DICTIONARY, true);
Literal strType = TO_TYPE_LITERAL(LITERAL_STRING, true);
Literal fnType = TO_TYPE_LITERAL(LITERAL_FUNCTION_NATIVE, true);
TYPE_PUSH_SUBTYPE(&type, strType);
TYPE_PUSH_SUBTYPE(&type, fnType);
//set scope
Literal dict = TO_DICTIONARY_LITERAL(dictionary);
declareScopeVariable(interpreter->scope, alias, type);
setScopeVariable(interpreter->scope, alias, dict, false);
//cleanup
freeLiteral(dict);
freeLiteral(type);
return 0;
}
//default
for (int i = 0; natives[i].name; i++) {
injectNativeFn(interpreter, natives[i].name, natives[i].fn);
}
return 0;
}
//file system API
static LiteralDictionary driveDictionary;
void initDriveDictionary() {
initLiteralDictionary(&driveDictionary);
}
void freeDriveDictionary() {
freeLiteralDictionary(&driveDictionary);
}
LiteralDictionary* getDriveDictionary() {
return &driveDictionary;
}

12
repl/lib_runner.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include "interpreter.h"
int hookRunner(Interpreter* interpreter, Literal identifier, Literal alias);
//file system API - these need to be set by the host
void initDriveDictionary();
void freeDriveDictionary();
LiteralDictionary* getDriveDictionary();
#define OPAQUE_TAG_RUNNER 100

View File

@@ -1,6 +1,7 @@
#include "repl_tools.h"
#include "lib_standard.h"
#include "lib_timer.h"
#include "lib_runner.h"
#include "console_colors.h"
@@ -27,6 +28,7 @@ void repl() {
//inject the libs
injectNativeHook(&interpreter, "standard", hookStandard);
injectNativeHook(&interpreter, "timer", hookTimer);
injectNativeHook(&interpreter, "runner", hookRunner);
for(;;) {
printf("> ");
@@ -88,6 +90,17 @@ void repl() {
int main(int argc, const char* argv[]) {
initCommand(argc, argv);
//lib setup (hacky - only really for this program)
initDriveDictionary();
Literal driveLiteral = TO_STRING_LITERAL(createRefString("scripts"));
Literal pathLiteral = TO_STRING_LITERAL(createRefString("scripts"));
setLiteralDictionary(getDriveDictionary(), driveLiteral, pathLiteral);
freeLiteral(driveLiteral);
freeLiteral(pathLiteral);
//command specific actions
if (command.error) {
usageCommand(argc, argv);
@@ -112,12 +125,20 @@ int main(int argc, const char* argv[]) {
//run source file
if (command.sourcefile) {
runSourceFile(command.sourcefile);
//lib cleanup
freeDriveDictionary();
return 0;
}
//run from stdin
if (command.source) {
runSource(command.source);
//lib cleanup
freeDriveDictionary();
return 0;
}
@@ -136,10 +157,17 @@ int main(int argc, const char* argv[]) {
//run binary
if (command.binaryfile) {
runBinaryFile(command.binaryfile);
//lib cleanup
freeDriveDictionary();
return 0;
}
repl();
//lib cleanup
freeDriveDictionary();
return 0;
}

View File

@@ -1,6 +1,7 @@
#include "repl_tools.h"
#include "lib_standard.h"
#include "lib_timer.h"
#include "lib_runner.h"
#include "console_colors.h"
@@ -18,7 +19,7 @@ char* readFile(char* path, size_t* fileSize) {
if (file == NULL) {
fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path);
exit(-1);
return NULL;
}
fseek(file, 0L, SEEK_END);
@@ -29,7 +30,7 @@ char* readFile(char* path, size_t* fileSize) {
if (buffer == NULL) {
fprintf(stderr, ERROR "Not enough memory to read \"%s\"\n" RESET, path);
exit(-1);
return NULL;
}
size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file);
@@ -38,7 +39,7 @@ char* readFile(char* path, size_t* fileSize) {
if (bytesRead < *fileSize) {
fprintf(stderr, ERROR "Could not read file \"%s\"\n" RESET, path);
exit(-1);
return NULL;
}
fclose(file);
@@ -46,22 +47,24 @@ char* readFile(char* path, size_t* fileSize) {
return buffer;
}
void writeFile(char* path, unsigned char* bytes, size_t size) {
int writeFile(char* path, unsigned char* bytes, size_t size) {
FILE* file = fopen(path, "wb");
if (file == NULL) {
fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path);
exit(-1);
return -1;
}
int written = fwrite(bytes, size, 1, file);
if (written != 1) {
fprintf(stderr, ERROR "Could not write file \"%s\"\n" RESET, path);
exit(-1);
return -1;
}
fclose(file);
return 0;
}
//repl functions
@@ -79,7 +82,6 @@ unsigned char* compileString(char* source, size_t* size) {
while(node != NULL) {
//pack up and leave
if (node->type == AST_NODE_ERROR) {
printf(ERROR "error node detected\n" RESET);
freeASTNode(node);
freeCompiler(&compiler);
freeParser(&parser);
@@ -110,6 +112,7 @@ void runBinary(unsigned char* tb, size_t size) {
//inject the libs
injectNativeHook(&interpreter, "standard", hookStandard);
injectNativeHook(&interpreter, "timer", hookTimer);
injectNativeHook(&interpreter, "runner", hookRunner);
runInterpreter(&interpreter, tb, size);
freeInterpreter(&interpreter);

View File

@@ -3,7 +3,7 @@
#include "toy_common.h"
char* readFile(char* path, size_t* fileSize);
void writeFile(char* path, unsigned char* bytes, size_t size);
int writeFile(char* path, unsigned char* bytes, size_t size);
unsigned char* compileString(char* source, size_t* size);