forked from fadookie/particleman
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from FxMorin/optimization-pass
- Loading branch information
Showing
15 changed files
with
227 additions
and
90 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.eliotlash.molang; | ||
|
||
import com.eliotlash.molang.ast.Expr; | ||
import com.eliotlash.molang.functions.FunctionDefinition; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* This holds a list of function that can be compiled without needing a context. | ||
* These functions, if only passed constants, can be compiled ahead of time into a constant. | ||
* <p> | ||
* You are able to add your own functions into this list. | ||
* | ||
* @author FX | ||
*/ | ||
public class ConstantFunctions { | ||
|
||
private static final List<FunctionDefinition> CONSTANT_FUNCTIONS = new ArrayList<>(); | ||
|
||
public static boolean isConstant(Expr.Call expr) { | ||
FunctionDefinition functionDefinition = new FunctionDefinition(expr.target(), expr.member()); | ||
return isFunctionDefinitionConstant(functionDefinition) && areArgumentsConstant(expr.arguments()); | ||
} | ||
|
||
public static boolean isFunctionDefinitionConstant(FunctionDefinition functionDefinition) { | ||
return CONSTANT_FUNCTIONS.contains(functionDefinition); | ||
} | ||
|
||
public static boolean areArgumentsConstant(List<Expr> arguments) { | ||
for (Expr expr : arguments) { | ||
if (!(expr instanceof Expr.Constant)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static void addConstantFunction(FunctionDefinition functionDefinition) { | ||
if (!CONSTANT_FUNCTIONS.contains(functionDefinition)) { | ||
CONSTANT_FUNCTIONS.add(functionDefinition); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,43 +1,22 @@ | ||
package com.eliotlash.molang.ast; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class Evaluatable { | ||
private Expr expr = null; | ||
private List<Stmt> stmts = null; | ||
private boolean constant = false; | ||
public interface Evaluatable { | ||
|
||
public Evaluatable(Expr expr) { | ||
this.expr = Objects.requireNonNull(expr); | ||
constant = expr instanceof Expr.Constant; | ||
default boolean isConstant() { | ||
return false; | ||
} | ||
|
||
public Evaluatable(List<Stmt> stmts) { | ||
this.stmts = Objects.requireNonNull(stmts); | ||
} | ||
|
||
public double evaluate(Evaluator evaluator) { | ||
if (stmts != null) { | ||
return evaluator.evaluate(stmts); | ||
} | ||
if (expr != null) { | ||
Double result = evaluator.evaluate(expr); | ||
return result == null ? 0 : result; | ||
} | ||
|
||
//should never get here | ||
default double getConstant() { | ||
return 0.0; | ||
} | ||
|
||
public boolean isConstant() { | ||
return constant; | ||
static Evaluatable of(Expr expression) { | ||
return new EvaluatableExpr(expression); | ||
} | ||
|
||
public double getConstant() { | ||
if(!isConstant()) { | ||
return 0.0; | ||
} | ||
return Evaluator.getGlobalEvaluator().evaluate(expr); | ||
static Evaluatable of(List<Stmt> statements) { | ||
return new EvaluatableStmt(statements); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/eliotlash/molang/ast/EvaluatableExpr.java
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,30 @@ | ||
package com.eliotlash.molang.ast; | ||
|
||
import java.util.Objects; | ||
|
||
public class EvaluatableExpr implements Evaluatable { | ||
|
||
private final Expr expr; | ||
private final boolean constant; | ||
|
||
public EvaluatableExpr(Expr expr) { | ||
this.expr = Objects.requireNonNull(expr); | ||
this.constant = expr instanceof Expr.Constant; | ||
} | ||
|
||
public double evaluate(Evaluator evaluator) { | ||
Double result = evaluator.evaluate(this.expr); | ||
return result == null ? 0 : result; | ||
} | ||
|
||
public boolean isConstant() { | ||
return this.constant; | ||
} | ||
|
||
public double getConstant() { | ||
if(!isConstant()) { | ||
return 0.0; | ||
} | ||
return Evaluator.getGlobalEvaluator().evaluate(this.expr); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/eliotlash/molang/ast/EvaluatableStmt.java
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,17 @@ | ||
package com.eliotlash.molang.ast; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class EvaluatableStmt implements Evaluatable { | ||
|
||
private final List<Stmt> stmts; | ||
|
||
public EvaluatableStmt(List<Stmt> stmts) { | ||
this.stmts = Objects.requireNonNull(stmts); | ||
} | ||
|
||
public double evaluate(Evaluator evaluator) { | ||
return evaluator.evaluate(this.stmts); | ||
} | ||
} |
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
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
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
Oops, something went wrong.