diff --git a/out/artifacts/jungle_jar/jungle.jar b/out/artifacts/jungle_jar/jungle.jar index b279e30..bf0fb7e 100644 Binary files a/out/artifacts/jungle_jar/jungle.jar and b/out/artifacts/jungle_jar/jungle.jar differ diff --git a/src/main/java/com/jungle/ast/NodeType.java b/src/main/java/com/jungle/ast/NodeType.java index 347d6ff..f746744 100644 --- a/src/main/java/com/jungle/ast/NodeType.java +++ b/src/main/java/com/jungle/ast/NodeType.java @@ -48,7 +48,7 @@ public enum NodeType { /* new Node(IF) * .withLeft("expression") - * .withRight("block"") + * .withRight("block") */ IF, diff --git a/src/main/java/com/jungle/compiler/visitor/IfVisitor.java b/src/main/java/com/jungle/compiler/visitor/IfVisitor.java index 8e53573..31a8761 100644 --- a/src/main/java/com/jungle/compiler/visitor/IfVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/IfVisitor.java @@ -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 { @@ -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"); } @@ -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"); + } + } }