Skip to content

Commit 6b2abed

Browse files
committed
split Stack.java and Functions.java
1 parent ef14b35 commit 6b2abed

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.stack_community.github.stack_java;
2+
3+
public record Functions(Stack.Executor executor, String command) {
4+
public Functions {
5+
// Commands of calculation
6+
7+
// addition
8+
if (command.equals("add")) {
9+
double b = executor.popStack().getNumber();
10+
double a = executor.popStack().getNumber();
11+
executor.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, a + b));
12+
}
13+
14+
// Subtraction
15+
else if (command.equals("sub")) {
16+
double b = executor.popStack().getNumber();
17+
double a = executor.popStack().getNumber();
18+
executor.stack.add(new Stack.Type(Stack.Type.TypeEnum.NUMBER, a - b));
19+
}
20+
21+
// Pop in stack
22+
else if (command.equals("pop")) {
23+
executor.popStack();
24+
}
25+
26+
// Exit Stack
27+
else if (command.equals("exit")) {
28+
double code = executor.popStack().getNumber();
29+
executor.exitmode = true;
30+
executor.exitcode = (int) code;
31+
if (executor.interpreterMode == false) {
32+
System.exit(executor.exitcode);
33+
}
34+
}
35+
36+
// TODO Other commands...
37+
38+
// When other string inputted
39+
else {
40+
executor.stack.add(new Stack.Type(Stack.Type.TypeEnum.STRING, command));
41+
}
42+
}
43+
}

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

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public void interpreter(String[] args) {
2525
Executor executor = new Stack.Executor(Mode.Debug, true);
2626
// REPL Execution
2727
while (!executor.exitmode) {
28-
String code = "";
28+
StringBuilder code = new StringBuilder();
2929
while (!executor.exitmode) {
3030
String enter = input(br, "> ");
31-
code += enter + "\n";
32-
if (enter.equals("")) {
31+
code.append(enter).append("\n");
32+
if (enter.isEmpty()) {
3333
break;
3434
}
3535
}
3636

37-
executor.evaluateProgram(code);
37+
executor.evaluateProgram(code.toString());
3838
}
3939

4040
try {
@@ -206,7 +206,7 @@ public static enum Mode {
206206
}
207207

208208
public static class Executor {
209-
private List<Type> stack; // Data stack
209+
protected List<Type> stack; // Data stack
210210
private Map<String, Type> memory; // Variable's memory
211211
private Mode mode; // Execution mode
212212
public boolean exitmode = false;
@@ -390,43 +390,7 @@ public void evaluateProgram(String code) { // evaluate string as program
390390

391391
// execute string as commands
392392
public void executeCommand(String command) {
393-
// Commands of calculation
394-
395-
// addition
396-
if (command.equals("add")) {
397-
double b = this.popStack().getNumber();
398-
double a = this.popStack().getNumber();
399-
this.stack.add(new Type(Type.TypeEnum.NUMBER, a + b));
400-
}
401-
402-
// Subtraction
403-
else if (command.equals("sub")) {
404-
double b = this.popStack().getNumber();
405-
double a = this.popStack().getNumber();
406-
this.stack.add(new Type(Type.TypeEnum.NUMBER, a - b));
407-
}
408-
409-
// Pop in stack
410-
else if (command.equals("pop")) {
411-
this.popStack();
412-
}
413-
414-
// Exit Stack
415-
else if (command.equals("exit")) {
416-
double code = this.popStack().getNumber();
417-
this.exitmode = true;
418-
this.exitcode = (int) code;
419-
if (this.interpreterMode == false) {
420-
System.exit(exitcode);
421-
}
422-
}
423-
424-
// TODO Other commands...
425-
426-
// When other string inputted
427-
else {
428-
this.stack.add(new Type(Type.TypeEnum.STRING, command));
429-
}
393+
new Functions(this, command);
430394
}
431395

432396
public Type popStack() { // Pop stack's top value

0 commit comments

Comments
 (0)