jack-compiler/main.c

138 lines
2.8 KiB
C
Raw Normal View History

2020-11-30 16:44:22 -05:00
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
2020-12-29 19:27:48 -05:00
#include <pthread.h>
#include <unistd.h>
2020-12-21 19:56:59 -05:00
#include "parser.h"
2020-12-20 13:58:10 -05:00
#include "compiler.h"
2020-12-29 19:27:48 -05:00
#include "io.h"
2020-12-20 13:58:10 -05:00
2020-12-29 19:27:48 -05:00
typedef struct unit {
FILELIST* file;
TOKEN* tokens;
CLASS* parsed;
COMPILER* compiler;
LINEBLOCK* compiled;
struct unit* next;
} COMPILEUNIT;
void* parseunit(void* input) {
COMPILEUNIT* unit = (COMPILEUNIT*)input;
unit->parsed = parse(unit->tokens, unit->file->name);
pthread_exit(NULL);
}
void* compileunit(void* input) {
COMPILEUNIT* unit = (COMPILEUNIT*)input;
unit->compiled = compileclass(unit->compiler, unit->parsed);
pthread_exit(NULL);
}
void waitthreads(pthread_t* threads, int amount) {
void* status;
int code;
for(int i = 0; i < amount; i++) {
code = pthread_join(threads[i], &status);
if(code) {
eprintf("Error while joining thread %i: %s\n", i, strerror(code));
exit(code);
}
}
}
void actonunits(COMPILEUNIT* units, void*(*fun)(void*)) {
pthread_t mythreads[_SC_THREAD_THREADS_MAX];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
COMPILEUNIT* curr = units;
int i;
int code;
do {
i = 0;
while(curr != NULL && i < _SC_THREAD_THREADS_MAX) {
2020-12-29 19:33:23 -05:00
code = pthread_create(&mythreads[i], &attr, fun, curr);
2020-12-29 19:27:48 -05:00
if(code) {
eprintf("Error while creating thread %i: %s\n", i, strerror(code));
exit(code);
}
curr = curr->next;
i++;
}
waitthreads(mythreads, i);
} while(i == _SC_THREAD_THREADS_MAX);
pthread_attr_destroy(&attr);
2020-12-20 13:58:10 -05:00
}
2020-11-30 16:44:22 -05:00
int main(int argc, char* argv[]) {
2020-12-29 19:27:48 -05:00
2020-11-30 16:44:22 -05:00
if(argc < 2) {
2020-12-29 19:27:48 -05:00
eprintf("Usage: %s {input file(s)}\n", argv[0]);
2020-11-30 16:44:22 -05:00
return 1;
}
2020-12-29 19:27:48 -05:00
FILELIST* files = getfiles(argv[1]);
FILELIST* curr = files->next;
2020-11-30 16:44:22 -05:00
2020-12-29 19:27:48 -05:00
COMPILEUNIT* head = (COMPILEUNIT*)malloc(sizeof(COMPILEUNIT));
head->file = files;
head->tokens = tokenize(files->fullname);
COMPILEUNIT* currunit = head;
while(curr != NULL) {
COMPILEUNIT* newunit = (COMPILEUNIT*)malloc(sizeof(COMPILEUNIT));
newunit->file = curr;
newunit->tokens = tokenize(curr->fullname);
currunit->next = newunit;
currunit = newunit;
curr = curr->next;
2020-11-30 16:44:22 -05:00
}
2020-12-29 19:27:48 -05:00
currunit->next = NULL;
actonunits(head, parseunit);
CLASS* headclass = head->parsed;
CLASS* currclass = headclass;
currunit = head->next;
while(currunit != NULL) {
currclass->next = currunit->parsed;
currclass = currunit->parsed;
currunit = currunit->next;
}
currclass->next = NULL;
COMPILER* compiler = mkcompiler(headclass);
currunit = head;
while(currunit != NULL) {
currunit->compiler = compiler;
currunit = currunit->next;
}
actonunits(head, compileunit);
2020-12-31 08:00:21 -05:00
currunit = head;
while(currunit != NULL) {
FILE* output = fopen(currunit->file->outname, "w");
if(output == NULL) {
eprintf("%s", strerror(errno));
exit(1);
}
printlns(currunit->compiled->head, output);
fclose(output);
currunit = currunit->next;
}
2020-11-30 16:44:22 -05:00
return 0;
}