Skip to content

Commit

Permalink
fix very very *many* bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Mar 6, 2023
1 parent 208ca35 commit 8260771
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.nickid2018</groupId>
<artifactId>smcl</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
<packaging>jar</packaging>
<name>smcl</name>
<url>https://github.com/Nickid2018/SMCL</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public static BinaryFunctionBuilder createBuilder(String name) {
}

@Override
public Statement create(Statement... statements) {
public Statement create(boolean optimize, Statement... statements) {
Statement source1 = statements[0];
Statement source2 = statements[1];
if(source1 instanceof NumberStatement && source2 instanceof NumberStatement) {
if(source1 instanceof NumberStatement && source2 instanceof NumberStatement && optimize) {
NumberObject value1 = source1.calculate(null);
value1 = resolveVariable1.accept(value1, source1.getSMCL());
domainCheck1.accept(value1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,5 @@ public String getName() {
* @param statements an array contains arguments
* @return a statement
*/
public abstract Statement create(Statement... statements);
public abstract Statement create(boolean optimize, Statement... statements);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ public static UnaryFunctionBuilder createBuilder(String name) {
return new UnaryFunctionBuilder(name);
}

@Override
public Statement create(Statement... statements) {
return create(true, statements);
}

@Override
public Statement create(boolean optimize, Statement... statements) {
Statement source = statements[0];
if (source instanceof NumberStatement) {
if (source instanceof NumberStatement && optimize) {
NumberObject value = source.calculate(null);
value = resolveVariable.accept(value, source.getSMCL());
domainCheck.accept(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class BinaryFunctionParser extends FunctionParser {

/**
* Construct a parser.
* @param unary the function builder
* @param binary the function builder
*/
public BinaryFunctionParser(BinaryFunctionBuilder unary) {
map = unary::create;
public BinaryFunctionParser(BinaryFunctionBuilder binary) {
map = statements -> binary.create(optimize, statements);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
*/
public abstract class FunctionParser {

protected boolean optimize = true;

public FunctionParser noOptimize() {
optimize = false;
return this;
}

/**
* Returns whether it has two or more arguments.
* @return true if the arguments count >1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class UnaryFunctionParser extends FunctionParser {
* @param unary the function builder
*/
public UnaryFunctionParser(UnaryFunctionBuilder unary) {
map = unary::create;
map = statements -> unary.create(optimize, statements);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,21 @@ public MathStatement(SMCLContext smcl, VariableList variables, boolean isNegativ
*/
public MathStatement(SMCLContext smcl, VariableList variables, boolean isNegative, List<Statement> subStatements) {
super(smcl, variables, isNegative);
this.subStatements = Collections.unmodifiableList(subStatements);
this.subStatements = Collections.unmodifiableList(expand(subStatements));
}

private static List<Statement> expand(List<Statement> statement) {
List<Statement> list = new ArrayList<>();
for (Statement en : statement) {
if (en instanceof MathStatement)
expand(((MathStatement) en).subStatements).stream()
.map(s -> en.isNegative() ? s.negate() : s)
.forEach(list::add);
else
list.add(en);
}
return list;
}

@Override
public Statement negate() {
Expand Down

0 comments on commit 8260771

Please sign in to comment.