Add __bool__ method call to branch conditions

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2023-04-07 02:38:38 -07:00
parent 4e505bd979
commit 6c55eb2477
3 changed files with 21 additions and 1 deletions

View File

@@ -108,6 +108,9 @@ impl Compiler {
let mut cond_thunk = self.emit_expr(&cond_block.inner().cond);
cond_thunk.push_front(Inst::Comment("elseif compare ".to_string()));
cond_thunk.push_back(Inst::Dupe);
cond_thunk.push_back(Inst::GetAttr(self.intern_attr(BOOL_METHOD)));
cond_thunk.push_back(Inst::Call(1));
cond_thunk.push_back(Inst::Compare);
// jump forward by the length of the body
cond_thunk.push_back(Inst::JumpRelative(
@@ -129,6 +132,9 @@ impl Compiler {
let mut cond_thunk = self.emit_expr(&if_true.inner().cond);
cond_thunk.push_front(Inst::Comment("if compare".to_string()));
cond_thunk.push_back(Inst::Dupe);
cond_thunk.push_back(Inst::GetAttr(self.intern_attr(BOOL_METHOD)));
cond_thunk.push_back(Inst::Call(1));
cond_thunk.push_back(Inst::Compare);
cond_thunk.push_back(Inst::JumpRelative(
// + 1 because otherwise we won't end up on the next instruction

View File

@@ -2,13 +2,14 @@ pub mod boolean;
pub mod builtin_fun;
pub mod builtins;
pub mod int;
pub mod names;
pub mod none;
pub mod str;
pub mod user_fun;
pub mod prelude {
// Module types
pub use super::{boolean::*, builtin_fun::*, int::*, none::*, str::*, user_fun::*};
pub use super::{boolean::*, builtin_fun::*, int::*, names::*, none::*, str::*, user_fun::*};
// Local types
pub use super::{Attrs, AttrsPtr, Obj, ObjPtr};
// Macros

13
src/obj/names.rs Normal file
View File

@@ -0,0 +1,13 @@
/*
use crate::obj::prelude::*;
use std::sync::LazyLock;
macro_rules! name {
($name:expr) => {
LazyLock::new(|| ObjPtr::new(Str::new($name)))
};
}
*/
pub const BOOL_METHOD: &str = "__bool__";
//pub static BOOL_METHOD_NAME: LazyLock<ObjPtr> = name!(BOOL_METHOD);