While statements implemented and do statements updated.
[Jack-Compiler.git] / jack.c
blob55bc49cf999f39542a11294405cca08c5cf84014
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 "token.h"
11 char *pC = NULL; /* pointer to code */
12 char pT[1000]; /* pointer to token */
13 char *pS = NULL;
14 token tk;
15 ttype ttyp;
17 void usage(void)
19 printf("\n<options> <file>\n");
20 printf("<file> Filename to compile.\n");
21 printf(" Filename must end with .jack.\n");
24 void settings_init(void)
26 settings.tokens = 0;
29 int main(int argc, char *argv[])
31 int i, size, file_loc = 1;
32 char FilenameBuff[80];
34 FILE *fpSource, *fpDest;
36 if(argc < 2) { exit_error(1, "No Input Files."); usage(); }
37 /* TODO: future versions will accept more than one file */
38 if(argc > 3) { exit_error(2, "Too Many Files Listed."); usage(); }
40 /* test for switches passed - test for location of file in argv */
41 if(argv[1][0] == '-')
43 file_loc = 2;
44 while(-1 != (i = getopt(argc, argv,
45 "-t" /* print tokenizer output */
46 "--help" /*print out usage statement */
47 ))) {
49 switch (i)
51 case 't':
52 settings.tokens = 1;
53 break;
54 case '-':
55 usage();
56 break;
61 if((fpSource = fopen(argv[file_loc], "r")) == NULL)
63 exit_error(3, "Cannot Open Input (Source) File");
66 strcpy(FilenameBuff, argv[file_loc]);
68 /* verify filename extension */
69 if(strstr(argv[file_loc], "jack") == NULL)
71 exit_error(5, "Filename Extension Not Correct."); usage();
74 /* modify output filename and then open output file */
75 pC = strrchr(FilenameBuff, '.');
76 if( pC == NULL){ exit_error(5, "Input File Has No Extension"); usage(); }
78 pC++;
79 strcpy(pC, "xml"); /* buffer should contain output filename */
81 if((fpDest = fopen(FilenameBuff, "w+")) == NULL)
83 exit_error(6, "Cannot Open Output (Object) File");
86 fseek(fpSource, 0, SEEK_END); /* seek to end of file */
87 size = ftell(fpSource); /* get current file pointer */
88 fseek(fpSource, 0, SEEK_SET); /* seek back to beginning of file */
89 /* proceed with allocating memory and reading the file */
90 pS = malloc((sizeof(char) * size)+1);
92 if(pS == NULL) { exit_error(7, "Cannot Allocate Memory For Source"); }
94 i = 0;
95 while(i < size)
97 pS[i] = getc(fpSource);
98 i++;
100 pS[i] = '\0';
102 pC = pS; /* move to beginning of source code */
103 parse_class(pS, pC, pT);
104 return 0;