jack-compiler/compiler/compiler-util.c

47 lines
1.0 KiB
C
Raw Normal View History

2020-12-31 18:41:22 -05:00
#include <stdlib.h>
#include "compiler-util.h"
LINE* opvarraw(SCOPE* s, char* op, VAR* v) {
char* tokens[] = { op, v->memsegment, itoa(v->index) };
2020-12-31 18:41:22 -05:00
LINE* ln = mksimpleln(tokens, strcount(tokens));
free(tokens[2]);
return ln;
}
2021-01-06 19:18:52 -05:00
LINE* pushvarraw(SCOPE* s, VAR* v) {
return opvarraw(s, "push", v);
}
2021-01-06 19:18:52 -05:00
LINE* pushvar(SCOPE* s, DEBUGINFO* d, const char* name) {
return opvarraw(s, "push", getvarmustexist(s, d, name));
}
2021-01-06 19:18:52 -05:00
LINE* popvar(SCOPE* s, DEBUGINFO* d, const char* name) {
return opvarraw(s, "pop", getvarmustexist(s, d, name));
}
LINE* poptemp() {
char* poptemp[] = { "pop", "temp", "0" };
return mksimpleln(poptemp, strcount(poptemp));
}
LINE* popthatadd() {
char* popthatadd[] = { "pop", "pointer", "1" };
return mksimpleln(popthatadd, strcount(popthatadd));
}
LINE* onetoken(char* str) {
LINE* ln = mkline(1);
addtoken(ln, ezheapstr(str));
ln->next = NULL;
return ln;
}
LINE* mksimpleln(char** tokens, int count) {
LINE* ln = mkline(count);
for(int i = 0; i < count; i++)
addtoken(ln, ezheapstr(tokens[i]));
ln->next = NULL;
return ln;
}