Skip to content
Snippets Groups Projects
Commit 5d4fe314 authored by Dominic Daniel Krämer's avatar Dominic Daniel Krämer
Browse files

changed a few variable names

parent 6a72028d
Branches
No related tags found
1 merge request!15Dominicsbranch
......@@ -3,8 +3,8 @@ package main;
/**
* <h1>ASTPrinter</h1>
* The ASTPrinter class creates a String out of an AST(represented through an Expression object)
* It prints it in a form the SwingFunctionPlotter class can work with it, it isn't primarily designed for
* returning a String that is perfect for human readability (e.g. Pow(x,3) would be printed instead of x^3)
* It prints it in a form the Evaluator & SwingFunctionPlotter class can work with it, it isn't primarily designed
* for returning a String that is perfect for human readability (e.g. Pow(x,3) would be printed instead of x^3)
*
* @version 1.0
* @since 09.01.2023
......
package main;
import java.util.LinkedList;
import java.util.List;
/**
* <h1>Evaluator</h1>
*
* The Evaluator class calculates the value of an expression with a given value for its
* variables. With the visitor pattern it visits each node of the AST(Abstract Syntax Tree) and returns its values
* or performs a recursive call it its a BinaryOperation
*
* @version 1.0
* @since 09.01.2023
*/
public class Evaluator implements Visitor<Double>{
//This value will be used for all variables
private int x;
/**
*
* @param binaryOP
* @return
*/
@Override
public Double visit(final Parser.BinaryOperation binaryOP) {
int range = 20;
List<Double> results = new LinkedList<>();
for(int index=-20; index<=range; index++) {
}
double result = binaryOP.leftExpression.accept(this);
double rOperand;
switch (binaryOP.operator.charAt(0)) {
case '+':
rOperand = binaryOP.rightExpression.accept(this);
result += rOperand;
break;
case '-':
rOperand = binaryOP.rightExpression.accept(this);
result -= rOperand;
break;
case '*':
if(binaryOP.rightExpression instanceof Parser.BinaryOperation) {;
if(((Parser.BinaryOperation) binaryOP.rightExpression).capsuled) {
rOperand = binaryOP.rightExpression.accept(this);
result *= rOperand;
}
else {
}
}
else {
rOperand = binaryOP.rightExpression.accept(this);
result *= rOperand;
}
break;
case '/':
if (binaryOP.rightExpression instanceof Parser.BinaryOperation) {
if(binaryOP.capsuled) {
rOperand = binaryOP.rightExpression.accept(this);
result /= rOperand;
}
else {
}
}
else {
rOperand = binaryOP.rightExpression.accept(this);
if(rOperand!=0) {
result /= rOperand;
}
else {
throw new ArithmeticException("Arithmetic Error: division by zero detected");
}
}
break;
case '^':
rOperand = binaryOP.rightExpression.accept(this);
for(int i=1; i<rOperand; i++) {
result = result*result;
}
break;
default:
throw new RuntimeException("SemanticError: unknown operand found in AST: " + binaryOP.operator);
}
return result;
}
/**
*
* @param variable
* @return
*/
@Override
public Double visit(final Parser.Variable variable) {
return variable.value * x;
}
public class Evaluator {
/**
*
* @param number
* @return
*/
@Override
public Double visit(final Parser.Number number) {
return Double.valueOf(number.digits);
public double calculate(String expression, int variableValue) {
int index = 0;
while(index<expression.length())
if(Character.isLetter(expression.charAt(index))) {
expression.replaceAll(expression.substring(index, index+2),"XXX");
}
/**
*
* @param decimal
* @return
*/
@Override
public Double visit(final Parser.Decimal decimal) {
return Double.valueOf(decimal.afterDot.digits + "." + decimal.beforeDot.digits);
return 0.0;
}
/**
* Visits each node of a given expression
* @param ast A given expression that should be evaluated
* @param x This value will be used for all tokens of type Variable
* @return Returns the calculated value of the expression
*/
public Double visit(final Parser.Expression ast, int x) {
this.x = x;
return ast.accept(this);
}
}
......@@ -11,6 +11,8 @@ import java.util.List;
*
* @version 1.0
* @since 09.01.2023
* @see #parse(List)
* @see Expression
*/
public class Parser {
......@@ -19,6 +21,9 @@ public class Parser {
* It represents an expression in the form of an Abstract Syntax Tree(AST)
* A method 'accept' is implemented, it is used for allowing other classes to interact with it
* through the visiting pattern
* @see BinaryOperation
* @see Variable
* @see Value
*/
public abstract class Expression {
public abstract <T> T accept(Visitor<T> visitor);
......@@ -28,6 +33,8 @@ public class Parser {
* This class represents a binary operation
* it contains a left expression, an operator, and a right expression
* if this operation is capsuled by brackets, the value of 'capsuled' will be true, else it will be false
* @see Variable
* @see Value
*/
public class BinaryOperation extends Expression {
Expression leftExpression;
......@@ -52,10 +59,8 @@ public class Parser {
* of the alphabet
*/
public class Variable extends Expression {
double value;
String variableName;
public Variable(String i) {
this.value = 1.0;
variableName = i;
}
public <T> T accept(Visitor<T> visitor) {
......@@ -66,6 +71,8 @@ public class Parser {
/**
* This abstract class is only used for heredity
* A value can either be a number or a decimal
* @see Decimal
* @see Number
*/
private abstract class Value extends Expression {
......@@ -86,6 +93,7 @@ public class Parser {
/**
* This class represents a decimal, it contains two numbers: the one before- and the one after the dot
* @see Number
*/
public class Decimal extends Value {
public Number beforeDot;
......@@ -126,6 +134,10 @@ public class Parser {
* @param ts The list of tokens
* @return The expression object that got parsed
* @throws ParserException If the list of tokens was empty
* @see #parseBinaryOperation(List)
* @see #parseVariable(List)
* @see #parseDecimal(String)
* @see #parseVariable(List)
*/
private Expression parseExpression(List<Lexer.Token> ts) throws ParserException {
if(ts.isEmpty()) {
......@@ -149,8 +161,9 @@ public class Parser {
* It splits up the tokens into a left and right expression and with a recursive call of parseExpression
* each part will be determined, until all BinaryOperations consist of finite left and right expressions
* @param ts The list of tokens
* @return null If the list does not contain a BinaryOperation (anymore) or: BinaryOperation The crated BinaryOP
* @return null If the list does not contain a BinaryOperation <br> or: BinaryOperation The crated BinaryOP
* @throws ParserException If the list of tokens was empty
* @see #parseBrackets(List)
* */
private BinaryOperation parseBinaryOperation(List<Lexer.Token> ts) throws ParserException {
if(ts.isEmpty()) {
......@@ -214,6 +227,7 @@ public class Parser {
* @return int The position of the token with the closing bracket or -1 if the first token wasn't a bracket
* @throws ParserException If the list of tokens was empty, a bracket never gets closed or there isn't any
* value between the brackets
* @see #parseCharacter(String, String)
*/
private int parseBrackets(List<Lexer.Token> ts) throws ParserException{
if(ts.isEmpty()) {
......@@ -257,6 +271,7 @@ public class Parser {
* Checks if a String only contains an allowed operator
* @param operator The string that represents the operator
* @throws ParserException If the operator token was too big, small or consisted of invalid characters
* @see #parseCharacter(String, String)
*/
private void parseOperator(String operator) throws ParserException {
if(operator.isEmpty()) {
......@@ -273,7 +288,8 @@ public class Parser {
/**
* Checks if a token is a variable:
* @param ts List of tokens
* @return null It the token isn't a variable or: Variable A new Variable that gets created with the received token
* @return null It the token isn't a variable <br>or: Variable A new Variable that gets created
* with the received token
* @throws ParserException If the list of tokens was empty or the size of the token was too big
* or too small for a variable
*/
......@@ -305,13 +321,11 @@ public class Parser {
/**
* Checks if a token is a value and if so creates and returns a new Number or Decimal object
* @param ts The List of the tokens
* @return null If the token isn't a value/number or: Value The created Number or Decimal Object
* @return null If the token isn't a value/number <br>or: Value The created Number or Decimal Object
* @throws ParserException If the list of tokens is empty or
* @see #parseDecimal(String)
* @see #parseNumber(String, boolean)
*/
//Checks if a token is a value:
//Yes: returns a new Number or Decimal that gets created with the received token
//No: returns null
private Value parseValue(List<Lexer.Token> ts) throws ParserException {
if (ts.isEmpty()) {
throw new ParserException("SyntaxError: empty token list");
......@@ -335,6 +349,7 @@ public class Parser {
* parses a String into a Decimal object that consists of two numbers
* @param data The String that contains the value
* @return Decimal Returns the new Decimal object
* @see #parseVariable(List)
*/
private Decimal parseDecimal(String data) throws ParserException {
if(data.isEmpty()) {
......@@ -354,6 +369,8 @@ public class Parser {
* @param data The String that contains the value
* @param afterDot If this Number will be used after a dot in a Decimal object
* @return Number Returns the new Number object
* @see #parseDigit(String)
* @see #parseDigitWithoutZero(String)
*/
private Number parseNumber(String data, boolean afterDot) throws ParserException{
if (data.isEmpty()) {
......@@ -383,6 +400,7 @@ public class Parser {
* checks if a String only contains numbers(including zero)
* @param data The String that gets checked
* @exception ParserException If the data is empty or does contain invalid characters
* @see #parseCharacter(String, String)
*/
private void parseDigit(String data) throws ParserException {
for(int index=0; index<data.length(); index++) {
......@@ -400,6 +418,7 @@ public class Parser {
* checks if a String only contains numbers(excluding zero)
* @param data The String that gets checked
* @exception ParserException If the data is empty or does contain invalid characters
* @see #parseCharacter(String, String)
*/
private void parseDigitWithoutZero(String data) throws ParserException {
if(data.isEmpty()) {
......
package main;
import java.util.List;
/**
* This interface is used to implement a visitor pattern
* in other classes. For more information:
* <a href=https://en.wikipedia.org/wiki/Visitor_pattern>Visitor Pattern</a>
*/
public interface Visitor<T>{
public T visit(final Parser.BinaryOperation binOp);
public T visit(final Parser.Variable variable);
......
package test;
import main.Evaluator;
import org.junit.Test;
public class EvaluatorTest {
@Test
public void testFirstExpression() {
var Evaluator = new Evaluator();
}
}
......@@ -9,7 +9,7 @@ import java.util.List;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class LexerTest {
public class LexerTest {
@Test
public void disassembleFirstStringWithLexer() {
......
......@@ -7,7 +7,7 @@ import org.junit.Test;
import java.util.LinkedList;
import java.util.List;
class ParserTest {
public class ParserTest {
@Test
public void testParseVariable() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment