jack-compiler/main.c

82 lines
1.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-31 18:12:31 -05:00
#include "threads.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"
#include "os.h"
2020-12-20 13:58:10 -05:00
2020-11-30 16:44:22 -05:00
int main(int argc, char* argv[]) {
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;
2021-01-03 14:08:54 -05:00
head->parser = mkparser(tokenize(files->fullname), files->name);
2020-12-29 19:27:48 -05:00
COMPILEUNIT* currunit = head;
while(curr != NULL) {
COMPILEUNIT* newunit = (COMPILEUNIT*)malloc(sizeof(COMPILEUNIT));
newunit->file = curr;
2021-01-03 14:08:54 -05:00
newunit->parser = mkparser(tokenize(curr->fullname), curr->name);
2020-12-29 19:27:48 -05:00
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);
}
2021-01-03 14:24:01 -05:00
if(currunit->compiled == NULL) {
eprintf("Class '%s' is empty; file '%s'\n", currunit->parsed->name, currunit->file->name);
exit(1);
}
2020-12-31 08:00:21 -05:00
printlns(currunit->compiled->head, output);
fclose(output);
2021-01-03 14:08:54 -05:00
COMPILEUNIT* next = currunit->next;
freeunit(currunit);
currunit = next;
2020-12-31 08:00:21 -05:00
}
2020-11-30 16:44:22 -05:00
2021-01-03 14:08:54 -05:00
freecompiler(compiler);
freetree(headclass);
freefilelist(files);
2020-11-30 16:44:22 -05:00
return 0;
}