Added tokens to fit specification.
[Jack-Compiler.git] / jack.c
blob2774973d809771b4279ce5db0d9ae1584a821c4d
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;
18 void usage(void)
20 printf("Usage: jack [options] file\n");
21 printf("Options:\n");
22 printf(" <file> Compile file without tokens\n");
23 printf(" Filename must end with .jack\n");
24 printf(" -h Display this information\n");
25 printf(" -t <file> Compile file and display tokens\n");
26 printf(" -u Run all unit tests\n");
29 void settings_init(void)
31 settings.tokens = 0;
32 settings.test = 0;
35 int main(int argc, char *argv[])
37 int i, size, file_loc = 1;
38 char FilenameBuff[80];
40 FILE *fpSource, *fpDest;
42 /* test for switches passed - test for location of file in argv */
43 file_loc = 2;
44 while ((i = getopt(argc, argv, "uht:")) != -1)
46 switch (i)
48 case 't':
49 settings.tokens = 1;
50 break;
51 case 'u':
52 settings.test = 1;
53 break;
54 case 'h':
55 usage();
56 exit(EXIT_SUCCESS);
57 break;
58 default:
59 usage();
60 exit(EXIT_SUCCESS);
64 if(settings.test)
66 printf("\nRUNNING ALL UNIT TESTS ON COMPILER.\n\n");
67 if(test_all() == 0)
69 printf("\nUNIT TEST COMPLETE - ALL TESTS RETURNED PASSED.\n\n");
70 } else {
71 exit_error(0, "UNIT TEST FAILED.\n\n");
73 exit(EXIT_SUCCESS);
76 if(argc < 2) { exit_error(1, "No Input Files."); usage(); }
77 /* TODO: future versions will accept more than one file */
78 if(argc > 3) { exit_error(2, "Too Many Files Listed."); usage(); }
80 if((fpSource = fopen(argv[file_loc], "r")) == NULL)
82 exit_error(3, "Cannot Open Input (Source) File");
85 strcpy(FilenameBuff, argv[file_loc]);
87 /* verify filename extension */
88 if(strstr(argv[file_loc], "jack") == NULL)
90 exit_error(5, "Filename Extension Not Correct."); usage();
93 /* modify output filename and then open output file */
94 pC = strrchr(FilenameBuff, '.');
95 if( pC == NULL){ exit_error(5, "Input File Has No Extension"); usage(); }
97 pC++;
98 strcpy(pC, "xml"); /* buffer should contain output filename */
100 if((fpDest = fopen(FilenameBuff, "w+")) == NULL)
102 exit_error(6, "Cannot Open Output (Object) File");
105 fseek(fpSource, 0, SEEK_END); /* seek to end of file */
106 size = ftell(fpSource); /* get current file pointer */
107 fseek(fpSource, 0, SEEK_SET); /* seek back to beginning of file */
108 /* proceed with allocating memory and reading the file */
109 pS = malloc((sizeof(char) * size)+1);
111 if(pS == NULL) { exit_error(7, "Cannot Allocate Memory For Source"); }
113 i = 0;
114 while(i < size)
116 pS[i] = getc(fpSource);
117 i++;
119 pS[i] = '\0';
121 pC = pS; /* move to beginning of source code */
122 parse_class(pS, pC, pT);
123 return 0;