From c0e5e4062d142e59e2ec5418905240030ab07a52 Mon Sep 17 00:00:00 2001
From: qwertzniki6 <104077966+bretzNiklas@users.noreply.github.com>
Date: Mon, 16 Jan 2023 18:52:54 +0100
Subject: [PATCH] Added comments

---
 Aufgabe4/src/Evaluator.java    |  34 +++++---
 Aufgabe4/src/Lexer.java        |  10 +++
 Aufgabe4/src/Main.java         |  46 ++--------
 Aufgabe4/src/Parser.java       |  31 +++++--
 Aufgabe4/src/Plotter.java      |  11 ++-
 Aufgabe4/src/ValuesToDraw.java |   1 +
 Aufgabe4/src/dbcTest.java      |  24 -----
 Aufgabe4/src/test.java         | 154 ---------------------------------
 8 files changed, 77 insertions(+), 234 deletions(-)
 delete mode 100644 Aufgabe4/src/dbcTest.java
 delete mode 100644 Aufgabe4/src/test.java

diff --git a/Aufgabe4/src/Evaluator.java b/Aufgabe4/src/Evaluator.java
index 5204369..adffc62 100644
--- a/Aufgabe4/src/Evaluator.java
+++ b/Aufgabe4/src/Evaluator.java
@@ -1,3 +1,5 @@
+import javax.management.BadAttributeValueExpException;
+
 import static org.valid4j.Assertive.*;
 
 
