Commits

Roman Dawydkin committed bf80850

More math commands.

Comments (0)

Files changed (1)

src/toy/MathCommand.java

 {
 	public static void installCommands(Environment env) {
 		new MathCommand(env, "+") {
-			protected void exec(Machine m,
-					BigDecimal num1, BigDecimal num2)
-			{
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
 				m.push(num1.add(num2));
 			}
 		};
 		new MathCommand(env, "-") {
-			protected void exec(Machine m,
-					BigDecimal num1, BigDecimal num2)
-			{
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
 				m.push(num1.subtract(num2));
 			}
 		};
 		new MathCommand(env, "*") {
-			protected void exec(Machine m,
-					BigDecimal num1, BigDecimal num2)
-			{
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
 				m.push(num1.multiply(num2));
 			}
 		};
 		new MathCommand(env, "/") {
-			protected void exec(Machine m,
-					BigDecimal num1, BigDecimal num2)
-			{
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
 				m.push(num1.divide(num2, BigDecimal.ROUND_HALF_UP));
 			}
 		};
+		new MathCommand(env, "%") {
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
+				m.push(num1.remainder(num2));
+			}
+		};
+		new MathCommand(env, "min") {
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
+				m.push(num1.min(num2));
+			}
+		};
+		new MathCommand(env, "max") {
+			protected void exec(Machine m, BigDecimal num1, BigDecimal num2) {
+				m.push(num1.max(num2));
+			}
+		};
+		new Command(env, "succ") {
+			public void exec(Machine m) {
+				final BigDecimal num = (BigDecimal) m.pop();
+				m.push(num.add(BigDecimal.ONE));
+			}
+		};
+		new Command(env, "pred") {
+			public void exec(Machine m) {
+				final BigDecimal num = (BigDecimal) m.pop();
+				m.push(num.subtract(BigDecimal.ONE));
+			}
+		};
+		new Command(env, "abs") {
+			public void exec(Machine m) {
+				final BigDecimal num = (BigDecimal) m.pop();
+				m.push(num.abs());
+			}
+		};
+		new Command(env, "sign") {
+			public void exec(Machine m) {
+				final BigDecimal num = (BigDecimal) m.pop();
+				m.push(BigDecimal.valueOf(num.signum()));
+			}
+		};
+		new Command(env, "fact") {
+			public void exec(Machine m) {
+				BigDecimal num = (BigDecimal) m.pop();
+				BigDecimal res = BigDecimal.ONE;
+				while (num.compareTo(BigDecimal.ONE) > 0) {
+					res = res.multiply(num);
+					num = num.subtract(BigDecimal.ONE);
+				}
+				m.push(res);
+			}
+		};
+		new Command(env, "exp") {
+			public void exec(Machine m) {
+				final BigDecimal num = (BigDecimal) m.pop();
+				m.push(BigDecimal.valueOf(Math.exp(num.doubleValue())));
+			}
+		};
+		new Command(env, "log") {
+			public void exec(Machine m) {
+				final BigDecimal num = (BigDecimal) m.pop();
+				m.push(BigDecimal.valueOf(Math.log(num.doubleValue())));
+			}
+		};
+		//	fib == [1 0] dip [swap [+] unary] times popd;
+		//	nfib == [1 1] dip [dup [+ succ] dip swap] times pop;
+		//	gcd == [0 >] [dup rollup rem] while pop;
+		new LiteralCommand(env, "true", true);
+		new LiteralCommand(env, "false", false);
+		new Command(env, "and") {
+			public void exec(Machine m) {
+				final boolean x = (Boolean) m.pop();
+				final boolean y = (Boolean) m.pop();
+				m.push(x & y);
+			}
+		};
+		new Command(env, "or") {
+			public void exec(Machine m) {
+				final boolean x = (Boolean) m.pop();
+				final boolean y = (Boolean) m.pop();
+				m.push(x | y);
+			}
+		};
+		new Command(env, "xor") {
+			public void exec(Machine m) {
+				final boolean x = (Boolean) m.pop();
+				final boolean y = (Boolean) m.pop();
+				m.push(y ^ x);
+			}
+		};
+		new Command(env, "not") {
+			public void exec(Machine m) {
+				final boolean x = (Boolean) m.pop();
+				m.push(!x);
+			}
+		};
 	}
 
 	public MathCommand(Environment env, String name) {