Skip to content

Commit b04f64e

Browse files
committed
Add Some Functions
1 parent 29cde44 commit b04f64e

File tree

2 files changed

+113
-7
lines changed

2 files changed

+113
-7
lines changed

src/main/java/io/stack_community/github/stack_java/Functions.java

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.stack_community.github.stack_java;
22

3-
43
import java.util.HashMap;
54
import java.util.function.Supplier;
65

@@ -21,7 +20,7 @@ public Functions(Stack.Executor executor) {
2120
{ // Add Builtin Commands
2221
// Commands of calculation
2322

24-
// addition
23+
// Addition
2524
addFunction("add", (e) -> {
2625
double b = e.popStack().getNumber();
2726
double a = e.popStack().getNumber();
@@ -35,9 +34,77 @@ public Functions(Stack.Executor executor) {
3534
e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, a - b));
3635
});
3736

38-
// Pop in stack
39-
addFunction("pop", (e) -> {
40-
e.popStack();
37+
// Multiplication
38+
addFunction("mul", (e) -> {
39+
double b = e.popStack().getNumber();
40+
double a = e.popStack().getNumber();
41+
e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, a * b));
42+
});
43+
44+
// Division
45+
addFunction("div", (e) -> {
46+
double b = e.popStack().getNumber();
47+
double a = e.popStack().getNumber();
48+
e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, a / b));
49+
});
50+
51+
// Remainder of division
52+
addFunction("mod", (e) -> {
53+
double b = e.popStack().getNumber();
54+
double a = e.popStack().getNumber();
55+
e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, a % b));
56+
});
57+
58+
// Exponentiation
59+
addFunction("pow", (e) -> {
60+
double b = e.popStack().getNumber();
61+
double a = e.popStack().getNumber();
62+
e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, Math.pow(a, b)));
63+
});
64+
65+
// Rounding off
66+
addFunction("round", (e) -> {
67+
double a = e.popStack().getNumber();
68+
e.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, Math.round(a)));
69+
});
70+
71+
// Trigonometric sine
72+
addFunction("sin", (e) -> {
73+
74+
});
75+
76+
// Commands of control
77+
78+
// Evaluate string as program
79+
addFunction("eval", (e) -> {
80+
String code = e.popStack().getString();
81+
e.evaluateProgram(code);
82+
});
83+
84+
// Conditional branch
85+
addFunction("if", (e) -> {
86+
boolean condition = e.popStack().getBool();
87+
String code_else = e.popStack().getString();
88+
String code_if = e.popStack().getString();
89+
90+
if (condition) {
91+
e.evaluateProgram(code_if);
92+
} else {
93+
e.evaluateProgram(code_else);
94+
}
95+
});
96+
97+
// Loop while condition is true
98+
addFunction("while", (e) -> {
99+
String cond = e.popStack().getString();
100+
String code = e.popStack().getString();
101+
102+
while (true) {
103+
e.evaluateProgram(cond);
104+
if (!e.popStack().getBool()) break;
105+
106+
e.evaluateProgram(code);
107+
}
41108
});
42109

43110
// Exit Stack
@@ -50,6 +117,45 @@ public Functions(Stack.Executor executor) {
50117
}
51118
});
52119

120+
// Commands of memory manage
121+
122+
// Pop in the stack
123+
addFunction("pop", Stack.Executor::popStack);
124+
125+
// Define variable at memory
126+
addFunction("var", (e) -> {
127+
String name = e.popStack().getString();
128+
Stack.Type data = e.popStack();
129+
if (e.memory.containsKey(name)) {
130+
e.memory.replace(name, data);
131+
} else {
132+
e.memory.put(name, data);
133+
}
134+
e.showVariables();
135+
});
136+
137+
// Free up memory space of variable
138+
addFunction("free", (e) -> {
139+
String name = e.popStack().getString();
140+
e.memory.remove(name);
141+
e.showVariables();
142+
});
143+
144+
// Copy stack's top value
145+
addFunction("copy", (e) -> {
146+
Stack.Type data = e.popStack();
147+
e.stack.add(data);
148+
e.stack.add(data);
149+
});
150+
151+
// Swap stack's top 2 value
152+
addFunction("swap", (e) -> {
153+
Stack.Type b = e.popStack();
154+
Stack.Type a = e.popStack();
155+
e.stack.add(b);
156+
e.stack.add(a);
157+
});
158+
53159
// Print String
54160
addFunction("print", (e) -> {
55161
String str = e.popStack().getString();

src/main/java/io/stack_community/github/stack_java/Stack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class Stack {
99
public static String name = "Stack Programming Language: Java Edition";
1010
public static String baseVersion = "1.12";
11-
public static String version = "1.0.0-Betaa";
11+
public static String version = "1.0.0-Beta";
1212

1313
public Stack() {
1414

@@ -205,7 +205,7 @@ public static enum Mode {
205205

206206
public static class Executor {
207207
protected List<Type> stack; // Data stack
208-
private Map<String, Type> memory; // Variable's memory
208+
protected Map<String, Type> memory; // Variable's memory
209209
private Mode mode; // Execution mode
210210
private Functions functions;
211211
public boolean exitmode = false;

0 commit comments

Comments
 (0)