16 token_print("class", OPEN
);
20 if(has_more_tokens(pC
) == true)
25 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
28 if(strcmp(pT
, "class") == 0 ) {
29 if(settings
.tokens
) { token_print("keyword", BOTH
); }
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)
40 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
43 if (tk
== IDENTIFIER
){
44 if(settings
.tokens
) { token_print("identifier", BOTH
); }
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)
55 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
60 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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)
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)
81 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
85 while(strcmp(pT
, "constructor") == 0 || strcmp(pT
, "function") == 0 || strcmp(pT
, "method") == 0)
88 if(has_more_tokens(pC
) == true)
93 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
99 if(settings
.tokens
) { token_print("symbol", BOTH
); }
101 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
107 token_print("class", CLOSE
);
111 void parse_class_var_dec()
115 token_print("classVarDec", OPEN
);
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
);
126 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
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
); }
136 compiler_error(41, "Token Must be Data Type.", pS
, pC
, pT
);
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
);
148 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
150 if(tk
== IDENTIFIER
) {
151 if(settings
.tokens
) { token_print("identifier", BOTH
); }
153 compiler_error(42, "Token Must be Variable Name", pS
, pC
, pT
);
157 if(has_more_tokens(pC
) == true)
159 pC
= advance(pC
, pT
);
162 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
167 if(settings
.tokens
) { token_print("symbol", BOTH
); }
168 if(has_more_tokens(pC
) == true)
170 pC
= advance(pC
, pT
);
173 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
175 if(tk
== IDENTIFIER
) {
176 if(settings
.tokens
) { token_print("identifier", BOTH
); }
178 compiler_error(42, "Token Must be Variable Name", pS
, pC
, pT
);
180 if(has_more_tokens(pC
) == true)
182 pC
= advance(pC
, pT
);
185 compiler_error(40, "Incomplete Class Declaration", pS
, pC
, pT
);
187 } while (*pT
== ',');
191 if(settings
.tokens
) { token_print("symbol", BOTH
); }
193 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
199 token_print("classVarDec", CLOSE
);
203 void parse_subroutine()
207 token_print("subroutineDec", OPEN
);
212 if(strcmp(pT
, "constructor") == 0 || strcmp(pT
, "function") == 0 || strcmp(pT
, "method") == 0)
214 if(settings
.tokens
) { token_print("keyword", BOTH
); }
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
);
225 if(tk
== KEYWORD
|| tk
== IDENTIFIER
)
227 if(strcmp(pT
, "void") == 0)
229 if(settings
.tokens
) { token_print("keyword", BOTH
); }
231 if(settings
.tokens
) { token_print("identifier", BOTH
); }
234 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
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
);
247 if(settings
.tokens
) { token_print("identifier", BOTH
); }
249 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
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
);
262 if(settings
.tokens
) { token_print("symbol", BOTH
); }
265 compiler_error(12, "Parameter List for Function Missing", pS
, pC
, pT
);
268 compiler_error(11, "Name of Function Must be an Identifier", pS
, pC
, pT
);
271 /* look for end of parameter list */
276 token_print("symbol", BOTH
);
277 token_print("subroutineBody", OPEN
);
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
);
291 if(settings
.tokens
) { token_print("symbol", BOTH
); }
292 if(has_more_tokens(pC
) == true)
294 pC
= advance(pC
, pT
);
297 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
302 compiler_error(9, "Could Not Complete Parse Tree of Subroutine. Incomplete Program", pS
, pC
, pT
);
309 if(settings
.tokens
) { token_print("symbol", BOTH
); }
311 compiler_error(17, "Could Not Complete Subroutine. Incomplete Program", pS
, pC
, pT
);
317 token_print("subroutineBody", CLOSE
);
319 token_print("subroutineDec", CLOSE
);
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
);
333 if(strcmp(pT
, "int") == 0 || strcmp(pT
, "char") == 0 || strcmp(pT
, "boolean") == 0)
335 if(settings
.tokens
) { token_print("keyword", BOTH
); }
337 compiler_error(14, "Incorrect Token Type in Parameter List. Looking for Datatype name.", pS
, pC
, pT
);
339 } else if(tk
== SYMBOL
&& *pT
== ')') {
343 token_print("parameterList", CLOSE
);
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
);
356 if(tk
== IDENTIFIER
) {
357 if(settings
.tokens
) { token_print("identifier", BOTH
); }
359 compiler_error(15, "Incorrect Token Type in Parameter List. Looking for Variable Identifier.", pS
, pC
, pT
);
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
);
371 if(settings
.tokens
) { token_print("symbol", BOTH
); }
373 } else if (*pT
== ')') { /* exit parse_params */
377 token_print("parameterList", CLOSE
);
381 compiler_error(16, "Incorrect Token Type in Parameter List. Looking for ',' or ')'", pS
, pC
, pT
);
384 compiler_error(13, "Could Not Complete Parameter List for Function", pS
, pC
, pT
);
390 /* look for token named 'var' */
391 if(strcmp(pT
, "var") == 0)
395 token_print("varDec", OPEN
);
396 token_print("symbol", BOTH
);
400 /* look for variable data type */
401 if(has_more_tokens(pC
) == true)
403 pC
= advance(pC
, 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
); }
412 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
416 /* look for identifier(s) for variable(s) */
418 if(has_more_tokens(pC
) == true)
420 pC
= advance(pC
, pT
);
423 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
427 if(settings
.tokens
) { token_print("identifier", BOTH
); }
430 if(has_more_tokens(pC
) == true)
432 pC
= advance(pC
, pT
);
435 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
439 if(settings
.tokens
) { token_print("symbol", BOTH
); }
441 } while (*pT
== ',');
443 if(has_more_tokens(pC
) == true)
445 pC
= advance(pC
, pT
);
448 compiler_error(17, "Could Not Complete Variable List of Subroutine. Incomplete Program", pS
, pC
, pT
);
453 void parse_statements()
457 token_print("statements", OPEN
);
461 if(strcmp(pT
, "let") == 0)
464 } else if(strcmp(pT
, "if") == 0)
467 } else if(strcmp(pT
, "while") == 0)
470 } else if(strcmp(pT
, "do") == 0)
473 } else if(strcmp(pT
, "return") == 0)
478 if(has_more_tokens(pC
) == true)
480 pC
= advance(pC
, pT
);
484 } while (strcmp(pT
, "let") == 0 || strcmp(pT
, "if") == 0 || strcmp(pT
, "while") == 0 || \
485 strcmp(pT
, "do") == 0 || strcmp(pT
, "return") == 0 );
489 token_print("statements", CLOSE
);
497 token_print("doStatement", OPEN
);
499 token_print("keyword", BOTH
);
502 if(has_more_tokens(pC
) == true)
504 pC
= advance(pC
, pT
);
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();
514 compiler_error(30, "Subroutine Name Must Be Listed Here", pS
, pC
, pT
);
519 if(settings
.tokens
) { token_print("symbol", BOTH
); }
521 compiler_error(33, "Could Not Find ';' Symbol At This Location.", pS
, pC
, pT
);
527 token_print("doStatement", CLOSE
);
536 token_print("letStatement", OPEN
);
538 token_print("keyword", BOTH
);
541 if(has_more_tokens(pC
) == true)
543 pC
= advance(pC
, pT
);
546 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
549 /* look for an identifier - must be a variable name */
552 if(settings
.tokens
) { token_print("identifier", BOTH
); }
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
);
563 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
569 if(settings
.tokens
) { token_print("symbol", BOTH
); }
570 if(has_more_tokens(pC
) == true)
572 pC
= advance(pC
, pT
);
575 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
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
);
589 compiler_error(20, "Could Not Find ']' Symbol At This Location", pS
, pC
, pT
);
595 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
605 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
612 if(settings
.tokens
) { token_print("symbol", BOTH
); }
614 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
620 token_print("letStatement", CLOSE
);
628 token_print("whileStatement", OPEN
);
630 token_print("keyword", BOTH
);
633 if(has_more_tokens(pC
) == true)
635 pC
= advance(pC
, pT
);
638 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS
, pC
, pT
);
643 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
653 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS
, pC
, pT
);
660 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
670 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS
, pC
, pT
);
675 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
685 compiler_error(47, "Could Not Complete While Statement. Incomplete Program", pS
, pC
, pT
);
692 if(settings
.tokens
) { token_print("symbol", BOTH
); }
694 compiler_error(46, "Could Not Find '}' Symbol At This Location", pS
, pC
, pT
);
700 token_print("whileStatement", CLOSE
);
708 token_print("returnStatement", OPEN
);
710 token_print("keyword", BOTH
);
714 if(has_more_tokens(pC
) == true)
716 pC
= advance(pC
, pT
);
719 compiler_error(20, "Could Not Complete Let Statement. Incomplete Program", pS
, pC
, pT
);
722 if (*pT
!= ';') { parse_expression(); }
726 if(settings
.tokens
) { token_print("symbol", BOTH
); }
728 compiler_error(33, "Could Not Find ';' Symbol At This Location", pS
, pC
, pT
);
733 token_print("returnStatement", CLOSE
);
741 token_print("ifStatement", OPEN
);
743 token_print("keyword", BOTH
);
746 if(has_more_tokens(pC
) == true)
748 pC
= advance(pC
, pT
);
751 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS
, pC
, pT
);
756 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
766 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS
, pC
, pT
);
773 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
783 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS
, pC
, pT
);
788 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
798 compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS
, pC
, pT
);
805 if(settings
.tokens
) { token_print("symbol", BOTH
); }
807 compiler_error(46, "Could Not Find '}' Symbol At This Location", pS
, pC
, pT
);
813 token_print("ifStatement", CLOSE
);
817 void parse_expression()
821 token_print("expression", OPEN
);
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
);
834 compiler_error(34, "Could Not Parse Expression. Incomplete Program", pS
, pC
, pT
);
841 token_print("expression", CLOSE
);
849 token_print("term", OPEN
);
855 if(settings
.tokens
) { token_print("intConst", BOTH
); }
856 if(has_more_tokens(pC
) == true)
858 pC
= advance(pC
, pT
);
861 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
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
);
874 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
881 if(settings
.tokens
) { token_print("keyword", BOTH
); }
883 if(has_more_tokens(pC
) == true)
885 pC
= advance(pC
, pT
);
888 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
894 if(settings
.tokens
) { token_print("identifier", BOTH
); }
896 if(has_more_tokens(pC
) == true)
898 pC
= advance(pC
, pT
);
901 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
908 if(settings
.tokens
) { token_print("symbol", BOTH
); }
910 if(has_more_tokens(pC
) == true)
912 pC
= advance(pC
, pT
);
915 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
920 if(settings
.tokens
) { token_print("symbol", BOTH
); }
921 /* parse_expression(); */
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
);
931 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
935 if(settings
.tokens
) { token_print("symbol", BOTH
); }
937 if(has_more_tokens(pC
) == true)
939 pC
= advance(pC
, pT
);
942 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
948 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
958 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
962 if(settings
.tokens
) { token_print("symbol", BOTH
); }
964 if(has_more_tokens(pC
) == true)
966 pC
= advance(pC
, pT
);
969 compiler_error(25, "Could Not Complete Term. Incomplete Program", pS
, pC
, pT
);
972 parse_subroutine_call();
979 token_print("term", CLOSE
);
983 void parse_subroutine_call()
987 if(settings
.tokens
) { token_print("identifier", BOTH
); }
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
);
997 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, 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
);
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
); }
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
);
1024 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
1030 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
1040 compiler_error(36, "Could Not Complete Subroutine Call. Incomplete Program", pS
, pC
, pT
);
1047 if(settings
.tokens
) { token_print("symbol", BOTH
); }
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
);
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
); }
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
);
1074 compiler_error(24, "Could Not Complete Expression List. Incomplete Program", pS
, pC
, pT
);
1080 if(settings
.tokens
) { token_print("expressionList", CLOSE
); }