Implemented if statements and made putuint work with 0
This commit is contained in:
parent
52f97d2fe4
commit
441d737cf1
4 changed files with 38 additions and 8 deletions
|
|
@ -8,16 +8,19 @@ endproc
|
||||||
|
|
||||||
proc putuint
|
proc putuint
|
||||||
@val
|
@val
|
||||||
|
if &val 0 == then
|
||||||
|
48 :putchar return
|
||||||
|
endif
|
||||||
1 @divisor
|
1 @divisor
|
||||||
|
|
||||||
while &val &divisor u/ 9 u> do
|
while &val &divisor u/ 9 u> do
|
||||||
10 &divisor u* @divisor
|
10 &divisor u* @divisor
|
||||||
endwhile
|
endwhile
|
||||||
|
|
||||||
while &divisor 0 u> do
|
while &divisor 0 u> do
|
||||||
&val &divisor u/ 10 u%
|
&val &divisor u/ 10 u%
|
||||||
48 u+ :putchar
|
48 u+ :putchar
|
||||||
|
|
||||||
&divisor 10 u/ @divisor
|
&divisor 10 u/ @divisor
|
||||||
endwhile
|
endwhile
|
||||||
endproc
|
endproc
|
||||||
|
|
|
||||||
|
|
@ -68,10 +68,21 @@ bool SBeq(Lz_SB *sb, const char *cstr) {
|
||||||
return !memcmp(sb->data, cstr, l);
|
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) {
|
static inline bool isKW(Lz_SB *sb) {
|
||||||
return
|
return
|
||||||
SBeq(sb, "proc")
|
SBeq(sb, "proc")
|
||||||
|| SBeq(sb, "endproc")
|
|| SBeq(sb, "endproc")
|
||||||
|
|| SBeq(sb, "return")
|
||||||
|| SBeq(sb, "ld8")
|
|| SBeq(sb, "ld8")
|
||||||
|| SBeq(sb, "st8")
|
|| SBeq(sb, "st8")
|
||||||
|| SBeq(sb, "dup")
|
|| SBeq(sb, "dup")
|
||||||
|
|
@ -199,6 +210,7 @@ codegen:
|
||||||
//printf("I must gen code i am now Amista Azozin\n");
|
//printf("I must gen code i am now Amista Azozin\n");
|
||||||
//fprintf(stderr,"Token type: %d\n", currTokenKind);
|
//fprintf(stderr,"Token type: %d\n", currTokenKind);
|
||||||
//fprintf(stderr,"Tok: "LZ_STR_FMT"\n", LZ_STR_PRINTF(tokenAccumulator));
|
//fprintf(stderr,"Tok: "LZ_STR_FMT"\n", LZ_STR_PRINTF(tokenAccumulator));
|
||||||
|
printf(" ; "LZ_STR_FMT"\n", LZ_STR_PRINTF(tokenAccumulator));
|
||||||
switch (currTokenKind) {
|
switch (currTokenKind) {
|
||||||
case TOKEN_KEYWORD: {
|
case TOKEN_KEYWORD: {
|
||||||
if (SBeq(&tokenAccumulator, "proc")) {
|
if (SBeq(&tokenAccumulator, "proc")) {
|
||||||
|
|
@ -208,9 +220,18 @@ codegen:
|
||||||
if (SBeq(&tokenAccumulator, "endproc")) {
|
if (SBeq(&tokenAccumulator, "endproc")) {
|
||||||
printf(" Ret\n");
|
printf(" Ret\n");
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
if (labelStack.cnt != 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Unmatched statement in function %s\n", funcName);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
funcName[0] = 0;
|
funcName[0] = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (SBeq(&tokenAccumulator, "return")) {
|
||||||
|
printf(" Ret\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (SBeq(&tokenAccumulator, "ld8")) {
|
if (SBeq(&tokenAccumulator, "ld8")) {
|
||||||
printf(" popstack 101\n");
|
printf(" popstack 101\n");
|
||||||
printf(" Ld8 102, 101\n");
|
printf(" Ld8 102, 101\n");
|
||||||
|
|
@ -238,6 +259,7 @@ codegen:
|
||||||
}
|
}
|
||||||
if (SBeq(&tokenAccumulator, "while")) {
|
if (SBeq(&tokenAccumulator, "while")) {
|
||||||
lz_da_append(labelStack, counter);
|
lz_da_append(labelStack, counter);
|
||||||
|
commentStack(&labelStack);
|
||||||
printf("__whilehead_%d:\n", counter);
|
printf("__whilehead_%d:\n", counter);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -251,24 +273,27 @@ codegen:
|
||||||
}
|
}
|
||||||
if (SBeq(&tokenAccumulator, "endwhile")) {
|
if (SBeq(&tokenAccumulator, "endwhile")) {
|
||||||
int lbl = lz_da_pop(labelStack);
|
int lbl = lz_da_pop(labelStack);
|
||||||
|
commentStack(&labelStack);
|
||||||
printf(" Jmp __whilehead_%d\n", lbl);
|
printf(" Jmp __whilehead_%d\n", lbl);
|
||||||
printf("__whiletail_%d:\n", lbl);
|
printf("__whiletail_%d:\n", lbl);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (SBeq(&tokenAccumulator, "if")) {
|
if (SBeq(&tokenAccumulator, "if")) {
|
||||||
lz_da_append(labelStack, counter);
|
lz_da_append(labelStack, counter);
|
||||||
|
commentStack(&labelStack);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (SBeq(&tokenAccumulator, "then")) {
|
if (SBeq(&tokenAccumulator, "then")) {
|
||||||
int lbl = labelStack.data[labelStack.cnt-1];
|
commentStack(&labelStack);
|
||||||
//int lbl = lz_da_pop(labelStack);
|
int lbl = lz_da_pop(labelStack);
|
||||||
//lz_da_append(labelStack, lbl);
|
lz_da_append(labelStack, lbl);
|
||||||
printf(" popstack 123\n");
|
printf(" popstack 123\n");
|
||||||
printf(" RSetF 123\n");
|
printf(" RSetF 123\n");
|
||||||
printf(" JmpIE __iftail_%d\n", lbl);
|
printf(" JmpIE __iftail_%d\n", lbl);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (SBeq(&tokenAccumulator, "endif")) {
|
if (SBeq(&tokenAccumulator, "endif")) {
|
||||||
|
commentStack(&labelStack);
|
||||||
int lbl = lz_da_pop(labelStack);
|
int lbl = lz_da_pop(labelStack);
|
||||||
printf("__iftail_%d:\n", lbl);
|
printf("__iftail_%d:\n", lbl);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -11,4 +11,3 @@ proc main
|
||||||
"Alright, have a nice day" :puts :newline
|
"Alright, have a nice day" :puts :newline
|
||||||
endif
|
endif
|
||||||
endproc
|
endproc
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,8 @@ $inc fvmstd.fp
|
||||||
|
|
||||||
proc main
|
proc main
|
||||||
"Hello World!" :puts :newline
|
"Hello World!" :puts :newline
|
||||||
23 :putuint
|
23 :putuint :newline
|
||||||
|
0 :putuint :newline
|
||||||
|
69 :putuint :newline
|
||||||
|
420 :putuint :newline
|
||||||
endproc
|
endproc
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue