Updated Toy

This commit is contained in:
2023-02-10 09:02:47 +00:00
parent bc3013b48b
commit c23502537f
16 changed files with 1246 additions and 50 deletions

View File

@@ -1,4 +1,6 @@
#include "repl_tools.h"
#include "lib_about.h"
#include "lib_compound.h"
#include "lib_standard.h"
#include "lib_timer.h"
#include "lib_runner.h"
@@ -14,7 +16,7 @@
#include <stdlib.h>
//IO functions
char* Toy_readFile(char* path, size_t* fileSize) {
const char* Toy_readFile(const char* path, size_t* fileSize) {
FILE* file = fopen(path, "rb");
if (file == NULL) {
@@ -47,7 +49,7 @@ char* Toy_readFile(char* path, size_t* fileSize) {
return buffer;
}
int Toy_writeFile(char* path, unsigned char* bytes, size_t size) {
int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size) {
FILE* file = fopen(path, "wb");
if (file == NULL) {
@@ -68,7 +70,7 @@ int Toy_writeFile(char* path, unsigned char* bytes, size_t size) {
}
//repl functions
unsigned char* Toy_compileString(char* source, size_t* size) {
const unsigned char* Toy_compileString(const char* source, size_t* size) {
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
@@ -94,7 +96,7 @@ unsigned char* Toy_compileString(char* source, size_t* size) {
}
//get the bytecode dump
unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
//cleanup
Toy_freeCompiler(&compiler);
@@ -105,11 +107,13 @@ unsigned char* Toy_compileString(char* source, size_t* size) {
return tb;
}
void Toy_runBinary(unsigned char* tb, size_t size) {
void Toy_runBinary(const unsigned char* tb, size_t size) {
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
//inject the libs
Toy_injectNativeHook(&interpreter, "about", Toy_hookAbout);
Toy_injectNativeHook(&interpreter, "compound", Toy_hookCompound);
Toy_injectNativeHook(&interpreter, "standard", Toy_hookStandard);
Toy_injectNativeHook(&interpreter, "timer", Toy_hookTimer);
Toy_injectNativeHook(&interpreter, "runner", Toy_hookRunner);
@@ -118,9 +122,9 @@ void Toy_runBinary(unsigned char* tb, size_t size) {
Toy_freeInterpreter(&interpreter);
}
void Toy_runBinaryFile(char* fname) {
void Toy_runBinaryFile(const char* fname) {
size_t size = 0; //not used
unsigned char* tb = (unsigned char*)Toy_readFile(fname, &size);
const unsigned char* tb = (const unsigned char*)Toy_readFile(fname, &size);
if (!tb) {
return;
}
@@ -128,9 +132,9 @@ void Toy_runBinaryFile(char* fname) {
//interpreter takes ownership of the binary data
}
void Toy_runSource(char* source) {
void Toy_runSource(const char* source) {
size_t size = 0;
unsigned char* tb = Toy_compileString(source, &size);
const unsigned char* tb = Toy_compileString(source, &size);
if (!tb) {
return;
}
@@ -138,9 +142,12 @@ void Toy_runSource(char* source) {
Toy_runBinary(tb, size);
}
void Toy_runSourceFile(char* fname) {
void Toy_runSourceFile(const char* fname) {
size_t size = 0; //not used
char* source = Toy_readFile(fname, &size);
const char* source = Toy_readFile(fname, &size);
if (!source) {
return;
}
Toy_runSource(source);
free((void*)source);
}