Skip to content

Commit

Permalink
feat: cast to number
Browse files Browse the repository at this point in the history
  • Loading branch information
psiayn committed Dec 30, 2023
1 parent de5dc86 commit 3296d84
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl Env {
global.insert("'".to_string(), ExprKind::Func(eval::list).into());
global.insert("map".to_string(), ExprKind::Func(eval::map).into());
global.insert("append".to_string(), ExprKind::Func(eval::append).into());
global.insert("number".to_string(), ExprKind::Func(eval::cast_as_num).into());

let mut scope_slab = Slab::new();

Expand Down
14 changes: 12 additions & 2 deletions src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod logical;
mod loops;
mod number;
mod relational;
mod types;

use std::io;

Expand All @@ -15,6 +16,7 @@ pub use logical::*;
pub use loops::*;
pub use number::*;
pub use relational::*;
pub use types::*;

use crate::{
ast::{Atom, Expr, ExprKind},
Expand All @@ -41,6 +43,12 @@ pub fn execute(exprs: &mut Vec<Expr>, env: &mut Env) -> Result<Expr, SpressoErro
exprs[0] = value?;
execute(exprs, env)
}
ExprKind::Atom(Atom::String(_)) => {
Ok(first_arg)
}
ExprKind::Atom(Atom::Number(_)) => {
Ok(first_arg)
}
ExprKind::Lambda(lambda) => execute_lambda(lambda, exprs[1..].to_vec(), env),
_ => Err(SpressoError::from(RuntimeError::from(format!(
"this is not something I can execute: {}",
Expand Down Expand Up @@ -87,8 +95,10 @@ pub fn print(args: Vec<Expr>, env: &mut Env) -> Result<Expr, SpressoError> {
Ok(Expr::from(ExprKind::Atom(Atom::Unit)))
}

pub fn input(args: Vec<Expr>, env: &mut Env) -> Result<Expr, SpressoError> {
print(args, env)?;
pub fn input(_args: Vec<Expr>, _env: &mut Env) -> Result<Expr, SpressoError> {
if _args.len() > 0 {
print(_args, _env)?;
}
let mut buffer = String::new();
if let Err(err) = io::stdin().read_line(&mut buffer) {
return Err(SpressoError::from(RuntimeError::from(format!("{}", err))));
Expand Down
23 changes: 23 additions & 0 deletions src/eval/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ fn number_op(
.maybe_with_tokens(tokens))
}


pub fn extract_num(expr: Expr, env: &mut Env) -> Result<Number, SpressoError> {
match expr.kind {
ExprKind::Atom(Atom::Number(number)) => Ok(number),
ExprKind::Atom(Atom::String(str)) => {
if let Ok(num) = str.parse::<i64>() {
Ok(Number::Int(num))
} else if let Ok(num) = str.parse::<f64>() {
Ok(Number::Float(num))
} else {
Err(SpressoError::from(NumericError {
err: "Tried to extract number from string but failed".to_string(),
}))
}
},
ExprKind::Atom(Atom::Symbol(ref symbol)) => {
if env.contains_key(symbol.as_str()) {
let sym = env[symbol.as_str()].clone();
Expand All @@ -48,6 +60,17 @@ pub fn extract_num(expr: Expr, env: &mut Env) -> Result<Number, SpressoError> {
let res = execute(&mut exprs, env)?;
match res.kind {
ExprKind::Atom(Atom::Number(num)) => Ok(num),
ExprKind::Atom(Atom::String(str)) => {
if let Ok(num) = str.parse::<i64>() {
Ok(Number::Int(num))
} else if let Ok(num) = str.parse::<f64>() {
Ok(Number::Float(num))
} else {
Err(SpressoError::from(NumericError {
err: "Tried to extract number from string but failed".to_string(),
}))
}
}
_ => Err(SpressoError::from(NumericError::from(format!(
"trying to perform arithmetic on non-number: {}",
res
Expand Down
18 changes: 18 additions & 0 deletions src/eval/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{
ast::{Expr, ExprKind, Atom},
env::Env,
errors::{SpressoError, RuntimeError},
TokenGiver,
TokenHoarder
};

use super::extract_num;

pub fn cast_as_num(args: Vec<Expr>, env: &mut Env) -> Result<Expr, SpressoError> {
if args.len() != 1 {
return Err(SpressoError::from(RuntimeError::from(
"number needs an expression to cast into a number",
)).maybe_with_tokens(args.get_tokens()));
}
return Ok(Expr::from(ExprKind::Atom(Atom::Number(extract_num(args[0].clone(), env)?))));
}

0 comments on commit 3296d84

Please sign in to comment.