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
);
68 while(strcmp(pT
, "static") == 0 || strcmp(pT
, "field") == 0)
70 parse_class_var_dec();
71 if(has_more_tokens(pC
) == true)
76 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
82 if(settings
.tokens
) { printf("</class>\n"); }
85 void parse_class_var_dec()
87 if(settings
.tokens
) { printf("<classVarDec>\n"); }
89 /* look for 'static' or 'field' */
90 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
91 if(has_more_tokens(pC
) == true)
96 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
100 if(tk
== IDENTIFIER
) {
101 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
102 } else if (tk
== KEYWORD
) {
103 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0) {
104 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
106 compiler_error(41, "Token Must be Data Type.", pS
, pC
, pT
);
109 compiler_error(41, "Token Must be Data Type", pS
, pC
, pT
);
112 /* look or variable name */
113 if(has_more_tokens(pC
) == true)
115 pC
= advance(pC
, pT
);
118 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
120 if(tk
== IDENTIFIER
) {
121 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
123 compiler_error(42, "Token Must be Variable Name", pS
, pC
, pT
);
127 if(has_more_tokens(pC
) == true)
129 pC
= advance(pC
, pT
);
132 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
137 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
138 if(has_more_tokens(pC
) == true)
140 pC
= advance(pC
, pT
);
143 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
145 if(tk
== IDENTIFIER
) {
146 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
148 compiler_error(42, "Token Must be Variable Name", pS
, pC
, pT
);
150 if(has_more_tokens(pC
) == true)
152 pC
= advance(pC
, pT
);
155 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
157 } while (*pT
== ',');
161 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
163 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
166 if(settings
.tokens
) { printf("</classVarDec>\n"); }
169 void parse_subroutine()
171 if(settings
.tokens
) { printf("<subroutineDec>\n"); }
174 if(strcmp(pT
, "constructor") == 0 || strcmp(pT
, "function") == 0 || strcmp(pT
, "method") == 0)
176 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
178 compiler_error(8, "Incorrect Token Found: Must be 'constructor', 'function', or 'method'", pS
, pC
, pT
);
182 /* look for return type of function */
183 if(has_more_tokens(pC
) == true)
185 pC
= advance(pC
, pT
);
187 if(tk
== KEYWORD
|| tk
== IDENTIFIER
)
189 if(strcmp(pT
, "void") == 0)
191 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
193 if(settings
.tokens
) { printf("\t<type>%s</type>\n", pT
); }
196 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
199 compiler_error(29, "Incorrect Token Type", pS
, pC
, pT
);
202 /* look for subroutine name */
203 if(has_more_tokens(pC
) == true)
205 pC
= advance(pC
, pT
);
209 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
211 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
214 compiler_error(10, "Incorrect Token Type. Looking for Keyword or Identifier.", pS
, pC
, pT
);
217 /* look for symbol '(' that specifies beginning of parameter list */
218 if(has_more_tokens(pC
) == true)
220 pC
= advance(pC
, pT
);
224 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
227 compiler_error(12, "Parameter List for Function Missing", pS
, pC
, pT
);
230 compiler_error(11, "Name of Function Must be an Identifier", pS
, pC
, pT
);
233 /* look for end of parameter list */
236 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
238 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
241 /* look for opening brace for block */
242 if(has_more_tokens(pC
) == true)
244 pC
= advance(pC
, pT
);
248 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
249 if(has_more_tokens(pC
) == true)
251 pC
= advance(pC
, pT
);
254 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
259 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
262 while(strcmp(pT
, "let") == 0 || strcmp(pT
, "if") == 0 || strcmp(pT
, "while") == 0 || strcmp(pT
, "do") == 0 || strcmp(pT
, "return") == 0)
265 if(has_more_tokens(pC
) == true)
267 pC
= advance(pC
, pT
);
270 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
274 if(settings
.tokens
) { printf("</subroutineDec>\n"); }
279 if(*pT
== '(') { if(settings
.tokens
) { printf("<parameterList>\n"); } }
281 /* look for datatype in parameter list */
282 if(has_more_tokens(pC
) == true)
284 pC
= advance(pC
, pT
);
287 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0)
289 if(settings
.tokens
) { printf("\t<keyword>%s</keyword>\n", pT
); }
291 compiler_error(14, "Incorrect Token Type in Parameter List. Looking for Datatype name.", pS
, pC
, pT
);
293 } else if(tk
== SYMBOL
&& *pT
== ')') { if(settings
.tokens
) { printf("</parameterList>\n"); } return; }
295 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
298 /* look for identifier for this parameter */
299 if(has_more_tokens(pC
) == true)
301 pC
= advance(pC
, pT
);
303 if(tk
== IDENTIFIER
) {
304 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
307 compiler_error(15, "Incorrect Token Type in Parameter List. Looking for Variable Identifier.", pS
, pC
, pT
);
310 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
313 /* are there more parameters? */
314 if(has_more_tokens(pC
) == true)
316 pC
= advance(pC
, pT
);
320 } else if (*pT
== ')') { /* exit parse_params */
321 if(settings
.tokens
) { printf("</parameterList>\n"); }
324 compiler_error(16, "Incorrect Token Type in Parameter List. Looking for ',' or ')'", pS
, pC
, pT
);
327 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
333 /* look for token named 'var' */
334 if(strcmp(pT
, "var") == 0)
336 if(settings
.tokens
) { printf("<varDec>\n\t<symbol>%s</symbol>\n", pT
); }
339 /* look for variable data type */
340 if(has_more_tokens(pC
) == true)
342 pC
= advance(pC
, pT
);
344 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0 || strcmp(pT
, "Array") == 0)
346 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
348 } else if (tk
== IDENTIFIER
) { /* could also be a custom class name */
349 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
351 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
355 /* look for identifier(s) for variable(s) */
357 if(has_more_tokens(pC
) == true)
359 pC
= advance(pC
, pT
);
362 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
366 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
369 if(has_more_tokens(pC
) == true)
371 pC
= advance(pC
, pT
);
374 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
378 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
380 } while (*pT
== ',');
382 if(has_more_tokens(pC
) == true)
384 pC
= advance(pC
, pT
);
387 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
392 void parse_statements()
394 if(strcmp(pT
, "let") == 0)
397 } else if(strcmp(pT
, "if") == 0)
400 } else if(strcmp(pT
, "while") == 0)
403 } else if(strcmp(pT
, "do") == 0)
406 } else if(strcmp(pT
, "return") == 0)
414 if(settings
.tokens
) { printf("<doStatement>\n\t<keyword>do</keyword>\n"); }
415 if(has_more_tokens(pC
) == true)
417 pC
= advance(pC
, pT
);
420 compiler_error(20, "Could Not Complete Do Statement. Incomplete Program", pS
, pC
, pT
);
423 /* must be an identifier */
424 if(tk
== IDENTIFIER
) {
425 parse_subroutine_call();
427 compiler_error(30, "Subroutine Name Must Be Listed Here", pS
, pC
, pT
);
430 if(settings
.tokens
) { printf("</doStatement>\n"); }
435 if(settings
.tokens
) { printf("<letStatement>\n\t<keyword>let</keyword>\n"); }
436 if(has_more_tokens(pC
) == true)
438 pC
= advance(pC
, pT
);
441 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
444 /* look for an identifier - must be a variable name */
447 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
449 compiler_error(31, "Could Not Find Identifier At This Location", pS
, pC
, pT
);
452 /* optional '[' for an array offset value */
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
);
463 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
469 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
475 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
477 compiler_error(32, "Could Not Find '=' Symbol At This Location", pS
, pC
, pT
);
480 /* parse_expression(); */
482 /* look for identifier */
483 if(has_more_tokens(pC
) == true)
485 pC
= advance(pC
, pT
);
488 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
493 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
495 compiler_error(20, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
499 if(has_more_tokens(pC
) == true)
501 pC
= advance(pC
, pT
);
504 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
509 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
511 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
514 if(settings
.tokens
) { printf("</letStatement>\n"); }
520 exit_error(0, "Parsing While");
525 if(settings
.tokens
) { printf("<returnStatement>\n\t<identifier>%s</identifier>\n", pT
); }
528 if(has_more_tokens(pC
) == true)
530 pC
= advance(pC
, pT
);
533 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
536 if (*pT
!= ';') { parse_expression(); }
540 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
542 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
544 if(settings
.tokens
) { printf("</returnStatement>\n"); }
549 exit_error(0, "Parsing If");
552 void parse_expression()
555 if(settings
.tokens
) { printf("<expression>%s</expression>\n", pT
); }
556 if(has_more_tokens(pC
) == true)
558 pC
= advance(pC
, pT
);
561 compiler_error(34, "Could Not Parse Expression. Incomplete Program", pS
, pC
, pT
);
563 } while (*pT
!= ']' && *pT
!= ';');
568 if(has_more_tokens(pC
) == true)
570 pC
= advance(pC
, pT
);
573 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
579 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
581 if(has_more_tokens(pC
) == true)
583 pC
= advance(pC
, pT
);
587 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
589 compiler_error(26, "Improperly Terminated Array Expression. Symbol ']' Required at this Location.", pS
, pC
, pT
);
592 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
596 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
600 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
606 if(strchr(UNARY_OP
, *pT
) != NULL
)
608 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
609 if(has_more_tokens(pC
) == true)
611 pC
= advance(pC
, pT
);
615 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
620 void parse_subroutine_call()
622 if(settings
.tokens
) { printf("<subroutineCall>\n"); }
625 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
627 compiler_error(35, "Could Not Find Class Name or Subroutine Name at This Location", pS
, pC
, pT
);
630 if(has_more_tokens(pC
) == true)
632 pC
= advance(pC
, pT
);
635 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
639 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
640 if(has_more_tokens(pC
) == true)
642 pC
= advance(pC
, pT
);
645 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
649 if(settings
.tokens
) { printf("\t<identifier>%s</identifier>\n", pT
); }
651 compiler_error(37, "Could Not Find Method Name or Subroutine Name at This Location", pS
, pC
, pT
);
655 if(*pT
!= '(') /* this for calls with no class name at beginning */
657 if(has_more_tokens(pC
) == true)
659 pC
= advance(pC
, pT
);
662 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
668 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
669 if(has_more_tokens(pC
) == true)
671 pC
= advance(pC
, pT
);
674 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
680 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
681 if(has_more_tokens(pC
) == true)
683 pC
= advance(pC
, pT
);
686 compiler_error(38, "Could Not Find Symbol ')' At This Location", pS
, pC
, pT
);
690 compiler_error(39, "Could Not Find Symbol '(' At This Location", pS
, pC
, pT
);
695 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
698 if(settings
.tokens
) { printf("<subroutineCall>\n"); }
701 void parse_expr_lst()
707 if(settings
.tokens
) { printf("\t<symbol>%s</symbol>\n", pT
); }
708 if(has_more_tokens(pC
) == true)
710 pC
= advance(pC
, pT
);
713 compiler_error(24, "Could Not Complete Expression List. Incomplete Program", pS
, pC
, pT
);