Skip to content

Commit

Permalink
feat: add bool and pop builtin funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Jun 15, 2024
1 parent 494f2bf commit fe09a74
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ For the language to be almost operational it should have:
- [x] floats
- from int
- from string
- [ ] booleans
- [x] booleans
- from string
- [x] strings
- concatenate
- append
- split
- [ ] arrays
- [x] arrays
- concatenate
- push
- pop
Expand Down
46 changes: 46 additions & 0 deletions runtime/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Builtin {
Str,
Int,
Float,
Bool,

Floor,
Ceil,
Expand All @@ -24,6 +25,7 @@ pub enum Builtin {
Split,

Push,
Pop,
}

impl Display for Builtin {
Expand All @@ -33,6 +35,7 @@ impl Display for Builtin {
Builtin::Str => write!(f, "str"),
Builtin::Int => write!(f, "int"),
Builtin::Float => write!(f, "float"),
Builtin::Bool => write!(f, "bool"),
Builtin::Floor => write!(f, "floor"),
Builtin::Ceil => write!(f, "ceil"),
Builtin::Round => write!(f, "round"),
Expand All @@ -41,6 +44,7 @@ impl Display for Builtin {
Builtin::Trim => write!(f, "trim"),
Builtin::Split => write!(f, "split"),
Builtin::Push => write!(f, "push"),
Builtin::Pop => write!(f, "pop"),
}
}
}
Expand All @@ -52,6 +56,7 @@ impl Builtin {
"str" => Self::Str,
"int" => Self::Int,
"float" => Self::Float,
"bool" => Self::Bool,
"floor" => Self::Floor,
"ceil" => Self::Ceil,
"round" => Self::Round,
Expand All @@ -60,6 +65,7 @@ impl Builtin {
"trim" => Self::Trim,
"split" => Self::Split,
"push" => Self::Push,
"pop" => Self::Pop,

_ => return None,
};
Expand All @@ -74,6 +80,7 @@ impl Builtin {
Builtin::Str => call_str(args),
Builtin::Int => call_int(args),
Builtin::Float => call_float(args),
Builtin::Bool => call_bool(args),

Builtin::Floor => call_round(args, |f| f.floor(), Builtin::Floor),
Builtin::Ceil => call_round(args, |f| f.ceil(), Builtin::Ceil),
Expand All @@ -87,6 +94,7 @@ impl Builtin {
Builtin::Split => call_split(args, gc),

Builtin::Push => call_push(args),
Builtin::Pop => call_pop(args),
}
}
}
Expand Down Expand Up @@ -185,6 +193,26 @@ fn call_float(args: &[Object]) -> Result<Object, ErrorKind> {
Ok(Object::Float(res))
}

fn call_bool(args: &[Object]) -> Result<Object, ErrorKind> {
validate_args_len(args, 1)?;

let res = match &args[0] {
Object::String(str) => match str.parse() {
Ok(res) => res,
Err(_) => return Ok(Object::Null),
},

obj => {
return Err(ErrorKind::InvalidBuiltinArg {
builtin: Builtin::Bool,
data_type: obj.into(),
})
}
};

Ok(Object::Boolean(res))
}

fn call_round<F>(args: &[Object], round: F, builtin: Builtin) -> Result<Object, ErrorKind>
where
F: Fn(f64) -> f64,
Expand Down Expand Up @@ -263,3 +291,21 @@ fn call_push(args: &[Object]) -> Result<Object, ErrorKind> {

Ok(Object::Null)
}

fn call_pop(args: &[Object]) -> Result<Object, ErrorKind> {
validate_args_len(args, 1)?;

let Object::Array(Array(arr)) = &args[0] else {
return Err(ErrorKind::InvalidBuiltinArg {
builtin: Builtin::Pop,
data_type: (&args[0]).into(),
});
};

let rc = arr.value.upgrade().unwrap();
let obj = rc.borrow_mut().pop();
match obj {
Some(obj) => Ok(obj),
None => Ok(Object::Null),
}
}
4 changes: 2 additions & 2 deletions runtime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ impl Display for ErrorKind {
),
ErrorKind::ReturnOutsideOfFunction => write!(f, "Return can't be used outside of a function."),
ErrorKind::InvalidFunctionCalee(dt) => write!(f,"Can only call functions, not {dt}"),
ErrorKind::InvalidNrOfArgs { expected, got } =>write!(f, "Invalid number of arguments, expected: {expected}, got: {got}"),
ErrorKind::InvalidNrOfArgs { expected, got } => write!(f, "Invalid number of arguments, expected: {expected}, got: {got}"),

ErrorKind::InvalidBuiltinArg { builtin, data_type } =>write!(f, "Can't call {builtin} on {data_type}."),
ErrorKind::InvalidBuiltinArg { builtin, data_type } => write!(f, "Can't call {builtin} on {data_type}."),
}
}
}
8 changes: 1 addition & 7 deletions runtime/src/vm/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,12 @@ impl Debug for Owner {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct GarbageCollector {
owners: HashMap<usize, Owner>,
}

impl GarbageCollector {
pub fn new() -> Self {
Self {
owners: HashMap::new(),
}
}

pub fn should_free(&self) -> bool {
self.owners.len() > MAX_OBJECTS
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct VirtualMachine {
impl VirtualMachine {
pub fn new() -> Self {
Self {
gc: GarbageCollector::new(),
gc: GarbageCollector::default(),
globals: vec![Object::Null; GLOBALS_SIZE],
frames: vec![],
stack: vec![Object::Null; STACK_SIZE],
Expand Down
41 changes: 41 additions & 0 deletions runtime/src/vm/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ fn builtin_float() {
}
}

#[test]
fn builtin_bool() {
let tests = [
("bool(\"true\")", Object::Boolean(true)),
("bool(\"false\")", Object::Boolean(false)),
];

for (input, expected) in tests {
run_test(input, Ok(expected));
}
}

#[test]
fn builtin_string() {
let tests = [
Expand Down Expand Up @@ -711,6 +723,35 @@ fn builtin_push() {
}
}

#[test]
fn builtin_pop() {
let tests = [
("pop([])", Object::Null),
("pop([1,2,3])", Object::Integer(3)),
];

for (input, expected) in tests {
run_test(input, Ok(expected));
}

let tests = [
("a = []\npop(a)\na", vec![]),
(
"a = [1,2,3]\npop(a)\na",
vec![Object::Integer(1), Object::Integer(2)],
),
];

for (input, expected) in tests {
let rc = Rc::new(RefCell::new(expected));
let arr = Array(gc::Ref {
value: Rc::downgrade(&rc),
id: 0,
});
run_test(input, Ok(Object::Array(arr)));
}
}

#[test]
fn use_statement() {
let tests = [
Expand Down

0 comments on commit fe09a74

Please sign in to comment.