14 if(settings
.tokens
) { printf("<class>\n"); }
16 if(has_more_tokens(pC
) == true)
21 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
24 if(strcmp(pT
, "class") == 0 ) {
25 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
27 compiler_error(40, "Incorrect Token Found: Must be 'class'", pS
, pC
, pT
);
30 /* look for class name */
31 if(has_more_tokens(pC
) == true)
36 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
39 if (tk
== IDENTIFIER
){
40 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
42 compiler_error(44, "Could Not Find Class Name or Subroutine Name at This Location", pS
, pC
, pT
);
45 /* look for '{' symbol */
46 if(has_more_tokens(pC
) == true)
51 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
55 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
57 compiler_error(44, "Could Not Find Class Name or Subroutine Name at This Location", pS
, pC
, pT
);
60 if(has_more_tokens(pC
) == true)
65 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
69 parse_class_var_dec();
70 if(has_more_tokens(pC
) == true)
75 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
77 } while (strcmp(pT
, "static") == 0 || strcmp(pT
, "field") == 0);
81 if(settings
.tokens
) { printf("</class>\n"); }
84 void parse_class_var_dec()
86 if(settings
.tokens
) { printf("<classVarDec>\n"); }
88 /* look for 'static' or 'field' */
89 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
90 if(has_more_tokens(pC
) == true)
95 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
99 if(tk
== IDENTIFIER
) {
100 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
101 } else if (tk
== KEYWORD
) {
102 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0) {
103 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
105 compiler_error(41, "Token Must be Data Type.", pS
, pC
, pT
);
108 compiler_error(41, "Token Must be Data Type", pS
, pC
, pT
);
111 /* look or variable name */
112 if(has_more_tokens(pC
) == true)
114 pC
= advance(pC
, pT
);
117 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
119 if(tk
== IDENTIFIER
) {
120 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
122 compiler_error(42, "Token Must be Variable Name", pS
, pC
, pT
);
126 if(has_more_tokens(pC
) == true)
128 pC
= advance(pC
, pT
);
131 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
136 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
137 if(has_more_tokens(pC
) == true)
139 pC
= advance(pC
, pT
);
142 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
144 if(tk
== IDENTIFIER
) {
145 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
147 compiler_error(42, "Token Must be Variable Name", pS
, pC
, pT
);
149 if(has_more_tokens(pC
) == true)
151 pC
= advance(pC
, pT
);
154 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
156 } while (*pT
== ',');
160 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
162 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
165 if(settings
.tokens
) { printf("</classVarDec>\n"); }
168 void parse_subroutine()
170 if(settings
.tokens
) { printf("<subroutineDec>\n"); }
171 if(has_more_tokens(pC
) == true)
173 pC
= advance(pC
, pT
);
176 if(strcmp(pT
, "constructor") == 0 || strcmp(pT
, "function") == 0 || strcmp(pT
, "method") == 0)
178 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
180 compiler_error(8, "Incorrect Token Found: Must be 'constructor', 'function', or 'method'", pS
, pC
, pT
);
185 /* look for return type of function */
186 if(has_more_tokens(pC
) == true)
188 pC
= advance(pC
, pT
);
190 if(tk
== KEYWORD
|| tk
== IDENTIFIER
)
192 if(strcmp(pT
, "void") == 0)
194 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
196 if(settings
.tokens
) { printf("\t<type>%s</type>\n", pT
); }
199 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
202 compiler_error(29, "Incorrect Token Type", pS
, pC
, pT
);
205 /* look for subroutine name */
206 if(has_more_tokens(pC
) == true)
208 pC
= advance(pC
, pT
);
212 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
214 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
217 compiler_error(10, "Incorrect Token Type. Looking for Keyword or Identifier.", pS
, pC
, pT
);
220 /* look for symbol '(' that specifies beginning of parameter list */
221 if(has_more_tokens(pC
) == true)
223 pC
= advance(pC
, pT
);
227 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
230 compiler_error(12, "Parameter List for Function Missing", pS
, pC
, pT
);
233 compiler_error(11, "Name of Function Must be an Identifier", pS
, pC
, pT
);
236 /* look for end of parameter list */
239 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
241 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
244 /* look for opening brace for block */
245 if(has_more_tokens(pC
) == true)
247 pC
= advance(pC
, pT
);
251 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
255 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
258 while(strcmp(pT
, "let") == 0 || strcmp(pT
, "if") == 0 || strcmp(pT
, "while") == 0 || strcmp(pT
, "do") == 0 || strcmp(pT
, "return") == 0)
261 if(has_more_tokens(pC
) == true)
263 pC
= advance(pC
, pT
);
266 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
270 if(settings
.tokens
) { printf("</subroutineDec>\n"); }
275 if(*pT
== '(') { if(settings
.tokens
) { printf("<parameterList>\n"); } }
277 /* look for datatype in parameter list */
278 if(has_more_tokens(pC
) == true)
280 pC
= advance(pC
, pT
);
283 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0)
285 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
287 compiler_error(14, "Incorrect Token Type in Parameter List. Looking for Datatype name.", pS
, pC
, pT
);
289 } else if(tk
== SYMBOL
&& *pT
== ')') { if(settings
.tokens
) { printf("</parameterList>\n"); } return; }
291 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
294 /* look for identifier for this parameter */
295 if(has_more_tokens(pC
) == true)
297 pC
= advance(pC
, pT
);
299 if(tk
== IDENTIFIER
) {
300 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
303 compiler_error(15, "Incorrect Token Type in Parameter List. Looking for Variable Identifier.", pS
, pC
, pT
);
306 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
309 /* are there more parameters? */
310 if(has_more_tokens(pC
) == true)
312 pC
= advance(pC
, pT
);
316 } else if (*pT
== ')') { /* exit parse_params */
317 if(settings
.tokens
) { printf("</parameterList>\n"); }
320 compiler_error(16, "Incorrect Token Type in Parameter List. Looking for ',' or ')'", pS
, pC
, pT
);
323 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
330 /* look for token named 'var' */
331 if(has_more_tokens(pC
) == true)
333 pC
= advance(pC
, pT
);
335 if(strcmp(pT
, "var") == 0)
337 if(settings
.tokens
) { printf("<varDec>\n"); }
341 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
344 /* look for variable data type */
345 if(has_more_tokens(pC
) == true)
347 pC
= advance(pC
, pT
);
349 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0 || strcmp(pT
, "Array") == 0)
351 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
352 /* look for identifier(s) for variable(s) */
355 if(has_more_tokens(pC
) == true)
357 pC
= advance(pC
, pT
);
361 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
365 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
371 /* could also be a custom class name */
374 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
375 if(has_more_tokens(pC
) == true)
377 pC
= advance(pC
, pT
);
380 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
384 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
388 if(has_more_tokens(pC
) == true)
390 pC
= advance(pC
, pT
);
392 if((tk
== SYMBOL
) && (*pT
== ';' || *pT
== ',')) {
393 if(*pT
== ',') { i
++; }
394 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
400 compiler_error(18, "Improperly Terminated Variable Declaration", pS
, pC
, pT
);
403 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
406 if(settings
.tokens
) { printf("<varDec>\n"); }
409 void parse_statements()
411 if(strcmp(pT
, "let") == 0)
414 } else if(strcmp(pT
, "if") == 0)
417 } else if(strcmp(pT
, "while") == 0)
420 } else if(strcmp(pT
, "do") == 0)
423 } else if(strcmp(pT
, "return") == 0)
431 if(settings
.tokens
) { printf("<doStatement>\n\t<keyword>do</keyword>\n"); }
432 if(has_more_tokens(pC
) == true)
434 pC
= advance(pC
, pT
);
437 compiler_error(20, "Could Not Complete Do Statement. Incomplete Program", pS
, pC
, pT
);
440 /* must be an identifier */
441 if(tk
== IDENTIFIER
) {
442 parse_subroutine_call();
444 compiler_error(30, "Subroutine Name Must Be Listed Here", pS
, pC
, pT
);
447 if(settings
.tokens
) { printf("</doStatement>\n"); }
452 if(settings
.tokens
) { printf("<letStatement>\n\t<keyword>let</keyword>\n"); }
453 if(has_more_tokens(pC
) == true)
455 pC
= advance(pC
, pT
);
458 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
461 /* look for an identifier - must be a variable name */
464 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
466 compiler_error(31, "Could Not Find Identifier At This Location", pS
, pC
, pT
);
469 /* optional '[' for an array offset value */
470 if(has_more_tokens(pC
) == true)
472 pC
= advance(pC
, pT
);
475 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
480 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
486 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
492 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
494 compiler_error(32, "Could Not Find '=' Symbol At This Location", pS
, pC
, pT
);
497 /* parse_expression(); */
499 /* look for identifier */
500 if(has_more_tokens(pC
) == true)
502 pC
= advance(pC
, pT
);
505 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
510 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
512 compiler_error(20, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
516 if(has_more_tokens(pC
) == true)
518 pC
= advance(pC
, pT
);
521 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
526 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
528 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
531 if(settings
.tokens
) { printf("</letStatement>\n"); }
537 exit_error(0, "Parsing While");
542 if(settings
.tokens
) { printf("<returnStatement>\n\t<identifier>%s</identifier>\n", pT
); }
545 if(has_more_tokens(pC
) == true)
547 pC
= advance(pC
, pT
);
550 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
555 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
557 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
559 if(settings
.tokens
) { printf("</returnStatement>\n"); }
564 exit_error(0, "Parsing If");
567 void parse_expression()
570 printf("<expression>%s</expression>\n", pT
);
571 if(has_more_tokens(pC
) == true)
573 pC
= advance(pC
, pT
);
576 compiler_error(34, "Could Not Parse Expression. Incomplete Program", pS
, pC
, pT
);
578 } while (*pT
!= ']' && *pT
!= ';');
583 if(has_more_tokens(pC
) == true)
585 pC
= advance(pC
, pT
);
588 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
594 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
596 if(has_more_tokens(pC
) == true)
598 pC
= advance(pC
, pT
);
602 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
604 compiler_error(26, "Improperly Terminated Array Expression. Symbol ']' Required at this Location.", pS
, pC
, pT
);
607 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
611 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
615 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
621 if(strchr(UNARY_OP
, *pT
) != NULL
)
623 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
624 if(has_more_tokens(pC
) == true)
626 pC
= advance(pC
, pT
);
630 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
635 void parse_subroutine_call()
637 if(settings
.tokens
) { printf("<subroutineCall>\n"); }
640 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
642 compiler_error(35, "Could Not Find Class Name or Subroutine Name at This Location", pS
, pC
, pT
);
645 if(has_more_tokens(pC
) == true)
647 pC
= advance(pC
, pT
);
650 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
654 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
655 if(has_more_tokens(pC
) == true)
657 pC
= advance(pC
, pT
);
660 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
664 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
666 compiler_error(37, "Could Not Find Method Name or Subroutine Name at This Location", pS
, pC
, pT
);
670 if(*pT
!= '(') /* this for calls with no class name at beginning */
672 if(has_more_tokens(pC
) == true)
674 pC
= advance(pC
, pT
);
677 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
683 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
684 if(has_more_tokens(pC
) == true)
686 pC
= advance(pC
, pT
);
689 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
695 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
696 if(has_more_tokens(pC
) == true)
698 pC
= advance(pC
, pT
);
701 compiler_error(38, "Could Not Find Symbol ')' At This Location", pS
, pC
, pT
);
705 compiler_error(39, "Could Not Find Symbol '(' At This Location", pS
, pC
, pT
);
710 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
713 if(settings
.tokens
) { printf("<subroutineCall>\n"); }
716 void parse_expr_lst()
722 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
723 if(has_more_tokens(pC
) == true)
725 pC
= advance(pC
, pT
);
728 compiler_error(24, "Could Not Complete Expression List. Incomplete Program", pS
, pC
, pT
);