summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code2/zpy.c
blob: 3f19a06db7b49c3cbe653beefb08b525ff2dbf97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "util.h"
#include "fileread.h"
#include "comp.h"
#include "appendsnprintf.h"

char infilename[128];
char outfilename[128];
char libs[64][128];
int libcount = 0;
bool omitc = false;
char compilerflags[64][128];
int compilerflagscount = 0;

//# this function return will deal with the args given to stdin, and put the needed values into globals
static void processargs(int argc, char **argv){
	for (int i = 1; i < argc; i++){
		if (argv[i][0] == '-'){
			switch (argv[i][1]){
				case 'o':
					i++;
					strcpy(outfilename, argv[i]);
					break;
				
				case 'i': 
					i++;
					strcpy(libs[libcount], argv[i]);
					libcount++;
					break;
				case 'c':
					omitc = true;
					break;
				case 'f':
					i++;
					strcpy(compilerflags[compilerflagscount], argv[i]);
					compilerflagscount++;
					break;
				default:
					die("unknown argument!");
					break;
			}
		}else strcpy(infilename, argv[i]);
	}
}

int main(int argc, char **argv){
	processargs(argc, argv);


	FILE *f = fopen(infilename, "r");
	if (f == NULL)
		die("no such file or directory");
	
	FILE *fout;
	if (omitc == false) fout = fopen("./tmp.zpy.c", "w");
	else  fout = fopen(outfilename, "w");
	

	if (fout == NULL) die("no such file or directory");

	strings *stringTokens = fileread(f);

	if (stringTokens == NULL) die("couldn't parse file, is it formated properly?");
	
	CompilerInit();

	fprintf(fout, "#include <zpylib.h>\n");

	for (int i = 0; i < libcount; i++) fprintf(fout, "#include <%s>\n", libs[i]);
	
	for (int i = 0; i < stringTokens->count; i++){
		stringTokens->strs[i]++;
		stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0';
		astNode *line = tokenize(stringTokens->strs[i]);
			
		Compile(line, fout, stringTokens->strs[i]);
	}
	fclose(fout);

	if (omitc == false){
		char *cmd = malloc(512);
		snprintf(cmd, 512, "cc -O3 ./tmp.zpy.c /usr/local/share/zpylib/*.o -o %s -I/usr/local/share/zpylib/include -Wno-implicit-function-declaration ", outfilename);
		for (int i = 0; i < compilerflagscount; i++){
			cmd = appendsnprintf(cmd, 512, "%s ", compilerflags[i]);
		}
		system(cmd);
		remove("./tmp.zpy.c");
	}
}