jack-compiler/misc/os.c

167 lines
4.4 KiB
C
Raw Permalink Normal View History

#include <stdlib.h>
#include <string.h>
#include "os.h"
#include "util.h"
2021-01-03 14:08:54 -05:00
CLASS* mkosclass(CLASS* os, const char* name) {
2020-12-31 12:09:42 -05:00
CLASS* c = (CLASS*)malloc(sizeof(CLASS));
c->name = ezheapstr(name);
c->subroutdecs = NULL;
2021-01-03 14:08:54 -05:00
c->next = os;
2020-12-31 12:09:42 -05:00
return c;
}
2020-12-31 14:49:38 -05:00
void adddec(CLASS* c, SUBROUTCLASS subroutclass, char* type, const char* name) {
SUBROUTDEC* dec = (SUBROUTDEC*)malloc(sizeof(SUBROUTDEC));
2020-12-31 12:09:42 -05:00
dec->class = c;
dec->subroutclass = subroutclass;
dec->name = ezheapstr(name);
2021-01-03 14:08:54 -05:00
dec->type = ezheapstr(type);
2020-12-31 12:09:42 -05:00
dec->next = c->subroutdecs;
c->subroutdecs = dec;
}
2021-01-03 14:08:54 -05:00
CLASS* mkmath(CLASS* os) {
CLASS* mathclass = mkosclass(os, "Math");
2020-12-31 12:09:42 -05:00
adddec(mathclass, function, "int", "multiply");
adddec(mathclass, function, "int", "divide");
2020-12-31 15:38:41 -05:00
adddec(mathclass, function, "int", "abs");
2020-12-31 12:09:42 -05:00
adddec(mathclass, function, "int", "min");
adddec(mathclass, function, "int", "max");
adddec(mathclass, function, "int", "sqrt");
2021-01-03 14:08:54 -05:00
return mathclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkstringclass(CLASS* os) {
CLASS* strclass = mkosclass(os, "String");
2020-12-31 12:09:42 -05:00
adddec(strclass, constructor, "String", "new");
adddec(strclass, method, "int", "dispose");
adddec(strclass, method, "int", "length");
adddec(strclass, method, "char", "charAt");
adddec(strclass, method, "void", "setCharAt");
adddec(strclass, method, "String", "appendChar");
adddec(strclass, method, "void", "eraseLastChar");
adddec(strclass, method, "int", "intValue");
adddec(strclass, method, "void", "setInt");
adddec(strclass, function, "char", "backSpace");
adddec(strclass, function, "char", "doubleQuote");
adddec(strclass, function, "char", "newLine");
2021-01-03 14:08:54 -05:00
return strclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkarray(CLASS* os) {
CLASS* arrclass = mkosclass(os, "Array");
2020-12-31 12:09:42 -05:00
adddec(arrclass, function, "Array", "new");
adddec(arrclass, method, "void", "dispose");
2021-01-03 14:08:54 -05:00
return arrclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkoutput(CLASS* os) {
CLASS* outclass = mkosclass(os, "Output");
2020-12-31 12:09:42 -05:00
adddec(outclass, function, "void", "moveCursor");
adddec(outclass, function, "void", "printChar");
adddec(outclass, function, "void", "printString");
adddec(outclass, function, "void", "printInt");
2020-12-31 15:38:41 -05:00
adddec(outclass, function, "void", "println");
2020-12-31 12:09:42 -05:00
adddec(outclass, function, "void", "backSpace");
2021-01-03 14:08:54 -05:00
return outclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkscreen(CLASS* os) {
CLASS* scrclass = mkosclass(os, "Screen");
2020-12-31 12:09:42 -05:00
adddec(scrclass, function, "void", "clearScreen");
adddec(scrclass, function, "void", "setColor");
adddec(scrclass, function, "void", "drawPixel");
adddec(scrclass, function, "void", "drawLine");
adddec(scrclass, function, "void", "drawRectangle");
adddec(scrclass, function, "void", "drawCircle");
2021-01-03 14:08:54 -05:00
return scrclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkkeyboard(CLASS* os) {
CLASS* kbdclass = mkosclass(os, "Keyboard");
2020-12-31 12:09:42 -05:00
adddec(kbdclass, function, "char", "keyPressed");
adddec(kbdclass, function, "char", "readChar");
adddec(kbdclass, function, "String", "readLine");
adddec(kbdclass, function, "int", "readInt");
2021-01-03 14:08:54 -05:00
return kbdclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkmemory(CLASS* os) {
CLASS* memclass = mkosclass(os, "Memory");
2020-12-31 12:09:42 -05:00
adddec(memclass, function, "int", "peek");
adddec(memclass, function, "void", "poke");
adddec(memclass, function, "Array", "alloc");
adddec(memclass, function, "void", "deAlloc");
2021-01-03 14:08:54 -05:00
return memclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mksys(CLASS* os) {
CLASS* sysclass = mkosclass(os, "Sys");
2020-12-31 12:09:42 -05:00
adddec(sysclass, function, "void", "halt");
adddec(sysclass, function, "void", "error");
adddec(sysclass, function, "void", "wait");
2021-01-03 14:08:54 -05:00
return sysclass;
}
2021-01-03 14:08:54 -05:00
CLASS* mkos() {
CLASS* os = mkmath(NULL);
os = mkstringclass(os);
os = mkarray(os);
os = mkoutput(os);
os = mkscreen(os);
os = mkkeyboard(os);
os = mkmemory(os);
os = mksys(os);
return os;
}
2021-01-03 14:08:54 -05:00
void freeossubroutdecs(SUBROUTDEC* d) {
2020-12-31 12:09:42 -05:00
free(d->name);
free(d->type);
2021-01-03 14:08:54 -05:00
SUBROUTDEC* next = d->next;
free(d);
if(next != NULL)
freeossubroutdecs(next);
2020-12-31 12:09:42 -05:00
}
2021-01-03 14:08:54 -05:00
void freeosclasses(CLASS* c) {
freeossubroutdecs(c->subroutdecs);
2020-12-31 12:09:42 -05:00
free(c->name);
2021-01-03 14:08:54 -05:00
CLASS* next = c->next;
free(c);
if(next != NULL)
freeosclasses(next);
}
2021-01-03 14:08:54 -05:00
void freeos(CLASS* os) {
freeosclasses(os);
2020-12-31 12:09:42 -05:00
}
SUBROUTDEC* getsubroutdecinclass(CLASS* c, const char* name) {
SUBROUTDEC* curr = c->subroutdecs;
while(curr != NULL) {
if(!strcmp(curr->name, name))
return curr;
curr = curr->next;
}
return NULL;
}
2021-01-03 14:08:54 -05:00
CLASS* getosclass(CLASS* os, const char* name) {
CLASS* curr = os;
2020-12-31 12:09:42 -05:00
while(curr != NULL) {
if(!strcmp(curr->name, name))
return curr;
curr = curr->next;
}
return NULL;
}
2021-01-03 14:08:54 -05:00
SUBROUTDEC* getossubroutdec(CLASS* os, SUBROUTCALL* call) {
CLASS* c = getosclass(os, call->parentname);
2020-12-31 12:09:42 -05:00
if(c == NULL)
return NULL;
2020-12-31 14:49:38 -05:00
return getsubroutdecinclass(c, call->name);
}