diff --git a/src/main/java/dk/camelot64/kickc/model/ConstantValueCalculator.java b/src/main/java/dk/camelot64/kickc/model/ConstantValueCalculator.java deleted file mode 100644 index ed8919cd9..000000000 --- a/src/main/java/dk/camelot64/kickc/model/ConstantValueCalculator.java +++ /dev/null @@ -1,38 +0,0 @@ -package dk.camelot64.kickc.model; - -import dk.camelot64.kickc.model.values.*; -import dk.camelot64.kickc.model.symbols.ConstantVar; -import dk.camelot64.kickc.model.symbols.ProgramScope; - -/** Can calculate the exact value for constants (used for type inference). */ -public class ConstantValueCalculator { - - public static ConstantLiteral calcValue(ProgramScope programScope, ConstantValue value) { - if(value instanceof ConstantInteger) { - return (ConstantLiteral) value; - } else if(value instanceof ConstantString) { - return (ConstantLiteral) value; - } else if(value instanceof ConstantChar) { - return (ConstantLiteral) value; - } else if(value instanceof ConstantRef) { - ConstantVar constantVar = programScope.getConstant((ConstantRef) value); - ConstantValue constantVarValue = constantVar.getValue(); - return calcValue(programScope, constantVarValue); - } else if(value instanceof ConstantUnary) { - ConstantUnary unary = (ConstantUnary) value; - return unary.getOperator().calculate(calcValue(programScope, unary.getOperand())); - } else if(value instanceof ConstantBinary) { - ConstantBinary binary = (ConstantBinary) value; - return binary.getOperator().calculate(calcValue(programScope, binary.getLeft()), calcValue(programScope, binary.getRight())); - } else if(value instanceof ConstantArrayList) { - // Cannot calculate value of inline array - throw new ConstantNotLiteral("Not literal "+value.toString()); - } else if(value instanceof ConstantArrayFilled) { - // Cannot calculate value of inline array - throw new ConstantNotLiteral("Not literal "+value.toString()); - } else { - throw new RuntimeException("Unknown constant value " + value); - } - } - -} diff --git a/src/main/java/dk/camelot64/kickc/model/types/SymbolTypeInference.java b/src/main/java/dk/camelot64/kickc/model/types/SymbolTypeInference.java index e13bbca1c..43d4b38ea 100644 --- a/src/main/java/dk/camelot64/kickc/model/types/SymbolTypeInference.java +++ b/src/main/java/dk/camelot64/kickc/model/types/SymbolTypeInference.java @@ -2,16 +2,15 @@ package dk.camelot64.kickc.model.types; import dk.camelot64.kickc.model.CompileError; import dk.camelot64.kickc.model.ConstantNotLiteral; -import dk.camelot64.kickc.model.ConstantValueCalculator; +import dk.camelot64.kickc.model.operators.Operator; import dk.camelot64.kickc.model.operators.OperatorBinary; import dk.camelot64.kickc.model.operators.OperatorUnary; +import dk.camelot64.kickc.model.operators.Operators; import dk.camelot64.kickc.model.statements.StatementAssignment; import dk.camelot64.kickc.model.statements.StatementCall; import dk.camelot64.kickc.model.statements.StatementLValue; -import dk.camelot64.kickc.model.operators.Operator; -import dk.camelot64.kickc.model.operators.Operators; -import dk.camelot64.kickc.model.values.*; import dk.camelot64.kickc.model.symbols.*; +import dk.camelot64.kickc.model.values.*; import java.util.ArrayList; import java.util.Arrays; @@ -35,7 +34,7 @@ public class SymbolTypeInference { if(rValue instanceof ConstantValue) { ConstantValue value = null; try { - value = operator.calculate(ConstantValueCalculator.calcValue(programScope, (ConstantValue) rValue)); + value = operator.calculate(((ConstantValue) rValue).calculateLiteral(programScope)); } catch(ConstantNotLiteral e) { value = null; } @@ -71,8 +70,8 @@ public class SymbolTypeInference { ConstantValue value = null; try { value = operator.calculate( - ConstantValueCalculator.calcValue(programScope, (ConstantValue) rValue1), - ConstantValueCalculator.calcValue(programScope, (ConstantValue) rValue2) + ((ConstantValue) rValue1).calculateLiteral(programScope), + ((ConstantValue) rValue2).calculateLiteral(programScope) ); } catch(ConstantNotLiteral e) { value = null; diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayFilled.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayFilled.java index 289bafaac..482b69f12 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayFilled.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayFilled.java @@ -33,8 +33,8 @@ public class ConstantArrayFilled implements ConstantValue { } @Override - public ConstantLiteral calculate(ProgramScope scope) { - throw new CompileError("Cannot calculate literal array"); + public ConstantLiteral calculateLiteral(ProgramScope scope) { + throw new ConstantNotLiteral("Cannot calculate literal array"); } @Override diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayList.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayList.java index 12f32592b..8e25a5dd8 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayList.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantArrayList.java @@ -35,8 +35,8 @@ public class ConstantArrayList implements ConstantValue { } @Override - public ConstantLiteral calculate(ProgramScope scope) { - throw new CompileError("Cannot calculate literal array"); + public ConstantLiteral calculateLiteral(ProgramScope scope) { + throw new ConstantNotLiteral("Cannot calculate literal array"); } @Override diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantBinary.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantBinary.java index 051c06f1e..16603375a 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantBinary.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantBinary.java @@ -1,7 +1,6 @@ package dk.camelot64.kickc.model.values; import dk.camelot64.kickc.model.*; -import dk.camelot64.kickc.model.operators.Operator; import dk.camelot64.kickc.model.operators.OperatorBinary; import dk.camelot64.kickc.model.symbols.ProgramScope; import dk.camelot64.kickc.model.types.SymbolType; @@ -38,8 +37,8 @@ public class ConstantBinary implements ConstantValue { } @Override - public ConstantLiteral calculate(ProgramScope scope) { - return operator.calculate(left.calculate(scope), right.calculate(scope)); + public ConstantLiteral calculateLiteral(ProgramScope scope) { + return operator.calculate(left.calculateLiteral(scope), right.calculateLiteral(scope)); } @Override diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantLiteral.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantLiteral.java index 58595f741..c5bccc23e 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantLiteral.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantLiteral.java @@ -8,7 +8,7 @@ public interface ConstantLiteral extends ConstantValue { T getValue(); @Override - default ConstantLiteral calculate(ProgramScope scope) { + default ConstantLiteral calculateLiteral(ProgramScope scope) { return this; } } diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java index 2e75cfb7b..caabee9f9 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java @@ -18,9 +18,9 @@ public class ConstantRef extends SymbolRef implements ConstantValue { } @Override - public ConstantLiteral calculate(ProgramScope scope) { + public ConstantLiteral calculateLiteral(ProgramScope scope) { ConstantVar constantVar = scope.getConstant(this); ConstantValue constantVarValue = constantVar.getValue(); - return constantVarValue.calculate(scope); + return constantVarValue.calculateLiteral(scope); } } diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantUnary.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantUnary.java index 9f56e3de7..dc57a6f11 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantUnary.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantUnary.java @@ -1,7 +1,6 @@ package dk.camelot64.kickc.model.values; import dk.camelot64.kickc.model.*; -import dk.camelot64.kickc.model.operators.Operator; import dk.camelot64.kickc.model.operators.OperatorUnary; import dk.camelot64.kickc.model.symbols.ProgramScope; import dk.camelot64.kickc.model.types.SymbolType; @@ -30,8 +29,8 @@ public class ConstantUnary implements ConstantValue { } @Override - public ConstantLiteral calculate(ProgramScope scope) { - return operator.calculate(operand.calculate(scope)); + public ConstantLiteral calculateLiteral(ProgramScope scope) { + return operator.calculate(operand.calculateLiteral(scope)); } @Override diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantValue.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantValue.java index 26f7796a7..d673b84e5 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantValue.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantValue.java @@ -8,6 +8,6 @@ public interface ConstantValue extends RValue { SymbolType getType(ProgramScope scope); - ConstantLiteral calculate(ProgramScope scope); + ConstantLiteral calculateLiteral(ProgramScope scope); } diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantVarPointer.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantVarPointer.java index a0c3cf86f..ed5dbea22 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantVarPointer.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantVarPointer.java @@ -31,7 +31,7 @@ public class ConstantVarPointer implements ConstantValue { } @Override - public ConstantLiteral calculate(ProgramScope scope) { + public ConstantLiteral calculateLiteral(ProgramScope scope) { throw new CompileError("Cannot calculate literal var pointer"); } diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java index 91642a73d..cd6cda87c 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java @@ -1,7 +1,6 @@ package dk.camelot64.kickc.passes; import dk.camelot64.kickc.model.*; -import dk.camelot64.kickc.model.operators.Operator; import dk.camelot64.kickc.model.operators.OperatorBinary; import dk.camelot64.kickc.model.operators.OperatorUnary; import dk.camelot64.kickc.model.operators.Operators; @@ -62,7 +61,7 @@ public class Pass2ConstantIdentification extends Pass2SsaOptimization { } else { throw new CompileError( "Constant variable has a non-matching type \n variable: " + variable.toString(getProgram()) + - "\n value: (" + valueType.toString() + ") " + ConstantValueCalculator.calcValue(getScope(), constVal) + + "\n value: (" + valueType.toString() + ") " + constVal.calculateLiteral(getScope()) + "\n value definition: " + constVal.toString(getProgram())); } }