Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JFMethod {
- private static final Pattern RETURN = Pattern.compile("\\s*return( [.&&^\\R])?;(.*)");
- private static final Pattern EXIT = Pattern.compile("\\s*exit;(.*)");
- private static final Pattern WAIT = Pattern.compile("\\s*wait;(.*)");
- private static final Pattern IF = Pattern.compile("\\s*if ?\\(([.&&^\\)]*)\\)( ?\\{)(.*)");
- private static final Pattern FOR = Pattern.compile("\\s*for ?\\(([.&&^,]), ?([.&&^,]), ?([.&&^\\)])\\) ?\\{(.*)");
- private static final Pattern WHILE = Pattern.compile("\\s*while ?\\(([.&&^\\)]\\) ?{(.*)");
- public static JFMethod compile (String s, List<JFAnnotationTemplate> temps) {
- HashMap<String, Object> map = getAnnotations(temps);
- Matcher m = Pattern.compile("((?:p{Lower}+ )*)(\\w+) (\\w+) ?\\(((?:(?:\\w+ \\w+, ?)+\\w+ \\w+)|void)?\\) ?\\{(?:\\R((?:\\s*.*\\R)*))\\}").matcher(s);
- addKeywords(map, m.group(1).stripTrailing().split(" "));
- map.put("type", JFType.getReturnType(m.group(2)));
- map.put("name", m.group(3));
- String params = m.group(4);
- if (params.equals("void")) params = "";
- String code = m.group(5);
- ByteArrayOutputStream bo = new ByteArrayOutputStream();
- DataOutput buffer = new DataOutputStream(bo);
- while (!code.isEmpty()) {
- m = RETURN.matcher(code);
- if (m.matches()) code = compileReturn(m, map, buffer);
- m = STOP.matcher(code);
- if (m.matches()) code = compileStop(m, map, buffer);
- m = WAIT.matcher(code);
- if (m.matches()) code = compileWait(m, map, buffer);
- m = IF.matcher(code);
- if (m.matches()) code = compileIf(m, map, buffer);
- m = FOR.matcher(code);
- if (m.matches()) code = compileFor(m, map, buffer);
- m = WHILE.matcher(code);
- if (m.matches()) code = compileWhile(m, map, buffer);
- code = compileValue(code, map, buffer);
- }
- return bo.toByteArray();
- }
- private static HashMap<String, Object> getAnnotations (List<JFAnnotationTemplate> temps) {
- return new HashMap<String, Object>();
- }
- private static void addKeywords (HashMap<String, Object> map, String[] keywords) {
- for (String s : keywords) {
- if ("reset".equals(s)) {
- if (map.containsKey("return type")) throw new IllegalArgumentException("keyword reset is incompatible with keyword " + ((String) map.get("return type")));
- map.put("return type", "reset");
- } else if ("interrupt".equals(s)) {
- if (map.containsKey("return type")) throw new IllegalArgumentException("keyword interrupt is incompatible with keyword " + ((String) map.get("return type")));
- map.put("return type", "interrupt");
- } else if ("pause".equals(s)) {
- if (map.containsKey("return type")) throw new IllegalArgumentException("keyword pause is incompatible with keyword " + ((String) map.get("return type")));
- map.put("return type", "pause");
- }
- }
- map.putIfAbsent("return type", "long");
- }
- private static void compileReturn (Matcher m, HashMap<String, Object> map, DataOutput buffer) {
- String rtype = (String) map.get("return type");
- JFValue value = m.group(1) == null ? null : JFValue.parse(m.group(1));
- if ("reset".equals(rtype))
- throw new IllegalArgumentException("cannot return from the reset method");
- else if ("interrupt".equals(rtype) || "pause".equals(rtype)) {
- if (value != null) throw new IllegalArgumentException("cannot return a value from an interrupt method");
- buffer.writeByte(0x40);
- } else if ("short".equals(rtype)) {
- if (value != null) value.write(0x7E1000);
- buffer.writeByte(0x60);
- } else if ("long".equals(rtype)) {
- if (value != null) value.write(0x7E1000);
- buffer.writeByte(0x6B);
- }
- return m.group(2);
- }
- private static void compileStop (Matcher m, HashMap<String, Object> map, DataOutput buffer) {
- buffer.writeFully(new byte[] { (byte) 0xA00000A9, (byte) -1, (byte) 0x00, (byte) 0xA00000AD, (byte) 0x40, (byte) 0x12, (byte) 0xDB});
- return m.group(1);
- }
- private static void compileWait (Matcher m, HashMap<String, Object> map, DataOutput buffer) {
- buffer.writeByte(0xCB);
- return m.group(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment