diff --git a/expressions.txt b/expressions.txt index 956089d3b8d3ba88c9d22f8958713a1d48b6d3a6..74f9b719cba435602fbafc8e493b634bc5f8ac4f 100644 --- a/expressions.txt +++ b/expressions.txt @@ -1,2 +1,2 @@ 1+(x^2)-(3*x) -2*x+0.5*(x^2)-1 \ No newline at end of file +2*x+0.5*(x^2+4)-1 \ No newline at end of file diff --git a/src/Exceptions/LexerException.java b/src/Exceptions/LexerException.java new file mode 100644 index 0000000000000000000000000000000000000000..28ac4224f58ebb0ee3bdf2b5f2c2f956ed85033a --- /dev/null +++ b/src/Exceptions/LexerException.java @@ -0,0 +1,7 @@ +package Exceptions; + +public class LexerException extends Exception { + public LexerException(String message) { + super(message); + } +} diff --git a/src/ParserException.java b/src/Exceptions/ParserException.java similarity index 85% rename from src/ParserException.java rename to src/Exceptions/ParserException.java index ef36000d208d794a262a186f4f844606f35ab1d0..d3e519539822d69c7e12cfa98772a1b87c0d874c 100644 --- a/src/ParserException.java +++ b/src/Exceptions/ParserException.java @@ -1,3 +1,5 @@ +package Exceptions; + public class ParserException extends Exception{ public ParserException(String message) { super(message); diff --git a/src/ASTPrinter.java b/src/main/ASTPrinter.java similarity index 98% rename from src/ASTPrinter.java rename to src/main/ASTPrinter.java index 8983653899e5b1c2c6fe66545fb25a77a9a60614..10669f24f21640f8f627bac93c2015ec2e3a16a8 100644 --- a/src/ASTPrinter.java +++ b/src/main/ASTPrinter.java @@ -1,3 +1,5 @@ +package main; + public class ASTPrinter implements Visitor<String>{ @Override public String visit(final Parser.BinaryOperation binaryOP) { diff --git a/src/Application.java b/src/main/Application.java similarity index 51% rename from src/Application.java rename to src/main/Application.java index b25cd9ffc6a1585f394ddb33f6508b39db69ceef..f65d712b5d0113f5e12565164d6cb5b0604925c1 100644 --- a/src/Application.java +++ b/src/main/Application.java @@ -1,4 +1,10 @@ +package main; + +import Exceptions.LexerException; +import Exceptions.ParserException; + import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.LinkedList; @@ -6,34 +12,47 @@ import java.util.List; public class Application { public static void main(String[] args) { + //A list that will contain a String for each line of the "expressions.txt" file List<String> expressions = new LinkedList<>(); - //Reads the file "expressions.txt" and adds its values to a list + //Reads the file "expressions.txt" and adds its values to the list expressions BufferedReader reader; - String dataPath = "expressions.txt"; + String datapath = "expressions.txt"; try { System.out.println("Reading the file...\n"); - reader = new BufferedReader(new FileReader(dataPath)); - String line = reader.readLine(); - while(line != null) { - expressions.add(line); - line = reader.readLine(); + reader = new BufferedReader(new FileReader(datapath)); + String currentLine = reader.readLine(); + while(currentLine !=null) { + expressions.add(currentLine); + currentLine = reader.readLine(); } reader.close(); } - catch (IOException e ) { + catch(IOException e) { e.printStackTrace(); } + //Iterate through every String + for (String expression: expressions) { + System.out.println("Expression: " + expression); + //Usage of the lexer, parser & evaluator + try { + //Create a list of tokens with the lexer + List<Lexer.Token> tokens = Lexer.lex(expression); + System.out.println("Lexer created tokens:"); + //prints out each token to the console + for(Lexer.Token token: tokens) { + System.out.println(token.toString()); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + /* //Iterates through every String and parses them into multiple tokens for (String value: expressions) { - System.out.println(value); - //creates a list of tokens out of the String - List<Lexer.Token> tokens = Lexer.lex(value); - - //prints each token out (with .toString()) - for(Lexer.Token singleToken: tokens) { - System.out.println(singleToken.toString()); - } Parser parser = new Parser(); try { Parser.Expression exp = parser.parse(tokens); @@ -48,6 +67,6 @@ public class Application { System.out.println(e.getMessage()); } System.out.println(); - } + }*/ } } diff --git a/src/Evaluator.java b/src/main/Evaluator.java similarity index 97% rename from src/Evaluator.java rename to src/main/Evaluator.java index b9979c778266a973dd424cf79e7eea1769dfcd1b..7f51f3ca78b76b92717ad16310dc3b3bc0a560c4 100644 --- a/src/Evaluator.java +++ b/src/main/Evaluator.java @@ -1,3 +1,5 @@ +package main; + public class Evaluator implements Visitor<Double>{ @Override @@ -16,6 +18,7 @@ public class Evaluator implements Visitor<Double>{ result -= rOperand; break; case '*': + rOperand *= rOperand; result *= rOperand; break; case '/': diff --git a/src/Lexer.java b/src/main/Lexer.java similarity index 90% rename from src/Lexer.java rename to src/main/Lexer.java index c3806955e133a727f09a745550f50d27c1aaa1a7..d84a782eea24adfdc713830ed8c2208b991caa4a 100644 --- a/src/Lexer.java +++ b/src/main/Lexer.java @@ -1,3 +1,7 @@ +package main; + +import Exceptions.LexerException; + import java.util.LinkedList; import java.util.List; @@ -9,7 +13,7 @@ public class Lexer { } //tokens with a TokenType that represents their type and a String with the actual data - //has for both values a getter method and overrides toString with it's fitting values + //has for both values a getter method and overrides toString with its fitting values public static class Token { protected TokenType type; protected String data; @@ -33,15 +37,19 @@ public class Lexer { } //creates a list of tokens from a given String - public static List<Token> lex(String input) { + public static List<Token> lex(String input) throws Exception{ + if(input.isEmpty()) { + throw new LexerException("SyntaxError: lexer received empty String"); + } + //the list that will be returned at the end List<Token> result = new LinkedList<Token>(); //for-loop that iterates through each character of the String for(int index=0; index < input.length();) { char current = input.charAt(index); - //checks if a character is a digit, then creates a token with one or multiple numbers - //with TokenType=NUMBER + //checks if a character is a digit, then creates a token with one or multiple + //numbers with TokenType=NUMBER if(Character.isDigit(current)) { int endIndex = index; if(endIndex<input.length()) { diff --git a/src/Parser.java b/src/main/Parser.java similarity index 98% rename from src/Parser.java rename to src/main/Parser.java index 527d6285b6fe88121e22c4938e0a8bde271f04c0..ca46fc9dd4cded7646645b36a3b038cddc5b9468 100644 --- a/src/Parser.java +++ b/src/main/Parser.java @@ -1,3 +1,7 @@ +package main; + +import Exceptions.ParserException; + import java.util.LinkedList; import java.util.List; @@ -21,6 +25,10 @@ public class Parser { public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); } + + public <T> T accept(Visitor<T> vistor, String operator) { + return vistor.visit(this); + } } public class Variable extends Expression { diff --git a/src/SwingFunctionPlotter.java b/src/main/SwingFunctionPlotter.java similarity index 98% rename from src/SwingFunctionPlotter.java rename to src/main/SwingFunctionPlotter.java index 110dc614322009d5b95b85ec38246c21fa8c4aba..d433aee2fe88e0274d6a01bbcb5e7ecea9a31d41 100644 --- a/src/SwingFunctionPlotter.java +++ b/src/main/SwingFunctionPlotter.java @@ -1,3 +1,5 @@ +package main; + import com.mindfusion.charting.FunctionSeries; import com.mindfusion.charting.swing.LineChart; diff --git a/src/Visitor.java b/src/main/Visitor.java similarity index 94% rename from src/Visitor.java rename to src/main/Visitor.java index 4e925c965f413d5d38a2b05dfbbbe37962cff874..88d87ae51a88b7e6f0edcfd8bf0213dbd26461e1 100644 --- a/src/Visitor.java +++ b/src/main/Visitor.java @@ -1,3 +1,5 @@ +package main; + public interface Visitor<T>{ public T visit(final Parser.BinaryOperation binOp); public T visit(final Parser.Variable variable); diff --git a/src/test/LexerTest.java b/src/test/LexerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..54356a6ec5573f4dc76a3cbd25d98d0a648e5ff9 --- /dev/null +++ b/src/test/LexerTest.java @@ -0,0 +1,59 @@ +package test; + +import org.junit.Test; +import main.*; + +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class LexerTest { + + @Test + void disassembleFirstStringWithLexer() { + String s = "3+x*(2^4)"; + List<Lexer.Token> tokens = new LinkedList<>(); + tokens.add(new Lexer.Token(Lexer.TokenType.NUMBER,"3")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"+")); + tokens.add(new Lexer.Token(Lexer.TokenType.VARIABLE,"x")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"*")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"(")); + tokens.add(new Lexer.Token(Lexer.TokenType.NUMBER,"2")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"^")); + tokens.add(new Lexer.Token(Lexer.TokenType.NUMBER,"4")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,")")); + try { + assertEquals(Lexer.lex(s),tokens); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + @Test + void disassembleSecondStringWithLexer() { + String s = "1 + (x^2 )- (3*x)"; + List<Lexer.Token> tokens = new LinkedList<>(); + tokens.add(new Lexer.Token(Lexer.TokenType.NUMBER,"1")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"+")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"(")); + tokens.add(new Lexer.Token(Lexer.TokenType.VARIABLE,"x")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"^")); + tokens.add(new Lexer.Token(Lexer.TokenType.NUMBER,"2")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,")")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"-")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"(")); + tokens.add(new Lexer.Token(Lexer.TokenType.NUMBER,"3")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,"*")); + tokens.add(new Lexer.Token(Lexer.TokenType.VARIABLE,"x")); + tokens.add(new Lexer.Token(Lexer.TokenType.SPECIAL,")")); + try { + assertEquals(Lexer.lex(s),tokens); + } + catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/test/ParserTest.java b/src/test/ParserTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0be7f64628e6179efc67d8d19221043802094627 --- /dev/null +++ b/src/test/ParserTest.java @@ -0,0 +1,4 @@ +package test; + +public class ParserTest { +}