private void ref_id() { if (tok.kind == TK.TILDE) { scan(); if (tok.kind == TK.NUM) { int num = Integer.parseInt(tok.string); scan(); if (num > curBlock) { System.err.println( "no such variable ~" + num + tok.string + " on line " + tok.lineNumber); System.exit(1); } if (searchTildaSpec(tok.string, num)) System.out.print("x_" + (curBlock - num) + tok.string); } else { if (searchTildaGlob(tok.string)) System.out.print("x_0" + tok.string); } mustbe(TK.ID); } else { if (searchUndeclared(tok.string)) { int numPrint = searchNum(tok.string); System.out.print("x_" + numPrint + tok.string); } mustbe(TK.ID); } }
private void declaration() { mustbe(TK.DECLARE); if (searchCurBlock(tok.string)) System.out.println("int x_" + curBlock + tok.string + ";"); mustbe(TK.ID); while (is(TK.COMMA)) { scan(); if (searchCurBlock(tok.string)) System.out.println("int x_" + curBlock + tok.string + ";"); mustbe(TK.ID); } }
private void assign() { ref_id(); System.out.print("="); mustbe(TK.ASSIGN); expr(); System.out.println(";"); }
private void factor() { if (tok.kind == TK.LPAREN) { System.out.print("("); scan(); expr(); System.out.print(")"); mustbe(TK.RPAREN); } else if (tok.kind == TK.NUM) { while (is(TK.NUM)) { System.out.print(tok.string); // System.out.println(tok+"In the Factor"); mustbe(TK.NUM); } } else { ref_id(); } }
private void If_statement() { System.out.print("if"); mustbe(TK.IF); guarded_command(); while (is(TK.ELSEIF)) { System.out.print("else if"); scan(); guarded_command(); } if (tok.kind == TK.ELSE) { System.out.print("else"); scan(); addBlock(); System.out.println("{"); block(); System.out.println("}"); removeBlock(); } mustbe(TK.ENDIF); }
private void guarded_command() { System.out.print("("); expr(); System.out.print(" <= 0"); mustbe(TK.THEN); System.out.print(")"); addBlock(); System.out.println("{"); block(); System.out.println("}"); removeBlock(); }
private void forl_statement() { System.out.print("for ("); scan(); System.out.print("int i = 0; i<=" + tok.string + "; i ++)"); addBlock(); System.out.print("{"); scan(); block(); mustbe(TK.ENDFORL); System.out.println("}"); removeBlock(); }
private void statement_list() { while (tok.kind == TK.ID || tok.kind == TK.PRINT || tok.kind == TK.DO || tok.kind == TK.IF || tok.kind == TK.TILDE || tok.kind == TK.FORL) { while (tok.kind == TK.ID || tok.kind == TK.TILDE) { // System.out.println(tok + "I am in ID"); assign(); // System.out.println(tok + "I am done in here"); } while (is(TK.PRINT)) { pr(); } while (is(TK.IF)) { If_statement(); } while (is(TK.DO)) { Do_statement(); } while (is(TK.FORL)) { forl_statement(); } while (is(TK.TILDE)) { ref_id(); if (is(TK.ASSIGN)) { System.out.print("="); mustbe(TK.ASSIGN); expr(); System.out.println(";"); } } } // big while loop // System.out.println("out of statement_list"); }
private void Do_statement() { System.out.print("while"); mustbe(TK.DO); guarded_command(); mustbe(TK.ENDDO); }