SforzandoCF

bruh

Feb 24th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import java.util.regex.Pattern;
  4. import java.util.regex.Matcher;
  5.  
  6. public class Main {
  7.     public static final Pattern EXIT = Pattern.compile("exit;");
  8.     public static final Pattern ASSEMBLY = Pattern.compile("assembly ?((\\(?:[0-9a-fA-F]{2})+)\\);");
  9.     public static final Pattern DEBUG_VALUE = Pattern.compile("(.*);");
  10.    
  11.     public static final Pattern CHARACTER_LITERAL = Pattern.compile("'(.{1,2})'");
  12.     public static final Pattern HEX_LITERAL = Pattern.compile("0x([0-9a-fA-F]+)");
  13.     public static final Pattern BINARY_LITERAL = Pattern.compile("0b([0-1]+)");
  14.     public static final Pattern OCTAL_LITERAL = Pattern.compile("0o?([0-7]+)");
  15.     public static final Pattern DECIMAL_LITERAL = Pattern.compile("(?:0d)?([0-9]+)");
  16.    
  17.     public static void main (String[] args) {
  18.         Scanner sc = new Scanner(System.in, System.getProperty("stdin.encoding"));
  19.         while (true) {
  20.             while (!sc.hasNextLine()) {}
  21.             System.out.println(compile(sc.nextLine()));
  22.         }
  23.     }
  24.    
  25.     public static String compile (String line) {
  26.         Matcher m = EXIT.matcher(line);
  27.         if (m.matches())
  28.             return "DB";
  29.         m = ASSEMBLY.matcher(line);
  30.         if (m.matches())
  31.             return m.group(1);
  32.         m = DEBUG_VALUE.matcher(line);
  33.         if (m.matches())
  34.             return compileValue(m.group(1));
  35.         return "Unknown syntax";
  36.     }
  37.    
  38.     public static String compileValue (String s) {
  39.         Matcher m = CHARACTER_LITERAL.matcher(s);
  40.         if (m.matches()) {
  41.             String ss = m.group(1);
  42.             if (ss.length() == 1)
  43.                 return "A9" + toTwoByteHex(ss.charAt(0));
  44.             if (ss.length() == 2)
  45.                 return "A2" + toTwoByteHex(ss.charAt(1)) + toTwoByteHex(ss.charAt(0));
  46.         }
  47.         m = HEX_LITERAL.matcher(s);
  48.         if (m.matches()) {
  49.             int i;
  50.             try {
  51.                 i = Integer.parseInt(m.group(1), 16);
  52.             } catch (NumberFormatException nfe) {
  53.                 return "not a number";
  54.             }
  55.             if ((i < 128) && (i >= -128))
  56.                 return "A9" + toTwoByteHex(i);
  57.             else if ((i < 65536) && (i >= -65536))
  58.                 return "A2" + toTwoByteHex(i);
  59.             else
  60.                 return "Integer overflow!";
  61.         }
  62.         m = BINARY_LITERAL.matcher(s);
  63.         if (m.matches()) {
  64.             int i;
  65.             try {
  66.                 i = Integer.parseInt(m.group(1), 2);
  67.             } catch (NumberFormatException nfe) {
  68.                 return "not a number";
  69.             }
  70.             if ((i < 128) && (i >= -128))
  71.                 return "A9" + toTwoByteHex(i);
  72.             else if ((i < 65536) && (i >= -65536))
  73.                 return "A2" + toTwoByteHex(i);
  74.             else
  75.                 return "Integer overflow!";
  76.         }
  77.         m = OCTAL_LITERAL.matcher(s);
  78.         if (m.matches()) {
  79.             int i;
  80.             try {
  81.                 i = Integer.parseInt(m.group(1), 8);
  82.             } catch (NumberFormatException nfe) {
  83.                 return "not a number";
  84.             }
  85.             if ((i < 128) && (i >= -128))
  86.                 return "A9" + toTwoByteHex(i);
  87.             else if ((i < 65536) && (i >= -65536))
  88.                 return "A2" + toTwoByteHex(i);
  89.             else
  90.                 return "Integer overflow!";
  91.         }
  92.         m = DECIMAL_LITERAL.matcher(s);
  93.         if (m.matches()) {
  94.             int i;
  95.             try {
  96.                 i = Integer.parseInt(m.group(1), 10);
  97.             } catch (NumberFormatException nfe) {
  98.                 return "not a number";
  99.             }
  100.             if ((i < 128) && (i >= -128))
  101.                 return "A9" + toTwoByteHex(i);
  102.             else if ((i < 65536) && (i >= -65536))
  103.                 return "A2" + toTwoByteHex(i);
  104.             else
  105.                 return "Integer overflow!";
  106.         }
  107.         return "Unknown syntax";
  108.     }
  109.    
  110.     public static String toTwoByteHex (int i) {
  111.         String s = Integer.toString(i, 16);
  112.         return "0".repeat(2 - s.length()) + s;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment