diff --git a/flagpole/src/main.c b/flagpole/src/main.c index 2a56dcc..6694ea1 100644 --- a/flagpole/src/main.c +++ b/flagpole/src/main.c @@ -69,7 +69,7 @@ bool SBeq(Lz_SB *sb, const char *cstr) { static inline bool isKW(Lz_SB *sb) { return - SBeq(sb, "proc") + SBeq(sb, "proc") || SBeq(sb, "endproc") || SBeq(sb, "ld8") || SBeq(sb, "st8") @@ -84,31 +84,41 @@ static inline bool isKW(Lz_SB *sb) { || SBeq(sb, "u*") || SBeq(sb, "u/") || SBeq(sb, "pop") + || SBeq(sb, "$inc") ; } int main(uint argc, char **argv) { int opt; - FILE *inpfile = stdin; + Lz_DA(FILE *) inpfiles = {}; while ((opt = getopt(argc, argv, "f:")) != -1) { switch(opt) { - case 'f': - inpfile = fopen(optarg, "r"); + case 'f': { + FILE *inpfile = fopen(optarg, "r"); if (!inpfile) { printf("Failed to open file %s\n", optarg); exit(1); } + lz_da_append(inpfiles, inpfile); break; - default: - break; + } + default: break; } } + if (inpfiles.cnt == 0) { + printf("No file requested\n"); + exit(1); + } + + FILE *currFile = lz_da_pop(inpfiles); + TokenKind currTokenKind; Lz_SB tokenAccumulator = {0}; int counter = 0; bool isQuote = false; bool isProc = false; + bool isInclude = false; Lz_DA(int) labelStack = {0}; char newchar; printf("include 'fvm.inc'\n"); @@ -128,7 +138,9 @@ int main(uint argc, char **argv) { printf(" Ld64 reg, STACK_SP\n"); printf(" AddR64 STACK_SP, 124\n"); printf("end macro\n\n"); - while ((newchar = fgetc(inpfile)) != EOF) { + // Genuinely idk why goto is possessing me today +compileStart: + while ((newchar = fgetc(currFile)) != EOF) { ++counter; if (isspace(newchar) && !isQuote) { if (tokenAccumulator.cnt == 0) continue; @@ -255,11 +267,24 @@ codegen: printf(" popstack 113\n"); break; } + if (SBeq(&tokenAccumulator, "$inc")) { + isInclude = true; + break; + } printf("Unimplemented keyword: "LZ_STR_FMT"\n", LZ_STR_PRINTF(tokenAccumulator)); exit(1); } case TOKEN_IDENTIFIER: { + if (isInclude) { + char *fname = malloc(tokenAccumulator.cnt+1); + memcpy(fname, tokenAccumulator.data, tokenAccumulator.cnt); + fname[tokenAccumulator.cnt] = 0; + FILE *f = fopen(fname, "rb"); + lz_da_append(inpfiles, f); + isInclude = false; + break; + } if (isProc) { printf("__fp_user_"LZ_STR_FMT":\n", LZ_STR_PRINTF(tokenAccumulator)); @@ -306,4 +331,13 @@ codegen: } tokenAccumulator.cnt = 0; } + if (inpfiles.cnt != 0) { + currFile = lz_da_pop(inpfiles); + tokenAccumulator.cnt = 0; + isProc = false; + isQuote = false; + isInclude = false; + labelStack.cnt = 0; + goto compileStart; + } } diff --git a/flagpole/tests/stdhw.fp b/flagpole/tests/stdhw.fp new file mode 100644 index 0000000..6d54fe8 --- /dev/null +++ b/flagpole/tests/stdhw.fp @@ -0,0 +1,5 @@ +$inc fvmstd.fp + +proc main + "Hello World!" :puts :newline +endproc diff --git a/flagpole/tests/truthmachine.fp b/flagpole/tests/truthmachine.fp index a08ecb6..158b7a5 100644 --- a/flagpole/tests/truthmachine.fp +++ b/flagpole/tests/truthmachine.fp @@ -1,27 +1,8 @@ -proc putchar - 0xCFFFFFFFFFFFFFFD st8 -endproc - -proc getchar - 0xCFFFFFFFFFFFFFFE ld8 -endproc - -proc puts - while dup ld8 0 == not do - dup ld8 :putchar - 1 u+ - endwhile - pop -endproc - -proc newline - 10 :putchar -endproc +$inc fvmstd.fp proc main :getchar - dup :putchar while dup "1" ld8 == do dup :putchar endwhile -endproc +endproc \ No newline at end of file