Implemented if statements and made putuint work with 0

This commit is contained in:
cannoli-fruit 2026-07-26 17:00:15 -04:00
commit 441d737cf1
4 changed files with 38 additions and 8 deletions

View file

@ -8,6 +8,9 @@ endproc
proc putuint
@val
if &val 0 == then
48 :putchar return
endif
1 @divisor
while &val &divisor u/ 9 u> do

View file

@ -68,10 +68,21 @@ bool SBeq(Lz_SB *sb, const char *cstr) {
return !memcmp(sb->data, cstr, l);
}
void commentStack(void *stack) {
typedef Lz_DA(int) intstack;
intstack *s = stack;
printf(" ; Stack:");
lz_da_foreach(int, x, *s) {
printf(" %d", *x);
}
printf("\n");
}
static inline bool isKW(Lz_SB *sb) {
return
SBeq(sb, "proc")
|| SBeq(sb, "endproc")
|| SBeq(sb, "return")
|| SBeq(sb, "ld8")
|| SBeq(sb, "st8")
|| SBeq(sb, "dup")
@ -199,6 +210,7 @@ codegen:
//printf("I must gen code i am now Amista Azozin\n");
//fprintf(stderr,"Token type: %d\n", currTokenKind);
//fprintf(stderr,"Tok: "LZ_STR_FMT"\n", LZ_STR_PRINTF(tokenAccumulator));
printf(" ; "LZ_STR_FMT"\n", LZ_STR_PRINTF(tokenAccumulator));
switch (currTokenKind) {
case TOKEN_KEYWORD: {
if (SBeq(&tokenAccumulator, "proc")) {
@ -208,9 +220,18 @@ codegen:
if (SBeq(&tokenAccumulator, "endproc")) {
printf(" Ret\n");
printf("\n\n");
if (labelStack.cnt != 0) {
fprintf(stderr,
"Unmatched statement in function %s\n", funcName);
exit(1);
}
funcName[0] = 0;
break;
}
if (SBeq(&tokenAccumulator, "return")) {
printf(" Ret\n");
break;
}
if (SBeq(&tokenAccumulator, "ld8")) {
printf(" popstack 101\n");
printf(" Ld8 102, 101\n");
@ -238,6 +259,7 @@ codegen:
}
if (SBeq(&tokenAccumulator, "while")) {
lz_da_append(labelStack, counter);
commentStack(&labelStack);
printf("__whilehead_%d:\n", counter);
break;
}
@ -251,24 +273,27 @@ codegen:
}
if (SBeq(&tokenAccumulator, "endwhile")) {
int lbl = lz_da_pop(labelStack);
commentStack(&labelStack);
printf(" Jmp __whilehead_%d\n", lbl);
printf("__whiletail_%d:\n", lbl);
break;
}
if (SBeq(&tokenAccumulator, "if")) {
lz_da_append(labelStack, counter);
commentStack(&labelStack);
break;
}
if (SBeq(&tokenAccumulator, "then")) {
int lbl = labelStack.data[labelStack.cnt-1];
//int lbl = lz_da_pop(labelStack);
//lz_da_append(labelStack, lbl);
commentStack(&labelStack);
int lbl = lz_da_pop(labelStack);
lz_da_append(labelStack, lbl);
printf(" popstack 123\n");
printf(" RSetF 123\n");
printf(" JmpIE __iftail_%d\n", lbl);
break;
}
if (SBeq(&tokenAccumulator, "endif")) {
commentStack(&labelStack);
int lbl = lz_da_pop(labelStack);
printf("__iftail_%d:\n", lbl);
break;

View file

@ -11,4 +11,3 @@ proc main
"Alright, have a nice day" :puts :newline
endif
endproc

View file

@ -2,5 +2,8 @@ $inc fvmstd.fp
proc main
"Hello World!" :puts :newline
23 :putuint
23 :putuint :newline
0 :putuint :newline
69 :putuint :newline
420 :putuint :newline
endproc