From 403c6a951e890d3e1914779ee35576859a38d33e Mon Sep 17 00:00:00 2001 From: Maƫl Gassmann Date: Fri, 11 Jun 2021 11:17:45 +0200 Subject: [+] implemented the 'shell' UI, [~] Small corrections had to be made in the parser --- .../src/main/java/ch/bfh/CalculatorLexer.java | 2 +- calculator-java/src/main/java/ch/bfh/Main.java | 28 ++++++++++++---------- .../main/java/ch/bfh/parser/ExpressionParser.java | 8 +++++-- .../src/main/java/ch/bfh/parser/Parser.java | 4 +--- 4 files changed, 24 insertions(+), 18 deletions(-) (limited to 'calculator-java') diff --git a/calculator-java/src/main/java/ch/bfh/CalculatorLexer.java b/calculator-java/src/main/java/ch/bfh/CalculatorLexer.java index ba10944..48d009e 100644 --- a/calculator-java/src/main/java/ch/bfh/CalculatorLexer.java +++ b/calculator-java/src/main/java/ch/bfh/CalculatorLexer.java @@ -82,7 +82,7 @@ public class CalculatorLexer { } else if (Character.isLetter(src.charAt(idx))) { - tok.value = 0; + tok.value = 0; tok.type = Token.ID; tok.str = identifier(); if (tok.str.compareTo("let")==0) tok.type = Token.LET; diff --git a/calculator-java/src/main/java/ch/bfh/Main.java b/calculator-java/src/main/java/ch/bfh/Main.java index b0dedf3..302f5ed 100644 --- a/calculator-java/src/main/java/ch/bfh/Main.java +++ b/calculator-java/src/main/java/ch/bfh/Main.java @@ -3,21 +3,25 @@ package ch.bfh; import ch.bfh.parser.ExpressionParser; import java.util.Arrays; +import java.util.Scanner; public class Main { public static void main(String[] args){ - CalculatorLexer cl = new CalculatorLexer(); - - //TODO - command line reader instead of arg - - // Combines the arguments & init the lexer - cl.initLexer( - Arrays.stream(args).reduce("", (expression, arg) -> expression + arg) - ); - - ExpressionParser ep = new ExpressionParser(cl); - - System.out.println(ep.getValue()); + Scanner scanner = new Scanner(System.in); + + while (true) { + CalculatorLexer cl = new CalculatorLexer(); + System.out.print("Type your expression: "); + String expression = scanner.nextLine(); + + try { + cl.initLexer(expression); + ExpressionParser ep = new ExpressionParser(cl); + System.out.println(ep.getValue()); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } } } diff --git a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java index 229e700..f96353e 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java @@ -49,6 +49,7 @@ public class ExpressionParser extends Parser { case Token.SUB: if (l != null && op == null) { // if the '-' sign is between two term (op) op = token; + this.ops.add(op); break; } else if (l != null && r != null) { // if we already found a 'l op r' we roll l = r; @@ -69,7 +70,7 @@ public class ExpressionParser extends Parser { } continue loop; case Token.PAR: - if (lastToken.type == Token.PAL) { // if equal to '(' it means it was created by a Factor + if (lastToken != null && lastToken.type == Token.PAL) { // if equal to '(' it means it was created by a Factor lastToken = token; break loop; } else @@ -260,7 +261,10 @@ public class ExpressionParser extends Parser { @Override public double getValue(){ if (expression != null) - return expression.getValue(); + if (isNegative) + return expression.getValue()*(-1); + else + return expression.getValue(); if (isNegative) return value*(-1); return value; diff --git a/calculator-java/src/main/java/ch/bfh/parser/Parser.java b/calculator-java/src/main/java/ch/bfh/parser/Parser.java index 755313a..9e048a6 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/Parser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/Parser.java @@ -9,8 +9,6 @@ abstract class Parser{ protected double value; protected abstract void parse(); - public double getValue(){ - return value; - } + public abstract double getValue(); } -- cgit v1.2.3