Skip to content

Commit

Permalink
chore: cleanup if-visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie authored and Eddie committed Jun 26, 2024
1 parent 186b23f commit 92db851
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Binary file modified out/artifacts/jungle_jar/jungle.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/java/com/jungle/ast/NodeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum NodeType {

/* new Node(IF)
* .withLeft("expression")
* .withRight("block"")
* .withRight("block")
*/
IF,

Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/jungle/compiler/visitor/IfVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ protected void visit(
throw new Error("expected if condition/expression");
}

OperandType conditionType = null;

if (conditionNode.getType() == NodeType.NOOP) {
logger.debug("assuming that the condition result is already on the operand stack");
} else {
Expand All @@ -112,18 +110,14 @@ protected void visit(
}
}

conditionType = context.peek();
OperandType conditionType = context.peek();

boolean isConditionTypeFloating = (
conditionType == OperandType.FLOAT ||
conditionType == OperandType.DOUBLE
);
if (isConditionTypeFloating) {
logger.warn("you may need to convert from a floating-point expression to an integer expression");
if (conditionType == OperandType.FLOAT || conditionType == OperandType.DOUBLE) {
logger.warn("you may need to convert from a floating-point expression to integer-like");
throw new Error("if condition/expression cannot evaluate floating-point values");
}

if (!OperandType.INTEGER_COMPUTATIONAL_TYPES.contains(conditionType)) {
if (OperandType.INTEGER_COMPUTATIONAL_TYPES.contains(conditionType) == false) {
throw new Error("if condition/expression expected to be integer-like");
}

Expand Down Expand Up @@ -223,5 +217,17 @@ enum CompareTo {
LESS_THAN_ZERO,
LESS_OR_EQUAL_THAN_ZERO,
GREATER_THAN_ZERO,
GREATER_OR_EQUAL_THAN_ZERO,
GREATER_OR_EQUAL_THAN_ZERO;

public int getOpcode() {
switch (this) {
case ZERO: return Opcodes.IFEQ;
case NONZERO: return Opcodes.IFNE;
case LESS_THAN_ZERO: return Opcodes.IFLT;
case LESS_OR_EQUAL_THAN_ZERO: return Opcodes.IFLE;
case GREATER_THAN_ZERO: return Opcodes.IFGT;
case GREATER_OR_EQUAL_THAN_ZERO: return Opcodes.IFGE;
default: throw new Error("unhandled if comparison");
}
}
}

0 comments on commit 92db851

Please sign in to comment.