Compare commits
10 commits
04a470d771
...
4e7e050bf6
Author | SHA1 | Date | |
---|---|---|---|
![]() |
4e7e050bf6 | ||
![]() |
f0dfa0c238 | ||
![]() |
fa078e8d3f | ||
![]() |
5340d5906f | ||
![]() |
2489bf56dc | ||
![]() |
c3abe2fe48 | ||
![]() |
b4a393cce7 | ||
![]() |
f630d138cb | ||
![]() |
5d30ca51ed | ||
![]() |
b01eda5e0e |
4 changed files with 84 additions and 6 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -88,7 +88,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "crisp"
|
||||
version = "0.1.0"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"miette",
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
[package]
|
||||
name = "crisp"
|
||||
version = "0.1.0"
|
||||
version = "0.1.3"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
name = "crisp"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
|
|
|
@ -11,7 +11,7 @@ pub mod reader;
|
|||
pub mod types;
|
||||
use types::*;
|
||||
|
||||
pub fn main() -> Result<()> {
|
||||
fn main() -> Result<()> {
|
||||
let mut rl = Editor::<(), rustyline::history::DefaultHistory>::new().unwrap();
|
||||
if rl.load_history(".mal-history").is_err() {
|
||||
eprintln!("No previous history.");
|
||||
|
|
82
src/types.rs
82
src/types.rs
|
@ -1,4 +1,4 @@
|
|||
use color_eyre::{eyre::eyre, Result};
|
||||
use miette::{miette, Result};
|
||||
use std::{num::ParseIntError, rc::Rc};
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
|
@ -46,7 +46,14 @@ impl Value {
|
|||
pub fn apply(&self, args: LispArgs) -> Result<Value> {
|
||||
match self {
|
||||
Self::Function(f, _) => f(args),
|
||||
_ => Err(eyre!("No function here, shouldn't call apply")),
|
||||
_ => Err(miette!("No function here, shouldn't call apply")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_list(&self) -> bool {
|
||||
match self {
|
||||
Self::List(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -100,6 +107,64 @@ impl From<&i64> for Value {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Value> for String {
|
||||
fn from(value: Value) -> Self {
|
||||
match value {
|
||||
Value::String(str) => str,
|
||||
Value::Symbol(Symbol(str)) => str,
|
||||
Value::Keyword(Keyword(str)) => str,
|
||||
_ => String::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Value> for String {
|
||||
fn from(value: &Value) -> Self {
|
||||
match value {
|
||||
Value::String(str) => str.clone(),
|
||||
Value::Symbol(Symbol(str)) => str.clone(),
|
||||
Value::Keyword(Keyword(str)) => str.clone(),
|
||||
_ => String::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Value> for i32 {
|
||||
fn from(value: Value) -> Self {
|
||||
match value {
|
||||
Value::Number(num) => num as i32,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Value> for i64 {
|
||||
fn from(value: Value) -> Self {
|
||||
match value {
|
||||
Value::Number(num) => num,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Value> for i32 {
|
||||
fn from(value: &Value) -> Self {
|
||||
match value {
|
||||
Value::Number(num) => *num as i32,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Value> for i64 {
|
||||
fn from(value: &Value) -> Self {
|
||||
match value {
|
||||
Value::Number(num) => *num,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Symbol(pub String);
|
||||
|
||||
|
@ -129,3 +194,16 @@ impl std::fmt::Display for Keyword {
|
|||
std::fmt::Display::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_conversions() {
|
||||
let value = Value::Symbol(Symbol("c1".to_owned()));
|
||||
let string = String::from("c1");
|
||||
let convert = String::from(value);
|
||||
assert_eq!(string, convert);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue