Skip to content

Commit

Permalink
Equality can be checked between unions, in all supported backends.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 6, 2022
1 parent 9a6bd7c commit c1d97e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
7 changes: 7 additions & 0 deletions eg/struct-equality.castile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fun main() {
a = 40 as string|integer
b = 40 as string|integer
if a == b {
print("it is")
}
}
11 changes: 11 additions & 0 deletions src/castile/backends/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
return !strcmp(tag, tv->tag);
}
int equal_tagged_value(struct tagged_value *tv1, struct tagged_value *tv2)
{
return is_tag(tv1->tag, tv2) && tv1->value == tv2->value;
}
"""


Expand Down Expand Up @@ -262,6 +267,12 @@ def compile(self, ast):
self.write(', ')
self.compile(ast.children[1])
self.write(')')
elif ast.value == '==' and isinstance(ast.children[0].type, Union):
self.write('equal_tagged_value(')
self.compile(ast.children[0])
self.write(', ')
self.compile(ast.children[1])
self.write(')')
else:
self.write('(')
self.compile(ast.children[0])
Expand Down
3 changes: 3 additions & 0 deletions src/castile/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def __init__(self, tag, value):
def __repr__(self):
return '(%r, %r)' % (self.tag, self.value)

def __eq__(self, other):
return self.tag == other.tag and self.value == other.value


def builtin_len(s):
return len(s)
Expand Down
20 changes: 10 additions & 10 deletions tests/Castile.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,16 @@ Equality cannot be checked between two values of different types.
| }
? mismatch

Equality can be checked between unions. (TODO)

/| fun main() {
/| a = 40 as string|integer
/| b = 40 as string|integer
/| if a == b {
/| print("it is")
/| }
/| }
/= ok
Equality can be checked between unions.

| fun main() {
| a = 40 as string|integer
| b = 40 as string|integer
| if a == b {
| print("it is")
| }
| }
= it is

| fun main() {
| a = 40 as string|integer
Expand Down

0 comments on commit c1d97e9

Please sign in to comment.