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

View File

@@ -7,6 +7,8 @@
#include "memory.h"
#include "../repl/repl_tools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -21,41 +23,7 @@ static void noErrorFn(const char* output) {
errorsTriggered++;
}
//compilation functions
char* readFile(char* path, size_t* fileSize) {
FILE* file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path);
exit(-1);
}
fseek(file, 0L, SEEK_END);
*fileSize = ftell(file);
rewind(file);
char* buffer = (char*)malloc(*fileSize + 1);
if (buffer == NULL) {
fprintf(stderr, ERROR "Not enough memory to read \"%s\"\n" RESET, path);
exit(-1);
}
size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file);
buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this
if (bytesRead < *fileSize) {
fprintf(stderr, ERROR "Could not read file \"%s\"\n" RESET, path);
exit(-1);
}
fclose(file);
return buffer;
}
unsigned char* compileString(char* source, size_t* size) {
unsigned char* compileStringCustom(char* source, size_t* size) {
Lexer lexer;
Parser parser;
Compiler compiler;
@@ -69,7 +37,7 @@ unsigned char* compileString(char* source, size_t* size) {
while(node != NULL) {
//pack up and leave
if (node->type == AST_NODE_ERROR) {
errorsTriggered++;
errorsTriggered++; //custom error catch
freeASTNode(node);
freeCompiler(&compiler);
freeParser(&parser);
@@ -93,7 +61,7 @@ unsigned char* compileString(char* source, size_t* size) {
return tb;
}
void runBinary(unsigned char* tb, size_t size) {
void runBinaryCustom(unsigned char* tb, size_t size) {
Interpreter interpreter;
initInterpreter(&interpreter);
@@ -105,19 +73,19 @@ void runBinary(unsigned char* tb, size_t size) {
freeInterpreter(&interpreter);
}
void runSource(char* source) {
void runSourceCustom(char* source) {
size_t size = 0;
unsigned char* tb = compileString(source, &size);
unsigned char* tb = compileStringCustom(source, &size);
if (!tb) {
return;
}
runBinary(tb, size);
runBinaryCustom(tb, size);
}
void runSourceFile(char* fname) {
void runSourceFileCustom(char* fname) {
size_t size = 0; //not used
char* source = readFile(fname, &size);
runSource(source);
runSourceCustom(source);
free((void*)source);
}
@@ -142,7 +110,7 @@ int main() {
char buffer[128];
snprintf(buffer, 128, "scripts/mustfail/%s", filenames[i]);
runSourceFile(buffer);
runSourceFileCustom(buffer);
if (errorsTriggered == 0) {
printf(ERROR "Expected error did not occur in %s\n" RESET, filenames[i]);