Skip to content

Commit

Permalink
chore: gracefully handle float and int expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie authored and Eddie committed Jun 19, 2024
1 parent eb3a8c1 commit 21dabbd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Binary file modified out/artifacts/jungle_jar/jungle.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public OperandStackContext() {
this.symbolTable = new SymbolTable();
}

public boolean isEmpty() {
return operandTypeStack.isEmpty();
}

@NotNull
public OperandType peek() {
return operandTypeStack.peek();
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/com/jungle/compiler/visitor/IfVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,37 @@ protected void visit(
throw new Error("expected if condition/expression");
}

OperandType conditionType = null;
try {
getExpressionVisitor().visit(mv, conditionNode, context);
} catch (Throwable t) {
throw new Error("if condition/expression is not resolvable", t);
boolean hasEvaluatedCondition = !context.isEmpty();
if (hasEvaluatedCondition) {
conditionType = context.peek();
// Expression may not be an integer like primitive
switch (conditionType) {
case OBJECT: {
throw new Error("not implemented");
}
default: break;
}
}
throw new Error("if condition/expression cannot be evaluated", t);
}
if (!OperandType.INTEGER_OPERATION_TYPES.contains(context.peek())) {
throw new Error("if condition/expression expected to be within the integer category");

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");
throw new Error("if condition/expression cannot evaluate floating-point values");
}

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

INode bodyNode = ast.getRight();
Expand Down

0 comments on commit 21dabbd

Please sign in to comment.