zpy.c (2187B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdbool.h> 4 5 #include "util.h" 6 #include "fileread.h" 7 #include "comp.h" 8 #include "appendsnprintf.h" 9 10 char infilename[128]; 11 char outfilename[128]; 12 char libs[64][128]; 13 int libcount = 0; 14 bool omitc = false; 15 char compilerflags[64][128]; 16 int compilerflagscount = 0; 17 18 //# this function return will deal with the args given to stdin, and put the needed values into globals 19 static void processargs(int argc, char **argv){ 20 for (int i = 1; i < argc; i++){ 21 if (argv[i][0] == '-'){ 22 switch (argv[i][1]){ 23 case 'o': 24 i++; 25 strcpy(outfilename, argv[i]); 26 break; 27 28 case 'i': 29 i++; 30 strcpy(libs[libcount], argv[i]); 31 libcount++; 32 break; 33 case 'c': 34 omitc = true; 35 break; 36 case 'f': 37 i++; 38 strcpy(compilerflags[compilerflagscount], argv[i]); 39 compilerflagscount++; 40 break; 41 default: 42 die("unknown argument!"); 43 break; 44 } 45 }else strcpy(infilename, argv[i]); 46 } 47 } 48 49 int main(int argc, char **argv){ 50 processargs(argc, argv); 51 52 53 FILE *f = fopen(infilename, "r"); 54 if (f == NULL) 55 die("no such file or directory"); 56 57 FILE *fout; 58 if (omitc == false) fout = fopen("./tmp.zpy.c", "w"); 59 else fout = fopen(outfilename, "w"); 60 61 62 if (fout == NULL) die("no such file or directory"); 63 64 strings *stringTokens = fileread(f); 65 66 if (stringTokens == NULL) die("couldn't parse file, is it formated properly?"); 67 68 CompilerInit(); 69 70 fprintf(fout, "#include <zpylib.h>\n"); 71 72 for (int i = 0; i < libcount; i++) fprintf(fout, "#include <%s>\n", libs[i]); 73 74 for (int i = 0; i < stringTokens->count; i++){ 75 stringTokens->strs[i]++; 76 stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0'; 77 astNode *line = tokenize(stringTokens->strs[i]); 78 79 Compile(line, fout, stringTokens->strs[i]); 80 } 81 fclose(fout); 82 83 if (omitc == false){ 84 char *cmd = malloc(512); 85 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); 86 for (int i = 0; i < compilerflagscount; i++){ 87 cmd = appendsnprintf(cmd, 512, "%s ", compilerflags[i]); 88 } 89 system(cmd); 90 remove("./tmp.zpy.c"); 91 } 92 }