This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added puts function and formatted project
- Loading branch information
Showing
10 changed files
with
1,337 additions
and
1,148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,94 @@ | ||
use crate::parser::ast::*; | ||
use crate::evaluate::object::*; | ||
use crate::parser::ast::*; | ||
|
||
pub type BuiltinFunction = fn(Vec<Object>) -> Result<Object, String>; | ||
|
||
pub fn get_builtin_functions() -> Vec<(Identifier, Object)>{ | ||
vec![ | ||
create_builtin("len", 1, builtin_len), | ||
create_builtin("first", 1, builtin_first), | ||
create_builtin("last", 1, builtin_last), | ||
create_builtin("rest", 1, builtin_rest), | ||
] | ||
pub fn get_builtin_functions() -> Vec<(Identifier, Object)> { | ||
vec![ | ||
create_builtin("len", 1, builtin_len), | ||
create_builtin("first", 1, builtin_first), | ||
create_builtin("last", 1, builtin_last), | ||
create_builtin("rest", 1, builtin_rest), | ||
create_builtin("puts", -1, builtin_puts), | ||
] | ||
} | ||
|
||
fn create_builtin(name: &str, param_count: i32, function: BuiltinFunction) -> (Identifier, Object) { | ||
( | ||
name.to_string(), | ||
Object::Builtin(name.to_string(), param_count, function), | ||
) | ||
} | ||
|
||
fn create_builtin(name: &str, param_count: usize, function: BuiltinFunction) ->(Identifier, Object){ | ||
( | ||
name.to_string(), | ||
Object::Builtin(name.to_string(), param_count, function) | ||
) | ||
fn builtin_len(input: Vec<Object>) -> Result<Object, String> { | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => Ok(Object::Integer(elem.len() as i32)), | ||
Object::String(s) => Ok(Object::Integer(s.len() as i32)), | ||
_ => Err(format!( | ||
"argument to 'len' not supported, got {}", | ||
input_object.type_string() | ||
)), | ||
} | ||
} | ||
|
||
fn builtin_len(input: Vec<Object>) -> Result<Object, String>{ | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => Ok(Object::Integer(elem.len() as i32)), | ||
Object::String(s) => Ok(Object::Integer(s.len() as i32)), | ||
_ => Err(format!("argument to 'len' not supported, got {}", input_object.type_string())), | ||
} | ||
fn builtin_first(input: Vec<Object>) -> Result<Object, String> { | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => { | ||
if elem.len() > 0 { | ||
Ok(elem[0].clone()) | ||
} else { | ||
Ok(Object::Null) | ||
} | ||
} | ||
_ => Err(format!( | ||
"argument to 'first' not supported, got {}", | ||
input_object.type_string() | ||
)), | ||
} | ||
} | ||
|
||
fn builtin_first(input: Vec<Object>) -> Result<Object, String>{ | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => { | ||
if elem.len() > 0{ | ||
Ok(elem[0].clone()) | ||
}else{ | ||
Ok(Object::Null) | ||
} | ||
}, | ||
_ => Err(format!("argument to 'first' not supported, got {}", input_object.type_string())), | ||
} | ||
fn builtin_last(input: Vec<Object>) -> Result<Object, String> { | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => { | ||
if elem.len() > 0 { | ||
Ok(elem[elem.len() - 1].clone()) | ||
} else { | ||
Ok(Object::Null) | ||
} | ||
} | ||
_ => Err(format!( | ||
"argument to 'last' not supported, got {}", | ||
input_object.type_string() | ||
)), | ||
} | ||
} | ||
|
||
fn builtin_last(input: Vec<Object>) -> Result<Object, String>{ | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => { | ||
if elem.len() > 0{ | ||
Ok(elem[elem.len()-1].clone()) | ||
}else{ | ||
Ok(Object::Null) | ||
} | ||
}, | ||
_ => Err(format!("argument to 'last' not supported, got {}", input_object.type_string())), | ||
} | ||
fn builtin_rest(input: Vec<Object>) -> Result<Object, String> { | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => { | ||
if elem.len() > 0 { | ||
let mut new_elem = elem.clone(); | ||
new_elem.remove(0); | ||
Ok(Object::Array(new_elem)) | ||
} else { | ||
Ok(Object::Null) | ||
} | ||
} | ||
_ => Err(format!( | ||
"argument to 'rest' not supported, got {}", | ||
input_object.type_string() | ||
)), | ||
} | ||
} | ||
|
||
fn builtin_rest(input: Vec<Object>) -> Result<Object, String>{ | ||
let input_object = &input[0]; | ||
match input_object { | ||
Object::Array(elem) => { | ||
if elem.len() > 0{ | ||
let mut new_elem = elem.clone(); | ||
new_elem.remove(0); | ||
Ok(Object::Array(new_elem)) | ||
}else{ | ||
Ok(Object::Null) | ||
} | ||
}, | ||
_ => Err(format!("argument to 'rest' not supported, got {}", input_object.type_string())), | ||
} | ||
} | ||
fn builtin_puts(input: Vec<Object>) -> Result<Object, String> { | ||
for object in input.iter() { | ||
println!("{}", object.inspect()); | ||
} | ||
|
||
Ok(Object::Null) | ||
} |
Oops, something went wrong.