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 f2af3c947..6cb623454 100644 --- a/src/main/java/dk/camelot64/kickc/model/types/SymbolTypeInference.java +++ b/src/main/java/dk/camelot64/kickc/model/types/SymbolTypeInference.java @@ -137,7 +137,7 @@ public class SymbolTypeInference { } } - public static SymbolType inferTypeRValue(ProgramScope symbols, StatementAssignment assignment) { + private static SymbolType inferTypeRValue(ProgramScope symbols, StatementAssignment assignment) { SymbolType rValueType; RValue rValue1 = assignment.getrValue1(); RValue rValue2 = assignment.getrValue2(); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2AssertTypeMatch.java b/src/main/java/dk/camelot64/kickc/passes/Pass2AssertTypeMatch.java index 5e9e3157d..2a1e626af 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2AssertTypeMatch.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2AssertTypeMatch.java @@ -9,6 +9,7 @@ import dk.camelot64.kickc.model.statements.StatementConditionalJump; import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolTypeConversion; import dk.camelot64.kickc.model.types.SymbolTypeInference; +import dk.camelot64.kickc.model.values.AssignmentRValue; import dk.camelot64.kickc.model.values.LValue; import dk.camelot64.kickc.model.values.RValue; @@ -50,7 +51,7 @@ public class Pass2AssertTypeMatch extends Pass2SsaAssertion { private void checkAssignment(StatementAssignment statement) { LValue lValue = statement.getlValue(); SymbolType lValueType = SymbolTypeInference.inferType(getScope(), lValue); - SymbolType rValueType = SymbolTypeInference.inferTypeRValue(getScope(), statement); + SymbolType rValueType = SymbolTypeInference.inferType(getScope(), new AssignmentRValue(statement)); if(SymbolTypeConversion.assignmentTypeMatch(lValueType, rValueType)) return; // Types do not match getLog().append("ERROR! Type mismatch (" + lValueType.getTypeName() + ") cannot be assigned from (" + rValueType.getTypeName() + "). In " + statement.toString(getProgram(), false)); diff --git a/src/main/java/dk/camelot64/kickc/passes/PassNTypeInference.java b/src/main/java/dk/camelot64/kickc/passes/PassNTypeInference.java index d37114431..365829e6a 100644 --- a/src/main/java/dk/camelot64/kickc/passes/PassNTypeInference.java +++ b/src/main/java/dk/camelot64/kickc/passes/PassNTypeInference.java @@ -3,14 +3,12 @@ package dk.camelot64.kickc.passes; import dk.camelot64.kickc.model.CompileError; import dk.camelot64.kickc.model.ControlFlowBlock; import dk.camelot64.kickc.model.Program; -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.statements.*; import dk.camelot64.kickc.model.symbols.Procedure; import dk.camelot64.kickc.model.symbols.ProgramScope; import dk.camelot64.kickc.model.symbols.Variable; import dk.camelot64.kickc.model.types.*; +import dk.camelot64.kickc.model.values.AssignmentRValue; import dk.camelot64.kickc.model.values.LValue; import dk.camelot64.kickc.model.values.RValue; import dk.camelot64.kickc.model.values.VariableRef; @@ -117,28 +115,8 @@ public class PassNTypeInference extends Pass2SsaOptimization { if(lValue instanceof VariableRef) { Variable symbol = programScope.getVariable((VariableRef) lValue); if(SymbolType.VAR.equals(symbol.getType()) || SymbolType.NUMBER.equals(symbol.getType())) { - // Unresolved symbol - perform inference - Operator operator = assignment.getOperator(); - if(assignment.getrValue1() == null && operator == null) { - // Copy operation - RValue rValue = assignment.getrValue2(); - SymbolType type = SymbolTypeInference.inferType(programScope, rValue); - setInferedType(program, assignment, symbol, type); - } else if(assignment.getrValue1() == null && operator instanceof OperatorUnary) { - // Unary operation - RValue rValue = assignment.getrValue2(); - SymbolType type = SymbolTypeInference.inferType(programScope, (OperatorUnary) operator, rValue); - setInferedType(program, assignment, symbol, type); - } else if(operator instanceof OperatorBinary) { - // Binary operation - SymbolType type = SymbolTypeInference.inferType( - programScope, assignment.getrValue1(), - (OperatorBinary) assignment.getOperator(), - assignment.getrValue2()); - setInferedType(program, assignment, symbol, type); - } else { - throw new CompileError("Cannot infer type of " + assignment); - } + SymbolType type = SymbolTypeInference.inferType(programScope, new AssignmentRValue(assignment)); + setInferedType(program, assignment, symbol, type); // If the type is an array or a string the symbol is constant if(symbol.getType() instanceof SymbolTypeArray) { symbol.setDeclaredConstant(true);