-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package interpreter; | ||
|
||
public interface Expression { | ||
|
||
double toEvaluate(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |