More functional Makefile.
[Jack-Compiler.git] / jack.c
blobb8e4baf3421a46256f87436490b92274f85bf758
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
6 #include "error.h"
7 #include "jack.h"
8 #include "parse.h"
9 #include "test.h"
10 #include "token.h"
12 char *pC = NULL; /* pointer to code */
13 char pT[1000]; /* pointer to token */
14 char *pS = NULL;
15 token tk;
16 ttype ttyp;
17 int space_count = 0; /* number of spaces before printing token */
19 void usage(void)
21 printf("Usage: jack [options] file\n");
22 printf("Options:\n");
23 printf(" <file> Compile file without tokens\n");
24 printf(" Filename must end with .jack\n");
25 printf(" -h Display this information\n");
26 printf(" -t <file> Compile file and display tokens\n");
27 printf(" -u Run all unit tests\n");
30 void settings_init(void)
32 settings.tokens = 0;
33 settings.test = 0;
36 int main(int argc, char *argv[])
38 int i, size, file_loc = 1;
39 char FilenameBuff[80];
41 FILE *fpSource, *fpDest;
43 /* test for switches passed - test for location of file in argv */
44 file_loc = 2;
45 while ((i = getopt(argc, argv, "uht:")) != -1)
47 switch (i)
49 case 't':
50 settings.tokens = 1;
51 break;
52 case 'u':
53 settings.test = 1;
54 break;
55 case 'h':
56 usage();
57 exit(EXIT_SUCCESS);
58 break;
59 default:
60 usage();
61 exit(EXIT_SUCCESS);
65 if(settings.test)
67 printf("\nRUNNING ALL UNIT TESTS ON COMPILER.\n\n");
68 if(test_all() == 0)
70 printf("\nUNIT TEST COMPLETE - ALL TESTS RETURNED PASSED.\n\n");
71 } else {
72 exit_error(0, "UNIT TEST FAILED.\n\n");
74 exit(EXIT_SUCCESS);
77 if(argc < 2) { exit_error(1, "No Input Files."); usage(); }
78 /* TODO: future versions will accept more than one file */
79 if(argc > 3) { exit_error(2, "Too Many Files Listed."); usage(); }
81 if((fpSource = fopen(argv[file_loc], "r")) == NULL)
83 exit_error(3, "Cannot Open Input (Source) File");
86 strcpy(FilenameBuff, argv[file_loc]);
88 /* verify filename extension */
89 if(strstr(argv[file_loc], "jack") == NULL)
91 exit_error(5, "Filename Extension Not Correct."); usage();
94 /* modify output filename and then open output file */
95 pC = strrchr(FilenameBuff, '.');
96 if( pC == NULL){ exit_error(5, "Input File Has No Extension"); usage(); }
98 pC++;
99 strcpy(pC, "xml"); /* buffer should contain output filename */
101 if((fpDest = fopen(FilenameBuff, "w+")) == NULL)
103 exit_error(6, "Cannot Open Output (Object) File");
106 fseek(fpSource, 0, SEEK_END); /* seek to end of file */
107 size = ftell(fpSource); /* get current file pointer */
108 fseek(fpSource, 0, SEEK_SET); /* seek back to beginning of file */
109 /* proceed with allocating memory and reading the file */
110 pS = malloc((sizeof(char) * size)+1);
112 if(pS == NULL) { exit_error(7, "Cannot Allocate Memory For Source"); }
114 i = 0;
115 while(i < size)
117 pS[i] = getc(fpSource);
118 i++;
120 pS[i] = '\0';
122 pC = pS; /* move to beginning of source code */
123 parse_class(pS, pC, pT);
124 return 0;