diff --git a/repl/repl_main.c b/repl/repl_main.c index 690b666..2f51250 100644 --- a/repl/repl_main.c +++ b/repl/repl_main.c @@ -18,7 +18,7 @@ #define INPUT_BUFFER_SIZE 2048 -void repl() { +void repl(const char* initialInput) { //repl does it's own thing for now bool error = false; @@ -36,11 +36,12 @@ void repl() { // Toy_injectNativeHook(&interpreter, "timer", Toy_hookTimer); for(;;) { - printf("> "); - - //handle EOF for exits - if (!fgets(input, INPUT_BUFFER_SIZE, stdin)) { - break; + if (!initialInput) { + //handle EOF for exits + printf("> "); + if (!fgets(input, INPUT_BUFFER_SIZE, stdin)) { + break; + } } //escape the repl (length of 5 to accomodate the newline) @@ -53,8 +54,8 @@ void repl() { Toy_Parser parser; Toy_Compiler compiler; - Toy_initLexer(&lexer, input); - Toy_private_setComments(&lexer, false); //BUGFIX: disable comments here + Toy_initLexer(&lexer, initialInput ? initialInput : input); + Toy_private_setComments(&lexer, initialInput != NULL); //BUGFIX: disable comments here Toy_initParser(&parser, &lexer); Toy_initCompiler(&compiler); @@ -89,6 +90,11 @@ void repl() { Toy_freeCompiler(&compiler); Toy_freeParser(&parser); error = false; + + if (initialInput) { + free((void*)initialInput); + initialInput = NULL; + } } Toy_freeInterpreter(&interpreter); @@ -204,7 +210,20 @@ int main(int argc, const char* argv[]) { 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 Toy_freeDriveDictionary(); diff --git a/scripts/level.toy b/scripts/level.toy index 5fd6bbd..29479ba 100644 --- a/scripts/level.toy +++ b/scripts/level.toy @@ -22,8 +22,8 @@ var tileset: [int: string] const = [ ]; //variables -var posX: int = 5; -var posY: int = 5; +var posX: int = 4; +var posY: int = 4; //functions fn draw() { @@ -59,3 +59,5 @@ fn move(xrel: int, yrel: int) { draw(); } +//initial display +move(0, 0); \ No newline at end of file diff --git a/source/toy_common.c b/source/toy_common.c index 9b0df00..5275fdc 100644 --- a/source/toy_common.c +++ b/source/toy_common.c @@ -28,6 +28,7 @@ void Toy_initCommandLine(int argc, const char* argv[]) { Toy_commandLine.compilefile = NULL; Toy_commandLine.outfile = "out.tb"; Toy_commandLine.source = NULL; + Toy_commandLine.initialfile = NULL; Toy_commandLine.verbose = false; 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; } + 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 if (i < argc) { 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[]) { - 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[]) { @@ -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("-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("-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[]) { diff --git a/source/toy_common.h b/source/toy_common.h index f2ce327..3b1b415 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -42,6 +42,7 @@ typedef struct { char* compilefile; char* outfile; //defaults to out.tb char* source; + char* initialfile; bool verbose; } Toy_CommandLine;