Refactoring
This commit is contained in:
parent
267e623dc3
commit
f2049dd085
@ -123,19 +123,15 @@ public final class AnalyzerPanel extends JPanel {
|
||||
}
|
||||
|
||||
private String getTextFromResource(String res) {
|
||||
try {
|
||||
InputStream is = getClass().getResourceAsStream(res);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
|
||||
StringBuilder text = new StringBuilder();
|
||||
final StringBuilder text = new StringBuilder();
|
||||
try (InputStream is = getClass().getResourceAsStream(res)) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
|
||||
String line;
|
||||
while( (line = reader.readLine()) != null ) {
|
||||
while ( (line = reader.readLine()) != null ) {
|
||||
text.append(line).append(System.lineSeparator());
|
||||
}
|
||||
|
||||
reader.close();
|
||||
return text.toString();
|
||||
}
|
||||
} catch (IOException ex) {}
|
||||
return "";
|
||||
return text.toString();
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package com.annimon.asm;
|
||||
|
||||
import com.annimon.asm.directives.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Êîíâåðòåð äèðåêòèâ.
|
||||
@ -39,8 +41,7 @@ public final class DirectiveConverter {
|
||||
* @return Integer ÷èñëî.
|
||||
*/
|
||||
public static Integer parseInteger(String text) {
|
||||
int value;
|
||||
if (text.toLowerCase().endsWith("h")) {
|
||||
if (text.toLowerCase(Locale.ENGLISH).endsWith("h")) {
|
||||
char first = Character.toLowerCase(text.charAt(0));
|
||||
if ( (first >= 'a') && (first <= 'f') ) {
|
||||
// Ïåðâûé ñèìâîë hex ÷èñëà íå äîëæåí áûòü îò a äî f
|
||||
@ -49,15 +50,13 @@ public final class DirectiveConverter {
|
||||
|
||||
String hex = text.substring(0, text.length() - 1);
|
||||
try {
|
||||
value = Integer.parseInt(hex, 16);
|
||||
return value;
|
||||
return Integer.parseInt(hex, 16);
|
||||
} catch (NumberFormatException nfe) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
try {
|
||||
value = Integer.parseInt(text);
|
||||
return value;
|
||||
return Integer.parseInt(text);
|
||||
} catch (NumberFormatException nfe) {
|
||||
return null;
|
||||
}
|
||||
@ -69,28 +68,28 @@ public final class DirectiveConverter {
|
||||
* @return ìàññèâ ñòðîê.
|
||||
*/
|
||||
public static String[] split(String text) {
|
||||
ArrayList<String> parts = new ArrayList<>();
|
||||
int length = text.length();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
final List<String> parts = new ArrayList<>();
|
||||
final int length = text.length();
|
||||
final StringBuilder word = new StringBuilder();
|
||||
for (int ch = 0; ch < length; ch++) {
|
||||
int i = text.charAt(ch);
|
||||
if (i == ' ') {
|
||||
if (sb.length() > 0) {
|
||||
parts.add(sb.toString().trim());
|
||||
sb.setLength(0);
|
||||
final int currentChar = text.charAt(ch);
|
||||
if (currentChar == ' ') {
|
||||
if (word.length() > 0) {
|
||||
parts.add(word.toString().trim());
|
||||
word.setLength(0);
|
||||
}
|
||||
} else if (i == ',') {
|
||||
if (sb.length() > 0) {
|
||||
parts.add(sb.toString().trim());
|
||||
} else if (currentChar == ',') {
|
||||
if (word.length() > 0) {
|
||||
parts.add(word.toString().trim());
|
||||
parts.add(",");
|
||||
sb.setLength(0);
|
||||
word.setLength(0);
|
||||
}
|
||||
} else {
|
||||
sb.append((char) i);
|
||||
word.append((char) currentChar);
|
||||
}
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
parts.add(sb.toString().trim());
|
||||
if (word.length() > 0) {
|
||||
parts.add(word.toString().trim());
|
||||
}
|
||||
String[] m = new String[parts.size()];
|
||||
m = parts.toArray(m);
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.annimon.asm;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author aNNiMON
|
||||
@ -11,6 +13,6 @@ public final class LexicLine {
|
||||
|
||||
public LexicLine(int lineNumber, int[] line) {
|
||||
this.lineNumber = lineNumber;
|
||||
this.line = line;
|
||||
this.line = Arrays.copyOf(line, line.length);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.annimon.asm;
|
||||
|
||||
import com.annimon.asm.directives.ID;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Ëåêñè÷åñêèé àíàëèçàòîð.
|
||||
@ -28,7 +29,7 @@ public final class LexicalAnalyzer {
|
||||
}
|
||||
|
||||
public String[] getLines() {
|
||||
return lines;
|
||||
return Arrays.copyOf(lines, lines.length);
|
||||
}
|
||||
|
||||
public void analyze() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.annimon.asm;
|
||||
|
||||
import com.annimon.asm.directives.ID;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -18,9 +19,10 @@ public final class ListingGenerateHelper {
|
||||
* @return ñòðîêîâîå çíà÷åíèå ÷èñëà â HEX.
|
||||
*/
|
||||
public static String toHexString(int value) {
|
||||
String str = Integer.toString(value, 16).toUpperCase();
|
||||
if (str.length() == 1) str = "0" + str;
|
||||
else if (str.length() == 3) str = "0" + str;
|
||||
String str = Integer.toString(value, 16).toUpperCase(Locale.ENGLISH);
|
||||
if ( (str.length() == 1) || (str.length() == 3) ) {
|
||||
return "0" + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
@ -75,7 +77,7 @@ public final class ListingGenerateHelper {
|
||||
* @return êîä ðåãèñòðà (3 áèòà).
|
||||
*/
|
||||
public static byte getRegisterCode(String register) {
|
||||
final String reg = register.toLowerCase();
|
||||
final String reg = register.toLowerCase(Locale.ENGLISH);
|
||||
|
||||
switch (reg) {
|
||||
case "ax": case "al": return 0b000;
|
||||
@ -97,7 +99,7 @@ public final class ListingGenerateHelper {
|
||||
* @return êîä ñåãìåíòíîãî ðåãèñòðà, ëèáî -1, åñëè ðåãèñòð íå ñåãìåíòíûé.
|
||||
*/
|
||||
public static byte getSegmentRegisterCode(String register) {
|
||||
final String reg = register.toLowerCase();
|
||||
final String reg = register.toLowerCase(Locale.ENGLISH);
|
||||
for (byte i = 0; i < SEGMENT_REGISTERS.length; i++) {
|
||||
if (reg.equals(SEGMENT_REGISTERS[i])) return i;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.annimon.asm.exceptions.ExceptionWithLineNumber;
|
||||
import com.annimon.asm.exceptions.FewArgumentsException;
|
||||
import com.annimon.asm.exceptions.TooManyArgumentsException;
|
||||
import com.annimon.asm.exceptions.WrongArgumentException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -18,16 +19,7 @@ public final class Add extends Directive implements ISyntaxChecker, IListingGene
|
||||
super("add", ID.ADD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(int lineNumber, int[] ids) throws ExceptionWithLineNumber {
|
||||
if (ids[0] != getId()) return false;
|
||||
|
||||
if (ids.length < 4) throw new FewArgumentsException(lineNumber);
|
||||
else if (ids.length > 4) throw new TooManyArgumentsException(lineNumber);
|
||||
|
||||
if (ids[2] != ID.COMMA) throw new CommaExpectedException(lineNumber);
|
||||
|
||||
int[] pairs = new int[] {
|
||||
private static final int[] PAIRS = {
|
||||
// Ðåãèñòð - Ðåãèñòð
|
||||
ID.REGISTER_BYTE, ID.REGISTER_BYTE,
|
||||
ID.REGISTER_WORD, ID.REGISTER_WORD,
|
||||
@ -54,9 +46,18 @@ public final class Add extends Directive implements ISyntaxChecker, IListingGene
|
||||
ID.VAR_WORD, ID.NUMBER_BYTE,
|
||||
};
|
||||
|
||||
@Override
|
||||
public boolean check(int lineNumber, int[] ids) throws ExceptionWithLineNumber {
|
||||
if (ids[0] != getId()) return false;
|
||||
|
||||
if (ids.length < 4) throw new FewArgumentsException(lineNumber);
|
||||
if (ids.length > 4) throw new TooManyArgumentsException(lineNumber);
|
||||
|
||||
if (ids[2] != ID.COMMA) throw new CommaExpectedException(lineNumber);
|
||||
|
||||
boolean correct = false;
|
||||
for(int i = 0; i < pairs.length; i += 2) {
|
||||
if ( (ids[1] == pairs[i]) && (ids[3] == pairs[i+1]) ) {
|
||||
for (int i = 0; i < PAIRS.length; i += 2) {
|
||||
if ( (ids[1] == PAIRS[i]) && (ids[3] == PAIRS[i+1]) ) {
|
||||
correct = true;
|
||||
break;
|
||||
}
|
||||
@ -103,8 +104,8 @@ public final class Add extends Directive implements ISyntaxChecker, IListingGene
|
||||
modreg <<= 6;
|
||||
|
||||
// r/m
|
||||
if (strs[1].toLowerCase().contains("si")) modreg |= 0b100;
|
||||
else if (strs[1].toLowerCase().contains("di")) modreg |= 0b101;
|
||||
if (strs[1].toLowerCase(Locale.ENGLISH).contains("si")) modreg |= 0b100;
|
||||
else if (strs[1].toLowerCase(Locale.ENGLISH).contains("di")) modreg |= 0b101;
|
||||
else modreg |= 0b110;
|
||||
|
||||
sb.append(ListingGenerateHelper.toHexString(val))
|
||||
|
@ -22,7 +22,7 @@ public final class Idiv extends Directive implements ISyntaxChecker, IListingGen
|
||||
if (ids[0] != getId()) return false;
|
||||
|
||||
if (ids.length < 2) throw new FewArgumentsException(lineNumber, 1);
|
||||
else if (ids.length > 2) throw new TooManyArgumentsException(lineNumber, 1);
|
||||
if (ids.length > 2) throw new TooManyArgumentsException(lineNumber, 1);
|
||||
|
||||
if ( (ids[1] != ID.REGISTER_BYTE) && (ids[1] != ID.REGISTER_WORD) &&
|
||||
(ids[1] != ID.VAR_BYTE) && (ids[1] != ID.VAR_WORD) ) {
|
||||
|
@ -22,7 +22,7 @@ public final class Mul extends Directive implements ISyntaxChecker, IListingGene
|
||||
if (ids[0] != getId()) return false;
|
||||
|
||||
if (ids.length < 2) throw new FewArgumentsException(lineNumber, 1);
|
||||
else if (ids.length > 2) throw new TooManyArgumentsException(lineNumber, 1);
|
||||
if (ids.length > 2) throw new TooManyArgumentsException(lineNumber, 1);
|
||||
|
||||
if ( (ids[1] != ID.REGISTER_BYTE) && (ids[1] != ID.REGISTER_WORD) &&
|
||||
(ids[1] != ID.VAR_BYTE) && (ids[1] != ID.VAR_WORD) ) {
|
||||
|
@ -28,7 +28,6 @@ public final class Push extends Directive implements ISyntaxChecker, IListingGen
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,7 +31,7 @@ public final class Variable extends Directive implements ISyntaxChecker, IListin
|
||||
if (ids[0] != getId()) return false;
|
||||
|
||||
if (ids.length < 3) throw new FewArgumentsException(lineNumber, 2);
|
||||
else if (ids.length > 3) throw new TooManyArgumentsException(lineNumber, 2);
|
||||
if (ids.length > 3) throw new TooManyArgumentsException(lineNumber, 2);
|
||||
|
||||
boolean db = ( (ids[1] == ID.DB) &&
|
||||
((ids[2] == ID.NUMBER_BYTE) || (ids[2] == ID.NUMBER_INFINITY)) );
|
||||
|
Loading…
Reference in New Issue
Block a user