| 7 | import java.util.Scanner; |
| 8 | |
| 9 | public class PCodeExecutor { |
| 10 | private final ArrayList<PCode> codes; |
| 11 | private final ArrayList<RetInfo> retInfos = new ArrayList<>(); |
| 12 | private final ArrayList<Integer> stack = new ArrayList<>(); |
| 13 | private int eip = 0; |
| 14 | private HashMap<String, Var> varTable = new HashMap<>(); |
| 15 | private final HashMap<String, Func> funcTable = new HashMap<>(); |
| 16 | private final HashMap<String, Integer> labelTable = new HashMap<>(); |
| 17 | |
| 18 | private int mainAddress; |
| 19 | |
| 20 | private final ArrayList<String> prints = new ArrayList<>(); |
| 21 | private FileWriter writer; |
| 22 | private Scanner scanner; |
| 23 | |
| 24 | public PCodeExecutor(ArrayList<PCode> codes, FileWriter writer, Scanner scanner) { |
| 25 | this.codes = codes; |
| 26 | this.writer = writer; |
| 27 | this.scanner = scanner; |
| 28 | for (int i = 0; i < codes.size(); i++) { |
| 29 | PCode code = codes.get(i); |
| 30 | // get main function address |
| 31 | if (code.getType().equals(CodeType.MAIN)) { |
| 32 | mainAddress = i; |
| 33 | } |
| 34 | // get all label |
| 35 | if (code.getType().equals(CodeType.LABEL)) { |
| 36 | labelTable.put((String) code.getValue1(), i); |
| 37 | } |
| 38 | //get all function |
| 39 | if (code.getType().equals(CodeType.FUNC)) { |
| 40 | funcTable.put((String) code.getValue1(), new Func(i, (int) code.getValue2())); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private void push(int i) { |
| 46 | stack.add(i); |
| 47 | } |
| 48 | |
| 49 | private int pop() { |
| 50 | return stack.remove(stack.size() - 1); |
| 51 | } |
| 52 | |
| 53 | private Var getVar(String ident) { |
| 54 | if (varTable.containsKey(ident)) { |
| 55 | return varTable.get(ident); |
| 56 | } else { |
| 57 | return retInfos.get(0).getVarTable().get(ident); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public void run() { |
| 62 | int callArgsNum = 0; |
| 63 | int nowArgsNum = 0; |
| 64 | boolean mainFlag = false; |
| 65 | ArrayList<Integer> rparas = new ArrayList<>(); |
| 66 | for (; eip < codes.size(); eip++) { |
nothing calls this directly
no outgoing calls
no test coverage detected