SHOW:
|
|
- or go back to the newest paste.
1 | static Token[] thompsonVM (Instruction[] program, String input) { | |
2 | int inputLength, beginIndex; | |
3 | char currentCharacter; | |
4 | ArrayList<Integer> currentList, nextList; | |
5 | ArrayList<Token> returnTokens; | |
6 | Token matchedToken; | |
7 | String currentMatchedString = ""; | |
8 | ||
9 | beginIndex = 0; //used to save the current match as a substring of the input | |
10 | inputLength = input.length(); //stored for slightly quicker runtime | |
11 | currentList = new ArrayList<>(); //current list of threads as integers (line number in the program) | |
12 | nextList = new ArrayList<>(); //next list of threads | |
13 | returnTokens = new ArrayList<>(); //list of matched strings in the form ID:'value' | |
14 | matchedToken = new Token("", ""); //the current matched token | |
15 | ||
16 | currentList.add(0); //start the program with a single thread at instruction 0 | |
17 | ||
18 | for (int characterIndex = 0; characterIndex <= inputLength; characterIndex++) { | |
19 | if (characterIndex != inputLength) { | |
20 | currentCharacter = input.charAt(characterIndex); | |
21 | } else { | |
22 | currentCharacter = 0; //if past the last character, set to null | |
23 | } | |
24 | for (int currentInstruction, instructionIndex = 0; instructionIndex < currentList.size(); instructionIndex++) { //for all instructions in currentList | |
25 | currentInstruction = currentList.get(instructionIndex); | |
26 | switch (program[currentInstruction].opCode) { | |
27 | case CHAR: | |
28 | CharInstruction charInst = (CharInstruction)program[currentInstruction]; | |
29 | if (!(currentCharacter >= charInst.lowerLimit && currentCharacter <= charInst.upperLimit)) { | |
30 | - | continue; |
30 | + | |
31 | nextList.add(currentInstruction + 1); | |
32 | System.out.println("Matched char " + currentCharacter); | |
33 | } | |
34 | - | continue; |
34 | + | break; |
35 | case MATCH: | |
36 | MatchInstruction matchInst = (MatchInstruction)program[currentInstruction]; | |
37 | currentMatchedString = (input.substring(beginIndex, characterIndex)); | |
38 | if (currentMatchedString.length() > matchedToken.getValue().length()) { | |
39 | matchedToken.setID(matchInst.tokenID); | |
40 | matchedToken.setValue(currentMatchedString); | |
41 | } | |
42 | System.out.println("Matched word " + currentMatchedString); | |
43 | continue; | |
44 | case JMP: | |
45 | JmpInstruction jmpInst = (JmpInstruction)program[currentInstruction]; | |
46 | currentList.add(jmpInst.jumpTo); | |
47 | break; | |
48 | case SPLIT: | |
49 | SplitInstruction splitInst = (SplitInstruction)program[currentInstruction]; | |
50 | currentList.add(splitInst.firstJump); | |
51 | currentList.add(splitInst.secondJump); | |
52 | break; | |
53 | } | |
54 | } | |
55 | ||
56 | currentList.clear(); | |
57 | currentList.addAll(nextList); | |
58 | nextList.clear(); | |
59 | ||
60 | if (currentList.isEmpty() && !matchedToken.getValue().isEmpty()) { //if all threads died | |
61 | currentList.add(0); | |
62 | returnTokens.add(matchedToken); | |
63 | matchedToken = new Token("", ""); | |
64 | beginIndex = characterIndex + 1; | |
65 | } | |
66 | } | |
67 | Token[] returnArray = new Token[returnTokens.size()]; | |
68 | returnTokens.toArray(returnArray); | |
69 | return returnArray; | |
70 | } |