Add os classes

This commit is contained in:
Augusto Gunsch 2020-12-31 14:09:42 -03:00
parent ffa4b8a5c8
commit e891326e0b
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
3 changed files with 99 additions and 102 deletions

View File

@ -113,7 +113,7 @@ CLASS* getclass(SCOPE* s, const char* name) {
} }
if(s->previous != NULL) if(s->previous != NULL)
return getclass(s->previous, name); return getclass(s->previous, name);
return NULL; return getosclass(name);
} }
SUBROUTDEC* getsubroutdecfromlist(SUBROUTDEC* start, char* name) { SUBROUTDEC* getsubroutdecfromlist(SUBROUTDEC* start, char* name) {

198
misc/os.c
View File

@ -3,102 +3,99 @@
#include "os.h" #include "os.h"
#include "util.h" #include "util.h"
#define MATH_COUNT 5 CLASS* classes = NULL;
SUBROUTDEC* math[MATH_COUNT];
#define STRING_COUNT 12
SUBROUTDEC* stringclass[STRING_COUNT];
#define ARRAY_COUNT 2
SUBROUTDEC* array[ARRAY_COUNT];
#define OUTPUT_COUNT 6
SUBROUTDEC* output[OUTPUT_COUNT];
#define SCREEN_COUNT 6
SUBROUTDEC* screen[SCREEN_COUNT];
#define KEYBOARD_COUNT 4
SUBROUTDEC* keyboard[KEYBOARD_COUNT];
#define MEMORY_COUNT 4
SUBROUTDEC* memory[MEMORY_COUNT];
#define SYS_COUNT 3
SUBROUTDEC* sys[SYS_COUNT];
CLASS* mkclass(const char* name) { CLASS* addclass(const char* name) {
CLASS* class = (CLASS*)malloc(sizeof(CLASS)); CLASS* c = (CLASS*)malloc(sizeof(CLASS));
class->name = ezheapstr(name); c->name = ezheapstr(name);
return class; c->subroutdecs = NULL;
c->next = classes;
classes = c;
return c;
} }
SUBROUTDEC* mkdec(const char* class, SUBROUTCLASS subroutclass, char* type, const char* name) { CLASS* adddec(CLASS* c, SUBROUTCLASS subroutclass, char* type, const char* name) {
SUBROUTDEC* dec = (SUBROUTDEC*)malloc(sizeof(SUBROUTDEC)); SUBROUTDEC* dec = (SUBROUTDEC*)malloc(sizeof(SUBROUTDEC));
dec->class = mkclass(class); dec->class = c;
dec->subroutclass = subroutclass; dec->subroutclass = subroutclass;
dec->type = type; dec->type = type;
dec->name = ezheapstr(name); dec->name = ezheapstr(name);
return dec; dec->next = c->subroutdecs;
c->subroutdecs = dec;
} }
void populatemath() { void populatemath() {
math[0] = mkdec("Math", function, "int", "multiply"); CLASS* mathclass = addclass("Math");
math[1] = mkdec("Math", function, "int", "divide"); adddec(mathclass, function, "int", "multiply");
math[2] = mkdec("Math", function, "int", "min"); adddec(mathclass, function, "int", "divide");
math[3] = mkdec("Math", function, "int", "max"); adddec(mathclass, function, "int", "min");
math[4] = mkdec("Math", function, "int", "sqrt"); adddec(mathclass, function, "int", "max");
adddec(mathclass, function, "int", "sqrt");
} }
void populatestringclass() { void populatestringclass() {
stringclass[0] = mkdec("String", constructor, "String", "new"); CLASS* strclass = addclass("String");
stringclass[1] = mkdec("String", method, "int", "dispose"); adddec(strclass, constructor, "String", "new");
stringclass[2] = mkdec("String", method, "int", "length"); adddec(strclass, method, "int", "dispose");
stringclass[3] = mkdec("String", method, "char", "charAt"); adddec(strclass, method, "int", "length");
stringclass[4] = mkdec("String", method, "void", "setCharAt"); adddec(strclass, method, "char", "charAt");
stringclass[5] = mkdec("String", method, "String", "appendChar"); adddec(strclass, method, "void", "setCharAt");
stringclass[6] = mkdec("String", method, "void", "eraseLastChar"); adddec(strclass, method, "String", "appendChar");
stringclass[7] = mkdec("String", method, "int", "intValue"); adddec(strclass, method, "void", "eraseLastChar");
stringclass[8] = mkdec("String", method, "void", "setInt"); adddec(strclass, method, "int", "intValue");
stringclass[9] = mkdec("String", function, "char", "backSpace"); adddec(strclass, method, "void", "setInt");
stringclass[10] = mkdec("String", function, "char", "doubleQuote"); adddec(strclass, function, "char", "backSpace");
stringclass[11] = mkdec("String", function, "char", "newLine"); adddec(strclass, function, "char", "doubleQuote");
adddec(strclass, function, "char", "newLine");
} }
void populatearray() { void populatearray() {
array[0] = mkdec("Array", function, "Array", "new"); CLASS* arrclass = addclass("Array");
array[1] = mkdec("Array", method, "void", "dispose"); adddec(arrclass, function, "Array", "new");
adddec(arrclass, method, "void", "dispose");
} }
void populateoutput() { void populateoutput() {
output[0] = mkdec("Output", function, "void", "moveCursor"); CLASS* outclass = addclass("Output");
output[1] = mkdec("Output", function, "void", "printChar"); adddec(outclass, function, "void", "moveCursor");
output[2] = mkdec("Output", function, "void", "printString"); adddec(outclass, function, "void", "printChar");
output[3] = mkdec("Output", function, "void", "printInt"); adddec(outclass, function, "void", "printString");
output[4] = mkdec("Output", function, "void", "printLn"); adddec(outclass, function, "void", "printInt");
output[5] = mkdec("Output", function, "void", "backSpace"); adddec(outclass, function, "void", "printLn");
adddec(outclass, function, "void", "backSpace");
} }
void populatescreen() { void populatescreen() {
screen[0] = mkdec("Screen", function, "void", "clearScreen"); CLASS* scrclass = addclass("Screen");
screen[1] = mkdec("Screen", function, "void", "setColor"); adddec(scrclass, function, "void", "clearScreen");
screen[2] = mkdec("Screen", function, "void", "drawPixel"); adddec(scrclass, function, "void", "setColor");
screen[3] = mkdec("Screen", function, "void", "drawLine"); adddec(scrclass, function, "void", "drawPixel");
screen[4] = mkdec("Screen", function, "void", "drawRectangle"); adddec(scrclass, function, "void", "drawLine");
screen[5] = mkdec("Screen", function, "void", "drawCircle"); adddec(scrclass, function, "void", "drawRectangle");
adddec(scrclass, function, "void", "drawCircle");
} }
void populatekeyboard() { void populatekeyboard() {
keyboard[0] = mkdec("Keyboard", function, "char", "keyPressed"); CLASS* kbdclass = addclass("Keyboard");
keyboard[1] = mkdec("Keyboard", function, "char", "readChar"); adddec(kbdclass, function, "char", "keyPressed");
keyboard[2] = mkdec("Keyboard", function, "String", "readLine"); adddec(kbdclass, function, "char", "readChar");
keyboard[3] = mkdec("Keyboard", function, "int", "readInt"); adddec(kbdclass, function, "String", "readLine");
adddec(kbdclass, function, "int", "readInt");
} }
void populatememory() { void populatememory() {
memory[0] = mkdec("Memory", function, "int", "peek"); CLASS* memclass = addclass("Memory");
memory[1] = mkdec("Memory", function, "void", "poke"); adddec(memclass, function, "int", "peek");
memory[2] = mkdec("Memory", function, "Array", "alloc"); adddec(memclass, function, "void", "poke");
memory[3] = mkdec("Memory", function, "void", "deAlloc"); adddec(memclass, function, "Array", "alloc");
adddec(memclass, function, "void", "deAlloc");
} }
void populatesys() { void populatesys() {
sys[0] = mkdec("Sys", function, "void", "halt"); CLASS* sysclass = addclass("Sys");
sys[1] = mkdec("Sys", function, "void", "error"); adddec(sysclass, function, "void", "halt");
sys[2] = mkdec("Sys", function, "void", "wait"); adddec(sysclass, function, "void", "error");
adddec(sysclass, function, "void", "wait");
} }
void populateos() { void populateos() {
@ -112,48 +109,47 @@ void populateos() {
populatesys(); populatesys();
} }
void freedecs(SUBROUTDEC** array, int size) { void freesubroutdecs(SUBROUTDEC* d) {
for(int i = 0; i < size; i++) { free(d->name);
free(array[i]->class->name); free(d->type);
free(array[i]->class); if(d->next != NULL)
free(array[i]->name); freesubroutdecs(d->next);
free(array[i]); }
}
void freeclasses(CLASS* c) {
freesubroutdecs(c->subroutdecs);
free(c->name);
if(c->next != NULL)
freeclasses(c->next);
} }
void freeos() { void freeos() {
freedecs(math, MATH_COUNT); freeclasses(classes);
freedecs(stringclass, STRING_COUNT);
freedecs(array, ARRAY_COUNT);
freedecs(output, OUTPUT_COUNT);
freedecs(screen, SCREEN_COUNT);
freedecs(keyboard, KEYBOARD_COUNT);
freedecs(memory, MEMORY_COUNT);
freedecs(sys, SYS_COUNT);
} }
SUBROUTDEC* getdec(SUBROUTDEC** array, int size, const char* name) { SUBROUTDEC* getsubroutdecinclass(CLASS* c, const char* name) {
for(int i = 0; i < size; i++) SUBROUTDEC* curr = c->subroutdecs;
if(!strcmp(array[i]->name, name)) while(curr != NULL) {
return array[i]; if(!strcmp(curr->name, name))
return curr;
curr = curr->next;
}
return NULL;
}
CLASS* getosclass(const char* name) {
CLASS* curr = classes;
while(curr != NULL) {
if(!strcmp(curr->name, name))
return curr;
curr = curr->next;
}
return NULL;
} }
SUBROUTDEC* getossubroutdec(SUBROUTCALL* call) { SUBROUTDEC* getossubroutdec(SUBROUTCALL* call) {
if(!strcmp(call->parentname, "Math")) CLASS* c = getosclass(call->parentname);
return getdec(math, MATH_COUNT, call->name); if(c == NULL)
if(!strcmp(call->parentname, "String")) return NULL;
return getdec(stringclass, STRING_COUNT, call->name); getsubroutdecinclass(c, call->name);
if(!strcmp(call->parentname, "Array"))
return getdec(array, ARRAY_COUNT, call->name);
if(!strcmp(call->parentname, "Output"))
return getdec(output, OUTPUT_COUNT, call->name);
if(!strcmp(call->parentname, "Screen"))
return getdec(screen, SCREEN_COUNT, call->name);
if(!strcmp(call->parentname, "Keyboard"))
return getdec(keyboard, KEYBOARD_COUNT, call->name);
if(!strcmp(call->parentname, "Memory"))
return getdec(memory, MEMORY_COUNT, call->name);
if(!strcmp(call->parentname, "Sys"))
return getdec(sys, SYS_COUNT, call->name);
return NULL;
} }

View File

@ -3,6 +3,7 @@
#include "parser-tree.h" #include "parser-tree.h"
SUBROUTDEC* getossubroutdec(SUBROUTCALL* call); SUBROUTDEC* getossubroutdec(SUBROUTCALL* call);
CLASS* getosclass(const char* name);
void populateos(); void populateos();
void freeos(); void freeos();