X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=src%2Fmjc2wsl.java;h=388f1ad39b965402297e05535b2a157482c3cd27;hp=44c0c67b202fe0d5b532eefdf470fa32e4e45816;hb=efe68ac5eb23f8a7711a44c4a7d4e830b64cd068;hpb=5c55fb61ab79584c65492755dab20935474ed15c diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index 44c0c67..388f1ad 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -193,7 +193,7 @@ public class mjc2wsl{ +"C:\" with mjc2wsl v "+versionN+"\";\n"); ret.append("BEGIN "); - ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t"); + ret.append("VAR < \n\t"); ret.append("mjvm_locals := ARRAY(1,0), "); ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0), "); ret.append("\n\tmjvm_arrays := < >, "); @@ -219,6 +219,20 @@ public class mjc2wsl{ return ret.toString(); } + private String createStartVar(String... vars){ + StringBuilder ret = new StringBuilder("VAR < "); + ret.append(vars[0] + " := 0"); + for (int i=1; i : "); + + return ret.toString(); + } + + private String createEndVar(){ + return "ENDVAR;"; + } + private String createLocal(int i) { // arrays start at 1 in WSL, so we need an offset return "mjvm_locals[" + (i + 1) + "]"; @@ -415,41 +429,53 @@ public class mjc2wsl{ } case add: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("tempres := tempb + tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case sub: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("tempres := tempb - tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case mul: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("tempres := tempb * tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case div: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;"); prl("tempres := tempb DIV tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case rem: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;"); prl("tempres := tempb MOD tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case neg: { + prl(createStartVar("tempa")); prl(createTopEStack()); prl(createToEStack("-tempa")); + prl(createEndVar()); break; } @@ -547,10 +573,13 @@ public class mjc2wsl{ case jle: case jgt: case jge: { + prl(createStartVar("tempa", "tempb")); prl(createTopTwoEStack()); prl("IF tempb " + getRelationFor(op) + " tempa THEN CALL a" + (counter + get2()) + " ELSE CALL a" + (counter + 1) + " FI;"); + prl(createEndVar()); + break; } @@ -587,8 +616,10 @@ public class mjc2wsl{ prl(createComment("char is read like a number", C_SPEC)); } case read: { + prl(createStartVar("tempa")); prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));"); prl(createToEStack("tempa")); + prl(createEndVar()); break; } @@ -601,8 +632,11 @@ public class mjc2wsl{ } case print: { // TODO printing numbers needs different lengths of spacing + prl(createStartVar("tempa", "tempb")); + prl(createTopTwoEStack()); prl("Print_MJ(tempb,tempa);"); + prl(createEndVar()); break; }