mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Got hooks working
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "lib_builtin.h"
|
||||
#include "builtin.h"
|
||||
|
||||
#include "memory.h"
|
||||
#include "literal.h"
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "interpreter.h"
|
||||
|
||||
int _index(Interpreter* interpreter, LiteralArray* arguments);
|
||||
int _dot(Interpreter* interpreter, LiteralArray* arguments);
|
||||
int _set(Interpreter* interpreter, LiteralArray* arguments);
|
||||
int _get(Interpreter* interpreter, LiteralArray* arguments);
|
||||
int _push(Interpreter* interpreter, LiteralArray* arguments);
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "keyword_types.h"
|
||||
#include "opcodes.h"
|
||||
|
||||
#include "lib_builtin.h"
|
||||
#include "builtin.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -56,6 +56,32 @@ bool injectNativeFn(Interpreter* interpreter, char* name, NativeFn func) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool injectNativeHook(Interpreter* interpreter, char* name, HookFn hook) {
|
||||
//reject reserved words
|
||||
if (findTypeByKeyword(name) != TOKEN_EOF) {
|
||||
interpreter->errorOutput("Can't inject a hook on an existing keyword\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
int identifierLength = strlen(name);
|
||||
Literal identifier = TO_IDENTIFIER_LITERAL(copyString(name, identifierLength), identifierLength);
|
||||
|
||||
//make sure the name isn't taken
|
||||
if (existsLiteralDictionary(interpreter->hooks, identifier)) {
|
||||
interpreter->errorOutput("Can't override an existing hook\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
Literal fn = TO_FUNCTION_LITERAL((void*)hook, 0);
|
||||
fn.type = LITERAL_FUNCTION_NATIVE;
|
||||
|
||||
setLiteralDictionary(interpreter->hooks, identifier, fn);
|
||||
|
||||
freeLiteral(identifier);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void parseCompoundToPureValues(Interpreter* interpreter, Literal* literalPtr) {
|
||||
if (IS_IDENTIFIER(*literalPtr)) {
|
||||
parseIdentifierToValue(interpreter, literalPtr);
|
||||
@@ -1180,6 +1206,7 @@ static bool execFnCall(Interpreter* interpreter, bool looseFirstArgument) {
|
||||
initLiteralArray(&inner.stack);
|
||||
inner.exports = interpreter->exports;
|
||||
inner.exportTypes = interpreter->exportTypes;
|
||||
inner.hooks = interpreter->hooks;
|
||||
setInterpreterPrint(&inner, interpreter->printOutput);
|
||||
setInterpreterAssert(&inner, interpreter->assertOutput);
|
||||
setInterpreterError(&inner, interpreter->errorOutput);
|
||||
@@ -1413,6 +1440,31 @@ static bool execImport(Interpreter* interpreter) {
|
||||
Literal alias = popLiteralArray(&interpreter->stack);
|
||||
Literal identifier = popLiteralArray(&interpreter->stack);
|
||||
|
||||
//access the hooks
|
||||
if (existsLiteralDictionary(interpreter->hooks, identifier)) {
|
||||
Literal func = getLiteralDictionary(interpreter->hooks, identifier);
|
||||
|
||||
if (!IS_FUNCTION_NATIVE(func)) {
|
||||
interpreter->errorOutput("Expected native function for a hook: ");
|
||||
printLiteralCustom(identifier, interpreter->errorOutput);
|
||||
interpreter->errorOutput("\"\n");
|
||||
|
||||
freeLiteral(func);
|
||||
freeLiteral(alias);
|
||||
freeLiteral(identifier);
|
||||
return false;
|
||||
}
|
||||
|
||||
HookFn fn = (HookFn)AS_FUNCTION(func).bytecode;
|
||||
|
||||
fn(interpreter, identifier, alias);
|
||||
|
||||
freeLiteral(func);
|
||||
freeLiteral(alias);
|
||||
freeLiteral(identifier);
|
||||
return true;
|
||||
}
|
||||
|
||||
Literal lit = getLiteralDictionary(interpreter->exports, identifier);
|
||||
Literal type = getLiteralDictionary(interpreter->exportTypes, identifier);
|
||||
|
||||
@@ -2321,6 +2373,8 @@ void initInterpreter(Interpreter* interpreter) {
|
||||
initLiteralDictionary(interpreter->exports);
|
||||
interpreter->exportTypes = ALLOCATE(LiteralDictionary, 1);
|
||||
initLiteralDictionary(interpreter->exportTypes);
|
||||
interpreter->hooks = ALLOCATE(LiteralDictionary, 1);
|
||||
initLiteralDictionary(interpreter->hooks);
|
||||
|
||||
//set up the output streams
|
||||
setInterpreterPrint(interpreter, printWrapper);
|
||||
@@ -2448,7 +2502,12 @@ void freeInterpreter(Interpreter* interpreter) {
|
||||
freeLiteralDictionary(interpreter->exports);
|
||||
FREE(LiteralDictionary, interpreter->exports);
|
||||
interpreter->exports = NULL;
|
||||
|
||||
freeLiteralDictionary(interpreter->exportTypes);
|
||||
FREE(LiteralDictionary, interpreter->exportTypes);
|
||||
interpreter->exportTypes = NULL;
|
||||
|
||||
freeLiteralDictionary(interpreter->hooks);
|
||||
FREE(LiteralDictionary, interpreter->hooks);
|
||||
interpreter->hooks = NULL;
|
||||
}
|
||||
|
||||
@@ -21,9 +21,11 @@ typedef struct Interpreter {
|
||||
Scope* scope;
|
||||
LiteralArray stack;
|
||||
|
||||
//output
|
||||
LiteralDictionary* exports; //read-write - interface with Toy from C - this is a pointer, since it works at a script-level
|
||||
LiteralDictionary* exportTypes;
|
||||
LiteralDictionary* hooks;
|
||||
|
||||
//debug outputs
|
||||
PrintFn printOutput;
|
||||
PrintFn assertOutput;
|
||||
PrintFn errorOutput;
|
||||
@@ -32,10 +34,12 @@ typedef struct Interpreter {
|
||||
bool panic;
|
||||
} Interpreter;
|
||||
|
||||
//native function API
|
||||
//native API
|
||||
typedef int (*NativeFn)(Interpreter* interpreter, LiteralArray* arguments);
|
||||
TOY_API bool injectNativeFn(Interpreter* interpreter, char* name, NativeFn func);
|
||||
//TODO: injectNativeHook
|
||||
|
||||
typedef int (*HookFn)(Interpreter* interpreter, Literal identifier, Literal alias);
|
||||
TOY_API bool injectNativeHook(Interpreter* interpreter, char* name, HookFn hook);
|
||||
|
||||
//utilities for the host program
|
||||
TOY_API bool parseIdentifierToValue(Interpreter* interpreter, Literal* literalPtr);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
CC=gcc
|
||||
|
||||
IDIR +=.
|
||||
CFLAGS +=$(addprefix -I,$(IDIR)) -g -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
|
||||
LIBS +=
|
||||
IDIR+=.
|
||||
CFLAGS+=$(addprefix -I,$(IDIR)) -g -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
|
||||
LIBS+=
|
||||
|
||||
ODIR = obj
|
||||
SRC = $(filter-out $(wildcard *main.c),$(wildcard *.c))
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(addprefix $(ODIR)/,$(SRC:.c=.o))
|
||||
|
||||
OUTNAME=toy
|
||||
@@ -26,17 +26,11 @@ else
|
||||
exit 1
|
||||
endif
|
||||
|
||||
REPLSRC = $(wildcard repl_main.c)
|
||||
REPLOUT = $(OUTNAME)repl.exe
|
||||
|
||||
all: library $(addprefix $(ODIR)/,$(REPLSRC:.c=.o))
|
||||
$(CC) -DTOY_IMPORT $(CFLAGS) -o ../$(OUTDIR)/$(REPLOUT) $(addprefix $(ODIR)/,$(REPLSRC:.c=.o)) $(LIBS) -L$(realpath $(shell pwd)/../$(OUTDIR)) -l$(OUTNAME)
|
||||
|
||||
library: $(OBJ)
|
||||
$(CC) -DTOY_EXPORT $(CFLAGS) -shared -o $(OUT) $(LIBLINE)
|
||||
|
||||
static: $(OBJ) $(addprefix $(ODIR)/,$(REPLSRC:.c=.o))
|
||||
$(CC) $(CFLAGS) -o ../$(OUTDIR)/$(REPLOUT) $(addprefix $(ODIR)/,$(REPLSRC:.c=.o)) $(OBJ) $(LIBS)
|
||||
static: $(OBJ)
|
||||
ar crs ../$(OUTDIR)/lib$(OUTNAME).a $(OBJ)
|
||||
|
||||
$(OBJ): | $(ODIR)
|
||||
|
||||
|
||||
@@ -1,258 +0,0 @@
|
||||
#include "console_colors.h"
|
||||
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "compiler.h"
|
||||
#include "interpreter.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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);
|
||||
}
|
||||
|
||||
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
|
||||
Node* node = scanParser(&parser);
|
||||
while(node != NULL) {
|
||||
//pack up and leave
|
||||
if (node->type == NODE_ERROR) {
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
//DEBUG
|
||||
// for (size_t i = 0; i < size; i++) {
|
||||
// printf("%d, ", tb[i]);
|
||||
// }
|
||||
// printf("\n");
|
||||
|
||||
runBinary(tb, size);
|
||||
}
|
||||
|
||||
void runSourceFile(char* fname) {
|
||||
size_t size = 0; //not used
|
||||
char* source = readFile(fname, &size);
|
||||
runSource(source);
|
||||
free((void*)source);
|
||||
}
|
||||
|
||||
void repl() {
|
||||
//repl does it's own thing for now
|
||||
bool error = false;
|
||||
|
||||
const int size = 2048;
|
||||
char input[size];
|
||||
memset(input, 0, size);
|
||||
|
||||
Interpreter interpreter; //persist the interpreter for the scopes
|
||||
initInterpreter(&interpreter);
|
||||
|
||||
for(;;) {
|
||||
printf("> ");
|
||||
fgets(input, size, stdin);
|
||||
|
||||
//setup this iteration
|
||||
Lexer lexer;
|
||||
Parser parser;
|
||||
Compiler compiler;
|
||||
|
||||
initLexer(&lexer, input);
|
||||
initParser(&parser, &lexer);
|
||||
initCompiler(&compiler);
|
||||
|
||||
//run this iteration
|
||||
Node* node = scanParser(&parser);
|
||||
while(node != NULL) {
|
||||
//pack up and restart
|
||||
if (node->type == NODE_ERROR) {
|
||||
printf(ERROR "error node detected\n" RESET);
|
||||
error = true;
|
||||
freeNode(node);
|
||||
break;
|
||||
}
|
||||
|
||||
writeCompiler(&compiler, node);
|
||||
freeNode(node);
|
||||
node = scanParser(&parser);
|
||||
}
|
||||
|
||||
if (!error) {
|
||||
//get the bytecode dump
|
||||
int size = 0;
|
||||
unsigned char* tb = collateCompiler(&compiler, &size);
|
||||
|
||||
//run the bytecode
|
||||
runInterpreter(&interpreter, tb, size);
|
||||
}
|
||||
|
||||
//clean up this iteration
|
||||
freeCompiler(&compiler);
|
||||
freeParser(&parser);
|
||||
error = false;
|
||||
}
|
||||
|
||||
freeInterpreter(&interpreter);
|
||||
}
|
||||
|
||||
//entry point
|
||||
int main(int argc, const char* argv[]) {
|
||||
initCommand(argc, argv);
|
||||
|
||||
//command specific actions
|
||||
if (command.error) {
|
||||
usageCommand(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (command.help) {
|
||||
helpCommand(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (command.version) {
|
||||
copyrightCommand(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//TODO: remove this when the interpreter meets the specification
|
||||
if (command.verbose) {
|
||||
printf(WARN "Warning! This interpreter is a work in progress, it does not yet meet the %d.%d.%d specification.\n" RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH);
|
||||
}
|
||||
|
||||
//run source file
|
||||
if (command.sourcefile) {
|
||||
runSourceFile(command.sourcefile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//run from stdin
|
||||
if (command.source) {
|
||||
runSource(command.source);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//compile source file
|
||||
if (command.compilefile && command.outfile) {
|
||||
size_t size = 0;
|
||||
char* source = readFile(command.compilefile, &size);
|
||||
unsigned char* tb = compileString(source, &size);
|
||||
if (!tb) {
|
||||
return 1;
|
||||
}
|
||||
writeFile(command.outfile, tb, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//run binary
|
||||
if (command.binaryfile) {
|
||||
runBinaryFile(command.binaryfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
repl();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user