// switch private static void rule_switch(DefaultMutableTreeNode parent) { DefaultMutableTreeNode node = null; DefaultMutableTreeNode parentS = parent; node = new DefaultMutableTreeNode("switch"); parent.add(node); String id = ""; if (tokens.get(currentToken).getWord().equals("(")) { node = new DefaultMutableTreeNode("("); parent.add(node); currentToken++; } if (tokens.get(currentToken).getToken().equals("IDENTIFIER")) { node = new DefaultMutableTreeNode("identifier(" + tokens.get(currentToken).getWord() + ")"); parent.add(node); int line = tokens.get(currentToken).getLine(); id = tokens.get(currentToken).getWord(); String type = SemanticAnalyzer.doesExist(id); String value = SemanticAnalyzer.getValue(id); if (type.equals("error")) SemanticAnalyzer.error(gui, 0, line, id); currentToken++; } if (tokens.get(currentToken).getWord().equals(")")) { node = new DefaultMutableTreeNode(")"); parent.add(node); currentToken++; } if (tokens.get(currentToken).getWord().equals("{")) { node = new DefaultMutableTreeNode("{"); parent.add(node); currentToken++; } if (tokens.get(currentToken).getWord().equals("case")) { node = new DefaultMutableTreeNode("case"); parentS.add(node); currentToken++; } rule_cases(parentS, node, id); if (tokens.get(currentToken).getWord().equals("default")) { node = new DefaultMutableTreeNode("default"); parent.add(node); currentToken++; CodeGenerator.addInstruction("JMP", "#labelD", "0"); codeLine++; rule_default(node); } if (tokens.get(currentToken).getWord().equals("}")) { node = new DefaultMutableTreeNode("}"); parent.add(node); currentToken++; } // codeLine++; // codeLine++; CodeGenerator.addLabel("labelES", codeLine); }
// cases private static void rule_cases( DefaultMutableTreeNode parent, DefaultMutableTreeNode caseParent, String id) { DefaultMutableTreeNode node = null; DefaultMutableTreeNode nodeP = caseParent; node = new DefaultMutableTreeNode("case"); caseParent.add(node); // int labelNum = 2; // int num = lNum; do { if (tokens.get(currentToken).getWord().equals("case")) { nodeP = new DefaultMutableTreeNode("case"); parent.add(nodeP); currentToken++; node = new DefaultMutableTreeNode("case"); nodeP.add(node); } String name = tokens.get(currentToken).getWord(); String type = tokens.get(currentToken).getToken(); String value = SemanticAnalyzer.getValue(name); if (type.equals("INTEGER")) { node = new DefaultMutableTreeNode("integer(" + name + ")"); nodeP.add(node); currentToken++; CodeGenerator.addInstruction("LOD", id, "0"); codeLine++; CodeGenerator.addInstruction("LIT", name, "0"); codeLine++; CodeGenerator.addInstruction("OPR", "15", "0"); codeLine++; CodeGenerator.addInstruction("JMC", "#label" + lNum, "false"); codeLine++; } else if (type.equals("OCTAL")) { node = new DefaultMutableTreeNode("octal(" + name + ")"); nodeP.add(node); currentToken++; CodeGenerator.addInstruction("LOD", id, "0"); codeLine++; CodeGenerator.addInstruction("LIT", name, "0"); codeLine++; CodeGenerator.addInstruction("OPR", "15", "0"); codeLine++; CodeGenerator.addInstruction("JMC", "#label" + lNum, "false"); codeLine++; } else if (type.equals("HEXIDECIMAL")) { node = new DefaultMutableTreeNode("hexidecimal(" + name + ")"); nodeP.add(node); currentToken++; CodeGenerator.addInstruction("LOD", id, "0"); codeLine++; CodeGenerator.addInstruction("LIT", name, "0"); codeLine++; CodeGenerator.addInstruction("OPR", "15", "0"); codeLine++; CodeGenerator.addInstruction("JMC", "#label" + lNum, "false"); codeLine++; } else if (type.equals("BINARY")) { node = new DefaultMutableTreeNode("binary(" + name + ")"); nodeP.add(node); currentToken++; CodeGenerator.addInstruction("LOD", id, "0"); codeLine++; CodeGenerator.addInstruction("LIT", name, "0"); codeLine++; CodeGenerator.addInstruction("OPR", "15", "0"); codeLine++; CodeGenerator.addInstruction("JMC", "#label" + lNum, "false"); codeLine++; } // else error if (tokens.get(currentToken).getWord().equals(":")) { node = new DefaultMutableTreeNode(":"); nodeP.add(node); currentToken++; } rule_program(nodeP); CodeGenerator.addInstruction("JMP", "#labelES", "0"); codeLine++; CodeGenerator.addLabel("label" + lNum, codeLine); lNum++; } while (tokens.get(currentToken).getWord().equals("case")); }