Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞️ Bugfix: Binary Operators support for non-Integer types #52

Open
wants to merge 6 commits into
base: vardec_varass_dependency
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/d.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,17 @@ jobs:
run: ./tlang typecheck source/tlang/testing/unused_vars.t --unusedVars true
- name: Unused variables check (negative case)
run: ./tlang typecheck source/tlang/testing/unused_vars_none.t --unusedVars true

- name: Invalid pointer arithmetic (both operands are pointer types)
run: |
set +e
./tlang typecheck source/tlang/testing/typecheck/simple_binops_ptr_bad.t
if [ $? = 255 ]
then
exit 0
else
exit 1
fi

emit:
needs: [build, unittests]
Expand Down Expand Up @@ -542,6 +553,11 @@ jobs:
exit 1
fi

- name: Equality between pointers (both operands are pointers)
run: |
set -e
./tlang compile source/tlang/testing/simple_binops_ptr.t



##################################
Expand Down
8 changes: 5 additions & 3 deletions source/tlang/compiler/codegen/emit/dgen.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -614,7 +616,7 @@ public final class DCodeEmitter : CodeEmitter
emit ~= "("~typeTransform(castingTo)~")";

/* The expression being casted */
emit ~= transform(uncastedInstruction);
emit ~= "("~transform(uncastedInstruction)~")";
}
else
{
Expand Down
6 changes: 5 additions & 1 deletion source/tlang/compiler/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ unittest


"source/tlang/testing/simple_pointer_array_syntax.t",

"source/tlang/testing/simple_binops_ptr.t"
];
foreach(string testFile; testFiles)
{
Expand Down Expand Up @@ -556,7 +558,9 @@ unittest
"source/tlang/testing/simple_literals4.t",
"source/tlang/testing/universal_coerce/simple_coerce_literal_bad.t",
"source/tlang/testing/universal_coerce/simple_coerce_literal_bad_stdalon.t",
"source/tlang/testing/simple_function_return_type_check_bad.t"
"source/tlang/testing/simple_function_return_type_check_bad.t",

"source/tlang/testing/typecheck/simple_binops_ptr_bad.t"
];

foreach(string testFileFail; testFilesFail)
Expand Down
25 changes: 22 additions & 3 deletions source/tlang/compiler/typecheck/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -1821,13 +1821,23 @@ public final class TypeChecker
}
}
}
/**
* If both left and right operands are pointers
* and the operator is not equality
*
* `<a> is Pointer, <b> 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
Expand All @@ -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
{
Expand Down
12 changes: 12 additions & 0 deletions source/tlang/testing/simple_binops_ptr.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module simple_binops_ptr;

int main()
{
int* ptr1 = 0;
int* ptr2 = 0;

int* ptr3 = 0;

ubyte cond = ptr1==ptr2;
return cond;
}
11 changes: 11 additions & 0 deletions source/tlang/testing/typecheck/simple_binops_ptr_bad.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module simple_binops_ptr_bad;

int main()
{
int* ptr1 = 0;
int* ptr2 = 0;

int* ptr3 = ptr1+ptr2;

return 0;
}
Loading