jack-compiler/compiler/compiler.c

47 lines
1.3 KiB
C
Raw Normal View History

2020-12-20 13:58:10 -05:00
#include <stdlib.h>
2021-01-03 14:08:54 -05:00
#include "os.h"
2020-12-31 18:02:04 -05:00
#include "compiler-structure.h"
2020-12-20 13:58:10 -05:00
#include "compiler.h"
2020-12-31 18:02:04 -05:00
/* This should be part of compiler-structure, but since it is used by other modules,
* it will stay here for convenience */
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileclass(COMPILER* c, CLASS* class) {
2020-12-20 13:58:10 -05:00
SCOPE* topscope = mkscope(c->globalscope);
2020-12-24 14:22:22 -05:00
if(class->vardecs != NULL)
2021-01-03 14:08:54 -05:00
addclassvardecs(topscope, class->vardecs);
2020-12-24 14:22:22 -05:00
if(class->subroutdecs != NULL)
2020-12-27 16:52:28 -05:00
topscope->subroutines = class->subroutdecs;
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
LINEBLOCK* output = NULL;
2020-12-21 18:35:41 -05:00
SUBROUTDEC* curr = class->subroutdecs;
2020-12-21 13:05:49 -05:00
while(curr != NULL) {
2021-01-03 14:08:54 -05:00
output = mergelnblks(output, compilesubroutdec(topscope, class, curr));
2020-12-21 13:05:49 -05:00
curr = curr->next;
2020-12-20 13:58:10 -05:00
}
2021-01-03 14:08:54 -05:00
freescope(topscope);
2020-12-21 13:05:49 -05:00
return output;
2020-12-20 13:58:10 -05:00
}
COMPILER* mkcompiler(CLASS* classes) {
COMPILER* c = (COMPILER*)malloc(sizeof(COMPILER));
c->globalscope = mkscope(NULL);
2021-01-03 14:08:54 -05:00
c->globalscope->compiler = c;
2020-12-27 16:52:28 -05:00
c->globalscope->classes = classes;
2020-12-24 14:22:22 -05:00
c->classes = classes;
2021-01-03 14:08:54 -05:00
c->os = mkos();
2020-12-31 08:00:21 -05:00
pthread_mutex_init(&(c->ifmutex), NULL);
pthread_mutex_init(&(c->whilemutex), NULL);
2020-12-31 11:39:07 -05:00
pthread_mutex_init(&(c->staticmutex), NULL);
2020-12-20 13:58:10 -05:00
return c;
}
2020-12-31 08:00:21 -05:00
void freecompiler(COMPILER* c) {
pthread_mutex_destroy(&(c->ifmutex));
pthread_mutex_destroy(&(c->whilemutex));
2020-12-31 11:39:07 -05:00
pthread_mutex_destroy(&(c->staticmutex));
2020-12-31 08:00:21 -05:00
// to be continued
2021-01-03 14:08:54 -05:00
freeos(c->os);
freescope(c->globalscope);
2020-12-31 08:00:21 -05:00
free(c);
}