From 3cdf77b85c9b0119ece772b4e5238b0b0220c828 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 6 Aug 2022 08:16:54 +0100 Subject: [PATCH] Repl working --- source/common.c | 5 -- source/repl_main.c | 162 ++++++++++++++++----------------------------- 2 files changed, 56 insertions(+), 111 deletions(-) diff --git a/source/common.c b/source/common.c index e669b1c..f01d437 100644 --- a/source/common.c +++ b/source/common.c @@ -51,11 +51,6 @@ void initCommand(int argc, const char* argv[]) { command.error = true; } - - //no arguments - if (argc == 1) { - command.error = true; - } } void usageCommand(int argc, const char* argv[]) { diff --git a/source/repl_main.c b/source/repl_main.c index f2edc56..7fff761 100644 --- a/source/repl_main.c +++ b/source/repl_main.c @@ -45,100 +45,12 @@ char* readFile(char* path) { return buffer; } -/* -//run functions void runString(char* source) { - Lexer lexer; - Parser parser; - Toy toy; - - initLexer(&lexer, source); - initParser(&parser, &lexer); - initToy(&toy); - - Chunk* chunk = scanParser(&parser); - - if (chunk->count > 1 && command.verbose) { - printChunk(chunk, " "); - } - - executeChunk(&toy, chunk); - - freeChunk(chunk); - - freeToy(&toy); - freeParser(&parser); -} - -void runFile(char* fname) { - char* source = readFile(fname); - - runString(source); - - free((void*)source); -} - -void repl() { - const int size = 2048; - char input[size]; - memset(input, 0, size); - - Parser parser; - Toy toy; - - initToy(&toy); - - for(;;) { - printf(">"); - fgets(input, size, stdin); - - //setup - Lexer lexer; - - initLexer(&lexer, input); - initParser(&parser, &lexer); - - //run - Chunk* chunk = scanParser(&parser); - - if (chunk->count > 1 && command.verbose) { - printChunk(chunk, " "); - } - - //clean up the memory - if (parser.error) { - freeChunk(chunk); - freeParser(&parser); - continue; - } - - executeChunk(&toy, chunk); - - if (toy.panic) { - toy.panic = false; - freeChunk(chunk); - freeParser(&parser); - continue; - } - - freeChunk(chunk); - - //cleanup - freeParser(&parser); - } - - freeToy(&toy); -} -*/ - -void debug() { Lexer lexer; Parser parser; Compiler compiler; Interpreter interpreter; - char* source = readFile(command.filename); - initLexer(&lexer, source); initParser(&parser, &lexer); initCompiler(&compiler); @@ -151,7 +63,7 @@ void debug() { node = scanParser(&parser); } - //get the data dump + //get the bytecode dump int size = 0; char* tb = collateCompiler(&compiler, &size); @@ -159,11 +71,63 @@ void debug() { freeCompiler(&compiler); freeParser(&parser); + //run the bytecode initInterpreter(&interpreter, tb, size); runInterpreter(&interpreter); freeInterpreter(&interpreter); } +void runFile(char* fname) { + char* source = readFile(fname); + runString(source); + free((void*)source); +} + +void repl() { + const int size = 2048; + char input[size]; + memset(input, 0, size); + + Interpreter interpreter; //persist the interpreter for the scopes + + 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) { + writeCompiler(&compiler, node); + freeNode(node); + node = scanParser(&parser); + } + + //get the bytecode dump + int size = 0; + char* tb = collateCompiler(&compiler, &size); + + //run the bytecode + initInterpreter(&interpreter, tb, size); + runInterpreter(&interpreter); + freeInterpreter(&interpreter); //TODO: option to retain the scopes + + //clean up this iteration + freeCompiler(&compiler); + freeParser(&parser); + } + + freeInterpreter(&interpreter); +} + //entry point int main(int argc, const char* argv[]) { initCommand(argc, argv); @@ -190,30 +154,16 @@ int main(int argc, const char* argv[]) { } if (command.filename) { - debug(); -// runFile(command.filename); + runFile(command.filename); return 0; } if (command.source) { -// runString(command.source); - - // Lexer lexer; - // initLexer(&lexer, command.source); - - // //debugging - // while(true) { - // Token token = scanLexer(&lexer); - - // if (token.type == TOKEN_EOF) { - // break; - // } - // } - + runString(command.source); return 0; } -// repl(); + repl(); return 0; } \ No newline at end of file