diff --git a/source/tlang/compiler/codegen/emit/dgen.d b/source/tlang/compiler/codegen/emit/dgen.d index ff55feef..5219f0b7 100644 --- a/source/tlang/compiler/codegen/emit/dgen.d +++ b/source/tlang/compiler/codegen/emit/dgen.d @@ -304,11 +304,13 @@ public final class DCodeEmitter : CodeEmitter * the cast out me thinks. * * See issue #140 (https://deavmi.assigned.network/git/tlang/tlang/issues/140#issuecomment-1892) + * + * Note, this would NOT be the case for equality */ Type leftHandOpType = (cast(Value)binOpInstr.lhs).getInstrType(); Type rightHandOpType = (cast(Value)binOpInstr.rhs).getInstrType(); - if(typeChecker.isPointerType(leftHandOpType)) + if(typeChecker.isPointerType(leftHandOpType) && binOpInstr.operator != SymbolType.EQUALS) { // Sanity check the other side should have been coerced to CastedValueInstruction CastedValueInstruction cvInstr = cast(CastedValueInstruction)binOpInstr.rhs; @@ -319,7 +321,7 @@ public final class DCodeEmitter : CodeEmitter // Relax the CV-instr to prevent it from emitting explicit cast code cvInstr.setRelax(true); } - else if(typeChecker.isPointerType(rightHandOpType)) + else if(typeChecker.isPointerType(rightHandOpType) && binOpInstr.operator != SymbolType.EQUALS) { // Sanity check the other side should have been coerced to CastedValueInstruction CastedValueInstruction cvInstr = cast(CastedValueInstruction)binOpInstr.lhs; @@ -614,7 +616,7 @@ public final class DCodeEmitter : CodeEmitter emit ~= "("~typeTransform(castingTo)~")"; /* The expression being casted */ - emit ~= transform(uncastedInstruction); + emit ~= "("~transform(uncastedInstruction)~")"; } else { diff --git a/source/tlang/compiler/typecheck/core.d b/source/tlang/compiler/typecheck/core.d index c2d2f677..561b5ea8 100644 --- a/source/tlang/compiler/typecheck/core.d +++ b/source/tlang/compiler/typecheck/core.d @@ -1821,13 +1821,23 @@ public final class TypeChecker } } } + /** + * If both left and right operands are pointers + * and the operator is not equality + * + * ` is Pointer, is Pointer` + */ + else if(binOperator != SymbolType.EQUALS && isPointerType(vLhsType) && isPointerType(vRhsType)) + { + expect("Both left hand side and right hand side cannot be pointers in any arithmetic operation, except equality"); + } else { // See issue #141: Binary Operators support for non-Integer types (https://deavmi.assigned.network/git/tlang/tlang/issues/141) ERROR("FIXME: We need to add support for this, class equality, and others like floats"); } - + /** * Refresh types as instructions may have changed in * the above enforcement call @@ -1848,8 +1858,17 @@ public final class TypeChecker Type chosenType; if(isSameType(vLhsType, vRhsType)) { - /* Left type + Right type = left/right type (just use left - it doesn't matter) */ - chosenType = vLhsType; + /* If equality then result is `ubyte` */ + if(binOperator == SymbolType.EQUALS) + { + chosenType = getType(this.program, "ubyte"); + } + /* Other cases */ + else + { + /* Left type + Right type = left/right type (just use left - it doesn't matter) */ + chosenType = vLhsType; + } } else {