Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- public class Main {
- public static final Pattern EXIT = Pattern.compile("exit;");
- public static final Pattern ASSEMBLY = Pattern.compile("assembly ?((\\(?:[0-9a-fA-F]{2})+)\\);");
- public static final Pattern DEBUG_VALUE = Pattern.compile("(.*);");
- public static final Pattern CHARACTER_LITERAL = Pattern.compile("'(.{1,2})'");
- public static final Pattern HEX_LITERAL = Pattern.compile("0x([0-9a-fA-F]+)");
- public static final Pattern BINARY_LITERAL = Pattern.compile("0b([0-1]+)");
- public static final Pattern OCTAL_LITERAL = Pattern.compile("0o?([0-7]+)");
- public static final Pattern DECIMAL_LITERAL = Pattern.compile("(?:0d)?([0-9]+)");
- public static void main (String[] args) {
- Scanner sc = new Scanner(System.in, System.getProperty("stdin.encoding"));
- while (true) {
- while (!sc.hasNextLine()) {}
- System.out.println(compile(sc.nextLine()));
- }
- }
- public static String compile (String line) {
- Matcher m = EXIT.matcher(line);
- if (m.matches())
- return "DB";
- m = ASSEMBLY.matcher(line);
- if (m.matches())
- return m.group(1);
- m = DEBUG_VALUE.matcher(line);
- if (m.matches())
- return compileValue(m.group(1));
- return "Unknown syntax";
- }
- public static String compileValue (String s) {
- Matcher m = CHARACTER_LITERAL.matcher(s);
- if (m.matches()) {
- String ss = m.group(1);
- if (ss.length() == 1)
- return "A9" + toTwoByteHex(ss.charAt(0));
- if (ss.length() == 2)
- return "A2" + toTwoByteHex(ss.charAt(1)) + toTwoByteHex(ss.charAt(0));
- }
- m = HEX_LITERAL.matcher(s);
- if (m.matches()) {
- int i;
- try {
- i = Integer.parseInt(m.group(1), 16);
- } catch (NumberFormatException nfe) {
- return "not a number";
- }
- if ((i < 128) && (i >= -128))
- return "A9" + toTwoByteHex(i);
- else if ((i < 65536) && (i >= -65536))
- return "A2" + toTwoByteHex(i);
- else
- return "Integer overflow!";
- }
- m = BINARY_LITERAL.matcher(s);
- if (m.matches()) {
- int i;
- try {
- i = Integer.parseInt(m.group(1), 2);
- } catch (NumberFormatException nfe) {
- return "not a number";
- }
- if ((i < 128) && (i >= -128))
- return "A9" + toTwoByteHex(i);
- else if ((i < 65536) && (i >= -65536))
- return "A2" + toTwoByteHex(i);
- else
- return "Integer overflow!";
- }
- m = OCTAL_LITERAL.matcher(s);
- if (m.matches()) {
- int i;
- try {
- i = Integer.parseInt(m.group(1), 8);
- } catch (NumberFormatException nfe) {
- return "not a number";
- }
- if ((i < 128) && (i >= -128))
- return "A9" + toTwoByteHex(i);
- else if ((i < 65536) && (i >= -65536))
- return "A2" + toTwoByteHex(i);
- else
- return "Integer overflow!";
- }
- m = DECIMAL_LITERAL.matcher(s);
- if (m.matches()) {
- int i;
- try {
- i = Integer.parseInt(m.group(1), 10);
- } catch (NumberFormatException nfe) {
- return "not a number";
- }
- if ((i < 128) && (i >= -128))
- return "A9" + toTwoByteHex(i);
- else if ((i < 65536) && (i >= -65536))
- return "A2" + toTwoByteHex(i);
- else
- return "Integer overflow!";
- }
- return "Unknown syntax";
- }
- public static String toTwoByteHex (int i) {
- String s = Integer.toString(i, 16);
- return "0".repeat(2 - s.length()) + s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment