Skip to content

Commit

Permalink
add print function
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Aug 4, 2024
1 parent 30e318c commit 737bf64
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
10 changes: 6 additions & 4 deletions crates/interpreter/src/eval/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod last;
mod len;
mod print;
mod push;
mod rest;

Expand All @@ -13,10 +14,11 @@ pub struct BuiltInFunctionObject {

pub(crate) fn lookup_builtins(identifier: &str) -> Option<Object> {
match identifier {
"len" => create_builtin("len", len::len),
"last" => create_builtin("last", last::last),
"rest" => create_builtin("rest", rest::rest),
"push" => create_builtin("push", push::push),
print::FN_NAME => create_builtin(print::FN_NAME, print::print),
len::FN_NAME => create_builtin(len::FN_NAME, len::len),
last::FN_NAME => create_builtin(last::FN_NAME, last::last),
rest::FN_NAME => create_builtin(rest::FN_NAME, rest::rest),
push::FN_NAME => create_builtin(push::FN_NAME, push::push),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/eval/builtin/last.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::eval::{eval_error::EvalError, objects::Object};

const FN_NAME: &str = "last";
pub const FN_NAME: &str = "last";
pub fn last(args: &[Object]) -> Result<Object, EvalError> {
if args.len() != 1 {
return Err(EvalError::BuiltInInvalidArguments(
Expand Down
5 changes: 3 additions & 2 deletions crates/interpreter/src/eval/builtin/len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::eval::{
objects::{Object, PrimitiveObject},
};

pub const FN_NAME: &str = "len";
pub fn len(args: &[Object]) -> Result<Object, EvalError> {
if args.len() != 1 {
return Err(EvalError::BuiltInInvalidArguments(
"len".to_string(),
FN_NAME.to_owned(),
format!("Expected 1 argument, got {}", args.len()),
));
}
Expand All @@ -23,7 +24,7 @@ pub fn len(args: &[Object]) -> Result<Object, EvalError> {
Ok(Object::primitive_from_int(string.len() as i32))
}
_ => Err(EvalError::BuiltInInvalidArguments(
"len".to_string(),
FN_NAME.to_owned(),
"Expected a string or array".to_string(),
)),
}
Expand Down
10 changes: 10 additions & 0 deletions crates/interpreter/src/eval/builtin/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::eval::{eval_error::EvalError, objects::Object};

pub const FN_NAME: &str = "print";
pub fn print(args: &[Object]) -> Result<Object, EvalError> {
for arg in args {
println!("{}", arg);
}

Ok(Object::Void)
}
2 changes: 1 addition & 1 deletion crates/interpreter/src/eval/builtin/push.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::eval::{eval_error::EvalError, objects::Object};

const FN_NAME: &str = "push";
pub const FN_NAME: &str = "push";
pub fn push(args: &[Object]) -> Result<Object, EvalError> {
if args.len() != 2 {
return Err(EvalError::BuiltInInvalidArguments(
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/eval/builtin/rest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::eval::{eval_error::EvalError, objects::Object};

const FN_NAME: &str = "rest";
pub const FN_NAME: &str = "rest";
pub fn rest(args: &[Object]) -> Result<Object, EvalError> {
if args.len() != 1 {
return Err(EvalError::BuiltInInvalidArguments(
Expand Down
20 changes: 19 additions & 1 deletion crates/interpreter/tests/builtins.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use interpreter::eval::{self, eval_error::EvalError, objects::Environment, EvaledProgram};
use interpreter::eval::{
self,
eval_error::EvalError,
objects::{Environment, Object},
EvaledProgram,
};

mod common;

Expand Down Expand Up @@ -106,3 +111,16 @@ fn len_error_test() {
}
}
}

#[test]
fn print_prints() {
let input = r#"print("Hello world")"#;

let scope = &mut Environment::new_env_reference();
let object = eval::eval(input, scope);

match object {
EvaledProgram::Valid(Object::Void) => {}
_ => panic!("Expected valid program"),
}
}

0 comments on commit 737bf64

Please sign in to comment.