Skip to content

Commit

Permalink
Add evaluate to Evaluatable
Browse files Browse the repository at this point in the history
  • Loading branch information
FxMorin committed Dec 22, 2023
1 parent 771f6ea commit 6e9c9dd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/eliotlash/molang/ast/Evaluatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public interface Evaluatable {

double evaluate(Evaluator evaluator);

default boolean isConstant() {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/eliotlash/molang/ast/EvaluatableExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ public EvaluatableExpr(Expr expr) {
this.constant = expr instanceof Expr.Constant;
}

@Override
public double evaluate(Evaluator evaluator) {
Double result = evaluator.evaluate(this.expr);
return result == null ? 0 : result;
}

@Override
public boolean isConstant() {
return this.constant;
}

@Override
public double getConstant() {
if(!isConstant()) {
return 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public EvaluatableStmt(List<Stmt> stmts) {
this.stmts = Objects.requireNonNull(stmts);
}

@Override
public double evaluate(Evaluator evaluator) {
return evaluator.evaluate(this.stmts);
}
Expand Down

0 comments on commit 6e9c9dd

Please sign in to comment.