add basic arithmatic operations

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-03-06 19:05:34 -08:00
parent f8ab31e9aa
commit a429b546d0
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
5 changed files with 364 additions and 6 deletions

View file

@ -16,7 +16,7 @@
*/
use std::fmt;
use std::marker::PhantomData;
use std::ops::Index;
use std::ops::{Add, Sub, Mul, Div, Index};
// Container
#[derive(Debug, Default)]
@ -268,6 +268,108 @@ impl PartialEq for Ctr {
}
}
impl Add<Ctr> for Ctr {
type Output = Ctr;
fn add(self, rh: Ctr) -> Ctr {
match self {
Ctr::Float(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn + rhn),
Ctr::Integer(rhn) => Ctr::Float(lhn + rhn as f64),
_ => unimplemented!("non-numeral on right hand side of add"),
},
Ctr::Integer(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn as f64 + rhn),
Ctr::Integer(rhn) => Ctr::Integer(lhn + rhn),
_ => unimplemented!("non-numeral on right hand side of add"),
},
Ctr::String(lhs) => match rh {
Ctr::String(rhs) => Ctr::String(lhs + &rhs),
_ => unimplemented!("add to string only implemented for other strings"),
},
_ => {
unimplemented!("datum does not support add")
}
}
}
}
impl Sub<Ctr> for Ctr {
type Output = Ctr;
fn sub(self, rh: Ctr) -> Ctr {
match self {
Ctr::Float(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn - rhn),
Ctr::Integer(rhn) => Ctr::Float(lhn - rhn as f64),
_ => unimplemented!("non-numeral on right hand side of sub"),
},
Ctr::Integer(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn as f64 - rhn),
Ctr::Integer(rhn) => Ctr::Integer(lhn - rhn),
_ => unimplemented!("non-numeral on right hand side of sub"),
},
_ => {
unimplemented!("datum does not support sub")
}
}
}
}
impl Mul<Ctr> for Ctr {
type Output = Ctr;
fn mul(self, rh: Ctr) -> Ctr {
match self {
Ctr::Float(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn * rhn),
Ctr::Integer(rhn) => Ctr::Float(lhn * rhn as f64),
_ => unimplemented!("non-numeral on right hand side of mul"),
},
Ctr::Integer(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn as f64 * rhn),
Ctr::Integer(rhn) => Ctr::Integer(lhn * rhn),
_ => unimplemented!("non-numeral on right hand side of mul"),
},
_ => {
unimplemented!("datum does not support mul")
}
}
}
}
impl Div<Ctr> for Ctr {
type Output = Ctr;
fn div(self, rh: Ctr) -> Ctr {
match self {
Ctr::Float(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn / rhn),
Ctr::Integer(rhn) => Ctr::Float(lhn / rhn as f64),
_ => unimplemented!("non-numeral on right hand side of div"),
},
Ctr::Integer(lhn) => match rh {
Ctr::Float(rhn) => Ctr::Float(lhn as f64 / rhn),
Ctr::Integer(rhn) => Ctr::Integer(lhn / rhn),
_ => unimplemented!("non-numeral on right hand side of div"),
},
_ => {
unimplemented!("datum does not support div")
}
}
}
}
impl fmt::Display for Seg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", seg_to_string(self, true))

View file

@ -23,6 +23,7 @@ pub mod append;
pub mod boolean;
pub mod control;
pub mod decl;
pub mod math;
//pub mod str;
/// static_stdlib
@ -183,6 +184,50 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
},
);
syms.insert(
"add".to_string(),
Symbol {
name: String::from("add"),
args: Args::Infinite,
conditional_branches: false,
docs: math::ADD_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::add_callback)),
},
);
syms.insert(
"sub".to_string(),
Symbol {
name: String::from("sub"),
args: Args::Infinite,
conditional_branches: false,
docs: math::SUB_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::sub_callback)),
},
);
syms.insert(
"div".to_string(),
Symbol {
name: String::from("div"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::DIV_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::div_callback)),
},
);
syms.insert(
"mul".to_string(),
Symbol {
name: String::from("mul"),
args: Args::Infinite,
conditional_branches: false,
docs: math::MUL_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::mul_callback)),
},
);
Ok(())
}

127
src/stl/math.rs Normal file
View file

@ -0,0 +1,127 @@
/* Copyright (C) 2021 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::segment::{Ctr, Seg};
use crate::sym::SymTable;
fn isnumeric(arg: &Ctr) -> bool {
match arg {
Ctr::Integer(_) => true,
Ctr::Float(_) => true,
_ => false,
}
}
pub const ADD_DOCSTRING: &str = "traverses over N args, which must all evaluate to an Integer or Float.
Adds each arg up to a final result. WARNING: does not acocunt for under/overflows.
Consult source code for a better understanding of how extreme values will be handled.";
pub fn add_callback(ast: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
let mut res = Ctr::Integer(0);
let mut culprit: Ctr = Ctr::None;
let type_consistent = ast.circuit(&mut |c: &Ctr| -> bool {
if !isnumeric(c) {
culprit = c.clone();
false
} else {
res = res.clone() + c.clone();
true
}
});
if !type_consistent {
Err(format!("{} is not a number!", culprit))
} else {
Ok(res)
}
}
pub const SUB_DOCSTRING: &str = "traverses over N args, which must all evaluate to an Integer or Float.
Subtracts each arg from the first leading to a final result. WARNING: does not acocunt for under/overflows.
Consult source code for a better understanding of how extreme values will be handled.";
pub fn sub_callback(ast: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
if !isnumeric(&*ast.car) {
return Err(format!("{} is not a number", &*ast.car))
}
let mut res = *ast.car.clone();
let mut culprit: Ctr = Ctr::None;
if let Ctr::Seg(ref subsequent_operands) = *ast.cdr {
let type_consistent = subsequent_operands.circuit(&mut |c: &Ctr| -> bool {
if !isnumeric(c) {
culprit = c.clone();
false
} else {
res = res.clone() - c.clone();
true
}
});
if !type_consistent {
Err(format!("{} is not a number", culprit))
} else {
Ok(res)
}
} else {
Err("requires at least two operands".to_string())
}
}
pub const DIV_DOCSTRING: &str = "takes two args, which must both evaluate to an Integer or Float.
divides arg1 by arg2. WARNING: does not acocunt for under/overflows or float precision.
Consult source code for a better understanding of how extreme values will be handled.";
pub fn div_callback(ast: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
let first = *ast.car.clone();
if !isnumeric(&first) {
return Err("first argument must be numeric".to_string());
}
let second: Ctr;
if let Ctr::Seg(ref s) = *ast.cdr {
second = *s.car.clone();
if !isnumeric(&second) {
return Err("second argument must be numeric".to_string());
}
Ok(first / second)
} else {
Err("impossible error: needs two arguments".to_string())
}
}
pub const MUL_DOCSTRING: &str = "traverses over N args, which must all evaluate to an Integer or Float.
Multiplies each arg up to a final result. WARNING: does not acocunt for under/overflows.
Consult source code for a better understanding of how extreme values will be handled.";
pub fn mul_callback(ast: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
let mut res = Ctr::Integer(1);
let mut culprit: Ctr = Ctr::None;
let type_consistent = ast.circuit(&mut |c: &Ctr| -> bool {
if !isnumeric(c) {
culprit = c.clone();
false
} else {
res = res.clone() * c.clone();
true
}
});
if !type_consistent {
Err(format!("{} is not a number!", culprit))
} else {
Ok(res)
}
}