12 char *pC
= NULL
; /* pointer to code */
13 char pT
[1000]; /* pointer to token */
17 int space_count
= 0; /* number of spaces before printing token */
21 printf("Usage: jack [options] file\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)
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 */
45 while ((i
= getopt(argc
, argv
, "uht:")) != -1)
67 printf("\nRUNNING ALL UNIT TESTS ON COMPILER.\n\n");
70 printf("\nUNIT TEST COMPLETE - ALL TESTS RETURNED PASSED.\n\n");
72 exit_error(0, "UNIT TEST FAILED.\n\n");
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(); }
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"); }
117 pS
[i
] = getc(fpSource
);
122 pC
= pS
; /* move to beginning of source code */
123 parse_class(pS
, pC
, pT
);