Skip to content

Commit

Permalink
Fixed boolean operations for objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
shin1m committed Dec 2, 2023
1 parent 6f34c4d commit 6eaed03
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
16 changes: 16 additions & 0 deletions include/xemmai/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ struct t_type_of<t_object>
{
return a_self.f_tag();
}
static bool f__not(const t_pvalue& a_self)
{
return false;
}
static bool f__equals(const t_pvalue& a_self, const t_pvalue& a_other)
{
return a_self == a_other;
Expand All @@ -148,6 +152,18 @@ struct t_type_of<t_object>
{
return a_self != a_other;
}
static t_pvalue f__and(const t_pvalue& a_self, const t_pvalue& a_other)
{
return a_other;
}
static t_pvalue f__xor(const t_pvalue& a_self, const t_pvalue& a_other)
{
return a_other ? t_pvalue(false) : a_self;
}
static t_pvalue f__or(const t_pvalue& a_self, const t_pvalue& a_other)
{
return a_self;
}
XEMMAI__LOCAL void f_define();

t_slot v_this;
Expand Down
8 changes: 4 additions & 4 deletions src/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void t_type::f_define()
(f_global()->f_symbol_set_at(), f_not_supported)
(f_global()->f_symbol_plus(), f_not_supported)
(f_global()->f_symbol_minus(), f_not_supported)
(f_global()->f_symbol_not(), f_not_supported)
(f_global()->f_symbol_not(), t_member<bool(*)(const t_pvalue&), f__not>())
(f_global()->f_symbol_complement(), f_not_supported)
(f_global()->f_symbol_multiply(), f_not_supported)
(f_global()->f_symbol_divide(), f_not_supported)
Expand All @@ -47,9 +47,9 @@ void t_type::f_define()
(f_global()->f_symbol_greater_equal(), f_not_supported)
(f_global()->f_symbol_equals(), t_member<bool(*)(const t_pvalue&, const t_pvalue&), f__equals>())
(f_global()->f_symbol_not_equals(), t_member<bool(*)(const t_pvalue&, const t_pvalue&), f__not_equals>())
(f_global()->f_symbol_and(), f_not_supported)
(f_global()->f_symbol_xor(), f_not_supported)
(f_global()->f_symbol_or(), f_not_supported)
(f_global()->f_symbol_and(), t_member<t_pvalue(*)(const t_pvalue&, const t_pvalue&), f__and>())
(f_global()->f_symbol_xor(), t_member<t_pvalue(*)(const t_pvalue&, const t_pvalue&), f__xor>())
(f_global()->f_symbol_or(), t_member<t_pvalue(*)(const t_pvalue&, const t_pvalue&), f__or>())
.f_derive<t_object>(t_object::f_of(this));
}

Expand Down
2 changes: 1 addition & 1 deletion test/directory.xm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ assert = @(x) x || throw Throwable("Assertion failed."

directory = os.Directory("" + os.Path(system.script) / ".."
try
while (entry = directory.read()) !== null
while entry = directory.read()
special = entry.permissions >> 9
owner = entry.permissions >> 6 & 7
group = entry.permissions >> 3 & 7
Expand Down
4 changes: 2 additions & 2 deletions test/pair.xm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Pair = Object + @
$__initialize = @(first, second)
$first = first
$second = second
$__string = @ $second === null ? $first.__string() : $first.__string() + " " + $second
$__string = @ $second ? $first.__string() + " " + $second : $first.__string()

List = Object + @
$list
$__initialize = @ $list = null
$__string = @ $list === null ? "()" : "(" + $list + ")"
$__string = @ $list ? "(" + $list + ")" : "()"
$push = @(value) $list = Pair(value, $list
$pop = @
value = $list.first
Expand Down
5 changes: 5 additions & 0 deletions test/primitives.xm
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ assert(Bar(1.0).__equals(1.0)
print("Bar(1.0).__equals(2.0) = " + Bar(1.0).__equals(2.0)
assert(!Bar(1.0).__equals(2.0)

assert(!"" === false
assert("" === ""
assert("" + "" === ""
assert(foo + "" === foo
assert("" + foo === foo
assert("" & foo === foo
assert(foo ^ "" === false
assert(foo ^ false === foo
assert(foo | "" === foo
10 changes: 5 additions & 5 deletions test/psmtp.xm
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ server = @(co, log)
x = co("250 Hello " + hostname + "\n"
continue
if x.substring(0, 11) == "MAIL FROM: "
if sender === null
if sender
x = co("503 Sender already specified\n"
else
sender = x.substring(11
x = co("250 Sender " + sender + "\n"
else
x = co("503 Sender already specified\n"
continue
if x.substring(0, 9) == "RCPT TO: "
recipient = x.substring(9
recipients = recipients + "\tTo: " + recipient + "\n"
x = co("250 Recipient " + recipient + "\n"
continue
if x == "DATA"
if sender === null
if !sender
x = co("503 Sender not specified\n"
else if recipients == ""
x = co("503 Recipients not specified\n"
else
mail = hostname === null ? "" : "\tReceived: from " + hostname + "\n"
mail = hostname ? "\tReceived: from " + hostname + "\n" : ""
mail = mail + "\tFrom: " + sender + "\n"
mail = mail + recipients
mail = mail + "\t\n"
Expand Down
12 changes: 6 additions & 6 deletions test/ring.xm
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ Ring = Object + @
$__initialize = @
$ring = null
$__string = @
if $ring === null
return "()"
else
if $ring
return "(" + $string($ring.next) + ")"
else
return "()"
$string = @(cell)
cell === $ring && return cell.value.__string(
return cell.value.__string() + " " + $string(cell.next)
$push = @(value)
cell = Cell(value
if $ring === null
cell.next = cell
else
if $ring
cell.next = $ring.next
$ring.next = cell
else
cell.next = cell
$ring = cell
$pop = @
cell = $ring.next
Expand Down

0 comments on commit 6eaed03

Please sign in to comment.