|
1 | 1 | package io.stack_community.github.stack_java; |
2 | 2 |
|
3 | 3 | import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Random; |
4 | 6 | import java.util.function.Supplier; |
5 | 7 |
|
6 | 8 | public class Functions { |
@@ -70,7 +72,60 @@ public Functions(Stack.Executor executor) { |
70 | 72 |
|
71 | 73 | // Trigonometric sine |
72 | 74 | addFunction("sin", (e) -> { |
| 75 | + double number = e.popStack().getNumber(); |
| 76 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, Math.sin(number))); |
| 77 | + }); |
| 78 | + |
| 79 | + // Trigonometric cosine |
| 80 | + addFunction("cos", (e) -> { |
| 81 | + double number = e.popStack().getNumber(); |
| 82 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, Math.cos(number))); |
| 83 | + }); |
| 84 | + |
| 85 | + // Trigonometric tangent |
| 86 | + addFunction("tan", (e) -> { |
| 87 | + double number = e.popStack().getNumber(); |
| 88 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, Math.tan(number))); |
| 89 | + }); |
| 90 | + |
| 91 | + // Logical operations of AND |
| 92 | + addFunction("and", (e) -> { |
| 93 | + boolean b = e.popStack().getBool(); |
| 94 | + boolean a = e.popStack().getBool(); |
| 95 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.BOOL, a && b)); |
| 96 | + }); |
| 97 | + |
| 98 | + // Logical operations of OR |
| 99 | + addFunction("or", (e) -> { |
| 100 | + boolean b = e.popStack().getBool(); |
| 101 | + boolean a = e.popStack().getBool(); |
| 102 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.BOOL, a || b)); |
| 103 | + }); |
| 104 | + |
| 105 | + // Logical operations of NOT |
| 106 | + addFunction("not", (e) -> { |
| 107 | + boolean a = e.popStack().getBool(); |
| 108 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.BOOL, !a)); |
| 109 | + }); |
| 110 | + |
| 111 | + // Judge is it equal |
| 112 | + addFunction("equal", (e) -> { |
| 113 | + String b = e.popStack().getString(); |
| 114 | + String a = e.popStack().getString(); |
| 115 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.BOOL, a.equals(b))); |
| 116 | + }); |
| 117 | + |
| 118 | + // Judge is it less |
| 119 | + addFunction("less", (e) -> { |
| 120 | + double b = e.popStack().getNumber(); |
| 121 | + double a = e.popStack().getNumber(); |
| 122 | + e.stack.add(new Stack.Type(Stack.Type.TypeEnum.BOOL, a < b)); |
| 123 | + }); |
73 | 124 |
|
| 125 | + addFunction("rand", (e) -> { |
| 126 | + List<Stack.Type> list = e.popStack().getList(); |
| 127 | + Stack.Type result = list.get(new Random().nextInt(list.size())); |
| 128 | + e.stack.add(result); |
74 | 129 | }); |
75 | 130 |
|
76 | 131 | // Commands of control |
|
0 commit comments