From 7640bfdfa3f43eb69e64257ebbd9833092923f61 Mon Sep 17 00:00:00 2001 From: Shin-ichi MORITA Date: Sun, 8 Jan 2023 23:09:30 +0900 Subject: [PATCH] Fixed boolean operations for objects. --- include/xemmai/type.h | 16 ++++++++++++++++ src/type.cc | 8 ++++---- test/directory.xm | 2 +- test/pair.xm | 4 ++-- test/primitives.xm | 5 +++++ test/psmtp.xm | 10 +++++----- test/ring.xm | 12 ++++++------ 7 files changed, 39 insertions(+), 18 deletions(-) diff --git a/include/xemmai/type.h b/include/xemmai/type.h index e45a1f7c..fbcf5d4b 100644 --- a/include/xemmai/type.h +++ b/include/xemmai/type.h @@ -140,6 +140,10 @@ struct t_type_of { 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; @@ -148,6 +152,18 @@ struct t_type_of { 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; diff --git a/src/type.cc b/src/type.cc index f84ae782..3608e626 100644 --- a/src/type.cc +++ b/src/type.cc @@ -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()) (f_global()->f_symbol_complement(), f_not_supported) (f_global()->f_symbol_multiply(), f_not_supported) (f_global()->f_symbol_divide(), f_not_supported) @@ -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()) (f_global()->f_symbol_not_equals(), t_member()) - (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()) + (f_global()->f_symbol_xor(), t_member()) + (f_global()->f_symbol_or(), t_member()) .f_derive(t_object::f_of(this)); } diff --git a/test/directory.xm b/test/directory.xm index 3ccd5fd0..6c429c12 100644 --- a/test/directory.xm +++ b/test/directory.xm @@ -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 diff --git a/test/pair.xm b/test/pair.xm index f6125212..2a2d8b95 100644 --- a/test/pair.xm +++ b/test/pair.xm @@ -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 diff --git a/test/primitives.xm b/test/primitives.xm index bb82fdd0..113ffaf9 100644 --- a/test/primitives.xm +++ b/test/primitives.xm @@ -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 diff --git a/test/psmtp.xm b/test/psmtp.xm index 8bc87c28..cfa69b71 100644 --- a/test/psmtp.xm +++ b/test/psmtp.xm @@ -11,11 +11,11 @@ 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 @@ -23,12 +23,12 @@ server = @(co, log) 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" diff --git a/test/ring.xm b/test/ring.xm index f65e3255..aba041e0 100644 --- a/test/ring.xm +++ b/test/ring.xm @@ -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