Repl working

This commit is contained in:
2022-08-06 08:16:54 +01:00
parent 7a3986af33
commit 3cdf77b85c
2 changed files with 56 additions and 111 deletions

View File

@@ -51,11 +51,6 @@ void initCommand(int argc, const char* argv[]) {
command.error = true; command.error = true;
} }
//no arguments
if (argc == 1) {
command.error = true;
}
} }
void usageCommand(int argc, const char* argv[]) { void usageCommand(int argc, const char* argv[]) {

View File

@@ -45,100 +45,12 @@ char* readFile(char* path) {
return buffer; return buffer;
} }
/*
//run functions
void runString(char* source) { 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; Lexer lexer;
Parser parser; Parser parser;
Compiler compiler; Compiler compiler;
Interpreter interpreter; Interpreter interpreter;
char* source = readFile(command.filename);
initLexer(&lexer, source); initLexer(&lexer, source);
initParser(&parser, &lexer); initParser(&parser, &lexer);
initCompiler(&compiler); initCompiler(&compiler);
@@ -151,7 +63,7 @@ void debug() {
node = scanParser(&parser); node = scanParser(&parser);
} }
//get the data dump //get the bytecode dump
int size = 0; int size = 0;
char* tb = collateCompiler(&compiler, &size); char* tb = collateCompiler(&compiler, &size);
@@ -159,11 +71,63 @@ void debug() {
freeCompiler(&compiler); freeCompiler(&compiler);
freeParser(&parser); freeParser(&parser);
//run the bytecode
initInterpreter(&interpreter, tb, size); initInterpreter(&interpreter, tb, size);
runInterpreter(&interpreter); runInterpreter(&interpreter);
freeInterpreter(&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 //entry point
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
initCommand(argc, argv); initCommand(argc, argv);
@@ -190,30 +154,16 @@ int main(int argc, const char* argv[]) {
} }
if (command.filename) { if (command.filename) {
debug(); runFile(command.filename);
// runFile(command.filename);
return 0; return 0;
} }
if (command.source) { if (command.source) {
// runString(command.source); runString(command.source);
// Lexer lexer;
// initLexer(&lexer, command.source);
// //debugging
// while(true) {
// Token token = scanLexer(&lexer);
// if (token.type == TOKEN_EOF) {
// break;
// }
// }
return 0; return 0;
} }
// repl(); repl();
return 0; return 0;
} }