gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - dup, dup2 commands
[mjc2wsl.git] / src / mjc2wsl.java
index 16b0e37..4b231aa 100644 (file)
@@ -120,6 +120,7 @@ public class mjc2wsl{
                ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t");
                ret.append("mjvm_locals := ARRAY(1,0), ");
                ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0), ");
                ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t");
                ret.append("mjvm_locals := ARRAY(1,0), ");
                ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0), ");
+               ret.append("\n\tmjvm_arrays := < >, ");
                ret.append("\n  mjvm_estack := < >, mjvm_mstack := < >, "); 
                ret.append("\n  mjvm_fp := 0, mjvm_sp := 0,");
                ret.append("\n  t_e_m_p := 0 > :");
                ret.append("\n  mjvm_estack := < >, mjvm_mstack := < >, "); 
                ret.append("\n  mjvm_fp := 0, mjvm_sp := 0,");
                ret.append("\n  t_e_m_p := 0 > :");
@@ -215,6 +216,14 @@ public class mjc2wsl{
                        return "mjvm_statics[" + (i+1)+"]";
        }
        
                        return "mjvm_statics[" + (i+1)+"]";
        }
        
+       private String genArray(int i){
+                       return "mjvm_arrays["+ i +"]";
+       }
+       
+       private String genArray(String i){
+                       return "mjvm_arrays["+ i+"]";
+       }
+       
        /**
         * Creates a WSL comment with care to quote chars.
         */
        /**
         * Creates a WSL comment with care to quote chars.
         */
@@ -419,21 +428,65 @@ public class mjc2wsl{
                        
                        case inc  : {
                                int b1 = get(), b2 = get();
                        
                        case inc  : {
                                int b1 = get(), b2 = get();
-                               prl(loc(b1)+" := " +loc(b1)+" + "+b2);
+                               prl(loc(b1)+" := " +loc(b1)+" + "+b2+";");
                                break;
                        }
                        //TODO new_ newarray
                                break;
                        }
                        //TODO new_ newarray
-                       case new_ :
-                                       get();//needs a short, but a byte will be taken bellow as well
-                       case newarray :{
+                       case new_ :{
                                        prl(createComment("memory allocation not processed properly", C_ERR));
                                        message("memory allocation  not processed properly", M_ERR);
                                        prl(createComment("memory allocation not processed properly", C_ERR));
                                        message("memory allocation  not processed properly", M_ERR);
-                                       get();
+                                       get2();
                                        break;
                        }
                                        break;
                        }
-                       //TODO aload, asstore, baload, bastore
-                       //TODO arraylength
-                       //TODO dup, dup2
+                       case newarray :{
+                                       get();// 0 - bytes, 1 - words; ignore for now
+                                       //TODO take into consideration 0/1
+                                       prl(getTop());
+                                       prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
+                                       prl(cmdToEStack("LENGTH(mjvm_arrays)"));
+                                       break;
+                       }
+                       
+                       case aload:
+                       case baload:{
+                               prl(getTopTwo());
+                               prl(cmdToEStack(genArray("tempb")+"[tempa+1]"));
+                               break;
+                       }
+                       case astore:
+                       case bastore:{
+                               prl(cmdFromEStack("tempres"));
+                               prl(getTopTwo());
+                               //we need to use a temparray as a pointer, WSL
+                               //otherwise tries to access it as a list of lists and fails
+                               prl("VAR < tempArray := "+genArray("tempb")+" > :");
+                               prl("tempArray[tempa+1]:=tempres ENDVAR;");
+                               break;
+                       }
+                       case arraylength :{
+                               prl(getTop());
+                               //TODO make an array length function of some sort!
+                               prl(createComment("array length not known - LENGTH not aplicable to arrays",C_ERR));
+                               message("array length not known - LENGTH not aplicable to arrays",M_ERR);
+                               prl(createComment("put 1 on the stack for consistency",C_SPEC));
+                               prl(cmdToEStack(1));
+                               break;
+                       }
+                       
+                       case dup : {
+                                       prl(getTop());
+                                       prl(cmdToEStack("tempa"));
+                                       prl(cmdToEStack("tempa"));
+                                       break;                                  
+                       }
+                       case dup2 : {
+                                       prl(getTopTwo());
+                                       prl(cmdToEStack("tempb"));
+                                       prl(cmdToEStack("tempa"));
+                                       prl(cmdToEStack("tempb"));
+                                       prl(cmdToEStack("tempa"));
+                                       break;                                  
+                       }
                        
                        case pop : {
                                        prl(cmdPopEStack());
                        
                        case pop : {
                                        prl(cmdPopEStack());
@@ -465,9 +518,9 @@ public class mjc2wsl{
                        }
 
                        case return_: {
                        }
 
                        case return_: {
-                               prl("IF EMPTY?(mjvm_mstack) THEN CALL Z FI");
-                               //else we let things return             
-                               prl("END b"+counter+" ==");
+                               //we let the actions return
+                               // there is nothing to clean up
+                               prl("SKIP END b"+counter+" ==");
                                break;
                        }
                        case enter: {
                                break;
                        }
                        case enter: {
@@ -486,24 +539,24 @@ public class mjc2wsl{
                        }
 
                        // read, print
                        }
 
                        // read, print
-                       case read:{
+                       case bread:{
                                //TODO make it a char for read
                                message("char is read like a number", M_WAR);
                                prl(createComment("char is read like a number",C_SPEC));
                        }
                                //TODO make it a char for read
                                message("char is read like a number", M_WAR);
                                prl(createComment("char is read like a number",C_SPEC));
                        }
-                       case bread: {
+                       case read: {
                                prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
                                prl(cmdToEStack("tempa"));
                                break;
                        }
 
                        // the prints
                                prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
                                prl(cmdToEStack("tempa"));
                                break;
                        }
 
                        // the prints
-                       case print:{
+                       case bprint:{
                                // TODO need to make it a char on print
                                message("chars will be printed as number codes", M_WAR);
                                prl(createComment("char will be printed as a number code",C_SPEC));
                        }
                                // TODO need to make it a char on print
                                message("chars will be printed as number codes", M_WAR);
                                prl(createComment("char will be printed as a number code",C_SPEC));
                        }
-                       case bprint: {
+                       case print: {
                                // TODO printing numbers needs different lengths of spacing
                                prl(getTopTwo());
                                pr(createComment("print spacing",C_SPEC));
                                // TODO printing numbers needs different lengths of spacing
                                prl(getTopTwo());
                                pr(createComment("print spacing",C_SPEC));
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner