Class parser now handles multiple subroutines.
[Jack-Compiler.git] / parse.c
blob31437845df82eaf2145f9996d63c42198a26df05
1 #include <ctype.h>
2 #include <stdbool.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
7 #include "error.h"
8 #include "jack.h"
9 #include "parse.h"
10 #include "token.h"
12 void parse_class()
14 if(settings.tokens)
16 token_print("class", OPEN);
17 space_count++;
20 if(has_more_tokens(pC) == true)
22 pC = advance(pC, pT);
23 tk = token_type(pT);
24 } else {
25 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
28 if(strcmp(pT, "class") == 0 ) {
29 if(settings.tokens) { token_print("keyword", BOTH); }
30 } else {
31 compiler_error(43, "Incorrect Token Found: Must be 'class'", pS, pC, pT);
34 /* look for class name */
35 if(has_more_tokens(pC) == true)
37 pC = advance(pC, pT);
38 tk = token_type(pT);
39 } else {
40 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
43 if (tk == IDENTIFIER){
44 if(settings.tokens) { token_print("identifier", BOTH); }
45 } else {
46 compiler_error(44, "Could Not Find Class Name or Subroutine Name at This Location", pS, pC, pT);
49 /* look for '{' symbol */
50 if(has_more_tokens(pC) == true)
52 pC = advance(pC, pT);
53 tk = token_type(pT);
54 } else {
55 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
58 if (tk == SYMBOL)
60 if(settings.tokens) { token_print("symbol", BOTH); }
61 } else {
62 compiler_error(44, "Could Not Find Class Name or Subroutine Name at This Location", pS, pC, pT);
65 if(has_more_tokens(pC) == true)
67 pC = advance(pC, pT);
68 tk = token_type(pT);
69 } else {
70 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
73 while(strcmp(pT, "static") == 0 || strcmp(pT, "field") == 0)
75 parse_class_var_dec();
76 if(has_more_tokens(pC) == true)
78 pC = advance(pC, pT);
79 tk = token_type(pT);
80 } else {
81 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
85 while(strcmp(pT, "constructor") == 0 || strcmp(pT, "function") == 0 || strcmp(pT, "method") == 0)
87 parse_subroutine();
88 if(has_more_tokens(pC) == true)
90 pC = advance(pC, pT);
91 tk = token_type(pT);
92 } else {
93 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
97 if(*pT == '}')
99 if(settings.tokens) { token_print("symbol", BOTH); }
100 } else {
101 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
104 if(settings.tokens)
106 space_count--;
107 token_print("class", CLOSE);
111 void parse_class_var_dec()
113 if(settings.tokens)
115 token_print("classVarDec", OPEN);
116 space_count++;
119 /* look for 'static' or 'field' */
120 if(settings.tokens) { token_print("keyword", BOTH); }
121 if(has_more_tokens(pC) == true)
123 pC = advance(pC, pT);
124 tk = token_type(pT);
125 } else {
126 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
129 /* look for type */
130 if(tk == IDENTIFIER) {
131 if(settings.tokens) { token_print("identifier", BOTH); }
132 } else if (tk == KEYWORD) {
133 if(strcmp(pT, "int") == 0 || strcmp(pT, "char") == 0 || strcmp(pT, "boolean") == 0) {
134 if(settings.tokens) { token_print("keyword", BOTH); }
135 } else {
136 compiler_error(41, "Token Must be Data Type.", pS, pC, pT);
138 } else {
139 compiler_error(41, "Token Must be Data Type", pS, pC, pT);
142 /* look or variable name */
143 if(has_more_tokens(pC) == true)
145 pC = advance(pC, pT);
146 tk = token_type(pT);
147 } else {
148 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
150 if(tk == IDENTIFIER) {
151 if(settings.tokens) { token_print("identifier", BOTH); }
152 } else {
153 compiler_error(42, "Token Must be Variable Name", pS, pC, pT);
156 /* look for ',' */
157 if(has_more_tokens(pC) == true)
159 pC = advance(pC, pT);
160 tk = token_type(pT);
161 } else {
162 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
165 if(*pT == ',') {
166 do {
167 if(settings.tokens) { token_print("symbol", BOTH); }
168 if(has_more_tokens(pC) == true)
170 pC = advance(pC, pT);
171 tk = token_type(pT);
172 } else {
173 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
175 if(tk == IDENTIFIER) {
176 if(settings.tokens) { token_print("identifier", BOTH); }
177 } else {
178 compiler_error(42, "Token Must be Variable Name", pS, pC, pT);
180 if(has_more_tokens(pC) == true)
182 pC = advance(pC, pT);
183 tk = token_type(pT);
184 } else {
185 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
187 } while (*pT == ',');
190 if(*pT == ';') {
191 if(settings.tokens) { token_print("symbol", BOTH); }
192 } else {
193 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS, pC, pT);
196 if(settings.tokens)
198 space_count--;
199 token_print("classVarDec", CLOSE);
203 void parse_subroutine()
205 if(settings.tokens)
207 token_print("subroutineDec", OPEN);
208 space_count++;
211 if(tk == KEYWORD) {
212 if(strcmp(pT, "constructor") == 0 || strcmp(pT, "function") == 0 || strcmp(pT, "method") == 0)
214 if(settings.tokens) { token_print("keyword", BOTH); }
215 } else {
216 compiler_error(8, "Incorrect Token Found: Must be 'constructor', 'function', or 'method'", pS, pC, pT);
220 /* look for return type of function */
221 if(has_more_tokens(pC) == true)
223 pC = advance(pC, pT);
224 tk = token_type(pT);
225 if(tk == KEYWORD || tk == IDENTIFIER)
227 if(strcmp(pT, "void") == 0)
229 if(settings.tokens) { token_print("keyword", BOTH); }
230 } else {
231 if(settings.tokens) { token_print("identifier", BOTH); }
233 } else {
234 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS, pC, pT);
236 } else {
237 compiler_error(29, "Incorrect Token Type", pS, pC, pT);
240 /* look for subroutine name */
241 if(has_more_tokens(pC) == true)
243 pC = advance(pC, pT);
244 tk = token_type(pT);
245 if(tk == IDENTIFIER)
247 if(settings.tokens) { token_print("identifier", BOTH); }
248 } else {
249 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS, pC, pT);
251 } else {
252 compiler_error(10, "Incorrect Token Type. Looking for Keyword or Identifier.", pS, pC, pT);
255 /* look for symbol '(' that specifies beginning of parameter list */
256 if(has_more_tokens(pC) == true)
258 pC = advance(pC, pT);
259 tk = token_type(pT);
260 if(*pT == '(')
262 if(settings.tokens) { token_print("symbol", BOTH); }
263 parse_params();
264 } else {
265 compiler_error(12, "Parameter List for Function Missing", pS, pC, pT);
267 } else {
268 compiler_error(11, "Name of Function Must be an Identifier", pS, pC, pT);
271 /* look for end of parameter list */
272 if(*pT == ')')
274 if(settings.tokens)
276 token_print("symbol", BOTH);
277 token_print("subroutineBody", OPEN);
278 space_count++;
280 } else {
281 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
284 /* look for opening brace for block */
285 if(has_more_tokens(pC) == true)
287 pC = advance(pC, pT);
288 tk = token_type(pT);
289 if(*pT == '{')
291 if(settings.tokens) { token_print("symbol", BOTH); }
292 if(has_more_tokens(pC) == true)
294 pC = advance(pC, pT);
295 tk = token_type(pT);
296 } else {
297 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
299 parse_var_dec();
301 } else {
302 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS, pC, pT);
305 parse_statements();
307 if(*pT == '}')
309 if(settings.tokens) { token_print("symbol", BOTH); }
310 } else {
311 compiler_error(17, "Could Not Complete Subroutine. Incomplete Program", pS, pC, pT);
314 if(settings.tokens)
316 space_count--;
317 token_print("subroutineBody", CLOSE);
318 space_count--;
319 token_print("subroutineDec", CLOSE);
323 void parse_params()
325 if(*pT == '(') { if(settings.tokens) { token_print("parameterList", OPEN); space_count++; } }
327 /* look for datatype in parameter list */
328 if(has_more_tokens(pC) == true)
330 pC = advance(pC, pT);
331 tk = token_type(pT);
332 if(tk == KEYWORD) {
333 if(strcmp(pT, "int") == 0 || strcmp(pT, "char") == 0 || strcmp(pT, "boolean") == 0)
335 if(settings.tokens) { token_print("keyword", BOTH); }
336 } else {
337 compiler_error(14, "Incorrect Token Type in Parameter List. Looking for Datatype name.", pS, pC, pT);
339 } else if(tk == SYMBOL && *pT == ')') {
340 if(settings.tokens)
342 space_count--;
343 token_print("parameterList", CLOSE);
345 return;
347 } else {
348 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
351 /* look for identifier for this parameter */
352 if(has_more_tokens(pC) == true)
354 pC = advance(pC, pT);
355 tk = token_type(pT);
356 if(tk == IDENTIFIER) {
357 if(settings.tokens) { token_print("identifier", BOTH); }
358 } else {
359 compiler_error(15, "Incorrect Token Type in Parameter List. Looking for Variable Identifier.", pS, pC, pT);
361 } else {
362 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
365 /* are there more parameters? */
366 if(has_more_tokens(pC) == true)
368 pC = advance(pC, pT);
369 tk = token_type(pT);
370 if(*pT == ',') {
371 if(settings.tokens) { token_print("symbol", BOTH); }
372 parse_params();
373 } else if (*pT == ')') { /* exit parse_params */
374 if(settings.tokens)
376 space_count--;
377 token_print("parameterList", CLOSE);
379 return;
380 } else {
381 compiler_error(16, "Incorrect Token Type in Parameter List. Looking for ',' or ')'", pS, pC, pT);
383 } else {
384 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
388 void parse_var_dec()
390 /* look for token named 'var' */
391 if(strcmp(pT, "var") == 0)
393 if(settings.tokens)
395 token_print("varDec", OPEN);
396 token_print("symbol", BOTH);
398 } else { return; }
400 /* look for variable data type */
401 if(has_more_tokens(pC) == true)
403 pC = advance(pC, pT);
404 tk = token_type(pT);
405 if(strcmp(pT, "int") == 0 || strcmp(pT, "char") == 0 || strcmp(pT, "boolean") == 0 || strcmp(pT, "Array") == 0)
407 if(settings.tokens) { token_print("identifier", BOTH); }
409 } else if (tk == IDENTIFIER) { /* could also be a custom class name */
410 if(settings.tokens) { token_print("identifier", BOTH); }
411 } else {
412 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
416 /* look for identifier(s) for variable(s) */
417 do {
418 if(has_more_tokens(pC) == true)
420 pC = advance(pC, pT);
421 tk = token_type(pT);
422 } else {
423 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
425 if(tk == IDENTIFIER)
427 if(settings.tokens) { token_print("identifier", BOTH); }
430 if(has_more_tokens(pC) == true)
432 pC = advance(pC, pT);
433 tk = token_type(pT);
434 } else {
435 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
437 if(tk == SYMBOL)
439 if(settings.tokens) { token_print("symbol", BOTH); }
441 } while (*pT == ',');
443 if(has_more_tokens(pC) == true)
445 pC = advance(pC, pT);
446 tk = token_type(pT);
447 } else {
448 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
450 parse_var_dec();
453 void parse_statements()
455 if(settings.tokens)
457 token_print("statements", OPEN);
458 space_count++;
460 do {
461 if(strcmp(pT, "let") == 0)
463 parse_let();
464 } else if(strcmp(pT, "if") == 0)
466 parse_if();
467 } else if(strcmp(pT, "while") == 0)
469 parse_while();
470 } else if(strcmp(pT, "do") == 0)
472 parse_do();
473 } else if(strcmp(pT, "return") == 0)
475 parse_return();
478 if(has_more_tokens(pC) == true)
480 pC = advance(pC, pT);
481 tk = token_type(pT);
484 } while (strcmp(pT, "let") == 0 || strcmp(pT, "if") == 0 || strcmp(pT, "while") == 0 || \
485 strcmp(pT, "do") == 0 || strcmp(pT, "return") == 0 );
486 if(settings.tokens)
488 space_count--;
489 token_print("statements", CLOSE);
493 void parse_do()
495 if(settings.tokens)
497 token_print("doStatement", OPEN);
498 space_count++;
499 token_print("keyword", BOTH);
502 if(has_more_tokens(pC) == true)
504 pC = advance(pC, pT);
505 tk = token_type(pT);
506 } else {
507 compiler_error(20, "Could Not Complete Do Statement. Incomplete Program", pS, pC, pT);
510 /* must be an identifier */
511 if(tk == IDENTIFIER) {
512 parse_subroutine_call();
513 } else {
514 compiler_error(30, "Subroutine Name Must Be Listed Here", pS, pC, pT);
517 if(*pT == ';')
519 if(settings.tokens) { token_print("symbol", BOTH); }
520 } else {
521 compiler_error(33, "Could Not Find ';' Symbol At This Location.", pS, pC, pT);
524 if(settings.tokens)
526 space_count--;
527 token_print("doStatement", CLOSE);
531 void parse_let()
533 int found_array = 0;
534 if(settings.tokens)
536 token_print("letStatement", OPEN);
537 space_count++;
538 token_print("keyword", BOTH);
541 if(has_more_tokens(pC) == true)
543 pC = advance(pC, pT);
544 tk = token_type(pT);
545 } else {
546 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
549 /* look for an identifier - must be a variable name */
550 if(tk == IDENTIFIER)
552 if(settings.tokens) { token_print("identifier", BOTH); }
553 } else {
554 compiler_error(31, "Could Not Find Identifier At This Location", pS, pC, pT);
557 /* optional '[' for an array offset value */
558 if(has_more_tokens(pC) == true)
560 pC = advance(pC, pT);
561 tk = token_type(pT);
562 } else {
563 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
566 if(*pT == '[')
568 found_array++;
569 if(settings.tokens) { token_print("symbol", BOTH); }
570 if(has_more_tokens(pC) == true)
572 pC = advance(pC, pT);
573 tk = token_type(pT);
574 } else {
575 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
577 parse_expression();
580 /* should be closing ']' here if variable was array */
581 if(found_array && *pT == ']')
583 if(settings.tokens) { token_print("symbol", BOTH); }
584 if(has_more_tokens(pC) == true)
586 pC = advance(pC, pT);
587 tk = token_type(pT);
588 } else {
589 compiler_error(20, "Could Not Find ']' Symbol At This Location", pS, pC, pT);
593 if(*pT == '=')
595 if(settings.tokens) { token_print("symbol", BOTH); }
596 } else {
597 compiler_error(32, "Could Not Find '=' Symbol At This Location", pS, pC, pT);
600 if(has_more_tokens(pC) == true)
602 pC = advance(pC, pT);
603 tk = token_type(pT);
604 } else {
605 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
608 parse_expression();
610 if(*pT == ';')
612 if(settings.tokens) { token_print("symbol", BOTH); }
613 } else {
614 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS, pC, pT);
617 if(settings.tokens)
619 space_count--;
620 token_print("letStatement", CLOSE);
624 void parse_while()
626 if(settings.tokens)
628 token_print("whileStatement", OPEN);
629 space_count++;
630 token_print("keyword", BOTH);
633 if(has_more_tokens(pC) == true)
635 pC = advance(pC, pT);
636 tk = token_type(pT);
637 } else {
638 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
641 if(*pT == '(')
643 if(settings.tokens) { token_print("symbol", BOTH); }
644 } else {
645 compiler_error(39, "Could Not Find '(' Symbol At This Location", pS, pC, pT);
648 if(has_more_tokens(pC) == true)
650 pC = advance(pC, pT);
651 tk = token_type(pT);
652 } else {
653 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
656 parse_expression();
658 if(*pT == ')')
660 if(settings.tokens) { token_print("symbol", BOTH); }
661 } else {
662 compiler_error(38, "Could Not Find ')' Symbol At This Location", pS, pC, pT);
665 if(has_more_tokens(pC) == true)
667 pC = advance(pC, pT);
668 tk = token_type(pT);
669 } else {
670 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
673 if(*pT == '{')
675 if(settings.tokens) { token_print("symbol", BOTH); }
676 } else {
677 compiler_error(45, "Could Not Find '{' Symbol At This Location", pS, pC, pT);
680 if(has_more_tokens(pC) == true)
682 pC = advance(pC, pT);
683 tk = token_type(pT);
684 } else {
685 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
688 parse_statements();
690 if(*pT == '}')
692 if(settings.tokens) { token_print("symbol", BOTH); }
693 } else {
694 compiler_error(46, "Could Not Find '}' Symbol At This Location", pS, pC, pT);
697 if(settings.tokens)
699 space_count--;
700 token_print("whileStatement", CLOSE);
704 void parse_return()
706 if(settings.tokens)
708 token_print("returnStatement", OPEN);
709 space_count++;
710 token_print("keyword", BOTH);
713 /* look for ';' */
714 if(has_more_tokens(pC) == true)
716 pC = advance(pC, pT);
717 tk = token_type(pT);
718 } else {
719 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
722 if (*pT != ';') { parse_expression(); }
724 if(*pT == ';')
726 if(settings.tokens) { token_print("symbol", BOTH); }
727 } else {
728 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS, pC, pT);
730 if(settings.tokens)
732 space_count--;
733 token_print("returnStatement", CLOSE);
737 void parse_if()
739 if(settings.tokens)
741 token_print("ifStatement", OPEN);
742 space_count++;
743 token_print("keyword", BOTH);
746 if(has_more_tokens(pC) == true)
748 pC = advance(pC, pT);
749 tk = token_type(pT);
750 } else {
751 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
754 if(*pT == '(')
756 if(settings.tokens) { token_print("symbol", BOTH); }
757 } else {
758 compiler_error(39, "Could Not Find '(' Symbol At This Location", pS, pC, pT);
761 if(has_more_tokens(pC) == true)
763 pC = advance(pC, pT);
764 tk = token_type(pT);
765 } else {
766 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
769 parse_expression();
771 if(*pT == ')')
773 if(settings.tokens) { token_print("symbol", BOTH); }
774 } else {
775 compiler_error(38, "Could Not Find ')' Symbol At This Location", pS, pC, pT);
778 if(has_more_tokens(pC) == true)
780 pC = advance(pC, pT);
781 tk = token_type(pT);
782 } else {
783 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
786 if(*pT == '{')
788 if(settings.tokens) { token_print("symbol", BOTH); }
789 } else {
790 compiler_error(45, "Could Not Find '{' Symbol At This Location", pS, pC, pT);
793 if(has_more_tokens(pC) == true)
795 pC = advance(pC, pT);
796 tk = token_type(pT);
797 } else {
798 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
801 parse_statements();
803 if(*pT == '}')
805 if(settings.tokens) { token_print("symbol", BOTH); }
806 } else {
807 compiler_error(46, "Could Not Find '}' Symbol At This Location", pS, pC, pT);
810 if(settings.tokens)
812 space_count--;
813 token_print("ifStatement", CLOSE);
817 void parse_expression()
819 if(settings.tokens)
821 token_print("expression", OPEN);
822 space_count++;
824 parse_term();
826 if(strchr(BINARY_OP, *pT) != NULL)
828 if(settings.tokens) { token_print("operator", BOTH); }
829 if(has_more_tokens(pC) == true)
831 pC = advance(pC, pT);
832 tk = token_type(pT);
833 } else {
834 compiler_error(34, "Could Not Parse Expression. Incomplete Program", pS, pC, pT);
836 parse_expression();
838 if(settings.tokens)
840 space_count--;
841 token_print("expression", CLOSE);
845 void parse_term()
847 if(settings.tokens)
849 token_print("term", OPEN);
850 space_count++;
853 if(tk == INT_CONST)
855 if(settings.tokens) { token_print("intConst", BOTH); }
856 if(has_more_tokens(pC) == true)
858 pC = advance(pC, pT);
859 tk = token_type(pT);
860 } else {
861 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
863 return;
866 if(strchr(UNARY_OP, *pT) != NULL)
868 if(settings.tokens) { token_print("unaryOperator", BOTH); }
869 if(has_more_tokens(pC) == true)
871 pC = advance(pC, pT);
872 tk = token_type(pT);
873 } else {
874 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
876 parse_term();
879 if(tk == KEYWORD)
881 if(settings.tokens) { token_print("keyword", BOTH); }
883 if(has_more_tokens(pC) == true)
885 pC = advance(pC, pT);
886 tk = token_type(pT);
887 } else {
888 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
892 if(tk == IDENTIFIER)
894 if(settings.tokens) { token_print("identifier", BOTH); }
896 if(has_more_tokens(pC) == true)
898 pC = advance(pC, pT);
899 tk = token_type(pT);
900 } else {
901 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
905 switch(*pT)
907 case '[':
908 if(settings.tokens) { token_print("symbol", BOTH); }
910 if(has_more_tokens(pC) == true)
912 pC = advance(pC, pT);
913 tk = token_type(pT);
914 } else {
915 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
917 parse_expression();
918 if(*pT == ']')
920 if(settings.tokens) { token_print("symbol", BOTH); }
921 /* parse_expression(); */
922 } else {
923 compiler_error(26, "Improperly Terminated Array Expression. Symbol ']' Required at this Location.", pS, pC, pT);
926 if(has_more_tokens(pC) == true)
928 pC = advance(pC, pT);
929 tk = token_type(pT);
930 } else {
931 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
933 break;
934 case '(':
935 if(settings.tokens) { token_print("symbol", BOTH); }
937 if(has_more_tokens(pC) == true)
939 pC = advance(pC, pT);
940 tk = token_type(pT);
941 } else {
942 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
945 parse_expression();
947 if (*pT == ')') {
948 if(settings.tokens) { token_print("symbol", BOTH); }
949 } else {
950 compiler_error(38, "Could Not Find Symbol ')' At This Location", pS, pC, pT);
953 if(has_more_tokens(pC) == true)
955 pC = advance(pC, pT);
956 tk = token_type(pT);
957 } else {
958 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
960 break;
961 case '.':
962 if(settings.tokens) { token_print("symbol", BOTH); }
964 if(has_more_tokens(pC) == true)
966 pC = advance(pC, pT);
967 tk = token_type(pT);
968 } else {
969 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
972 parse_subroutine_call();
973 break;
976 if(settings.tokens)
978 space_count--;
979 token_print("term", CLOSE);
983 void parse_subroutine_call()
985 if(tk == IDENTIFIER)
987 if(settings.tokens) { token_print("identifier", BOTH); }
988 } else {
989 compiler_error(35, "Could Not Find Class Name or Subroutine Name at This Location", pS, pC, pT);
992 if(has_more_tokens(pC) == true)
994 pC = advance(pC, pT);
995 tk = token_type(pT);
996 } else {
997 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
1000 if (*pT == '.') {
1001 if(settings.tokens) { token_print("symbol", BOTH); }
1002 if(has_more_tokens(pC) == true)
1004 pC = advance(pC, pT);
1005 tk = token_type(pT);
1006 } else {
1007 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
1009 if(tk == IDENTIFIER)
1011 if(settings.tokens) { token_print("identifier", BOTH); }
1012 } else {
1013 compiler_error(37, "Could Not Find Method Name or Subroutine Name at This Location", pS, pC, pT);
1017 if(*pT != '(') /* this for calls with no class name at beginning */
1019 if(has_more_tokens(pC) == true)
1021 pC = advance(pC, pT);
1022 tk = token_type(pT);
1023 } else {
1024 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
1028 if(*pT == '(')
1030 if(settings.tokens) { token_print("symbol", BOTH); }
1031 } else {
1032 compiler_error(39, "Could Not Find Symbol '(' At This Location", pS, pC, pT);
1035 if(has_more_tokens(pC) == true)
1037 pC = advance(pC, pT);
1038 tk = token_type(pT);
1039 } else {
1040 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
1043 parse_expr_lst();
1045 if(*pT == ')')
1047 if(settings.tokens) { token_print("symbol", BOTH); }
1048 } else {
1049 compiler_error(38, "Could Not Find Symbol ')' At This Location", pS, pC, pT);
1052 if(has_more_tokens(pC) == true)
1054 pC = advance(pC, pT);
1055 tk = token_type(pT);
1056 } else {
1057 compiler_error(24, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
1061 void parse_expr_lst()
1063 if(settings.tokens) { token_print("expressionList", OPEN); }
1064 while(*pT != ')')
1066 if(*pT == ',')
1068 if(settings.tokens) { token_print("symbol", BOTH); }
1069 if(has_more_tokens(pC) == true)
1071 pC = advance(pC, pT);
1072 tk = token_type(pT);
1073 } else {
1074 compiler_error(24, "Could Not Complete Expression List. Incomplete Program", pS, pC, pT);
1076 } else {
1077 parse_expression();
1080 if(settings.tokens) { token_print("expressionList", CLOSE); }