Added the -t option to the repl

This commit is contained in:
2023-02-11 14:51:19 +00:00
parent 5343e1054d
commit 77a128e0f7
4 changed files with 43 additions and 12 deletions

View File

@@ -18,7 +18,7 @@
#define INPUT_BUFFER_SIZE 2048 #define INPUT_BUFFER_SIZE 2048
void repl() { void repl(const char* initialInput) {
//repl does it's own thing for now //repl does it's own thing for now
bool error = false; bool error = false;
@@ -36,12 +36,13 @@ void repl() {
// Toy_injectNativeHook(&interpreter, "timer", Toy_hookTimer); // Toy_injectNativeHook(&interpreter, "timer", Toy_hookTimer);
for(;;) { for(;;) {
printf("> "); if (!initialInput) {
//handle EOF for exits //handle EOF for exits
printf("> ");
if (!fgets(input, INPUT_BUFFER_SIZE, stdin)) { if (!fgets(input, INPUT_BUFFER_SIZE, stdin)) {
break; break;
} }
}
//escape the repl (length of 5 to accomodate the newline) //escape the repl (length of 5 to accomodate the newline)
if (strlen(input) == 5 && (!strncmp(input, "exit", 4) || !strncmp(input, "quit", 4))) { if (strlen(input) == 5 && (!strncmp(input, "exit", 4) || !strncmp(input, "quit", 4))) {
@@ -53,8 +54,8 @@ void repl() {
Toy_Parser parser; Toy_Parser parser;
Toy_Compiler compiler; Toy_Compiler compiler;
Toy_initLexer(&lexer, input); Toy_initLexer(&lexer, initialInput ? initialInput : input);
Toy_private_setComments(&lexer, false); //BUGFIX: disable comments here Toy_private_setComments(&lexer, initialInput != NULL); //BUGFIX: disable comments here
Toy_initParser(&parser, &lexer); Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler); Toy_initCompiler(&compiler);
@@ -89,6 +90,11 @@ void repl() {
Toy_freeCompiler(&compiler); Toy_freeCompiler(&compiler);
Toy_freeParser(&parser); Toy_freeParser(&parser);
error = false; error = false;
if (initialInput) {
free((void*)initialInput);
initialInput = NULL;
}
} }
Toy_freeInterpreter(&interpreter); Toy_freeInterpreter(&interpreter);
@@ -204,7 +210,20 @@ int main(int argc, const char* argv[]) {
return 0; return 0;
} }
repl(); const char* initialSource = NULL;
if (Toy_commandLine.initialfile) {
//only works on toy files
const char* s = strrchr(Toy_commandLine.initialfile, '.');
if (!s || strcmp(s, ".toy")) {
fprintf(stderr, TOY_CC_ERROR "Bad file extension passed to %s (expected '.toy', found '%s')" TOY_CC_RESET, argv[0], s);
return -1;
}
size_t size;
initialSource = Toy_readFile(Toy_commandLine.initialfile, &size);
}
repl(initialSource);
//lib cleanup //lib cleanup
Toy_freeDriveDictionary(); Toy_freeDriveDictionary();

View File

@@ -22,8 +22,8 @@ var tileset: [int: string] const = [
]; ];
//variables //variables
var posX: int = 5; var posX: int = 4;
var posY: int = 5; var posY: int = 4;
//functions //functions
fn draw() { fn draw() {
@@ -59,3 +59,5 @@ fn move(xrel: int, yrel: int) {
draw(); draw();
} }
//initial display
move(0, 0);

View File

@@ -28,6 +28,7 @@ void Toy_initCommandLine(int argc, const char* argv[]) {
Toy_commandLine.compilefile = NULL; Toy_commandLine.compilefile = NULL;
Toy_commandLine.outfile = "out.tb"; Toy_commandLine.outfile = "out.tb";
Toy_commandLine.source = NULL; Toy_commandLine.source = NULL;
Toy_commandLine.initialfile = NULL;
Toy_commandLine.verbose = false; Toy_commandLine.verbose = false;
for (int i = 1; i < argc; i++) { //start at 1 to skip the program name for (int i = 1; i < argc; i++) { //start at 1 to skip the program name
@@ -79,6 +80,13 @@ void Toy_initCommandLine(int argc, const char* argv[]) {
continue; continue;
} }
if ((!strcmp(argv[i], "-t") || !strcmp(argv[i], "--initial")) && i + 1 < argc) {
Toy_commandLine.initialfile = (char*)argv[i + 1];
i++;
Toy_commandLine.error = false;
continue;
}
//option without a flag + ending in .tb = binary input //option without a flag + ending in .tb = binary input
if (i < argc) { if (i < argc) {
if (strncmp(&(argv[i][strlen(argv[i]) - 3]), ".tb", 3) == 0) { if (strncmp(&(argv[i][strlen(argv[i]) - 3]), ".tb", 3) == 0) {
@@ -94,7 +102,7 @@ void Toy_initCommandLine(int argc, const char* argv[]) {
} }
void Toy_usageCommandLine(int argc, const char* argv[]) { void Toy_usageCommandLine(int argc, const char* argv[]) {
printf("Usage: %s [ file.tb | -h | -v | [ -d ][ -f file.toy | -i source | -c file.toy [ -o outfile.tb ]]]\n\n", argv[0]); printf("Usage: %s [ file.tb | -h | -v | -d | -f file.toy | -i source | -c file.toy -o out.tb | -t file.toy ]\n\n", argv[0]);
} }
void Toy_helpCommandLine(int argc, const char* argv[]) { void Toy_helpCommandLine(int argc, const char* argv[]) {
@@ -108,6 +116,7 @@ void Toy_helpCommandLine(int argc, const char* argv[]) {
printf("-i\t| --input source\tParse, compile and execute this given string of source code.\n\n"); printf("-i\t| --input source\tParse, compile and execute this given string of source code.\n\n");
printf("-c\t| --compile filename\tParse and compile the specified source file into an output file.\n\n"); printf("-c\t| --compile filename\tParse and compile the specified source file into an output file.\n\n");
printf("-o\t| --output outfile\tName of the output file built with --compile (default: out.tb).\n\n"); printf("-o\t| --output outfile\tName of the output file built with --compile (default: out.tb).\n\n");
printf("-t\t| --initial filename\tStart the repl as normal, after first running the given file.\n\n");
} }
void Toy_copyrightCommandLine(int argc, const char* argv[]) { void Toy_copyrightCommandLine(int argc, const char* argv[]) {

View File

@@ -42,6 +42,7 @@ typedef struct {
char* compilefile; char* compilefile;
char* outfile; //defaults to out.tb char* outfile; //defaults to out.tb
char* source; char* source;
char* initialfile;
bool verbose; bool verbose;
} Toy_CommandLine; } Toy_CommandLine;