Change BoolObj::new to return a Gc pointer, add T and F builtins

Since we're able to coerce objects easily, we're changing bools to be
interned behind the scenes.

T and F builtin functions are added because that's probably the easiest
way to implement true and false constants without making new
lexer/parsing rules.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-01-25 16:47:19 -08:00
parent fa0203b441
commit 427d354857
2 changed files with 19 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
use crate::obj::{Obj, VTable};
use crate::syn::span::Span;
use crate::vm::{error::*, machine::Machine};
use gc::{Finalize, Trace};
use gc::{Finalize, Gc, Trace};
pub type Bool = bool;
@@ -12,11 +12,14 @@ pub struct BoolObj {
}
impl BoolObj {
pub fn new(value: Bool) -> Self {
BoolObj {
value,
vtable: vtable! {},
pub fn new(value: Bool) -> Gc<Self> {
thread_local! {
static BOOLS: [Gc<BoolObj>; 2] = [
Gc::new(BoolObj { value: false, vtable: vtable! {} }),
Gc::new(BoolObj { value: true, vtable: vtable! {} }),
];
}
BOOLS.with(|bools| Gc::clone(&bools[value as usize]))
}
#[allow(dead_code)]

View File

@@ -1,4 +1,4 @@
use crate::obj::{builtin::BuiltinExit, str::StrObj, Obj};
use crate::obj::prelude::*;
use crate::vm::{
error::RuntimeError,
machine::{Machine, MachineBuilder},
@@ -22,6 +22,16 @@ impl MachineBuilder {
panic!();
});
self.register_builtin_fun("T", |machine, _| {
machine.stack_push(BoolObj::new(true))?;
Ok(BuiltinExit::Return)
});
self.register_builtin_fun("F", |machine, _| {
machine.stack_push(BoolObj::new(false))?;
Ok(BuiltinExit::Return)
});
/*
//
// if