From: Doni Pracner Date: Tue, 22 Apr 2014 21:24:45 +0000 (+0200) Subject: refactor - generalise stack operations for easier future changes X-Git-Tag: v0.1.6~6 X-Git-Url: https://svarog.pmf.uns.ac.rs/gitweb/?a=commitdiff_plain;h=3163f812ec467db6a4af65840c52133c77fd9eb6;p=mjc2wsl.git refactor - generalise stack operations for easier future changes --- diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index 388f1ad..8717711 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -271,26 +271,31 @@ public class mjc2wsl{ public static String createComment(String str, char type) { return "C:\"" + type + str.replace("\"", "''") + "\";"; } + + // generalised stack operations + + private String createToStack(String stack, String var){ + return stack + " := <" + var + " > ++ " + stack +";"; + } - //Expression stack + private String createFromStack(String stack, String var){ + return var + ":= HEAD("+stack+"); "+stack+" := TAIL("+stack+");"; + } +//Expression stack private String createToEStack(int i) { - String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;"; - if (genPrintEStackOnChange) - res += "PRINT(\"eStack\",mjvm_estack);"; - return res; + return createToEStack(i+""); } private String createToEStack(String i) { - String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;"; + String res = createToStack("mjvm_estack", i); if (genPrintEStackOnChange) res += "PRINT(\"eStack\",mjvm_estack);"; return res; } private String createFromEStack(String st) { - String res = st - + " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);"; + String res = createFromStack("mjvm_estack",st); if (genPrintEStackOnChange) res += "PRINT(\"eStack\",mjvm_estack);"; return res; @@ -314,15 +319,15 @@ public class mjc2wsl{ //Method stack private String createToMStack(int i) { - return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;"; + return createToMStack(i+""); } private String createToMStack(String i) { - return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;"; + return createToStack("mjvm_mstack", i); } private String createFromMStack(String st) { - return st + " := HEAD(mjvm_mstack); mjvm_mstack := TAIL(mjvm_mstack);"; + return createFromStack("mjvm_mstack", st); } private String getRelationFor(int opcode) throws Exception {