Added tokens to fit specification.
[Jack-Compiler.git] / parse.c
blob7ed198047a877697f252ce6e471a91830c623770
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) { printf("<class>\n"); }
16 if(has_more_tokens(pC) == true)
18 pC = advance(pC, pT);
19 tk = token_type(pT);
20 } else {
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); }
26 } else {
27 compiler_error(43, "Incorrect Token Found: Must be 'class'", pS, pC, pT);
30 /* look for class name */
31 if(has_more_tokens(pC) == true)
33 pC = advance(pC, pT);
34 tk = token_type(pT);
35 } else {
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); }
41 } else {
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)
48 pC = advance(pC, pT);
49 tk = token_type(pT);
50 } else {
51 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
54 if (tk == SYMBOL){
55 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
56 } else {
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)
62 pC = advance(pC, pT);
63 tk = token_type(pT);
64 } else {
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)
73 pC = advance(pC, pT);
74 tk = token_type(pT);
75 } else {
76 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
80 parse_subroutine();
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)
93 pC = advance(pC, pT);
94 tk = token_type(pT);
95 } else {
96 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
99 /* look for type */
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); }
105 } else {
106 compiler_error(41, "Token Must be Data Type.", pS, pC, pT);
108 } else {
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);
116 tk = token_type(pT);
117 } else {
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); }
122 } else {
123 compiler_error(42, "Token Must be Variable Name", pS, pC, pT);
126 /* look for ',' */
127 if(has_more_tokens(pC) == true)
129 pC = advance(pC, pT);
130 tk = token_type(pT);
131 } else {
132 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
135 if(*pT == ',') {
136 do {
137 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
138 if(has_more_tokens(pC) == true)
140 pC = advance(pC, pT);
141 tk = token_type(pT);
142 } else {
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); }
147 } else {
148 compiler_error(42, "Token Must be Variable Name", pS, pC, pT);
150 if(has_more_tokens(pC) == true)
152 pC = advance(pC, pT);
153 tk = token_type(pT);
154 } else {
155 compiler_error(40, "Incomplete Class Declaration", pS, pC, pT);
157 } while (*pT == ',');
160 if(*pT == ';') {
161 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
162 } else {
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"); }
173 if(tk == KEYWORD) {
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); }
177 } else {
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);
186 tk = token_type(pT);
187 if(tk == KEYWORD || tk == IDENTIFIER)
189 if(strcmp(pT, "void") == 0)
191 if(settings.tokens) { printf("\t<keyword>%s</keyword>\n", pT); }
192 } else {
193 if(settings.tokens) { printf("\t<type>%s</type>\n", pT); }
195 } else {
196 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS, pC, pT);
198 } else {
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);
206 tk = token_type(pT);
207 if(tk == IDENTIFIER)
209 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
210 } else {
211 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS, pC, pT);
213 } else {
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);
221 tk = token_type(pT);
222 if(*pT == '(')
224 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
225 parse_params();
226 } else {
227 compiler_error(12, "Parameter List for Function Missing", pS, pC, pT);
229 } else {
230 compiler_error(11, "Name of Function Must be an Identifier", pS, pC, pT);
233 /* look for end of parameter list */
234 if(*pT == ')')
236 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n<subroutineBody>\n", pT); }
237 } else {
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);
245 tk = token_type(pT);
246 if(*pT == '{')
248 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
249 if(has_more_tokens(pC) == true)
251 pC = advance(pC, pT);
252 tk = token_type(pT);
253 } else {
254 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
256 parse_var_dec();
258 } else {
259 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS, pC, pT);
262 parse_statements();
264 if(settings.tokens) { printf("</subroutineBody>\n</subroutineDec>\n"); }
267 void parse_params()
269 if(*pT == '(') { if(settings.tokens) { printf("<parameterList>\n"); } }
271 /* look for datatype in parameter list */
272 if(has_more_tokens(pC) == true)
274 pC = advance(pC, pT);
275 tk = token_type(pT);
276 if(tk == KEYWORD) {
277 if(strcmp(pT, "int") == 0 || strcmp(pT, "char") == 0 || strcmp(pT, "boolean") == 0)
279 if(settings.tokens) { printf("\t<keyword>%s</keyword>\n", pT); }
280 } else {
281 compiler_error(14, "Incorrect Token Type in Parameter List. Looking for Datatype name.", pS, pC, pT);
283 } else if(tk == SYMBOL && *pT == ')') { if(settings.tokens) { printf("</parameterList>\n"); } return; }
284 } else {
285 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
288 /* look for identifier for this parameter */
289 if(has_more_tokens(pC) == true)
291 pC = advance(pC, pT);
292 tk = token_type(pT);
293 if(tk == IDENTIFIER) {
294 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
295 } else {
296 compiler_error(15, "Incorrect Token Type in Parameter List. Looking for Variable Identifier.", pS, pC, pT);
298 } else {
299 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
302 /* are there more parameters? */
303 if(has_more_tokens(pC) == true)
305 pC = advance(pC, pT);
306 tk = token_type(pT);
307 if(*pT == ',') {
308 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
309 parse_params();
310 } else if (*pT == ')') { /* exit parse_params */
311 if(settings.tokens) { printf("</parameterList>\n"); }
312 return;
313 } else {
314 compiler_error(16, "Incorrect Token Type in Parameter List. Looking for ',' or ')'", pS, pC, pT);
316 } else {
317 compiler_error(13, "Could Not Complete Parameter List for Function", pS, pC, pT);
321 void parse_var_dec()
323 /* look for token named 'var' */
324 if(strcmp(pT, "var") == 0)
326 if(settings.tokens) { printf("<varDec>\n\t<symbol>%s</symbol>\n", pT); }
327 } else { return; }
329 /* look for variable data type */
330 if(has_more_tokens(pC) == true)
332 pC = advance(pC, pT);
333 tk = token_type(pT);
334 if(strcmp(pT, "int") == 0 || strcmp(pT, "char") == 0 || strcmp(pT, "boolean") == 0 || strcmp(pT, "Array") == 0)
336 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
338 } else if (tk == IDENTIFIER) { /* could also be a custom class name */
339 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
340 } else {
341 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
345 /* look for identifier(s) for variable(s) */
346 do {
347 if(has_more_tokens(pC) == true)
349 pC = advance(pC, pT);
350 tk = token_type(pT);
351 } else {
352 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
354 if(tk == IDENTIFIER)
356 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
359 if(has_more_tokens(pC) == true)
361 pC = advance(pC, pT);
362 tk = token_type(pT);
363 } else {
364 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
366 if(tk == SYMBOL)
368 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
370 } while (*pT == ',');
372 if(has_more_tokens(pC) == true)
374 pC = advance(pC, pT);
375 tk = token_type(pT);
376 } else {
377 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS, pC, pT);
379 parse_var_dec();
382 void parse_statements()
384 if(settings.tokens) { printf("<statements>\n"); }
385 do {
386 if(strcmp(pT, "let") == 0)
388 parse_let();
389 } else if(strcmp(pT, "if") == 0)
391 parse_if();
392 } else if(strcmp(pT, "while") == 0)
394 parse_while();
395 } else if(strcmp(pT, "do") == 0)
397 parse_do();
398 } else if(strcmp(pT, "return") == 0)
400 parse_return();
403 if(has_more_tokens(pC) == true)
405 pC = advance(pC, pT);
406 tk = token_type(pT);
409 } while (strcmp(pT, "let") == 0 || strcmp(pT, "if") == 0 || strcmp(pT, "while") == 0 || \
410 strcmp(pT, "do") == 0 || strcmp(pT, "return") == 0 );
411 if(settings.tokens) { printf("</statements>\n"); }
414 void parse_do()
416 if(settings.tokens) { printf("<doStatement>\n\t<keyword>do</keyword>\n"); }
418 if(has_more_tokens(pC) == true)
420 pC = advance(pC, pT);
421 tk = token_type(pT);
422 } else {
423 compiler_error(20, "Could Not Complete Do Statement. Incomplete Program", pS, pC, pT);
426 /* must be an identifier */
427 if(tk == IDENTIFIER) {
428 parse_subroutine_call();
429 } else {
430 compiler_error(30, "Subroutine Name Must Be Listed Here", pS, pC, pT);
433 if(*pT == ';')
435 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
436 } else {
437 compiler_error(33, "Could Not Find ';' Symbol At This Location.", pS, pC, pT);
440 if(settings.tokens) { printf("</doStatement>\n"); }
443 void parse_let()
445 int found_array = 0;
446 if(settings.tokens) { printf("<letStatement>\n\t<keyword>let</keyword>\n"); }
447 if(has_more_tokens(pC) == true)
449 pC = advance(pC, pT);
450 tk = token_type(pT);
451 } else {
452 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
455 /* look for an identifier - must be a variable name */
456 if(tk == IDENTIFIER)
458 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
459 } else {
460 compiler_error(31, "Could Not Find Identifier At This Location", pS, pC, pT);
463 /* optional '[' for an array offset value */
464 if(has_more_tokens(pC) == true)
466 pC = advance(pC, pT);
467 tk = token_type(pT);
468 } else {
469 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
472 if(*pT == '[')
474 found_array++;
475 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
476 if(has_more_tokens(pC) == true)
478 pC = advance(pC, pT);
479 tk = token_type(pT);
480 } else {
481 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
483 parse_expression();
486 /* should be closing ']' here if variable was array */
487 if(found_array && *pT == ']')
489 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
490 if(has_more_tokens(pC) == true)
492 pC = advance(pC, pT);
493 tk = token_type(pT);
494 } else {
495 compiler_error(20, "Could Not Find ']' Symbol At This Location", pS, pC, pT);
499 if(*pT == '=')
501 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
502 } else {
503 compiler_error(32, "Could Not Find '=' Symbol At This Location", pS, pC, pT);
506 if(has_more_tokens(pC) == true)
508 pC = advance(pC, pT);
509 tk = token_type(pT);
510 } else {
511 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
514 parse_expression();
516 if(*pT == ';')
518 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
519 } else {
520 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS, pC, pT);
523 if(settings.tokens) { printf("</letStatement>\n"); }
526 void parse_while()
528 if(settings.tokens) { printf("<whileStatement>\n\t<identifier>%s</identifier>\n", pT); }
530 if(has_more_tokens(pC) == true)
532 pC = advance(pC, pT);
533 tk = token_type(pT);
534 } else {
535 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
538 if(*pT == '(')
540 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
541 } else {
542 compiler_error(39, "Could Not Find '(' Symbol At This Location", pS, pC, pT);
545 if(has_more_tokens(pC) == true)
547 pC = advance(pC, pT);
548 tk = token_type(pT);
549 } else {
550 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
553 parse_expression();
555 if(*pT == ')')
557 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
558 } else {
559 compiler_error(38, "Could Not Find ')' Symbol At This Location", pS, pC, pT);
562 if(has_more_tokens(pC) == true)
564 pC = advance(pC, pT);
565 tk = token_type(pT);
566 } else {
567 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
570 if(*pT == '{')
572 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
573 } else {
574 compiler_error(45, "Could Not Find '{' Symbol At This Location", pS, pC, pT);
577 if(has_more_tokens(pC) == true)
579 pC = advance(pC, pT);
580 tk = token_type(pT);
581 } else {
582 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS, pC, pT);
585 parse_statements();
587 if(*pT == '}')
589 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
590 } else {
591 compiler_error(46, "Could Not Find '}' Symbol At This Location", pS, pC, pT);
594 if(settings.tokens) { printf("</whileStatement>\n"); }
597 void parse_return()
599 if(settings.tokens) { printf("<returnStatement>\n\t<identifier>%s</identifier>\n", pT); }
601 /* look for ';' */
602 if(has_more_tokens(pC) == true)
604 pC = advance(pC, pT);
605 tk = token_type(pT);
606 } else {
607 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS, pC, pT);
610 if (*pT != ';') { parse_expression(); }
612 if(*pT == ';')
614 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
615 } else {
616 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS, pC, pT);
618 if(settings.tokens) { printf("</returnStatement>\n"); }
621 void parse_if()
623 if(settings.tokens) { printf("<ifStatement>\n\t<identifier>%s</identifier>\n", pT); }
625 if(has_more_tokens(pC) == true)
627 pC = advance(pC, pT);
628 tk = token_type(pT);
629 } else {
630 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
633 if(*pT == '(')
635 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
636 } else {
637 compiler_error(39, "Could Not Find '(' Symbol At This Location", pS, pC, pT);
640 if(has_more_tokens(pC) == true)
642 pC = advance(pC, pT);
643 tk = token_type(pT);
644 } else {
645 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
648 parse_expression();
650 if(*pT == ')')
652 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
653 } else {
654 compiler_error(38, "Could Not Find ')' Symbol At This Location", pS, pC, pT);
657 if(has_more_tokens(pC) == true)
659 pC = advance(pC, pT);
660 tk = token_type(pT);
661 } else {
662 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
665 if(*pT == '{')
667 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
668 } else {
669 compiler_error(45, "Could Not Find '{' Symbol At This Location", pS, pC, pT);
672 if(has_more_tokens(pC) == true)
674 pC = advance(pC, pT);
675 tk = token_type(pT);
676 } else {
677 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT);
680 parse_statements();
682 if(*pT == '}')
684 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
685 } else {
686 compiler_error(46, "Could Not Find '}' Symbol At This Location", pS, pC, pT);
689 if(settings.tokens) { printf("</ifStatement>\n"); }
692 void parse_expression()
694 if(settings.tokens) { printf("\t<expression>\n"); }
695 parse_term();
697 if(strchr(BINARY_OP, *pT) != NULL)
699 if(settings.tokens) { printf("\t<operator>%s</operator>\n", pT); }
700 if(has_more_tokens(pC) == true)
702 pC = advance(pC, pT);
703 tk = token_type(pT);
704 } else {
705 compiler_error(34, "Could Not Parse Expression. Incomplete Program", pS, pC, pT);
707 parse_expression();
709 if(settings.tokens) { printf("\t</expression>\n"); }
712 void parse_term()
714 if(settings.tokens) { printf("<term>\n"); }
715 if(tk == INT_CONST)
717 if(settings.tokens) { printf("\t<intConst>%s</intConst>\n", pT); }
718 if(has_more_tokens(pC) == true)
720 pC = advance(pC, pT);
721 tk = token_type(pT);
722 } else {
723 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
725 return;
728 if(strchr(UNARY_OP, *pT) != NULL)
730 if(settings.tokens) { printf("<unaryOperator>%s</unaryOperator>\n", pT); }
731 if(has_more_tokens(pC) == true)
733 pC = advance(pC, pT);
734 tk = token_type(pT);
735 } else {
736 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
738 parse_term();
741 if(tk == IDENTIFIER)
743 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
745 if(has_more_tokens(pC) == true)
747 pC = advance(pC, pT);
748 tk = token_type(pT);
749 } else {
750 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
752 parse_term();
755 if(tk == KEYWORD)
757 if(settings.tokens) { printf("\t<keyword>%s</keywordr>\n", pT); }
759 if(has_more_tokens(pC) == true)
761 pC = advance(pC, pT);
762 tk = token_type(pT);
763 } else {
764 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
768 switch(*pT)
770 case '[':
771 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
773 if(has_more_tokens(pC) == true)
775 pC = advance(pC, pT);
776 tk = token_type(pT);
777 } else {
778 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
780 parse_expression();
781 if(*pT == ']')
783 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
784 /* parse_expression(); */
785 } else {
786 compiler_error(26, "Improperly Terminated Array Expression. Symbol ']' Required at this Location.", pS, pC, pT);
789 if(has_more_tokens(pC) == true)
791 pC = advance(pC, pT);
792 tk = token_type(pT);
793 } else {
794 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
796 break;
797 case '(':
798 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
800 if(has_more_tokens(pC) == true)
802 pC = advance(pC, pT);
803 tk = token_type(pT);
804 } else {
805 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
808 parse_expression();
810 if (*pT == ')') {
811 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
812 } else {
813 compiler_error(38, "Could Not Find Symbol ')' At This Location", pS, pC, pT);
816 if(has_more_tokens(pC) == true)
818 pC = advance(pC, pT);
819 tk = token_type(pT);
820 } else {
821 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
823 break;
824 case '.':
825 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
827 if(has_more_tokens(pC) == true)
829 pC = advance(pC, pT);
830 tk = token_type(pT);
831 } else {
832 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS, pC, pT);
835 parse_subroutine_call();
836 break;
837 default:
838 return;
840 if(settings.tokens) { printf("</term>\n"); }
843 void parse_subroutine_call()
845 if(settings.tokens) { printf("\t<subroutineCall>\n"); }
846 if(tk == IDENTIFIER)
848 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
849 } else {
850 compiler_error(35, "Could Not Find Class Name or Subroutine Name at This Location", pS, pC, pT);
853 if(has_more_tokens(pC) == true)
855 pC = advance(pC, pT);
856 tk = token_type(pT);
857 } else {
858 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
861 if (*pT == '.') {
862 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
863 if(has_more_tokens(pC) == true)
865 pC = advance(pC, pT);
866 tk = token_type(pT);
867 } else {
868 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
870 if(tk == IDENTIFIER)
872 if(settings.tokens) { printf("\t<identifier>%s</identifier>\n", pT); }
873 } else {
874 compiler_error(37, "Could Not Find Method Name or Subroutine Name at This Location", pS, pC, pT);
878 if(*pT != '(') /* this for calls with no class name at beginning */
880 if(has_more_tokens(pC) == true)
882 pC = advance(pC, pT);
883 tk = token_type(pT);
884 } else {
885 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
889 if(*pT == '(')
891 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
892 } else {
893 compiler_error(39, "Could Not Find Symbol '(' At This Location", pS, pC, pT);
896 if(has_more_tokens(pC) == true)
898 pC = advance(pC, pT);
899 tk = token_type(pT);
900 } else {
901 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
904 parse_expr_lst();
906 if(*pT == ')')
908 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
909 } else {
910 compiler_error(38, "Could Not Find Symbol ')' At This Location", pS, pC, pT);
913 if(has_more_tokens(pC) == true)
915 pC = advance(pC, pT);
916 tk = token_type(pT);
917 } else {
918 compiler_error(24, "Could Not Complete Subroutine Call. Incomplete Program", pS, pC, pT);
921 if(settings.tokens) { printf("\t</subroutineCall>\n"); }
924 void parse_expr_lst()
926 if(settings.tokens) { printf("\t<expressionList>\n"); }
927 while(*pT != ')')
929 if(*pT == ',')
931 if(settings.tokens) { printf("\t<symbol>%s</symbol>\n", pT); }
932 if(has_more_tokens(pC) == true)
934 pC = advance(pC, pT);
935 tk = token_type(pT);
936 } else {
937 compiler_error(24, "Could Not Complete Expression List. Incomplete Program", pS, pC, pT);
939 } else {
940 parse_expression();
943 if(settings.tokens) { printf("\t</expressionList>\n"); }