Skip to content

Commit

Permalink
Add Interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Mar 23, 2020
1 parent 8613392 commit ed98438
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Project that implements some design patterns using Java 13.
- Observer
- Factory
- Flyweight
- Memento
- Memento
- Interpreter
21 changes: 21 additions & 0 deletions src/interpreter/Annotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
###############################
# Interpreter #
###############################





##### Language - EN

-


##### Language - PT-BR

- Tenho uma arvore, onde eu avalia os nós de baixo para cima, cada nó sabe "de avaliar".
Ex: A soma não é dois números, é duas expressões que podem ser qualquer coisa.

- A ideia é ter uma arvore, e essa arvore se "alto" avaliar.

- O padrão Interpreter é geralmente útil para interpretar DSLs (se você não sabe o que é uma DSL.
7 changes: 7 additions & 0 deletions src/interpreter/Expression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interpreter;

public interface Expression {

double toEvaluate();

}
31 changes: 31 additions & 0 deletions src/interpreter/InterpreterMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package interpreter;

public class InterpreterMain {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test Interpreter");


Expression exLeft1 = new Subtraction(new Number(2.5), new Number(10));// -7.5
Expression exRight1 = new Sum(new Number(5), new Number(10));//15

Expression sum1 = new Sum(exLeft1, exRight1);// (-7.5) + 15

System.out.println("Result Expression 1: " + sum1.toEvaluate());//7.5 ->> (2.5 - 10) + (5 + 10)


System.out.println("-------------------------");


Expression exLeft2 = new Multiplication(new Number(3), new Number(3));//9
Expression exRight2 = new Sum(new Number(0.5), new Number(0.5));//1

Expression sum2 = new Sum(exLeft2, exRight2);// 9 + 1

System.out.println("Result Expression 1: " + sum2.toEvaluate());//10 ->> (3 * 9) + (0.5 + 0.5)


System.out.println("--------------------------------------------- End Test Interpreter");
}

}
20 changes: 20 additions & 0 deletions src/interpreter/Multiplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package interpreter;

public class Multiplication implements Expression {

private Expression exLeft;
private Expression exRight;

public Multiplication(Expression exLeft, Expression exRight){
this.exLeft = exLeft;
this.exRight = exRight;
}

@Override
public double toEvaluate() {
double valueLeft = this.exLeft.toEvaluate();//evaluate yourself
double valueRight = this.exRight.toEvaluate();//evaluate yourself
return valueLeft * valueRight;
}

}
16 changes: 16 additions & 0 deletions src/interpreter/Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package interpreter;

public class Number implements Expression{

private double num;

public Number(double num){
this.num = num;
}

@Override
public double toEvaluate() {
return this.num;
}

}
20 changes: 20 additions & 0 deletions src/interpreter/Subtraction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package interpreter;

public class Subtraction implements Expression {

private Expression exLeft;
private Expression exRight;

public Subtraction(Expression exLeft, Expression exRight){
this.exLeft = exLeft;
this.exRight = exRight;
}

@Override
public double toEvaluate() {
double valueLeft = this.exLeft.toEvaluate();//evaluate yourself
double valueRight = this.exRight.toEvaluate();//evaluate yourself
return valueLeft - valueRight;
}

}
20 changes: 20 additions & 0 deletions src/interpreter/Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package interpreter;

public class Sum implements Expression{

private Expression exLeft;
private Expression exRight;

public Sum(Expression exLeft, Expression exRight){
this.exLeft = exLeft;
this.exRight = exRight;
}

@Override
public double toEvaluate() {
double valueLeft = this.exLeft.toEvaluate();//evaluate yourself
double valueRight = this.exRight.toEvaluate();//evaluate yourself
return valueLeft + valueRight;
}

}

0 comments on commit ed98438

Please sign in to comment.