Add string compiling

This commit is contained in:
Augusto Gunsch 2020-12-31 14:54:14 -03:00
parent e891326e0b
commit 2e86c2b9e5
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
1 changed files with 36 additions and 4 deletions

View File

@ -152,6 +152,41 @@ LINEBLOCK* compilekeywordconst(SCOPE* s, TERM* t) {
exit(1); exit(1);
} }
char* toascii(char c) {
char* result = (char*)malloc(sizeof(char) * (countplaces(c) + 1));
sprintf(result, "%i", c);
return result;
}
LINEBLOCK* compilestrconst(char* str) {
if(str[0] == '\0')
return NULL;
char* pushchar[] = { "push", "constant", "" };
pushchar[2] = toascii(str[0]);
LINEBLOCK* blk = mklnblk(mksimpleln(pushchar, strcount(pushchar)));
free(pushchar[2]);
char* appendchar[] = { "call", "String.appendChar", "2" };
appendln(blk, mksimpleln(appendchar, strcount(appendchar)));
int i = 1;
char c;
while(c = str[i], c != '\0') {
pushchar[2] = toascii(c);
appendln(blk, mksimpleln(pushchar, strcount(pushchar)));
free(pushchar[2]);
appendln(blk, mksimpleln(appendchar, strcount(appendchar)));
i++;
}
char* strsize[] = { "push", "constant", itoa(i) };
char* mknew[] = { "call", "String.new", "1" };
appendlnbefore(blk, mksimpleln(mknew, strcount(mknew)));
appendlnbefore(blk, mksimpleln(strsize, strcount(strsize)));
free(strsize[2]);
}
LINEBLOCK* compileterm(SCOPE* s, TERM* t) { LINEBLOCK* compileterm(SCOPE* s, TERM* t) {
if(t->type == intconstant) return pushconstant(t->integer); if(t->type == intconstant) return pushconstant(t->integer);
if(t->type == unaryopterm) return pushunaryopterm(s, t); if(t->type == unaryopterm) return pushunaryopterm(s, t);
@ -159,10 +194,7 @@ LINEBLOCK* compileterm(SCOPE* s, TERM* t) {
if(t->type == varname) return pushvar(s, t->string); if(t->type == varname) return pushvar(s, t->string);
if(t->type == subroutcall) return compilesubroutcall(s, t->call); if(t->type == subroutcall) return compilesubroutcall(s, t->call);
if(t->type == keywordconstant) return compilekeywordconst(s, t); if(t->type == keywordconstant) return compilekeywordconst(s, t);
else { if(t->type == stringconstant) return compilestrconst(t->string);
eprintf("Unsupported term yet %i\n", t->type);
exit(1);
}
} }
LINEBLOCK* compileexpression(SCOPE* s, TERM* e) { LINEBLOCK* compileexpression(SCOPE* s, TERM* e) {