Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class HSPAL {
- static int register=0;
- static Random r=new Random();
- static List<Stack<Integer>> stacks=new ArrayList<Stack<Integer>>()
- {
- private static final long serialVersionUID = 1L;
- {
- for(int i=0;i<=255;i++)add(new Stack<Integer>());
- }};
- static Map<Integer,Integer> checkpoints=new HashMap<Integer,Integer>();
- public static int status=0;
- @SuppressWarnings("resource")
- public static void main(String[] a) throws IOException{
- PrintStream error=new PrintStream("error.txt");
- try{String lastInput="";
- String s1=a.length>=1?a[0]:"code.txt";
- File k=new File(s1);
- String s2=a.length>=2?a[1]:"input.txt";
- String s3=a.length>=3?a[2]:"output.txt";
- (new File(s3)).createNewFile();
- (new File(s2)).createNewFile();
- Scanner input=new Scanner(new File(s2));
- PrintStream output=new PrintStream(s3);
- k.createNewFile();
- BufferedReader reader=new BufferedReader(new FileReader(k));
- List<String> commands=new ArrayList<String>();
- String c=reader.readLine();
- int line=0;
- while(c!=null){
- line++;
- if(c.length()%6!=0)throw new IllegalArgumentException("Invalid syntax on line "+line+", the length of each line must be a multiple of six.");
- if(c.length()==0){continue ;}
- for(int x=0;x<c.length()/6;x++){
- String command=c.substring(6*x, 6*(x+1));
- 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.");}
- commands.add(command);
- }
- c=reader.readLine();
- }
- for(int x=0;x<commands.size();x++){
- String command=commands.get(x);
- if(command.substring(0, 2).equals("00")){
- int key=Integer.parseInt(command.substring(2,6),16);
- if(checkpoints.get(key)!=null)throw new IllegalArgumentException("Duplicate checkpoint on line "+(x+1));
- checkpoints.put(key,x);
- }
- }
- for(int x=0;x<commands.size();x++){
- String command=commands.get(x);
- if(command.substring(0, 2).equals("01")){
- Integer y=checkpoints.get(Integer.parseInt(command.substring(2,6),16));
- if(y==null)throw new IllegalArgumentException("Invalid checkpoint!");
- x=y;
- }
- else if(command.substring(0, 2).equals("02")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- Integer y=checkpoints.get(stacks.get(stackAddress).pop());
- if(y==null)throw new IllegalArgumentException("Invalid checkpoint!");
- x=y;
- }
- else if(command.substring(0, 2).equals("03")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- Integer y=stacks.get(stackAddress).pop();
- if(y!=0)x++;
- }
- else if(command.substring(0, 2).equals("04")){
- status=Integer.parseInt(command.substring(2,6),16);
- break ;
- }
- else if(command.substring(0, 2).equals("10")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- if(lastInput.length()==0){
- if(input.hasNext()){lastInput=input.nextLine();}else{stacks.get(stackAddress).push(0);continue ;};
- }
- stacks.get(stackAddress).push((int)lastInput.charAt(0));
- lastInput=lastInput.substring(1);
- }
- else if(command.substring(0, 2).equals("11")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- int value=input.hasNext()?input.nextInt():0;
- if(value<0)throw new IllegalArgumentException();
- stacks.get(stackAddress).push(value);
- }
- else if(command.substring(0, 2).equals("12")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- int m;
- Stack<Integer> stack=stacks.get(stackAddress);
- try{m=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
- output.println(m);
- }
- else if(command.substring(0, 2).equals("13")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- int charCode=stacks.get(stackAddress).pop();
- if(charCode==0){
- output.println();
- }else{
- output.print((char)charCode);
- }
- }
- else if(command.substring(0, 2).equals("14")){
- int stackAddress=Integer.parseInt(command.substring(2,4),16);
- Stack<Integer> stack=stacks.get(stackAddress);
- while(stack.size()!=0){
- int charCode=stacks.get(stackAddress).pop();
- if(charCode==0){
- output.println();
- }else{
- output.print((char)charCode);
- }
- }
- }
- else if(command.substring(0, 2).equals("20")){
- register=Integer.parseInt(command.substring(2,6),16);
- }
- else if(command.substring(0, 2).equals("21")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- int m=stack.pop(),n=stack.pop();
- stack.push(Math.min(m+n,65535));
- }
- else if(command.substring(0, 2).equals("22")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- int m=stack.pop(),n=stack.pop();
- stack.push(Math.max(0, m-n));
- }
- else if(command.substring(0, 2).equals("23")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- int m=stack.pop(),n=stack.pop();
- stack.push(Math.min(m*n,65535));
- }
- else if(command.substring(0, 2).equals("24")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- int m=stack.pop(),n=stack.pop();
- stack.push(m/n);
- }
- else if(command.substring(0, 2).equals("25")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- int m=stack.pop(),n=stack.pop();
- stack.push((int)Math.min((long)Math.pow(m, n),65535));
- }
- else if(command.substring(0, 2).equals("26")){
- register=r.nextInt(1+Integer.parseInt(command.substring(2,6),16));
- }
- else if(command.substring(0, 2).equals("30")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
- int m=stack.pop(),n=stack.pop();
- stack2.push((m==n)?1:0);
- }else if(command.substring(0, 2).equals("31")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
- int m=stack.pop(),n=stack.pop();
- stack2.push((m>n)?1:0);
- }else if(command.substring(0, 2).equals("32")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
- int m,n;
- try{m=stack.pop();n=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
- stack2.push((m<n)?1:0);
- }else if(command.substring(0, 2).equals("33")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
- int m=stack.pop(),n=stack.pop();
- stack2.push((m+n!=0)?1:0);
- }else if(command.substring(0, 2).equals("34")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
- int m,n;
- try{m=stack.pop();n=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
- stack2.push((m*n!=0)?1:0);
- }else if(command.substring(0, 2).equals("35")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- Stack<Integer> stack2=stacks.get(Integer.parseInt(command.substring(4,6),16));
- int m,n;
- try{m=stack.pop();n=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
- stack2.push((m*n==0&&m+n!=0)?1:0);
- }else if(command.substring(0, 2).equals("36")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- stack.push((register==0)?1:0);
- }
- else if(command.substring(0, 2).equals("40")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- stack.push(register);
- register=0;
- }
- else if(command.substring(0, 2).equals("41")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- try{register=stack.pop();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
- }else if(command.substring(0, 2).equals("42")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- try{register=stack.peek();}catch(Exception e){throw new Exception("Empty stack exception on line "+(x+1));}
- }else if(command.substring(0, 2).equals("43")){
- Stack<Integer> stack=stacks.get(Integer.parseInt(command.substring(2,4),16));
- register=Math.min(stack.size(),65535);
- }
- else if(command.substring(0, 2).equals("00")){
- }
- else{
- String s=command.substring(0, 2);
- throw new IllegalArgumentException("Invalid syntax on line "+(x+1)+":"+s+" is not a legal command");
- }
- }}catch(Exception exc){
- System.out.println("An exception was thrown!");
- exc.printStackTrace(System.out);
- exc.printStackTrace(error);
- status=-1;
- }
- PrintStream statusout=new PrintStream("status.txt");
- statusout.print(status);
- System.out.print("Halting with status "+status);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement