Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.*;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class Loader{
- static int getANumber(){
- try{
- System.out.println("Enter a number.");
- return in.nextInt();
- }catch(InputMismatchException e){
- System.out.print("Illegal input. ");
- return getANumber();
- }
- }
- static void throwError(String error,String sender,int line){
- throwError(error+" (module "+sender+", line"+line+")");
- }
- static void throwError(String error){
- JFrame j=new JFrame();
- j.setAlwaysOnTop(true);
- JOptionPane.showMessageDialog(j,"Error: "+error,"",JOptionPane.ERROR_MESSAGE);
- System.exit(0);
- }
- static void throwWarning(String error){
- JFrame j=new JFrame();
- j.setAlwaysOnTop(true);
- JOptionPane.showMessageDialog(j,"Warning: "+error,"",JOptionPane.WARNING_MESSAGE);
- }
- static class Variable implements Cloneable{
- public Variable(String name,int value){
- this.name=name;
- v=value;
- }
- public Variable(String name,char value){
- this.name=name;
- v=value;
- }
- public final String name;
- private int v=0;
- public int get(){
- return v;
- }
- public Variable clone(){
- return new Variable(name,v);
- }
- public void set(int val){
- v=val;
- }
- public boolean equals(Object o){
- if(o instanceof Variable){
- return ((Variable)o).get()==v;
- }
- if(o instanceof Number){
- return ((Number)o).intValue()==v;
- }
- return false;
- }
- }
- static Scanner in=new Scanner(System.in);
- static class VariableCollection implements Cloneable{
- private List<Variable>v=new ArrayList<>();
- public VariableCollection clone(){
- VariableCollection c=new VariableCollection();
- for(Variable var:v){
- c.v.add(var.clone());
- }
- return c;
- }
- public Variable getByName(String name){
- for(Variable var:v){
- if(var.name.equals(name))return var;
- }
- Variable var=new Variable(name,0);
- v.add(var);
- return var;
- }
- }
- static class Runtime implements Cloneable{
- private VariableCollection c;
- private int[] pos,neg;
- public Runtime(){
- c=new VariableCollection();
- pos=new int[5];
- neg=new int[5];
- }
- public Runtime clone(){
- Runtime r=new Runtime();
- r.c=this.c.clone();
- r.pos=pos.clone();
- r.neg=neg.clone();
- return r;
- }
- public int get(int c){
- if(c<0){
- int i=-(1+c);
- while(neg.length<1+i){
- neg=Arrays.copyOf(neg,(int)(1.25*neg.length));
- }
- return neg[i];
- }
- while(pos.length<1+c){
- pos=Arrays.copyOf(pos,(int)(1.25*pos.length));
- }
- return pos[c];
- }
- public int set(int c,int v){
- if(c<0){
- int i=-(1+c);
- while(neg.length<1+i){
- neg=Arrays.copyOf(neg,(int)(1.25*neg.length));
- }
- return neg[i]=v;
- }
- while(pos.length<1+c){
- pos=Arrays.copyOf(pos,(int)(1.25*pos.length));
- }
- return pos[c]=v;
- }
- }
- static int expr(String s,Runtime r,String name,int l){
- if(s.matches("[A-Z]+")){
- return r.c.getByName(s).get();
- }
- try{if(s.startsWith("loadip")){
- return load(s.split(" ")[1],r);
- }
- if(s.startsWith("load")){
- return load(s.split(" ")[1],r.clone());
- }}catch(FileNotFoundException e){
- throwError("module "+s.split(" ")[1]+" does not exist.",name,l);
- }
- if(s.startsWith("~")){
- return expr(s.substring(1),r,name,l)==0?1:0;
- }
- if(s.startsWith("-")){
- return -1*expr(s.substring(1),r,name,l);
- }
- if(s.startsWith("incr *")){
- String t=s.substring(6);
- int i=expr(t,r,name,l);
- return r.set(i,r.get(i)+1);
- }
- if(s.startsWith("decr *")){
- String t=s.substring(6);
- int i=expr(t,r,name,l);
- return r.set(i,r.get(i)-1);
- }
- if(s.startsWith("*")){
- String t=s.substring(1);
- int i=expr(t,r,name,l);
- return r.get(i);
- }
- if(s.startsWith("incr")){
- String t=s.split(" ")[1];
- if(!t.matches("[A-Z]+")){
- throwError(t+" is not a legal variable name.",name,l);
- }
- Variable v=r.c.getByName(t);
- v.set(v.get()+1);
- return v.get();
- }
- if(s.startsWith("decr")){
- String t=s.split(" ")[1];
- if(!t.matches("[A-Z]+")){
- throwError(t+" is not a legal variable name.",name,l);
- }
- Variable v=r.c.getByName(t);
- v.set(v.get()-1);
- return v.get();
- }
- if(s.startsWith("set")){
- int z=s.indexOf(" =");
- int val=expr(s.substring(2+z),r,name,l);
- String t=s.substring(4,z);
- for(String u:t.split(",")){
- if(u.startsWith("*")){
- r.set(expr(u.substring(1),r,name,l),val);
- continue;
- }
- if(!u.matches("[A-Z]+")){
- throwError(t+" is not a legal variable name.",name,l);
- }
- Variable v=r.c.getByName(u);
- v.set(val);
- }
- return val;
- }
- if(s.startsWith("readline")){
- int i=expr(s.substring(9),r,name,l);
- try{char[]k=in.nextLine().toCharArray();
- for(char c:k){
- r.set(i,(int)c);
- i++;
- }
- return k.length;
- }catch(Exception e){
- return 0;
- }
- }
- if(s.equals("@IN")){
- return getANumber();
- }
- if(s.matches("^\\d+$")){
- return Integer.parseInt(s);
- }
- throwError("Could not evaluate expression "+s,name,l);
- return 0;
- }
- static String process(String line){
- line=line.replaceAll("^\\s+","");
- line=line.replaceAll("!!.+$","");
- return line;
- }
- static int load(String mod,Runtime r) throws FileNotFoundException{
- Scanner c=null;
- c=new Scanner(new File(mod+".ldr"));
- List<String>lines=new ArrayList<>();
- while(c.hasNext())lines.add(process(c.nextLine()));
- c.close();
- int z=0;
- loop:for(String line:lines){
- z++;
- if(line.isEmpty())continue;
- int COND=line.indexOf(":");
- while(COND!=-1){
- if(0!=expr(line.substring(0,COND),r,mod,z)){
- line=line.substring(COND+1);
- COND=line.indexOf(":");
- }else{
- continue loop;
- }
- }
- if(line.startsWith("yield")){
- return expr(line.substring(6),r,mod,z);
- }
- if(line.startsWith("print ")){
- for(String s:line.substring(6).split(",")){
- System.out.println(expr(s,r,mod,z));
- }
- continue;
- }
- if(line.startsWith("printst ")){
- for(int k=expr(line.substring(8),r,mod,z);r.get(k)!=0;k++){
- System.out.print((char)r.get(k));
- }
- System.out.println();
- continue;
- }
- if(line.startsWith("erase ")){
- for(int k=expr(line.substring(6),r,mod,z);r.get(k)!=0;k++){
- r.set(k,0);
- }
- continue;
- }
- if(line.startsWith("printchar ")){
- for(String s:line.substring(10).split(",")){
- System.out.print((char)expr(s,r,mod,z));
- }
- System.out.println();
- continue;
- }
- if(line.startsWith("printbool ")){
- System.out.println(0==expr(line.substring(10),r,mod,z)?"FALSE":"TRUE");
- continue;
- }
- if(line.startsWith("printf \"")){
- int q=line.lastIndexOf("\"");
- Iterator<String>val;
- try{val=Arrays.asList(line.substring(2+q).split(",")).iterator();}catch(Exception e){val=null;}
- String t=line.substring(8,q);
- int i=t.indexOf("%n"),j=t.indexOf("%c"),k=t.indexOf("%b");
- while(true){
- if((j==-1||i<j)&&(k==-1||i<k)&&i!=-1){
- t=t.replaceFirst("%n",""+expr(val.next(),r,mod,z));
- }else if(j!=-1&&(k==-1||j<k)){
- t=t.replaceFirst("%c",""+(char)expr(val.next(),r,mod,z));
- }else if(k!=-1){
- t=t.replaceFirst("%b",0==expr(val.next(),r,mod,z)?"FALSE":"TRUE");
- }else{
- break;
- }
- i=t.indexOf("%n");j=t.indexOf("%c");
- }
- System.out.println(t);
- continue;
- }
- if(line.startsWith("throw \"")){
- int q=line.lastIndexOf("\"");
- Iterator<String>val;
- try{val=Arrays.asList(line.substring(2+q).split(",")).iterator();}catch(Exception e){val=null;}
- String t=line.substring(7,q);
- int i=t.indexOf("%n"),j=t.indexOf("%c"),k=t.indexOf("%b");
- while(true){
- if((j==-1||i<j)&&(k==-1||i<k)&&i!=-1){
- t=t.replaceFirst("%n",""+expr(val.next(),r,mod,z));
- }else if(j!=-1&&(k==-1||j<k)){
- t=t.replaceFirst("%c",""+(char)expr(val.next(),r,mod,z));
- }else if(k!=-1){
- t=t.replaceFirst("%b",0==expr(val.next(),r,mod,z)?"FALSE":"TRUE");
- }else{
- break;
- }
- i=t.indexOf("%n");j=t.indexOf("%c");
- }
- throwError(t);
- }
- if(line.startsWith("warn \"")){
- int q=line.lastIndexOf("\"");
- Iterator<String>val;
- try{val=Arrays.asList(line.substring(2+q).split(",")).iterator();}catch(Exception e){val=null;}
- String t=line.substring(6,q);
- int i=t.indexOf("%n"),j=t.indexOf("%c"),k=t.indexOf("%b");
- while(true){
- if((j==-1||i<j)&&(k==-1||i<k)&&i!=-1){
- t=t.replaceFirst("%n",""+expr(val.next(),r,mod,z));
- }else if(j!=-1&&(k==-1||j<k)){
- t=t.replaceFirst("%c",""+(char)expr(val.next(),r,mod,z));
- }else if(k!=-1){
- t=t.replaceFirst("%b",0==expr(val.next(),r,mod,z)?"FALSE":"TRUE");
- }else{
- break;
- }
- i=t.indexOf("%n");j=t.indexOf("%c");
- }
- throwWarning(t);
- continue;
- }
- if(line.startsWith("write \"")){
- int q=line.lastIndexOf("\"");
- int val=expr(line.substring(2+q),r,mod,z);
- String t=line.substring(7,q);
- for(char k:t.toCharArray()){
- r.set(val,(int)k);
- val++;
- }
- continue;
- }
- if(line.equals("exit"))System.exit(0);
- expr(line,r,mod,z);
- }
- return 0;
- }
- public static void main(String[]args) throws IOException{
- if(args.length==0){
- args=new String[]{in.nextLine()};
- }
- try{
- load(args[0],new Runtime());
- }catch(StackOverflowError e){
- throwError("Stack Overflow");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement