Skip to content

Commit

Permalink
macro wip
Browse files Browse the repository at this point in the history
  • Loading branch information
scenemax3d committed Jul 21, 2024
1 parent e6c3b5d commit d34c9e6
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/main/java/com/scenemaxeng/compiler/MacroFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ public class MacroFilter {

private HashMap<String, JSONObject> macroRules;

class LexicographicComparator implements Comparator<Object> {
static class LexicographicComparator implements Comparator<Object> {
@Override
public int compare(Object a, Object b) {
HashMap<String, String> a1 = (HashMap<String, String>) a;
HashMap<String, String> b1 = (HashMap<String, String>) b;
int len = a1.get("pat").length() - b1.get("pat").length();
JSONObject a1 = (JSONObject) a;
JSONObject b1 = (JSONObject) b;
int len = 0;
try {
len = a1.getString("pat").length() - b1.getString("pat").length();
} catch (JSONException e) {
e.printStackTrace();
}

if (len > 0) {
return -1;
Expand Down Expand Up @@ -91,15 +96,15 @@ public ApplyMacroResults apply(String prg) {

List<Object> patternsSorted = new ArrayList<>();// patterns.toList();
for (int i = 0; i < patterns.length(); i++) {
patternsSorted.add(patterns.getString(i));
patternsSorted.add(patterns.getJSONObject(i));
}
Collections.sort(patternsSorted, new LexicographicComparator());

for (int i = 0; i < patternsSorted.size(); ++i) {

HashMap<String, String> item = (HashMap<String, String>) patternsSorted.get(i);
JSONObject item = (JSONObject) patternsSorted.get(i);

String pat = item.get("pat");
String pat = item.getString("pat");
if (pat.length() == 0) {
continue;
}
Expand All @@ -122,7 +127,7 @@ public ApplyMacroResults apply(String prg) {
}
}

String program = item.get("prg");
String program = item.getString("prg");

Pattern p = Pattern.compile(pat);
Matcher m = p.matcher(mr.finalPrg);
Expand Down

0 comments on commit d34c9e6

Please sign in to comment.