STL rework

- fixed lots of bugs
- overlay libraries work correctly and reliably
- the STL is an overlay library
- examples updated
This commit is contained in:
2023-06-17 21:12:23 +01:00
parent 5bb8a12fc2
commit aebbf51228
91 changed files with 1444 additions and 1395 deletions

39
src/stl/str.rs Normal file
View File

@@ -0,0 +1,39 @@
use super::litconv::{with_str, with_uint};
use super::RuntimeError;
use crate::interner::Interner;
use crate::pipeline::ConstTree;
use crate::{define_fn, Literal};
define_fn! {expr=x in
/// Append a string to another
pub Concatenate {
a: String as with_str(x, |s| Ok(s.clone())),
b: String as with_str(x, |s| Ok(s.clone()))
} => Ok(Literal::Str(a.to_owned() + b).into())
}
define_fn! {expr=x in
pub CharAt {
s: String as with_str(x, |s| Ok(s.clone())),
i: u64 as with_uint(x, Ok)
} => {
if let Some(c) = s.chars().nth(*i as usize) {
Ok(Literal::Char(c).into())
} else {
RuntimeError::fail(
"Character index out of bounds".to_string(),
"indexing string",
)?
}
}
}
pub fn str(i: &Interner) -> ConstTree {
ConstTree::tree([(
i.i("str"),
ConstTree::tree([
(i.i("concatenate"), ConstTree::xfn(Concatenate)),
(i.i("char_at"), ConstTree::xfn(CharAt)),
]),
)])
}