789101112131415
public abstract class LogicalOperator implements Operator { public static class Not implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); stack.push(stack.pop() == 0 ? 1 : 0); }
232425262728293031323334
/** * Pops one entry from the stack. Pushes a 1 if the entry is greater than 0, pushes a 0 otherwise */ public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); Long b = stack.pop(); Long a = stack.pop(); stack.push(a > b ? 1 : 0); }
89101112131415
// Adds the two top most numbers on the stack public static class Add implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); stack.push((stack.pop() + stack.pop())); }
2122232425262728
// Subtracts the two top most numbers on the stack public static class Subtract implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); stack.push(0 - (stack.pop() - stack.pop())); }
3435363738394041
// Adds the two top most numbers on the stack public static class Multiply implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); stack.push((stack.pop() * stack.pop())); }
47484950515253545556575859606162
// Divides the two top most numbers on the stack public static class Divide implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); long b = stack.pop(); long a = stack.pop(); if (b == 0) { stack.push(0); } else { stack.push(a / b); } }
68697071727374757677787980818283
// Divides the two top most numbers on the stack public static class Modulus implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); long b = stack.pop(); long a = stack.pop(); if (b == 0) { stack.push(0); } else { stack.push(a % b); } }
910111213141516
// Clones the first element on the stack public static class OutputChar implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); System.out.print((char)stack.pop()); }
2223242526272829
// Swaps the first two values on the stack public static class OutputNum implements Operator { public void perform(Funge funge) { Stack stack = funge.getStackStack().getStack(); System.out.print(stack.pop()); }
383940414243444546474849
public void perform(Funge funge) { try { Stack stack = funge.getStackStack().getStack(); stack.push(System.in.read()); } catch (IOException err) { //TODO: Handle this exception as per spec err.printStackTrace(); }