Moved compilation functions into repl_tools
This commit is contained in:
2
Toy
2
Toy
Submodule Toy updated: 5686677383...8b04939430
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "lib_engine.h"
|
#include "lib_engine.h"
|
||||||
#include "lib_standard.h"
|
#include "lib_standard.h"
|
||||||
|
#include "repl_tools.h"
|
||||||
|
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
@@ -24,79 +25,6 @@ static void fatalError(char* message) {
|
|||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//compilation functions
|
|
||||||
//TODO: move these to their own file
|
|
||||||
static 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned char* compileString(char* source, size_t* size) {
|
|
||||||
Lexer lexer;
|
|
||||||
Parser parser;
|
|
||||||
Compiler compiler;
|
|
||||||
|
|
||||||
initLexer(&lexer, source);
|
|
||||||
initParser(&parser, &lexer);
|
|
||||||
initCompiler(&compiler);
|
|
||||||
|
|
||||||
//run the parser until the end of the source
|
|
||||||
ASTNode* node = scanParser(&parser);
|
|
||||||
while(node != NULL) {
|
|
||||||
//pack up and leave
|
|
||||||
if (node->type == AST_NODEERROR) {
|
|
||||||
printf(ERROR "error node detected\n" RESET);
|
|
||||||
freeNode(node);
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeParser(&parser);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
writeCompiler(&compiler, node);
|
|
||||||
freeNode(node);
|
|
||||||
node = scanParser(&parser);
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the bytecode dump
|
|
||||||
unsigned char* tb = collateCompiler(&compiler, (int*)(size));
|
|
||||||
|
|
||||||
//cleanup
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeParser(&parser);
|
|
||||||
//no lexer to clean up
|
|
||||||
|
|
||||||
//finally
|
|
||||||
return tb;
|
|
||||||
}
|
|
||||||
|
|
||||||
//exposed functions
|
//exposed functions
|
||||||
void initEngine() {
|
void initEngine() {
|
||||||
//clear
|
//clear
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "lib_engine.h"
|
#include "lib_engine.h"
|
||||||
|
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
|
#include "repl_tools.h"
|
||||||
|
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "literal_array.h"
|
#include "literal_array.h"
|
||||||
@@ -11,85 +12,6 @@ static void fatalError(char* message) {
|
|||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//compilation functions
|
|
||||||
//TODO: move these to their own file
|
|
||||||
#include "console_colors.h"
|
|
||||||
#include "lexer.h"
|
|
||||||
#include "parser.h"
|
|
||||||
#include "compiler.h"
|
|
||||||
#include "interpreter.h"
|
|
||||||
|
|
||||||
static 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned char* compileString(char* source, size_t* size) {
|
|
||||||
Lexer lexer;
|
|
||||||
Parser parser;
|
|
||||||
Compiler compiler;
|
|
||||||
|
|
||||||
initLexer(&lexer, source);
|
|
||||||
initParser(&parser, &lexer);
|
|
||||||
initCompiler(&compiler);
|
|
||||||
|
|
||||||
//run the parser until the end of the source
|
|
||||||
ASTNode* node = scanParser(&parser);
|
|
||||||
while(node != NULL) {
|
|
||||||
//pack up and leave
|
|
||||||
if (node->type == AST_NODEERROR) {
|
|
||||||
printf(ERROR "error node detected\n" RESET);
|
|
||||||
freeNode(node);
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeParser(&parser);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
writeCompiler(&compiler, node);
|
|
||||||
freeNode(node);
|
|
||||||
node = scanParser(&parser);
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the bytecode dump
|
|
||||||
unsigned char* tb = collateCompiler(&compiler, (int*)(size));
|
|
||||||
|
|
||||||
//cleanup
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeParser(&parser);
|
|
||||||
//no lexer to clean up
|
|
||||||
|
|
||||||
//finally
|
|
||||||
return tb;
|
|
||||||
}
|
|
||||||
|
|
||||||
//native functions to be called
|
//native functions to be called
|
||||||
static int nativeInitWindow(Interpreter* interpreter, LiteralArray* arguments) {
|
static int nativeInitWindow(Interpreter* interpreter, LiteralArray* arguments) {
|
||||||
if (engine.window != NULL) {
|
if (engine.window != NULL) {
|
||||||
|
|||||||
141
core/repl_tools.c
Normal file
141
core/repl_tools.c
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
#include "repl_tools.h"
|
||||||
|
#include "lib_standard.h"
|
||||||
|
|
||||||
|
#include "console_colors.h"
|
||||||
|
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "compiler.h"
|
||||||
|
#include "interpreter.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
//IO 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
int written = fwrite(bytes, size, 1, file);
|
||||||
|
|
||||||
|
if (written != 1) {
|
||||||
|
fprintf(stderr, ERROR "Could not write file \"%s\"\n" RESET, path);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
//repl functions
|
||||||
|
unsigned char* compileString(char* source, size_t* size) {
|
||||||
|
Lexer lexer;
|
||||||
|
Parser parser;
|
||||||
|
Compiler compiler;
|
||||||
|
|
||||||
|
initLexer(&lexer, source);
|
||||||
|
initParser(&parser, &lexer);
|
||||||
|
initCompiler(&compiler);
|
||||||
|
|
||||||
|
//run the parser until the end of the source
|
||||||
|
ASTNode* node = scanParser(&parser);
|
||||||
|
while(node != NULL) {
|
||||||
|
//pack up and leave
|
||||||
|
if (node->type == AST_NODEERROR) {
|
||||||
|
printf(ERROR "error node detected\n" RESET);
|
||||||
|
freeNode(node);
|
||||||
|
freeCompiler(&compiler);
|
||||||
|
freeParser(&parser);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCompiler(&compiler, node);
|
||||||
|
freeNode(node);
|
||||||
|
node = scanParser(&parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
//get the bytecode dump
|
||||||
|
unsigned char* tb = collateCompiler(&compiler, (int*)(size));
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
freeCompiler(&compiler);
|
||||||
|
freeParser(&parser);
|
||||||
|
//no lexer to clean up
|
||||||
|
|
||||||
|
//finally
|
||||||
|
return tb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void runBinary(unsigned char* tb, size_t size) {
|
||||||
|
Interpreter interpreter;
|
||||||
|
initInterpreter(&interpreter);
|
||||||
|
|
||||||
|
//inject the libs
|
||||||
|
injectNativeHook(&interpreter, "standard", hookStandard);
|
||||||
|
|
||||||
|
runInterpreter(&interpreter, tb, size);
|
||||||
|
freeInterpreter(&interpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runBinaryFile(char* fname) {
|
||||||
|
size_t size = 0; //not used
|
||||||
|
unsigned char* tb = (unsigned char*)readFile(fname, &size);
|
||||||
|
if (!tb) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
runBinary(tb, size);
|
||||||
|
//interpreter takes ownership of the binary data
|
||||||
|
}
|
||||||
|
|
||||||
|
void runSource(char* source) {
|
||||||
|
size_t size = 0;
|
||||||
|
unsigned char* tb = compileString(source, &size);
|
||||||
|
if (!tb) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
runBinary(tb, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runSourceFile(char* fname) {
|
||||||
|
size_t size = 0; //not used
|
||||||
|
char* source = readFile(fname, &size);
|
||||||
|
runSource(source);
|
||||||
|
free((void*)source);
|
||||||
|
}
|
||||||
14
core/repl_tools.h
Normal file
14
core/repl_tools.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
char* readFile(char* path, size_t* fileSize);
|
||||||
|
void writeFile(char* path, unsigned char* bytes, size_t size);
|
||||||
|
|
||||||
|
unsigned char* compileString(char* source, size_t* size);
|
||||||
|
|
||||||
|
void runBinary(unsigned char* tb, size_t size);
|
||||||
|
void runBinaryFile(char* fname);
|
||||||
|
void runSource(char* source);
|
||||||
|
void runSourceFile(char* fname);
|
||||||
|
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "interpreter.h"
|
#include "interpreter.h"
|
||||||
|
#include "repl_tools.h"
|
||||||
#include "console_colors.h"
|
#include "console_colors.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -15,78 +16,6 @@ static void noPrintFn(const char* output) {
|
|||||||
//NO OP
|
//NO OP
|
||||||
}
|
}
|
||||||
|
|
||||||
//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) {
|
|
||||||
Lexer lexer;
|
|
||||||
Parser parser;
|
|
||||||
Compiler compiler;
|
|
||||||
|
|
||||||
initLexer(&lexer, source);
|
|
||||||
initParser(&parser, &lexer);
|
|
||||||
initCompiler(&compiler);
|
|
||||||
|
|
||||||
//run the parser until the end of the source
|
|
||||||
ASTNode* node = scanParser(&parser);
|
|
||||||
while(node != NULL) {
|
|
||||||
//pack up and leave
|
|
||||||
if (node->type == AST_NODEERROR) {
|
|
||||||
printf(ERROR "error node detected\n" RESET);
|
|
||||||
freeNode(node);
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeParser(&parser);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
writeCompiler(&compiler, node);
|
|
||||||
freeNode(node);
|
|
||||||
node = scanParser(&parser);
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the bytecode dump
|
|
||||||
unsigned char* tb = collateCompiler(&compiler, (int*)(size));
|
|
||||||
|
|
||||||
//cleanup
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeParser(&parser);
|
|
||||||
//no lexer to clean up
|
|
||||||
|
|
||||||
//finally
|
|
||||||
return tb;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{
|
{
|
||||||
//setup interpreter
|
//setup interpreter
|
||||||
|
|||||||
Reference in New Issue
Block a user