From bfcaeeaf7d866771f516b5598817058e64cc7db4 Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Mon, 3 Jun 2024 14:48:20 -0400 Subject: [PATCH] fixed boolean operators --- cerise-c.m | 5 +++-- cerise-c.rb | 5 +++++ runtime.h | 27 +++++++++++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cerise-c.m b/cerise-c.m index 54064a2..1bd5c13 100644 --- a/cerise-c.m +++ b/cerise-c.m @@ -19,6 +19,7 @@ TestBoolOps() { } init() { - TestLiterals() - return 42.0 + assert TestLiterals() + assert TestBoolOps() + return true } diff --git a/cerise-c.rb b/cerise-c.rb index f7f904e..775c73a 100755 --- a/cerise-c.rb +++ b/cerise-c.rb @@ -1196,6 +1196,11 @@ module Codegen end end +## +# Add Builtin Functions +## + + ## # Emit the Code diff --git a/runtime.h b/runtime.h index 62aa671..c32d62d 100644 --- a/runtime.h +++ b/runtime.h @@ -97,9 +97,9 @@ static inline bool IsString(Value val) { /* C Type Conversions *************************************************/ -static inline int32_t ValueAsBool(Value val) { +static inline bool ValueAsBool(Value val) { assert(IsBool(val)); - return (bool)val.as_uint64; + return (bool)(val.as_uint64 == VALUE_TRUE); } static inline int32_t ValueAsInt(Value val) { @@ -149,7 +149,7 @@ static inline Value MakeNil(void) { } static inline Value MakeBool(bool b) { - return (Value){ .as_uint64 = b ? VALUE_TRUE : VALUE_FALSE }; + return (Value){ .as_uint64 = (b ? VALUE_TRUE : VALUE_FALSE) }; } static inline Value MakeInt(int32_t i) { @@ -258,13 +258,28 @@ static inline Value OpGt(Value left, Value right) { } static inline Value OpGtEq(Value left, Value right) { - return MakeNil(); + Value result; + result = MakeBool(false); + return result; } static inline Value OpEq(Value left, Value right) { - return MakeNil(); + Value result; + if (IsBool(left)) { + result = MakeBool(ValueAsBool(left) == ValueAsBool(right)); + } else { + result = MakeBool(false); + } + return result; } static inline Value OpNeq(Value left, Value right) { - return MakeNil(); + Value result; + if (IsBool(left)) { + printf("%d != %d\n", ValueAsBool(left), ValueAsBool(right)); + result = MakeBool(ValueAsBool(left) != ValueAsBool(right)); + } else { + result = MakeBool(false); + } + return result; } -- 2.54.0