SforzandoCF

jfmethod

Jan 16th, 2026
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. public class JFMethod {
  2.     private static final Pattern RETURN = Pattern.compile("\\s*return( [.&&^\\R])?;(.*)");
  3.     private static final Pattern EXIT = Pattern.compile("\\s*exit;(.*)");
  4.     private static final Pattern WAIT = Pattern.compile("\\s*wait;(.*)");
  5.     private static final Pattern IF = Pattern.compile("\\s*if ?\\(([.&&^\\)]*)\\)( ?\\{)(.*)");
  6.     private static final Pattern FOR = Pattern.compile("\\s*for ?\\(([.&&^,]), ?([.&&^,]), ?([.&&^\\)])\\) ?\\{(.*)");
  7.     private static final Pattern WHILE = Pattern.compile("\\s*while ?\\(([.&&^\\)]\\) ?{(.*)");
  8.    
  9.     public static JFMethod compile (String s, List<JFAnnotationTemplate> temps) {
  10.         HashMap<String, Object> map = getAnnotations(temps);
  11.         Matcher m = Pattern.compile("((?:p{Lower}+ )*)(\\w+) (\\w+) ?\\(((?:(?:\\w+ \\w+, ?)+\\w+ \\w+)|void)?\\) ?\\{(?:\\R((?:\\s*.*\\R)*))\\}").matcher(s);
  12.         addKeywords(map, m.group(1).stripTrailing().split(" "));
  13.         map.put("type", JFType.getReturnType(m.group(2)));
  14.         map.put("name", m.group(3));
  15.         String params = m.group(4);
  16.         if (params.equals("void")) params = "";
  17.         String code = m.group(5);
  18.         ByteArrayOutputStream bo = new ByteArrayOutputStream();
  19.         DataOutput buffer = new DataOutputStream(bo);
  20.         while (!code.isEmpty()) {
  21.             m = RETURN.matcher(code);
  22.             if (m.matches()) code = compileReturn(m, map, buffer);
  23.             m = STOP.matcher(code);
  24.             if (m.matches()) code = compileStop(m, map, buffer);
  25.             m = WAIT.matcher(code);
  26.             if (m.matches()) code = compileWait(m, map, buffer);
  27.             m = IF.matcher(code);
  28.             if (m.matches()) code = compileIf(m, map, buffer);
  29.             m = FOR.matcher(code);
  30.             if (m.matches()) code = compileFor(m, map, buffer);
  31.             m = WHILE.matcher(code);
  32.             if (m.matches()) code = compileWhile(m, map, buffer);
  33.             code = compileValue(code, map, buffer);
  34.         }
  35.         return bo.toByteArray();
  36.     }
  37.    
  38.     private static HashMap<String, Object> getAnnotations (List<JFAnnotationTemplate> temps) {
  39.         return new HashMap<String, Object>();
  40.     }
  41.    
  42.     private static void addKeywords (HashMap<String, Object> map, String[] keywords) {
  43.         for (String s : keywords) {
  44.             if ("reset".equals(s)) {
  45.                 if (map.containsKey("return type")) throw new IllegalArgumentException("keyword reset is incompatible with keyword " + ((String) map.get("return type")));
  46.                 map.put("return type", "reset");
  47.             } else if ("interrupt".equals(s)) {
  48.                 if (map.containsKey("return type")) throw new IllegalArgumentException("keyword interrupt is incompatible with keyword " + ((String) map.get("return type")));
  49.                 map.put("return type", "interrupt");
  50.             } else if ("pause".equals(s)) {
  51.                 if (map.containsKey("return type")) throw new IllegalArgumentException("keyword pause is incompatible with keyword " + ((String) map.get("return type")));
  52.                 map.put("return type", "pause");
  53.             }
  54.         }
  55.         map.putIfAbsent("return type", "long");
  56.     }
  57.    
  58.     private static void compileReturn (Matcher m, HashMap<String, Object> map, DataOutput buffer) {
  59.         String rtype = (String) map.get("return type");
  60.         JFValue value = m.group(1) == null ? null : JFValue.parse(m.group(1));
  61.         if ("reset".equals(rtype))
  62.             throw new IllegalArgumentException("cannot return from the reset method");
  63.         else if ("interrupt".equals(rtype) || "pause".equals(rtype)) {
  64.             if (value != null) throw new IllegalArgumentException("cannot return a value from an interrupt method");
  65.             buffer.writeByte(0x40);
  66.         } else if ("short".equals(rtype)) {
  67.             if (value != null) value.write(0x7E1000);
  68.             buffer.writeByte(0x60);
  69.         } else if ("long".equals(rtype)) {
  70.             if (value != null) value.write(0x7E1000);
  71.             buffer.writeByte(0x6B);
  72.         }
  73.         return m.group(2);
  74.     }
  75.    
  76.     private static void compileStop (Matcher m, HashMap<String, Object> map, DataOutput buffer) {
  77.         buffer.writeFully(new byte[] { (byte) 0xA00000A9, (byte) -1, (byte) 0x00, (byte) 0xA00000AD, (byte) 0x40, (byte) 0x12, (byte) 0xDB});
  78.         return m.group(1);
  79.     }
  80.    
  81.     private static void compileWait (Matcher m, HashMap<String, Object> map, DataOutput buffer) {
  82.         buffer.writeByte(0xCB);
  83.         return m.group(1);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment