Advertisement
SuperJedi224

Loader Interpreter

Aug 22nd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.40 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.*;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class Loader{
  10.     static int getANumber(){
  11.         try{
  12.             System.out.println("Enter a number.");
  13.             return in.nextInt();
  14.         }catch(InputMismatchException e){
  15.             System.out.print("Illegal input. ");
  16.             return getANumber();
  17.         }
  18.     }
  19.     static void throwError(String error,String sender,int line){
  20.         throwError(error+" (module "+sender+", line"+line+")");
  21.     }
  22.     static void throwError(String error){
  23.         JFrame j=new JFrame();
  24.         j.setAlwaysOnTop(true);
  25.         JOptionPane.showMessageDialog(j,"Error: "+error,"",JOptionPane.ERROR_MESSAGE);
  26.         System.exit(0);
  27.     }
  28.     static void throwWarning(String error){
  29.         JFrame j=new JFrame();
  30.         j.setAlwaysOnTop(true);
  31.         JOptionPane.showMessageDialog(j,"Warning: "+error,"",JOptionPane.WARNING_MESSAGE);
  32.     }
  33.     static class Variable implements Cloneable{
  34.         public Variable(String name,int value){
  35.             this.name=name;
  36.             v=value;
  37.         }
  38.         public Variable(String name,char value){
  39.             this.name=name;
  40.             v=value;
  41.         }
  42.         public final String name;
  43.         private int v=0;
  44.         public int get(){
  45.             return v;
  46.         }
  47.         public Variable clone(){
  48.             return new Variable(name,v);
  49.         }
  50.         public void set(int val){
  51.             v=val;
  52.         }
  53.         public boolean equals(Object o){
  54.             if(o instanceof Variable){
  55.                 return ((Variable)o).get()==v;
  56.             }
  57.             if(o instanceof Number){
  58.                 return ((Number)o).intValue()==v;
  59.             }
  60.             return false;
  61.         }
  62.     }
  63.     static Scanner in=new Scanner(System.in);
  64.     static class VariableCollection implements Cloneable{
  65.         private List<Variable>v=new ArrayList<>();
  66.         public VariableCollection clone(){
  67.             VariableCollection c=new VariableCollection();
  68.             for(Variable var:v){
  69.                 c.v.add(var.clone());
  70.             }
  71.             return c;
  72.         }
  73.         public Variable getByName(String name){
  74.             for(Variable var:v){
  75.                 if(var.name.equals(name))return var;
  76.             }
  77.             Variable var=new Variable(name,0);
  78.             v.add(var);
  79.             return var;
  80.         }
  81.     }
  82.     static class Runtime implements Cloneable{
  83.         private VariableCollection c;
  84.         private int[] pos,neg;
  85.         public Runtime(){
  86.             c=new VariableCollection();
  87.             pos=new int[5];
  88.             neg=new int[5];
  89.         }
  90.         public Runtime clone(){
  91.             Runtime r=new Runtime();
  92.             r.c=this.c.clone();
  93.             r.pos=pos.clone();
  94.             r.neg=neg.clone();
  95.             return r;
  96.         }
  97.         public int get(int c){
  98.             if(c<0){
  99.                 int i=-(1+c);
  100.                 while(neg.length<1+i){
  101.                     neg=Arrays.copyOf(neg,(int)(1.25*neg.length));
  102.                 }
  103.                 return neg[i];
  104.             }
  105.             while(pos.length<1+c){
  106.                 pos=Arrays.copyOf(pos,(int)(1.25*pos.length));
  107.             }
  108.             return pos[c];
  109.         }
  110.         public int set(int c,int v){
  111.             if(c<0){
  112.                 int i=-(1+c);
  113.                 while(neg.length<1+i){
  114.                     neg=Arrays.copyOf(neg,(int)(1.25*neg.length));
  115.                 }
  116.                 return neg[i]=v;
  117.             }
  118.             while(pos.length<1+c){
  119.                 pos=Arrays.copyOf(pos,(int)(1.25*pos.length));
  120.             }
  121.             return pos[c]=v;
  122.         }
  123.     }
  124.     static int expr(String s,Runtime r,String name,int l){
  125.         if(s.matches("[A-Z]+")){
  126.             return r.c.getByName(s).get();
  127.         }
  128.         try{if(s.startsWith("loadip")){
  129.             return load(s.split(" ")[1],r);
  130.         }
  131.         if(s.startsWith("load")){
  132.             return load(s.split(" ")[1],r.clone());
  133.         }}catch(FileNotFoundException e){
  134.             throwError("module "+s.split(" ")[1]+" does not exist.",name,l);
  135.         }
  136.         if(s.startsWith("~")){
  137.             return expr(s.substring(1),r,name,l)==0?1:0;
  138.         }
  139.         if(s.startsWith("-")){
  140.             return -1*expr(s.substring(1),r,name,l);
  141.         }
  142.         if(s.startsWith("incr *")){
  143.             String t=s.substring(6);
  144.             int i=expr(t,r,name,l);
  145.             return r.set(i,r.get(i)+1);
  146.         }
  147.         if(s.startsWith("decr *")){
  148.             String t=s.substring(6);
  149.             int i=expr(t,r,name,l);
  150.             return r.set(i,r.get(i)-1);
  151.         }
  152.         if(s.startsWith("*")){
  153.             String t=s.substring(1);
  154.             int i=expr(t,r,name,l);
  155.             return r.get(i);
  156.         }
  157.         if(s.startsWith("incr")){
  158.             String t=s.split(" ")[1];
  159.             if(!t.matches("[A-Z]+")){
  160.                 throwError(t+" is not a legal variable name.",name,l);
  161.             }
  162.             Variable v=r.c.getByName(t);
  163.             v.set(v.get()+1);
  164.             return v.get();
  165.         }
  166.         if(s.startsWith("decr")){
  167.             String t=s.split(" ")[1];
  168.             if(!t.matches("[A-Z]+")){
  169.                 throwError(t+" is not a legal variable name.",name,l);
  170.             }
  171.             Variable v=r.c.getByName(t);
  172.             v.set(v.get()-1);
  173.             return v.get();
  174.         }
  175.         if(s.startsWith("set")){
  176.             int z=s.indexOf(" =");
  177.             int val=expr(s.substring(2+z),r,name,l);
  178.             String t=s.substring(4,z);
  179.             for(String u:t.split(",")){
  180.                 if(u.startsWith("*")){
  181.                     r.set(expr(u.substring(1),r,name,l),val);
  182.                     continue;
  183.                 }
  184.                 if(!u.matches("[A-Z]+")){
  185.                     throwError(t+" is not a legal variable name.",name,l);
  186.                 }
  187.                 Variable v=r.c.getByName(u);
  188.                 v.set(val);
  189.             }
  190.             return val;
  191.         }
  192.         if(s.startsWith("readline")){
  193.             int i=expr(s.substring(9),r,name,l);
  194.             try{char[]k=in.nextLine().toCharArray();
  195.             for(char c:k){
  196.                 r.set(i,(int)c);
  197.                 i++;
  198.             }
  199.             return k.length;
  200.             }catch(Exception e){
  201.                 return 0;
  202.             }
  203.         }
  204.         if(s.equals("@IN")){
  205.             return getANumber();
  206.         }
  207.         if(s.matches("^\\d+$")){
  208.             return Integer.parseInt(s);
  209.         }
  210.         throwError("Could not evaluate expression "+s,name,l);
  211.         return 0;
  212.     }
  213.     static String process(String line){
  214.         line=line.replaceAll("^\\s+","");
  215.         line=line.replaceAll("!!.+$","");
  216.         return line;
  217.     }
  218.     static int load(String mod,Runtime r) throws FileNotFoundException{
  219.         Scanner c=null;
  220.         c=new Scanner(new File(mod+".ldr"));
  221.         List<String>lines=new ArrayList<>();
  222.         while(c.hasNext())lines.add(process(c.nextLine()));
  223.         c.close();
  224.         int z=0;
  225.         loop:for(String line:lines){
  226.             z++;
  227.             if(line.isEmpty())continue;
  228.             int COND=line.indexOf(":");
  229.             while(COND!=-1){
  230.                 if(0!=expr(line.substring(0,COND),r,mod,z)){
  231.                     line=line.substring(COND+1);
  232.                     COND=line.indexOf(":");
  233.                 }else{
  234.                     continue loop;
  235.                 }
  236.             }
  237.             if(line.startsWith("yield")){
  238.                 return expr(line.substring(6),r,mod,z);
  239.             }
  240.             if(line.startsWith("print ")){
  241.                 for(String s:line.substring(6).split(",")){
  242.                     System.out.println(expr(s,r,mod,z));
  243.                 }
  244.                 continue;
  245.             }
  246.             if(line.startsWith("printst ")){               
  247.                 for(int k=expr(line.substring(8),r,mod,z);r.get(k)!=0;k++){
  248.                     System.out.print((char)r.get(k));
  249.                 }
  250.                 System.out.println();
  251.                 continue;
  252.             }
  253.             if(line.startsWith("erase ")){             
  254.                 for(int k=expr(line.substring(6),r,mod,z);r.get(k)!=0;k++){
  255.                     r.set(k,0);
  256.                 }
  257.                 continue;
  258.             }
  259.             if(line.startsWith("printchar ")){
  260.                 for(String s:line.substring(10).split(",")){
  261.                     System.out.print((char)expr(s,r,mod,z));
  262.                 }
  263.                 System.out.println();
  264.                 continue;
  265.             }
  266.             if(line.startsWith("printbool ")){
  267.                 System.out.println(0==expr(line.substring(10),r,mod,z)?"FALSE":"TRUE");
  268.                 continue;
  269.             }
  270.             if(line.startsWith("printf \"")){
  271.                 int q=line.lastIndexOf("\"");
  272.                 Iterator<String>val;
  273.                 try{val=Arrays.asList(line.substring(2+q).split(",")).iterator();}catch(Exception e){val=null;}
  274.                 String t=line.substring(8,q);
  275.                 int i=t.indexOf("%n"),j=t.indexOf("%c"),k=t.indexOf("%b");
  276.                 while(true){
  277.                     if((j==-1||i<j)&&(k==-1||i<k)&&i!=-1){
  278.                         t=t.replaceFirst("%n",""+expr(val.next(),r,mod,z));
  279.                     }else if(j!=-1&&(k==-1||j<k)){
  280.                         t=t.replaceFirst("%c",""+(char)expr(val.next(),r,mod,z));
  281.                     }else if(k!=-1){
  282.                         t=t.replaceFirst("%b",0==expr(val.next(),r,mod,z)?"FALSE":"TRUE");
  283.                     }else{
  284.                         break;
  285.                     }
  286.                     i=t.indexOf("%n");j=t.indexOf("%c");
  287.                 }
  288.                 System.out.println(t);
  289.                 continue;
  290.             }
  291.             if(line.startsWith("throw \"")){
  292.                 int q=line.lastIndexOf("\"");
  293.                 Iterator<String>val;
  294.                 try{val=Arrays.asList(line.substring(2+q).split(",")).iterator();}catch(Exception e){val=null;}
  295.                 String t=line.substring(7,q);
  296.                 int i=t.indexOf("%n"),j=t.indexOf("%c"),k=t.indexOf("%b");
  297.                 while(true){
  298.                     if((j==-1||i<j)&&(k==-1||i<k)&&i!=-1){
  299.                         t=t.replaceFirst("%n",""+expr(val.next(),r,mod,z));
  300.                     }else if(j!=-1&&(k==-1||j<k)){
  301.                         t=t.replaceFirst("%c",""+(char)expr(val.next(),r,mod,z));
  302.                     }else if(k!=-1){
  303.                         t=t.replaceFirst("%b",0==expr(val.next(),r,mod,z)?"FALSE":"TRUE");
  304.                     }else{
  305.                         break;
  306.                     }
  307.                     i=t.indexOf("%n");j=t.indexOf("%c");
  308.                 }
  309.                 throwError(t);
  310.             }
  311.             if(line.startsWith("warn \"")){
  312.                 int q=line.lastIndexOf("\"");
  313.                 Iterator<String>val;
  314.                 try{val=Arrays.asList(line.substring(2+q).split(",")).iterator();}catch(Exception e){val=null;}
  315.                 String t=line.substring(6,q);
  316.                 int i=t.indexOf("%n"),j=t.indexOf("%c"),k=t.indexOf("%b");
  317.                 while(true){
  318.                     if((j==-1||i<j)&&(k==-1||i<k)&&i!=-1){
  319.                         t=t.replaceFirst("%n",""+expr(val.next(),r,mod,z));
  320.                     }else if(j!=-1&&(k==-1||j<k)){
  321.                         t=t.replaceFirst("%c",""+(char)expr(val.next(),r,mod,z));
  322.                     }else if(k!=-1){
  323.                         t=t.replaceFirst("%b",0==expr(val.next(),r,mod,z)?"FALSE":"TRUE");
  324.                     }else{
  325.                         break;
  326.                     }
  327.                     i=t.indexOf("%n");j=t.indexOf("%c");
  328.                 }
  329.                 throwWarning(t);
  330.                 continue;
  331.             }
  332.             if(line.startsWith("write \"")){
  333.                 int q=line.lastIndexOf("\"");
  334.                 int val=expr(line.substring(2+q),r,mod,z);
  335.                 String t=line.substring(7,q);
  336.                 for(char k:t.toCharArray()){
  337.                     r.set(val,(int)k);
  338.                     val++;
  339.                 }
  340.                 continue;
  341.             }
  342.             if(line.equals("exit"))System.exit(0);
  343.             expr(line,r,mod,z);
  344.         }
  345.         return 0;
  346.     }
  347.     public static void main(String[]args) throws IOException{
  348.         if(args.length==0){
  349.             args=new String[]{in.nextLine()};
  350.         }
  351.         try{
  352.             load(args[0],new Runtime());
  353.         }catch(StackOverflowError e){
  354.             throwError("Stack Overflow");
  355.         }
  356.     }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement