-
Notifications
You must be signed in to change notification settings - Fork 1
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
7 changed files
with
145 additions
and
9 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
27 changes: 27 additions & 0 deletions
27
...main/java/dev/tenfont/cpmm/elements/expressions/binaryexpressions/EqualityExpression.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,27 @@ | ||
package dev.tenfont.cpmm.elements.expressions.binaryexpressions; | ||
|
||
import dev.tenfont.cpmm.lang.Parser; | ||
import dev.tenfont.cpmm.lang.components.BinaryExpression; | ||
import dev.tenfont.cpmm.lang.components.Context; | ||
import dev.tenfont.cpmm.lang.components.Expression; | ||
import dev.tenfont.cpmm.lang.components.TokenType; | ||
|
||
public class EqualityExpression extends BinaryExpression { | ||
private Expression right; | ||
|
||
public EqualityExpression(Expression left) { | ||
super(left); | ||
} | ||
|
||
@Override | ||
public Object get(Context context) { | ||
return getLeft().get(context).equals(right.get(context)); | ||
} | ||
|
||
@Override | ||
public boolean init(Parser parser, Context context) { | ||
parser.eat(TokenType.EQUALITY_OPERATOR); | ||
right = parser.parseExpression(context); | ||
return true; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ava/dev/tenfont/cpmm/elements/expressions/binaryexpressions/MultiplicativeExpression.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,44 @@ | ||
package dev.tenfont.cpmm.elements.expressions.binaryexpressions; | ||
|
||
import dev.tenfont.cpmm.lang.Parser; | ||
import dev.tenfont.cpmm.lang.components.BinaryExpression; | ||
import dev.tenfont.cpmm.lang.components.Context; | ||
import dev.tenfont.cpmm.lang.components.Expression; | ||
import dev.tenfont.cpmm.lang.components.TokenType; | ||
import dev.tenfont.cpmm.util.TypeUtils; | ||
|
||
public class MultiplicativeExpression extends BinaryExpression { | ||
private char operator; | ||
private Expression right; | ||
|
||
public MultiplicativeExpression(Expression left) { | ||
super(left); | ||
} | ||
|
||
@Override | ||
public Object get(Context context) { | ||
Object leftValue = getLeft().get(context); | ||
Object rightValue = right.get(context); | ||
switch (operator) { | ||
case '*' -> { | ||
if (leftValue instanceof String) { | ||
return ((String) leftValue).repeat((int) TypeUtils.toDouble(rightValue)); | ||
} else if (rightValue instanceof String) { | ||
return ((String) rightValue).repeat((int) TypeUtils.toDouble(leftValue)); | ||
} | ||
return TypeUtils.toDouble(leftValue) * TypeUtils.toDouble(rightValue); | ||
} | ||
case '/' -> { | ||
return TypeUtils.toDouble(leftValue) / TypeUtils.toDouble(rightValue); | ||
} | ||
default -> throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean init(Parser parser, Context context) { | ||
operator = parser.eat(TokenType.MULTIPLICATIVE_OPERATOR, Character.class); | ||
right = parser.parseExpression(context); | ||
return true; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...in/java/dev/tenfont/cpmm/elements/expressions/binaryexpressions/RelationalExpression.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,37 @@ | ||
package dev.tenfont.cpmm.elements.expressions.binaryexpressions; | ||
|
||
import dev.tenfont.cpmm.lang.Parser; | ||
import dev.tenfont.cpmm.lang.components.BinaryExpression; | ||
import dev.tenfont.cpmm.lang.components.Context; | ||
import dev.tenfont.cpmm.lang.components.Expression; | ||
import dev.tenfont.cpmm.lang.components.TokenType; | ||
import dev.tenfont.cpmm.util.TypeUtils; | ||
|
||
public class RelationalExpression extends BinaryExpression { | ||
private String operator; | ||
private Expression right; | ||
|
||
public RelationalExpression(Expression left) { | ||
super(left); | ||
} | ||
|
||
@Override | ||
public Object get(Context context) { | ||
double leftValue = TypeUtils.toDouble(getLeft().get(context)); | ||
double rightValue = TypeUtils.toDouble(right.get(context)); | ||
return switch (operator) { | ||
case ">" -> leftValue > rightValue; | ||
case ">=" -> leftValue >= rightValue; | ||
case "<" -> leftValue < rightValue; | ||
case "<=" -> leftValue <= rightValue; | ||
default -> throw new IllegalArgumentException(); | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean init(Parser parser, Context context) { | ||
operator = parser.eat(TokenType.RELATIONAL_OPERATOR, String.class); | ||
right = parser.parseExpression(context); | ||
return true; | ||
} | ||
} |
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