@@ -5,34 +7,44 @@ public class Evaluator {
 
     private float counter;
 
+    // sets current value that variables are replaced with
     public void setCounter(float counter) {
         this.counter = counter;
     }
 
 
+
     public float visit(AstExpression node) {
 
+        // if expression has not null expression as attribute: visit this expression
         if(node.astExpression != null) {
 
             return visit(node.astExpression);
 
-        } else if(node.astBinaryOp != null) {
+        }
+        // if expression has not null binary op as attribute: visit binary op
+        else if(node.astBinaryOp != null) {
 
             return visit(node.astBinaryOp);
 
-        } else {
-
-            return visit(node.astValue);
+        }
 
+        // visit value
+        else {
+            try {
+                return visit(node.astValue);
+            } catch (NumberFormatException e) {
+                e.printStackTrace();
+                throw e;
+            }
         }
 
+
     }
 
     public float visit(AstBinaryOp node) {
 
-
-        //visit(node.astExpression1);
-
+        // visits both expressions, does math according to op operator
         switch(node.astOperator.astOperatorContent) {
 
             case "+":
@@ -51,10 +63,8 @@ public class Evaluator {
         return -111;
     }
 
-    // AstExpression = ( (5 + 5.5) + x )
-    // a) visit( (5 + 5.5) ) = 5 + 5.5
-    // b)
-    // c)
+
+    // returns value, parses depending on if variable, number or decimal
     public float visit(AstValue node) {
 
         if(node.astVariable != null) {
@@ -79,6 +89,8 @@ public class Evaluator {
 
     }
 
+
+    // returns current counter as variable value
     public float visit(AstVariable node) {
 
         return counter;
diff --git a/Aufgabe4/src/Lexer.java b/Aufgabe4/src/Lexer.java
index 69f1e94..b088057 100644
--- a/Aufgabe4/src/Lexer.java
+++ b/Aufgabe4/src/Lexer.java
@@ -14,6 +14,7 @@ public class Lexer {
         require(s != null);
         require(s.length() > 0);
 
+
         ArrayList<String> separatedChars = separateChars(s);
 
         ArrayList<String> tokensAsStrings = buildSubstringsFromSeparatedChars(separatedChars);
@@ -24,6 +25,10 @@ public class Lexer {
         return tokenList;
     }
 
+
+
+    // separates char with "|" symbol
+    // if char is a representation of an integer: doesn't add separator symbol
     private ArrayList<String> separateChars(String stringToTokenize) {
 
         ArrayList<String> separated = new ArrayList<>();
@@ -50,6 +55,9 @@ public class Lexer {
         return separated;
     }
 
+
+    // takes separated chars and builds an arrayList with substrings
+    // values are put into the same substring if both represent integers and are directly after each other
     private ArrayList<String> buildSubstringsFromSeparatedChars(ArrayList<String> separated) {
 
         ArrayList<String> tokensAsStrings = new ArrayList<>();
@@ -74,6 +82,8 @@ public class Lexer {
 
     }
 
+    // substrings get parsed into tokens, if substring is an integer: create number token
+    // if substring is a character: create var token, otherwise: create special token
     private ArrayList<Token> constructTokenList(ArrayList<String> tokensAsStrings) {
 
         ArrayList<Token> tokens = new ArrayList<>();
diff --git a/Aufgabe4/src/Main.java b/Aufgabe4/src/Main.java
index 969c5c8..fe06dd0 100644
--- a/Aufgabe4/src/Main.java
+++ b/Aufgabe4/src/Main.java
@@ -6,9 +6,7 @@ public class Main {
 
     public static void main(String[] args) throws Exception {
 
-
-
-        // values above integer space will produce errors
+        // Get expressions from file and store them in a string list
 
         ArrayList<String> stringsFromFile = new ArrayList<>();
 
@@ -32,6 +30,7 @@ public class Main {
         }
 
 
+        // Lex the strings in the string list
 
         Lexer lexer = new Lexer();
 
@@ -44,6 +43,8 @@ public class Main {
 
         }
 
+        // Parse the tokens in the token list
+
 
         Parser p = new Parser();
 
@@ -60,6 +61,8 @@ public class Main {
 
         }
 
+        // evaluate the ast and get the values f(x) from -20 to 20 in steps of one
+        // these get written into an integer array
 
         Evaluator e = new Evaluator();
 
@@ -86,44 +89,13 @@ public class Main {
 
         }
 
-        ValuesToDraw valuesToDraw = new ValuesToDraw(arraysOfXValues);
-
-        Plotter.plot(valuesToDraw);
-
-
-    }
-
-    public static void printAst(AstExpression astExpression) {
-
-        if(astExpression.astExpression != null) {
-            System.out.print("(");
-            printAst(astExpression.astExpression);
-            System.out.print( ")");
-            return;
-        }
-
-        if(astExpression.astValue != null) {
-
-            if(astExpression.astValue.astNumber != null) {
-
-                System.out.print(astExpression.astValue.astNumber.astDigitWoz.astDigitWozContent);
+        // creates ValuesToDraw object to transfer data to plotter
 
-            } else if (astExpression.astValue.astVariable != null) {
+        ValuesToDraw valuesToDraw = new ValuesToDraw(arraysOfXValues);
 
-                System.out.print(astExpression.astValue.astVariable.astVariable);
 
-            } else {
-                System.out.println(astExpression.astValue.astDecimal.astDigitBeforeComma.astDigitWoz.astDigitWozContent
-                        + "."
-                        + astExpression.astValue.astDecimal.astDigitAfterComma.astDigitWoz.astDigitWozContent);
-            }
-            return;
-        }
+        Plotter.plot(valuesToDraw);
 
-        printAst(astExpression.astBinaryOp.astExpression1);
-        System.out.print(astExpression.astBinaryOp.astOperator.astOperatorContent);
-        printAst(astExpression.astBinaryOp.astExpression2);
 
     }
-
 }
diff --git a/Aufgabe4/src/Parser.java b/Aufgabe4/src/Parser.java
index 111598d..3387ee6 100644
--- a/Aufgabe4/src/Parser.java
+++ b/Aufgabe4/src/Parser.java
@@ -6,6 +6,7 @@ import static org.valid4j.Assertive.*;
 
 public class Parser {
 
+    // parses the initial list of tokens
     public AstExpression parse(ArrayList<Token> tokenList) throws Exception {
         require(tokenList.size() > 0);
 
@@ -17,6 +18,7 @@ public class Parser {
         return root;
     }
 
+
     private AstExpression parseAstExpression (ArrayList<Token> tokenList) throws Exception {
 
         AstExpression toReturn = new AstExpression();
@@ -127,14 +129,18 @@ public class Parser {
 
         boolean flagAdditionSubtraction = false, flagMultiplicationDivision = false, flagPower = false;
 
+
+        // this for loop determines the first operator that is not in any brackets
         for(Token t : tokenList) {
 
 
+            // keeps track of number of brackets and saves current bracket to lastBracket
             if(t.getTokenString().equals("(") || t.getTokenString().equals(")")) {
                 numberOfBrackets++;
                 lastBracket = t.tokenString;
             }
 
+            // if last bracket is ")" and numberOfBrackets is divisible by 2 (meaning we are on the top level): activate AdditionSubtraction flag and save the operator
             if( ((t.getTokenString().equals("+") || t.getTokenString().equals("-") && (numberOfBrackets % 2 == 0) ) && lastBracket.equals((")"))) ) {
 
                 if(!flagAdditionSubtraction) {
@@ -143,6 +149,8 @@ public class Parser {
                 }
 
             }
+
+            // if last bracket is ")" and numberOfBrackets is divisible by 2: activate Multiplication flag and save the operator
             if( ((t.getTokenString().equals("*") || t.getTokenString().equals("/")) && (numberOfBrackets % 2 == 0) ) && lastBracket.equals((")") )) {
 
                 if(!flagMultiplicationDivision) {
@@ -151,22 +159,25 @@ public class Parser {
                 }
 
             }
+
+            // if last bracket is ")" and numberOfBrackets is divisible by 2: activate power flag and save the operator
             if( t.getTokenString().equals("^") && (numberOfBrackets % 2 == 0) && lastBracket.equals((")") )) {
 
                 if(!flagPower) {
                     flagPower = true;
                     positionOfFirstTopLevelPower = i;
                 }
-
             }
 
-
             i++;
-
         }
 
+        // if there is an addition operator on the top level: split expression at this operator
+        // if no additionOperator and there is a multiplication operator: split Exp at multp operator
+        // else: split at power operator
         if(flagAdditionSubtraction) {
             positionOfRelevantOperator = positionOfFirstTopLevelAdditionOrSubtraction;
+
         } else if (flagMultiplicationDivision){
             positionOfRelevantOperator = positionOfFirstTopLevelMultiplicationOrDivision;
         } else if (flagPower) {
@@ -174,8 +185,8 @@ public class Parser {
         }
 
 
-        // Make two Token lists before and after the expression, parse each as expression
 
+        // Make two Token lists before and after the expression, parse each as expression
         toReturn.astExpression1 = parseAstExpression(new ArrayList<>(tokenList.subList(0, positionOfRelevantOperator)));
         toReturn.astOperator = parseOperator(tokenList.get(positionOfRelevantOperator));
         toReturn.astExpression2 = parseAstExpression(new ArrayList<>(tokenList.subList(positionOfRelevantOperator + 1, tokenList.size())));
@@ -198,6 +209,7 @@ public class Parser {
 
         boolean containsSpecialToken = false;
 
+        // check if value contains special token
         for(Token t : valueTokens) {
 
             if(t.getTokenType().equals(TokenType.special)) {
@@ -229,6 +241,7 @@ public class Parser {
         return toReturn;
     }
 
+
     private AstVariable parseVariable (ArrayList<Token> valueTokens) {
         AstVariable toReturn = new AstVariable();
 
@@ -244,6 +257,7 @@ public class Parser {
         int i = 0;
         int positionOfDecimalPoint = -1;
 
+        // determines position of dot
         for(Token t : decimalTokens) {
 
             if(t.getTokenType().equals(TokenType.special)) {
@@ -255,10 +269,10 @@ public class Parser {
 
         }
 
-        // parse digits before comma as list and save it into toReturn.before
-
 
+        // parse digits before comma as list and save it into toReturn.before
 
+        // if decimal point is in second position (e.g. 1.276) make a new List with one token (.subList operation does not support subList with same index twice
         if (positionOfDecimalPoint == 1) {
 
             ArrayList<Token> temp = new ArrayList<>();
@@ -267,7 +281,6 @@ public class Parser {
 
         } else {
 
-
             try {
 
                 toReturn.astDigitBeforeComma = parseNumber(new ArrayList<>(decimalTokens.subList(0, positionOfDecimalPoint - 1)));
@@ -279,6 +292,7 @@ public class Parser {
         }
 
 
+        // same as above but check if for 3247.3
         if (positionOfDecimalPoint +  1 - decimalTokens.size() == 0) {
 
             ArrayList<Token> temp = new ArrayList<>();
@@ -309,6 +323,9 @@ public class Parser {
             return toReturn;
         }
 
+
+
+        // parse rest as regular digit
         numberTokens.stream()
                 .skip(1)
                 .forEach(token -> {
diff --git a/Aufgabe4/src/Plotter.java b/Aufgabe4/src/Plotter.java
index 5036fd7..0b334a1 100644
--- a/Aufgabe4/src/Plotter.java
+++ b/Aufgabe4/src/Plotter.java
@@ -55,6 +55,7 @@ class PlotterPanel extends JPanel {
         this.valuesToDraw = valuesToDraw;
 
 
+
         addMouseListener(new MouseAdapter() {
             public void mousePressed(MouseEvent e) {
 
@@ -65,8 +66,12 @@ class PlotterPanel extends JPanel {
 
                 int i = 0;
 
+                // gets values to draw from valuesToDraw object
+
                 int[] xCords = valuesToDraw.getXCords();
 
+                // offsets values to print in screen middle
+
                 for(int cord : xCords) {
                     yCordsWithOffset[i] = (ZERO_POINT_Y + (cord * -1 * 10));
                     i++;
@@ -77,18 +82,21 @@ class PlotterPanel extends JPanel {
 
                 int[] yCords = valuesToDraw.getYCords();
 
+                // offsets values to print in screen middle
+
                 for(int cord : yCords) {
                     xCordsWithOffset[j] = ZERO_POINT_X + (cord * 10);
                     j++;
                 }
 
 
+                // writes values into classes attribute
 
                 PlotterPanel.this.yCords = yCordsWithOffset;
                 PlotterPanel.this.xCords = xCordsWithOffset;
 
 
-
+                // update to show updated attributes
 
                 updateUI();
             }
@@ -134,6 +142,7 @@ class PlotterPanel extends JPanel {
     private void drawLine(Graphics g){
 
 
+        // gets classes attributes and paints them
 
         Graphics2D g2d = (Graphics2D) g;
         g2d.setColor(Color.RED);
diff --git a/Aufgabe4/src/ValuesToDraw.java b/Aufgabe4/src/ValuesToDraw.java
index 4b8b062..143f205 100644
--- a/Aufgabe4/src/ValuesToDraw.java
+++ b/Aufgabe4/src/ValuesToDraw.java
@@ -1,5 +1,6 @@
 import java.util.ArrayList;
 
+// class is used to transfer function values from main method to plotter
 public class ValuesToDraw {
 
     private ArrayList<int[]> xCordArrays;
diff --git a/Aufgabe4/src/dbcTest.java b/Aufgabe4/src/dbcTest.java
deleted file mode 100644
index f9b1cf2..0000000
--- a/Aufgabe4/src/dbcTest.java
+++ /dev/null
@@ -1,24 +0,0 @@
-
-
-import static org.valid4j.Assertive.*;
-
-public class dbcTest {
-
-    public static void main(String[] args) {
-
-        print(null);
-
-    }
-
-    public static void print(String s) {
-
-        System.out.println(s);
-
-        int i = 5;
-
-        ensure(i > 6);
-
-
-    }
-
-}
diff --git a/Aufgabe4/src/test.java b/Aufgabe4/src/test.java
deleted file mode 100644
index 8de00b8..0000000
--- a/Aufgabe4/src/test.java
+++ /dev/null
@@ -1,154 +0,0 @@
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-
-public class test {
-    /*
-    public static void main(String[] args) {
-
-        String ausdruck = "((3 * 5 * ( 4 +  3)) + ((5  + 4 ( 4*4 )) + 4))";
-
-        Lexer lexer = new Lexer();
-
-
-        // [23x] + [0,5x^2 - 1]
-
-
-
-
-
-        Parser p = new Parser();
-
-        ArrayList<Token> tokenList = lexer.lex(ausdruck);
-
-
-        int positionOfFirstOperator = 0;
-        int numberOfBrackets = 0;
-        int i = 0;
-        String lastBracket = ")";
-
-        for(Token t : tokenList) {
-
-            if(t.getTokenString().equals("(") || t.getTokenString().equals(")")) {
-                numberOfBrackets++;
-                lastBracket = t.tokenString;
-            }
-
-            if( ((t.getTokenString().equals("+") || t.getTokenString().equals("-") || t.getTokenString().equals("*")
-                    || t.getTokenString().equals("/")) && (numberOfBrackets % 2 == 0) ) && lastBracket.equals((")") )) {
-
-                positionOfFirstOperator = i;
-                break;
-
-            }
-            i++;
-
-        }
-
-        int j = 0;
-
-        System.out.println(positionOfFirstOperator);
-
-        for(Token t : tokenList) {
-            System.out.print(t.getTokenString() + " index=");
-            System.out.println(j);
-            j++;
-        }
-
-
-
-        List<Token> davor;
-
-        List<Token> danach;
-
-        davor = tokenList.subList(0, positionOfFirstOperator);
-        danach = tokenList.subList(positionOfFirstOperator + 1 , tokenList.size());
-
-
-        for(Token t : davor) {
-            System.out.println(t.getTokenString());
-        }
-
-        System.out.println("---------------------------------");
-
-        for(Token t : danach) {
-            System.out.println(t.getTokenString());
-        }
-
-
-
-
-
-    }
-
-
-
-    public static void main(String[] args) {
-
-        String ausdruck = "((1+2) + (3+5))";
-        String ausdruck2 = "(1+2) ()+ (3+5)";
-
-        Lexer lexer = new Lexer();
-
-        ArrayList<Token> test = lexer.lex(ausdruck);
-        ArrayList<Token> test2 = lexer.lex(ausdruck2);
-
-        System.out.println(testmethode(test));
-        System.out.println(testmethode(test2));
-
-
-
-    }
-    public static boolean testmethode(ArrayList<Token> arrList) {
-
-        boolean bracketsAroundEverything = false;
-
-        for(int i = arrList.size() - 2; i > 0; i--) {
-            if(arrList.get(i).tokenString.equals(")") || arrList.get(i).tokenString.equals("(")) {
-                if (arrList.get(i).tokenString.equals(")")) {
-                    return true;
-                }
-                return false;
-            }
-        }
-
-        System.out.println("geht hier rein");
-        return false;
-
-
-    }
-
-
-
-    public static void main(String[] args) {
-
-        //3 + 2 + i
-
-
-        ArrayList<Object> test = new ArrayList<>(3, 2, )
-
-
-
-        for(int i = 0; i < 10; i++) {
-
-            zwischenwert = 5 * 4 + i;
-
-        }
-
-    }
-
-     */
-
-    public static void main(String[] args) {
-
-        float eins = 1;
-        float zwei = 2;
-
-
-        System.out.println(Float.parseFloat(eins + "." + zwei));
-
-    }
-}
-
-- 
GitLab