inter macro

 package mylpcc;

import java.util.*; import java.io.*; class Macro { String name; List<String> parameters; List<String> body; public Macro(String name) { this.name = name; this.parameters = new ArrayList<>(); this.body = new ArrayList<>(); } } public class TerminalMacroProcessor { private Map<String, Macro> macroDefTable = new HashMap<>(); private List<String> inputLines = new ArrayList<>(); private List<String> outputLines = new ArrayList<>(); private int currentLine = 0; public static void main(String[] args) { TerminalMacroProcessor processor = new TerminalMacroProcessor(); processor.readInputFromTerminal(); processor.passOne(); processor.passTwo(); processor.printOutput(); } public void readInputFromTerminal() { System.out.println("Enter your assembly code (type 'END' on a new line to finish):"); Scanner scanner = new Scanner(System.in); while (true) { String line = scanner.nextLine().trim(); if (line.equals("END")) { inputLines.add(line); break; } if (!line.isEmpty()) { inputLines.add(line); } } } public void passOne() { System.out.println("\n=== PASS 1 ==="); while (currentLine < inputLines.size()) { String line = inputLines.get(currentLine); if (line.equalsIgnoreCase("MACRO")) { // Now macro name and parameters are expected in the next line(s) processMacroDefinition(); } else { currentLine++; } } currentLine = 0; // Reset for pass two } private void processMacroDefinition() { currentLine++; // Move to macro name line if (currentLine >= inputLines.size()) { System.out.println("Syntax Error: Missing macro name after MACRO"); return; } String macroDecl = inputLines.get(currentLine); String[] parts = macroDecl.split("\\s+"); if (parts.length == 0 || parts[0].isEmpty()) { System.out.println("Syntax Error: Invalid macro definition at line " + currentLine + ". Macro name missing."); return; } String macroName = parts[0]; Macro macro = new Macro(macroName); // Handle parameters if any if (parts.length > 1) { for (int i = 1; i < parts.length; i++) { String param = parts[i].replace(",", ""); macro.parameters.add(param); } } currentLine++; // Collect macro body until MEND while (currentLine < inputLines.size()) { String line = inputLines.get(currentLine); if (line.equalsIgnoreCase("MEND")) { break; } macro.body.add(line); currentLine++; } if (currentLine >= inputLines.size()) { System.out.println("Syntax Error: MEND not found for macro " + macroName); return; } currentLine++; // Skip MEND // Add to macro definition table macroDefTable.put(macroName, macro); System.out.println("Macro Defined: " + macroName + " with " + macro.parameters.size() + " parameters"); } public void passTwo() { System.out.println("\n=== PASS 2 ==="); while (currentLine < inputLines.size()) { String line = inputLines.get(currentLine); if (line.equalsIgnoreCase("END")) { break; } else if (macroDefTable.containsKey(line.split("\\s+")[0])) { expandMacro(line); currentLine++; } else if (line.equalsIgnoreCase("MACRO")) { // Skip macro definition lines in pass two skipMacroDefinition(); // DON'T increment currentLine here - skipMacroDefinition already handled it } else { outputLines.add(line); System.out.println("Output: " + line); currentLine++; } } } private void expandMacro(String macroCall) { String[] parts = macroCall.split("\\s+"); String macroName = parts[0]; Macro macro = macroDefTable.get(macroName); // Handle arguments Map<String, String> argMap = new HashMap<>(); if (parts.length > 1) { for (int i = 1; i < parts.length; i++) { String arg = parts[i].replace(",", ""); if (i - 1 < macro.parameters.size()) { argMap.put(macro.parameters.get(i - 1), arg); } } } System.out.println("Expanding Macro: " + macroName + " with args: " + argMap); // Expand macro body with argument substitution for (String bodyLine : macro.body) { String expandedLine = bodyLine; // Replace parameters with actual arguments for (Map.Entry<String, String> entry : argMap.entrySet()) { expandedLine = expandedLine.replaceAll("\\b" + entry.getKey() + "\\b", entry.getValue()); } // Handle nested macro calls String[] bodyParts = expandedLine.split("\\s+"); if (macroDefTable.containsKey(bodyParts[0])) { expandMacro(expandedLine); } else { outputLines.add(expandedLine); System.out.println("Output: " + expandedLine); } } } private void skipMacroDefinition() { currentLine++; while (currentLine < inputLines.size()) { String line = inputLines.get(currentLine); if (line.equalsIgnoreCase("MEND")) { break; } currentLine++; } currentLine++; // Skip MEND } public void printOutput() { System.out.println("\n=== FINAL OUTPUT ==="); for (String line : outputLines) { System.out.println(line); } } }



#ip

MACRO

INCR &ARG LOAD &ARG ADD 1 STORE &ARG MEND INCR A INCR B END


Comments

Popular posts from this blog

literal

yacc evaluate

built in functions