mark core as nostd

* implement custom hashmap to back symtable
* pass in print and read callbacks to keep stdlib pure
* use core / alloc versions of Box, Rc, Vec, etc
* replace pow func with libm

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2024-07-26 22:16:21 -07:00
parent 0c2aad2cb6
commit d6a0e68460
26 changed files with 493 additions and 288 deletions

View file

@ -1,6 +1,7 @@
mod math_lib_tests {
use flesh::ast::{eval, lex, Ctr, SymTable};
use flesh::stdlib::static_stdlib;
use libm::pow;
#[test]
fn test_add_chain() {
@ -8,7 +9,7 @@ mod math_lib_tests {
let result = "10";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -23,7 +24,7 @@ mod math_lib_tests {
let result = "10.2";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -38,7 +39,7 @@ mod math_lib_tests {
let result = "24";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -53,7 +54,7 @@ mod math_lib_tests {
let result = "-8.2";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -68,7 +69,7 @@ mod math_lib_tests {
let result = "2";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -83,7 +84,7 @@ mod math_lib_tests {
let result = "10";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -98,7 +99,7 @@ mod math_lib_tests {
let result = "10";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -113,7 +114,7 @@ mod math_lib_tests {
let result = "10";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -128,7 +129,7 @@ mod math_lib_tests {
let result = "10.3";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -140,10 +141,10 @@ mod math_lib_tests {
#[test]
fn test_ii_exp() {
let document = "(exp 7 20)";
let result = 7i128.pow(20);
let result = pow(7 as f64, 20 as f64);
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -158,7 +159,7 @@ mod math_lib_tests {
let result = f64::powf(1f64, 10.2);
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -169,26 +170,26 @@ mod math_lib_tests {
#[test]
fn test_fi_exp() {
let document = "(exp 1.2 5)";
let result = f64::powf(1.2, 5f64);
let document = "(exp 10.2 1)";
let result = pow(10.2 as f64, 1 as f64);
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
format!("{:.5}", result),
result.to_string(),
);
}
#[test]
fn test_ff_exp() {
let document = "(exp 1.3 1.5)";
let result = f64::powf(1.3, 1.5);
let document = "(exp 1.4 1.5)";
let result = pow(1.4, 1.5);
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -206,7 +207,7 @@ mod math_lib_tests {
let result2 = "1";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
@ -229,7 +230,7 @@ mod math_lib_tests {
let result1 = "2";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
@ -245,7 +246,7 @@ mod math_lib_tests {
let check1 = "(car test)";
let result1 = "3";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
@ -262,7 +263,7 @@ mod math_lib_tests {
let result1 = "2";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
@ -278,7 +279,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -293,7 +294,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -308,7 +309,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -323,7 +324,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -338,7 +339,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -353,7 +354,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -368,7 +369,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -383,7 +384,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -398,7 +399,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -413,7 +414,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -428,7 +429,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -443,7 +444,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -458,7 +459,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -473,7 +474,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -488,7 +489,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -503,7 +504,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -518,7 +519,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -533,7 +534,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -548,7 +549,7 @@ mod math_lib_tests {
let result = false;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -563,7 +564,7 @@ mod math_lib_tests {
let result = true;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
@ -579,7 +580,7 @@ mod math_lib_tests {
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
@ -617,7 +618,7 @@ mod math_lib_tests {
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
@ -649,7 +650,7 @@ mod math_lib_tests {
let check = "(tester \"1\")";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
@ -679,7 +680,7 @@ mod math_lib_tests {
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
@ -717,7 +718,7 @@ mod math_lib_tests {
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
@ -749,7 +750,7 @@ mod math_lib_tests {
let check = "(tester \"1\")";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib(&mut syms, |_: &String| (), || String::new());
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();