diff --git a/Aufgabe4/Ast.java b/Aufgabe4/Ast.java index b2b34883c6e80cfa9ebb24bf8374b4dd0dde8400..672355e5269cb3edf902756f04df43533e8f2983 100644 --- a/Aufgabe4/Ast.java +++ b/Aufgabe4/Ast.java @@ -30,7 +30,12 @@ class AstValue extends Ast { AstNumber astNumber; AstDecimal astDecimal; - boolean isVariable; + AstVariable astVariable; +} + +class AstVariable extends Ast { + + String astVariable; } diff --git a/Aufgabe4/Evaluator.java b/Aufgabe4/Evaluator.java new file mode 100644 index 0000000000000000000000000000000000000000..dd17f1944ee5ed157bc793d16419f07e50068d66 --- /dev/null +++ b/Aufgabe4/Evaluator.java @@ -0,0 +1,85 @@ + + +public class Evaluator { + + private float counter; + + public void setCounter(float counter) { + this.counter = counter; + } + + + public float visit(AstExpression node) { + + if(node.astExpression != null) { + + return visit(node.astExpression); + + } else if(node.astBinaryOp != null) { + + return visit(node.astBinaryOp); + + } else { + + return visit(node.astValue); + + } + + } + + public float visit(AstBinaryOp node) { + + visit(node.astExpression1); + + switch(node.astOperator.astOperator) { + + case "+": + return visit(node.astExpression1) + visit(node.astExpression2); + case "-": + return visit(node.astExpression1) - visit(node.astExpression2); + case "*": + return visit(node.astExpression1) * visit(node.astExpression2); + case "/": + return visit(node.astExpression1) / visit(node.astExpression2); + case "^": + return (float) Math.pow(visit(node.astExpression1), visit(node.astExpression2)); + } + + return -111; + } + + // AstExpression = ( (5 + 5.5) + x ) + // a) visit( (5 + 5.5) ) = 5 + 5.5 + // b) + // c) + public float visit(AstValue node) { + + if(node.astVariable != null) { + + return visit(node.astVariable); + + } else if (node.astNumber != null){ + + return visit(node.astNumber); + + } else { + + return Float.parseFloat(visit(node.astDecimal.astDigitBeforeComma) + + "." + + visit(node.astDecimal.astDigitAfterComma)); + } + } + + public int visit(AstNumber node) { + + return node.astDigitWoz.astDigitWozContent; + + } + + public float visit(AstVariable node) { + + return counter; + + } + +} diff --git a/Aufgabe4/EvaluatorBackup.java b/Aufgabe4/EvaluatorBackup.java new file mode 100644 index 0000000000000000000000000000000000000000..ba22527d283094a4543788ae381ed62c188cceb2 --- /dev/null +++ b/Aufgabe4/EvaluatorBackup.java @@ -0,0 +1,69 @@ + + +public class EvaluatorBackup { + + + public void visit(AstExpression node) { + + if(node.astExpression != null) { + + visit(node.astExpression); + + } else if(node.astBinaryOp != null) { + + visit(node.astBinaryOp); + + } else { + + visit(node.astValue); + + } + + } + + public void visit(AstBinaryOp node) { + + visit(node.astExpression1); + System.out.print(node.astOperator.astOperator); + visit(node.astExpression2); + + } + + // AstExpression = ( (5 + 5.5) + x ) + // a) visit( (5 + 5.5) ) = 5 + 5.5 + // b) + // c) + public void visit(AstValue node) { + + if(node.astVariable != null) { + + visit(node.astVariable); + + + } else if (node.astNumber != null){ + + visit(node.astNumber); + + } else { + + visit(node.astDecimal.astDigitBeforeComma); + System.out.print("."); + visit(node.astDecimal.astDigitAfterComma); + } + + + } + + public void visit(AstNumber node) { + + System.out.print(node.astDigitWoz.astDigitWozContent); + + } + + public void visit(AstVariable node) { + + System.out.print(node.astVariable); + + } + +} diff --git a/Aufgabe4/Parser.java b/Aufgabe4/Parser.java index f97b6615140bef44e5018c2eb329b0122acd84bf..63d0511d43375f6988156982eaa07b120e049ae8 100644 --- a/Aufgabe4/Parser.java +++ b/Aufgabe4/Parser.java @@ -1,5 +1,3 @@ -import java.sql.Array; -import java.sql.SQLOutput; import java.util.ArrayList; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -39,7 +37,9 @@ public class Parser { } - if(t.getTokenString().equals("+") || t.getTokenString().equals("-") || t.getTokenString().equals("*") || t.getTokenString().equals("/")) { + if(t.getTokenString().equals("+") || t.getTokenString().equals("-") || + t.getTokenString().equals("*") || t.getTokenString().equals("/") || + t.getTokenString().equals("^")) { isValue = false; break; } @@ -97,6 +97,8 @@ public class Parser { } + //(5+2)+(1+2) + private AstBinaryOp parseBinaryOp (ArrayList<Token> tokenList) { @@ -116,7 +118,7 @@ public class Parser { } if( ((t.getTokenString().equals("+") || t.getTokenString().equals("-") || t.getTokenString().equals("*") - || t.getTokenString().equals("/")) && (numberOfBrackets % 2 == 0) ) && lastBracket.equals((")") )) { + || t.getTokenString().equals("/") || t.getTokenString().equals("^")) && (numberOfBrackets % 2 == 0) ) && lastBracket.equals((")") )) { positionOfFirstTopLevelOperator = i; break; @@ -166,12 +168,29 @@ public class Parser { } else { - toReturn.astNumber = parseNumber(valueTokens); + // Check if token is number or variable and parse accordingly + + if(valueTokens.get(0).getTokenType().equals(TokenType.var)) { + + toReturn.astVariable = parseVariable(valueTokens); + + } else { + toReturn.astNumber = parseNumber(valueTokens); + } + } return toReturn; } + private AstVariable parseVariable (ArrayList<Token> valueTokens) { + AstVariable toReturn = new AstVariable(); + + toReturn.astVariable = valueTokens.get(0).getTokenString(); + + return toReturn; + } + private AstDecimal parseDecimal (ArrayList<Token> decimalTokens) { AstDecimal toReturn = new AstDecimal(); diff --git a/Aufgabe4/SwingPaintDemo3.java b/Aufgabe4/SwingPaintDemo3.java new file mode 100644 index 0000000000000000000000000000000000000000..2fdf3f39ca246ad8e362aea1639eccdfe53f1fe3 --- /dev/null +++ b/Aufgabe4/SwingPaintDemo3.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +import javax.swing.SwingUtilities; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.BorderFactory; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.event.MouseEvent; +import java.awt.event.MouseAdapter; + +public class SwingPaintDemo3 { + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + createAndShowGUI(); + } + }); + } + + private static void createAndShowGUI() { + System.out.println("Created GUI on EDT? "+ + SwingUtilities.isEventDispatchThread()); + JFrame f = new JFrame("Swing Paint Demo"); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.add(new MyPanel()); + f.pack(); + f.setVisible(true); + } +} + +class MyPanel extends JPanel { + + private int squareX = 50; + private int squareY = 50; + private int squareW = 20; + private int squareH = 20; + + public MyPanel() { + + setBorder(BorderFactory.createLineBorder(Color.black)); + + addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + moveSquare(e.getX(),e.getY()); + } + }); + + addMouseMotionListener(new MouseAdapter() { + public void mouseDragged(MouseEvent e) { + moveSquare(e.getX(),e.getY()); + } + }); + + } + + private void moveSquare(int x, int y) { + int OFFSET = 1; + if ((squareX!=x) || (squareY!=y)) { + repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET); + squareX=x; + squareY=y; + repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET); + } + } + + + public Dimension getPreferredSize() { + return new Dimension(250,200); + } + + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawString("This is my custom Panel!",10,20); + g.setColor(Color.RED); + g.fillRect(squareX,squareY,squareW,squareH); + g.setColor(Color.BLACK); + g.drawRect(squareX,squareY,squareW,squareH); + } +} diff --git a/Aufgabe4/expressions.txt b/Aufgabe4/expressions.txt new file mode 100644 index 0000000000000000000000000000000000000000..4853f59b3bfe953cd141589fdf7ca4309b90025f --- /dev/null +++ b/Aufgabe4/expressions.txt @@ -0,0 +1,7 @@ +3+3+x +(3^2)-5 +1+2+3+4+5 +(22*2)-5 +5+5+55 +1+x +x+2 \ No newline at end of file diff --git a/Aufgabe4/main.java b/Aufgabe4/main.java index 057cd27350f6b39ab112673ce4ecce97c9b891bb..4aac734df56a343d1102a4ef2f683b561906ac57 100644 --- a/Aufgabe4/main.java +++ b/Aufgabe4/main.java @@ -1,69 +1,76 @@ +import java.io.*; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Scanner; + public class main { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { // values above integer space will produce errors - String ausdruck = "((5 + 1) + (3+4))"; + ArrayList<String> stringsFromFile = new ArrayList<>(); - Lexer lexer = new Lexer(); + String filePath = "./Aufgabe4/expressions.txt"; + Scanner scan = null; + try { - // [23x] + [0,5x^2 - 1] + scan = new Scanner(new File(filePath)); + while (scan.hasNext()) { - lexer.lex(ausdruck).forEach(token -> { - System.out.println(token.getTokenString()); - System.out.println(token.getTokenType()); - }); + stringsFromFile.add(scan.nextLine()); + } - Parser p = new Parser(); + scan.close(); + + } catch (FileNotFoundException | NullPointerException e) { + e.printStackTrace(); + } - AstExpression result = p.parse(lexer.lex(ausdruck)); - printAst(result); + Lexer lexer = new Lexer(); -/* - do { + ArrayList<ArrayList<Token>> tokenListList = new ArrayList<>(); - try { - System.out.println(result.astBinaryOp.astExpression1.astValue.astNumber.astDigitWoz.astDigitWozContent); - } catch (NullPointerException n) { - System.out.println(result.astBinaryOp.astExpression1.astValue.astDecimal.astDigitBeforeComma.astDigitWoz.astDigitWozContent - + "." - + result.astBinaryOp.astExpression1.astValue.astDecimal.astDigitAfterComma.astDigitWoz.astDigitWozContent); - } + for(String s : stringsFromFile) { + + tokenListList.add(lexer.lex(s)); - System.out.println(result.astBinaryOp.astOperator.astOperator); + } - if (result.astBinaryOp.astExpression2.astValue != null) { - try { - System.out.println(result.astBinaryOp.astExpression2.astValue.astNumber.astDigitWoz.astDigitWozContent); - } catch (NullPointerException n) { + Parser p = new Parser(); - System.out.println(result.astBinaryOp.astExpression2.astValue.astDecimal.astDigitBeforeComma.astDigitWoz.astDigitWozContent - + "." - + result.astBinaryOp.astExpression2.astValue.astDecimal.astDigitAfterComma.astDigitWoz.astDigitWozContent); - } + ArrayList<AstExpression> rootAstExpressions = new ArrayList<>(); - break; + for(ArrayList<Token> tokenList : tokenListList) { + rootAstExpressions.add(p.parse(tokenList)); + } - } else { - result = result.astBinaryOp.astExpression2; + Evaluator e = new Evaluator(); + + for(AstExpression astExpression : rootAstExpressions) { + for(int i = -20; i < 21; i++) { + e.setCounter(i); + System.out.println(e.visit(astExpression)); } + System.out.println("------------"); + + } + - } while (true); - */ + //printAst(result); } @@ -82,6 +89,10 @@ public class main { System.out.print(astExpression.astValue.astNumber.astDigitWoz.astDigitWozContent); + } else if (astExpression.astValue.astVariable != null) { + + System.out.print(astExpression.astValue.astVariable.astVariable); + } else { System.out.println(astExpression.astValue.astDecimal.astDigitBeforeComma.astDigitWoz.astDigitWozContent + "." diff --git a/Aufgabe4/test.java b/Aufgabe4/test.java index cdf56ab4b0d79407d539437abdb973697b4dc17f..8de00b8e9871a9d8ffdf2b0eaf85c83daf27d0c7 100644 --- a/Aufgabe4/test.java +++ b/Aufgabe4/test.java @@ -83,7 +83,7 @@ public class test { } - */ + public static void main(String[] args) { @@ -119,5 +119,36 @@ public class test { } + + + + 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)); + + } } diff --git a/out/production/inf3_git/main.class b/out/production/inf3_git/main.class index ef5c641e7481d83366e656c310a54d13a9807d7d..30c986cdc3065702569085f00fe7a7ea4446b4c2 100644 Binary files a/out/production/inf3_git/main.class and b/out/production/inf3_git/main.class differ