Repl works, also fixed a few small bugs

This commit is contained in:
2025-01-31 10:16:06 +11:00
parent 002651f95d
commit bfed4e23f3
5 changed files with 43 additions and 25 deletions

View File

@@ -1,4 +1,9 @@
#include "toy.h" #include "toy_console_colors.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_module_builder.h"
#include "toy_vm.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -111,21 +116,21 @@ static void printCallback(const char* msg) {
} }
static void errorAndExitCallback(const char* msg) { static void errorAndExitCallback(const char* msg) {
fprintf(stderr, "Error: %s\n", msg); fprintf(stderr, TOY_CC_ERROR "Error: %s\n" TOY_CC_RESET, msg);
exit(-1); exit(-1);
} }
static void errorAndContinueCallback(const char* msg) { static void errorAndContinueCallback(const char* msg) {
fprintf(stderr, "Error: %s\n", msg); fprintf(stderr, TOY_CC_ERROR "Error: %s\n" TOY_CC_RESET, msg);
} }
static void assertFailureAndExitCallback(const char* msg) { static void assertFailureAndExitCallback(const char* msg) {
fprintf(stderr, "Assert Failure: %s\n", msg); fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s\n" TOY_CC_RESET, msg);
exit(-1); exit(-1);
} }
static void assertFailureAndContinueCallback(const char* msg) { static void assertFailureAndContinueCallback(const char* msg) {
fprintf(stderr, "Assert Failure: %s\n", msg); fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s\n" TOY_CC_RESET, msg);
} }
static void noOpCallback(const char* msg) { static void noOpCallback(const char* msg) {
@@ -222,7 +227,7 @@ CmdLine parseCmdLine(int argc, const char* argv[]) {
} }
else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")) { else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")) {
if (argc < i + 1) { if (argc <= i + 1) {
cmd.error = true; cmd.error = true;
} }
else { else {
@@ -291,7 +296,7 @@ int repl(const char* filepath) {
Toy_VM vm; Toy_VM vm;
Toy_initVM(&vm); Toy_initVM(&vm);
printf("%s> ", prompt); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt and begin
//read from the terminal //read from the terminal
while(fgets(inputBuffer, INPUT_BUFFER_SIZE, stdin)) { while(fgets(inputBuffer, INPUT_BUFFER_SIZE, stdin)) {
@@ -301,8 +306,8 @@ int repl(const char* filepath) {
inputBuffer[--length] = '\0'; inputBuffer[--length] = '\0';
} }
if (length == 0) { if (length == 0 || !inputBuffer[ strspn(inputBuffer, " \r\n\t") ]) {
printf("%s> ", prompt); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt and restart
continue; continue;
} }
@@ -311,7 +316,7 @@ int repl(const char* filepath) {
break; break;
} }
//parse the input, prep the VM for run //parse the input, prep the VM for execution
Toy_Lexer lexer; Toy_Lexer lexer;
Toy_bindLexer(&lexer, inputBuffer); Toy_bindLexer(&lexer, inputBuffer);
Toy_Parser parser; Toy_Parser parser;
@@ -325,15 +330,16 @@ int repl(const char* filepath) {
continue; continue;
} }
Toy_ModuleBundle bc = Toy_compileModuleBundle(ast); void* buffer = Toy_compileModuleBuilder(ast);
Toy_bindVM(&vm, &bc); Toy_Module module = Toy_parseModule(buffer);
Toy_bindVM(&vm, &module);
//run //run
Toy_runVM(&vm); Toy_runVM(&vm);
//free the bytecode, and leave the VM ready for the next loop //free the memory, and leave the VM ready for the next loop
Toy_resetVM(&vm); Toy_resetVM(&vm);
Toy_freeModuleBundle(bc); free(buffer);
printf("%s> ", prompt); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt
} }
@@ -435,7 +441,7 @@ int main(int argc, const char* argv[]) {
versionCmdLine(argc, argv); versionCmdLine(argc, argv);
} }
else if (cmd.infile != NULL) { else if (cmd.infile != NULL) {
//run the given file //read the source file
int size; int size;
unsigned char* source = readFile(cmd.infile, &size); unsigned char* source = readFile(cmd.infile, &size);
@@ -462,6 +468,7 @@ int main(int argc, const char* argv[]) {
cmd.infile = NULL; cmd.infile = NULL;
cmd.infileLength = 0; cmd.infileLength = 0;
//compile the source code
Toy_Lexer lexer; Toy_Lexer lexer;
Toy_bindLexer(&lexer, (char*)source); Toy_bindLexer(&lexer, (char*)source);
@@ -472,15 +479,17 @@ int main(int argc, const char* argv[]) {
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Ast* ast = Toy_scanParser(&bucket, &parser); Toy_Ast* ast = Toy_scanParser(&bucket, &parser);
void* buffer = Toy_compileModuleBuilder(ast);
Toy_freeBucket(&bucket);
free(source);
Toy_ModuleBundle bc = Toy_compileModuleBundle(ast); //run the compiled code
//run the setup
Toy_VM vm; Toy_VM vm;
Toy_initVM(&vm); Toy_initVM(&vm);
Toy_bindVM(&vm, &bc);
//run Toy_Module module = Toy_parseModule(buffer);
Toy_bindVM(&vm, &module);
Toy_runVM(&vm); Toy_runVM(&vm);
//print the debug info //print the debug info
@@ -491,9 +500,7 @@ int main(int argc, const char* argv[]) {
//cleanup //cleanup
Toy_freeVM(&vm); Toy_freeVM(&vm);
Toy_freeModuleBundle(bc); free(buffer);
Toy_freeBucket(&bucket);
free(source);
} }
else { else {
repl(argv[0]); repl(argv[0]);

View File

@@ -41,6 +41,7 @@ NOTE: you need both font AND background for these to work
#define TOY_CC_NOTICE TOY_CC_FONT_GREEN TOY_CC_BACK_BLACK #define TOY_CC_NOTICE TOY_CC_FONT_GREEN TOY_CC_BACK_BLACK
#define TOY_CC_WARN TOY_CC_FONT_YELLOW TOY_CC_BACK_BLACK #define TOY_CC_WARN TOY_CC_FONT_YELLOW TOY_CC_BACK_BLACK
#define TOY_CC_ERROR TOY_CC_FONT_RED TOY_CC_BACK_BLACK #define TOY_CC_ERROR TOY_CC_FONT_RED TOY_CC_BACK_BLACK
#define TOY_CC_ASSERT TOY_CC_FONT_PURPLE TOY_CC_BACK_BLACK
#define TOY_CC_RESET "\033[0m" #define TOY_CC_RESET "\033[0m"
//for unsupported platforms, these become no-ops //for unsupported platforms, these become no-ops
@@ -72,6 +73,7 @@ NOTE: you need both font AND background for these to work
#define TOY_CC_NOTICE TOY_CC_FONT_GREEN TOY_CC_BACK_BLACK #define TOY_CC_NOTICE TOY_CC_FONT_GREEN TOY_CC_BACK_BLACK
#define TOY_CC_WARN TOY_CC_FONT_YELLOW TOY_CC_BACK_BLACK #define TOY_CC_WARN TOY_CC_FONT_YELLOW TOY_CC_BACK_BLACK
#define TOY_CC_ERROR TOY_CC_FONT_RED TOY_CC_BACK_BLACK #define TOY_CC_ERROR TOY_CC_FONT_RED TOY_CC_BACK_BLACK
#define TOY_CC_ASSERT TOY_CC_FONT_PURPLE TOY_CC_BACK_BLACK
#define TOY_CC_RESET #define TOY_CC_RESET
#endif #endif

View File

@@ -8,6 +8,10 @@ static inline unsigned int readUnsignedInt(unsigned char** handle) {
} }
Toy_Module Toy_parseModule(unsigned char* ptr) { Toy_Module Toy_parseModule(unsigned char* ptr) {
if (ptr == NULL) {
return (Toy_Module){ 0 };
}
Toy_Module module; Toy_Module module;
module.scopePtr = NULL; module.scopePtr = NULL;

View File

@@ -982,7 +982,7 @@ void Toy_inheritVM(Toy_VM* vm, Toy_VM* parent) {
Toy_resetVM(vm); Toy_resetVM(vm);
} }
void Toy_bindVMToModule(Toy_VM* vm, Toy_Module* module) { void Toy_bindVM(Toy_VM* vm, Toy_Module* module) {
vm->code = module->code; vm->code = module->code;
vm->jumpsCount = module->jumpsCount; vm->jumpsCount = module->jumpsCount;
@@ -1000,6 +1000,11 @@ void Toy_bindVMToModule(Toy_VM* vm, Toy_Module* module) {
} }
void Toy_runVM(Toy_VM* vm) { void Toy_runVM(Toy_VM* vm) {
if (vm->codeAddr == 0) {
//ignore uninitialized VMs or empty modules
return;
}
//TODO: read params into scope //TODO: read params into scope
//prep the program counter for execution //prep the program counter for execution

View File

@@ -47,7 +47,7 @@ TOY_API void Toy_resetVM(Toy_VM* vm); //persists memory
TOY_API void Toy_initVM(Toy_VM* vm); //creates memory TOY_API void Toy_initVM(Toy_VM* vm); //creates memory
TOY_API void Toy_inheritVM(Toy_VM* vm, Toy_VM* parent); //inherits memory TOY_API void Toy_inheritVM(Toy_VM* vm, Toy_VM* parent); //inherits memory
TOY_API void Toy_bindVMToModule(Toy_VM* vm, Toy_Module* module); TOY_API void Toy_bindVM(Toy_VM* vm, Toy_Module* module);
TOY_API void Toy_runVM(Toy_VM* vm); TOY_API void Toy_runVM(Toy_VM* vm);
TOY_API void Toy_freeVM(Toy_VM* vm); TOY_API void Toy_freeVM(Toy_VM* vm);