Advertisement
SuperJedi224

HSPAL Interpreter

Apr 7th, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.12 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class HSPAL {
  5.     static int register=0;
  6.     static Random r=new Random();
  7.     static List<Stack<Integer>> stacks=new ArrayList<Stack<Integer>>()
  8.             {
  9.                 private static final long serialVersionUID = 1L;
  10.  
  11.             {
  12.                 for(int i=0;i<=255;i++)add(new Stack<Integer>());
  13.             }};
  14.     static Map<Integer,Integer> checkpoints=new HashMap<Integer,Integer>();
  15.     public static int status=0;
  16.     @SuppressWarnings("resource")
  17.     public static void main(String[] a) throws IOException{
  18.         PrintStream error=new PrintStream("error.txt");
  19.         try{String lastInput="";
  20.         String s1=a.length>=1?a[0]:"code.txt";
  21.         File k=new File(s1);
  22.         String s2=a.length>=2?a[1]:"input.txt";
  23.         String s3=a.length>=3?a[2]:"output.txt";
  24.         (new File(s3)).createNewFile();
  25.         (new File(s2)).createNewFile();
  26.         Scanner input=new Scanner(new File(s2));
  27.         PrintStream output=new PrintStream(s3);
  28.         k.createNewFile();
  29.         BufferedReader reader=new BufferedReader(new FileReader(k));
  30.         List<String> commands=new ArrayList<String>();
  31.         String c=reader.readLine();
  32.         int line=0;
  33.         while(c!=null){
  34.             line++;
  35.             if(c.length()%6!=0)throw new IllegalArgumentException("Invalid syntax on line "+line+", the length of each line must be a multiple of six.");
  36.             if(c.length()==0){continue ;}
  37.             for(int x=0;x<c.length()/6;x++){
  38.                 String command=c.substring(6*x, 6*(x+1));
  39.                 try{Integer.parseInt(command,16);}catch(Exception e){throw new IllegalArgumentException("Invalid syntax on line "+line+", each command must be a valid base-16 integer.");}
  40.                 commands.add(command);
  41.             }
  42.             c=reader.readLine();
  43.         }
  44.         for(int x=0;x<commands.size();x++){
  45.             String command=commands.get(x);
  46.             if(command.substring(0, 2).equals("00")){
  47.                 int key=Integer.parseInt(command.substring(2,6),16);
  48.                 if(checkpoints.get(key)!=null)throw new IllegalArgumentException("Duplicate checkpoint on line "+(x+1));
  49.                 checkpoints.put(key,x);
  50.             }
  51.         }
  52.         for(int x=0;x<commands.size();x++){
  53.             String command=commands.get(x);
  54.             if(command.substring(0, 2).equals("01")){
  55.                 Integer y=checkpoints.get(Integer.parseInt(command.substring(2,6),16));
  56.                 if(y==null)throw new IllegalArgumentException("Invalid checkpoint!");
  57.                 x=y;
  58.             }
  59.             else if(command.substring(0, 2).equals("02")){
  60.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  61.                 Integer y=checkpoints.get(stacks.get(stackAddress).pop());
  62.                 if(y==null)throw new IllegalArgumentException("Invalid checkpoint!");
  63.                 x=y;
  64.             }
  65.             else if(command.substring(0, 2).equals("03")){
  66.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  67.                 Integer y=stacks.get(stackAddress).pop();
  68.                 if(y!=0)x++;
  69.             }
  70.             else if(command.substring(0, 2).equals("04")){
  71.                 status=Integer.parseInt(command.substring(2,6),16);
  72.                 break ;
  73.             }
  74.             else if(command.substring(0, 2).equals("10")){
  75.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  76.                 if(lastInput.length()==0){
  77.                     if(input.hasNext()){lastInput=input.nextLine();}else{stacks.get(stackAddress).push(0);continue ;};
  78.                 }
  79.                 stacks.get(stackAddress).push((int)lastInput.charAt(0));
  80.                 lastInput=lastInput.substring(1);
  81.             }
  82.             else if(command.substring(0, 2).equals("11")){
  83.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  84.                 int value=input.hasNext()?input.nextInt():0;
  85.                 if(value<0)throw new IllegalArgumentException();
  86.                 stacks.get(stackAddress).push(value);
  87.             }
  88.             else if(command.substring(0, 2).equals("12")){
  89.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  90.                 int m;
  91.                 Stack<Integer> stack=stacks.get(stackAddress);
  92.                 try{m=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
  93.                 output.println(m);
  94.             }
  95.             else if(command.substring(0, 2).equals("13")){
  96.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  97.                 int charCode=stacks.get(stackAddress).pop();
  98.                 if(charCode==0){
  99.                     output.println();
  100.                 }else{
  101.                     output.print((char)charCode);
  102.                 }
  103.             }
  104.             else if(command.substring(0, 2).equals("14")){
  105.                 int stackAddress=Integer.parseInt(command.substring(2,4),16);
  106.                 Stack<Integer> stack=stacks.get(stackAddress);
  107.                 while(stack.size()!=0){
  108.                     int charCode=stacks.get(stackAddress).pop();
  109.                     if(charCode==0){
  110.                         output.println();
  111.                     }else{
  112.                         output.print((char)charCode);
  113.                     }
  114.                 }
  115.             }
  116.             else if(command.substring(0, 2).equals("20")){
  117.                 register=Integer.parseInt(command.substring(2,6),16);
  118.             }
  119.             else if(command.substring(0, 2).equals("21")){
  120.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  121.                 int m=stack.pop(),n=stack.pop();
  122.                 stack.push(Math.min(m+n,65535));
  123.             }
  124.             else if(command.substring(0, 2).equals("22")){
  125.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  126.                 int m=stack.pop(),n=stack.pop();
  127.                 stack.push(Math.max(0, m-n));
  128.             }
  129.             else if(command.substring(0, 2).equals("23")){
  130.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  131.                 int m=stack.pop(),n=stack.pop();
  132.                 stack.push(Math.min(m*n,65535));
  133.             }
  134.             else if(command.substring(0, 2).equals("24")){
  135.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  136.                 int m=stack.pop(),n=stack.pop();
  137.                 stack.push(m/n);
  138.             }
  139.             else if(command.substring(0, 2).equals("25")){
  140.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  141.                 int m=stack.pop(),n=stack.pop();
  142.                 stack.push((int)Math.min((long)Math.pow(m, n),65535));
  143.             }
  144.             else if(command.substring(0, 2).equals("26")){
  145.                 register=r.nextInt(1+Integer.parseInt(command.substring(2,6),16));
  146.             }
  147.             else if(command.substring(0, 2).equals("30")){
  148.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  149.                 Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
  150.                 int m=stack.pop(),n=stack.pop();
  151.                 stack2.push((m==n)?1:0);
  152.             }else if(command.substring(0, 2).equals("31")){
  153.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  154.                 Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
  155.                 int m=stack.pop(),n=stack.pop();
  156.                 stack2.push((m>n)?1:0);
  157.             }else if(command.substring(0, 2).equals("32")){
  158.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  159.                 Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
  160.                 int m,n;
  161.                 try{m=stack.pop();n=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
  162.                 stack2.push((m<n)?1:0);
  163.             }else if(command.substring(0, 2).equals("33")){
  164.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  165.                 Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
  166.                 int m=stack.pop(),n=stack.pop();
  167.                 stack2.push((m+n!=0)?1:0);
  168.             }else if(command.substring(0, 2).equals("34")){
  169.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  170.                 Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
  171.                 int m,n;
  172.                 try{m=stack.pop();n=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
  173.                 stack2.push((m*n!=0)?1:0);
  174.             }else if(command.substring(0, 2).equals("35")){
  175.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  176.                 Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
  177.                 int m,n;
  178.                 try{m=stack.pop();n=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
  179.                 stack2.push((m*n==0&&m+n!=0)?1:0);
  180.             }else if(command.substring(0, 2).equals("36")){
  181.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  182.                 stack.push((register==0)?1:0);
  183.             }
  184.             else if(command.substring(0, 2).equals("40")){
  185.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  186.                 stack.push(register);
  187.                 register=0;
  188.             }
  189.             else if(command.substring(0, 2).equals("41")){
  190.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  191.                 try{register=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
  192.             }else if(command.substring(0, 2).equals("42")){
  193.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  194.                 try{register=stack.peek();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
  195.             }else if(command.substring(0, 2).equals("43")){
  196.                 Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
  197.                 register=Math.min(stack.size(),65535);
  198.             }
  199.             else if(command.substring(0, 2).equals("00")){
  200.             }
  201.             else{
  202.                 String s=command.substring(0, 2);
  203.                 throw new IllegalArgumentException("Invalid syntax on line "+(x+1)+":"+s+" is not a legal command");
  204.             }
  205.         }}catch(Exception exc){
  206.             System.out.println("An exception was thrown!");
  207.             exc.printStackTrace(System.out);
  208.             exc.printStackTrace(error);
  209.             status=-1;
  210.         }
  211.         PrintStream statusout=new PrintStream("status.txt");
  212.         statusout.print(status);
  213.         System.out.print("Halting with status "+status);
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement