Skip to content

Commit 5c560b4

Browse files
feat: Do basic Shell
1 parent 29f82ac commit 5c560b4

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed
Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
11
package net.marcellperger.mathexpr.interactive;
22

3+
import net.marcellperger.mathexpr.MathSymbol;
4+
import net.marcellperger.mathexpr.parser.ExprParseException;
5+
import net.marcellperger.mathexpr.parser.Parser;
6+
import net.marcellperger.mathexpr.util.Input;
7+
import net.marcellperger.mathexpr.util.MathUtil;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.io.PrintStream;
312
import java.util.Arrays;
413

514
import static net.marcellperger.mathexpr.util.Input.input;
615

716
public class Shell {
17+
Input in;
18+
PrintStream out;
19+
20+
public Shell() {
21+
in = new Input();
22+
out = System.out;
23+
}
24+
825
public static void main(String[] args) {
9-
System.out.println("args = " + Arrays.toString(args));
10-
System.out.print("Enter something: ");
11-
System.out.println("You entered " + input());
26+
new Shell().run();
27+
}
28+
29+
// TODO run() until exit better - parse exit command/Ctrl+C
30+
// TODO fails badly with unchecked exception on parsing `12 + (`
31+
32+
public void run() {
33+
//noinspection InfiniteLoopStatement
34+
while (true) getAndRunCommand();
35+
}
36+
37+
public void getAndRunCommand() {
38+
runCommand(in.getInput(">? "));
39+
}
40+
public void runCommand(String cmd) {
41+
@Nullable MathSymbol sym = parseCmdOrPrintError(cmd);
42+
if(sym == null) return;
43+
double value = sym.calculateValue();
44+
out.println(MathUtil.roundToSigFigs(value, 10));
45+
}
46+
public @Nullable MathSymbol parseCmdOrPrintError(String cmd) {
47+
// No special commands so pass straight to parser
48+
Parser p = new Parser(cmd);
49+
try {
50+
return p.parse();
51+
} catch (ExprParseException exc) {
52+
out.println(exc);
53+
return null;
54+
}
1255
}
1356
}

src/main/java/net/marcellperger/mathexpr/util/Input.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public String getInput() {
4646
return sc.nextLine();
4747
}
4848
public String getInput(String prompt) {
49-
Objects.requireNonNull(ps, "An OutputStream is required to use Input.getInput(prompt)").println(prompt);
49+
Objects.requireNonNull(ps, "An OutputStream is required to use Input.getInput(prompt)").print(prompt);
5050
return sc.nextLine();
5151
}
5252

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.marcellperger.mathexpr.util;
2+
3+
import org.jetbrains.annotations.Range;
4+
5+
public class MathUtil {
6+
protected MathUtil() {}
7+
8+
public static double roundToSigFigs(double value, @Range(from=1, to=Integer.MAX_VALUE) int sigFigs) {
9+
int mostSignificantDigit = (int)Math.floor(Math.log10(value));
10+
int roundToDigit = mostSignificantDigit - sigFigs + 1;
11+
return roundToDP(value, roundToDigit);
12+
}
13+
14+
public static double roundToNearest(double value, double nearest) {
15+
return Math.round(value / nearest) * nearest;
16+
}
17+
public static double roundToDP(double value, int decimalPlaces) {
18+
return roundToNearest(value, Math.pow(10, decimalPlaces));
19+
}
20+
}

0 commit comments

Comments
 (0)