Merge branch 'master' into future

This commit is contained in:
gnembon committed 2021-08-25 15:19:30 -03:00
commit 7e67966fd2
9 files changed
+56 -23

No files matched your search

+21 -13
View File
@@ -1162,20 +1162,28 @@ public class Expression
for (Map.Entry<String, String> pair : functionalEquivalence.entrySet()) {
String operator = pair.getKey();
String function = pair.getValue();
if (symbol.equals(operator) || symbol.equals(function)) {
List<ExpressionNode> newargs = new ArrayList<>();
boolean contracted = false;
for (ExpressionNode arg : node.args) {
String argsymbol = arg.token.surface; // child is unoptimized and also same class
if ((argsymbol.equals(operator) || argsymbol.equals(function)) && (!(arg.op instanceof LazyValue.ContextFreeLazyValue))) {
newargs.addAll(arg.args);
contracted = true;
} else {
newargs.add(arg);
}
}
if (contracted) {
if ((symbol.equals(operator) || symbol.equals(function)) && node.args.size() > 0)
{
boolean leftOptimizable = operators.get(operator).isLeftAssoc();
ExpressionNode optimizedChild = node.args.get(leftOptimizable?0:(node.args.size()-1));
String type = optimizedChild.token.surface;
if ((type.equals(operator) || type.equals(function)) && (!(optimizedChild.op instanceof LazyValue.ContextFreeLazyValue)))
{
optimized = true;
List<ExpressionNode> newargs = new ArrayList<>();
if (leftOptimizable)
{
newargs.addAll(optimizedChild.args);
for (int i = 1; i < node.args.size(); i++)
newargs.add(node.args.get(i));
}
else
{
for (int i = 0; i < node.args.size()-1; i++)
newargs.add(node.args.get(i));
newargs.addAll(optimizedChild.args);
}
if (CarpetSettings.scriptsDebugging)
CarpetScriptServer.LOG.info(" - " + symbol + "(" + node.args.size() + ") => " + function + "(" + newargs.size() + ") at line " + (node.token.lineno + 1) + ", node depth " + indent);
node.token.morph(Tokenizer.Token.TokenType.FUNCTION, function);
@@ -93,7 +93,7 @@ public class Inventories {
CarpetContext cc = (CarpetContext)c;
if (lv.size() < 1) throw new InternalExpressionException("'recipe_data' requires at least one argument");
String recipeName = lv.get(0).getString();
RecipeType type = RecipeType.CRAFTING;
RecipeType<?> type = RecipeType.CRAFTING;
if (lv.size() > 1)
{
String recipeType = lv.get(1).getString();
@@ -20,12 +20,23 @@ public class Arithmetic {
expression.addUnaryFunction("fact", (v) ->
{
long number = NumericValue.asNumber(v).getLong();
long factorial = 1;
if (number < 21)
{
long factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}
return new NumericValue(factorial);
}
// values over 21 will exceed long limits
double factorial = 1.0;
for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}
return new NumericValue(factorial);
});
expression.addMathematicalUnaryFunction("sin", (d) -> Math.sin(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d)));