forked from Orchid/orchid
bug fixes and performance improvements
This commit is contained in:
33
src/external/str/char_at.rs
vendored
33
src/external/str/char_at.rs
vendored
@@ -1,11 +1,10 @@
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::external::assertion_error::AssertionError;
|
||||
use crate::external::litconv::{with_str, with_uint};
|
||||
use crate::external::runtime_error::RuntimeError;
|
||||
use crate::representations::{Literal, Primitive};
|
||||
use crate::{atomic_impl, atomic_redirect, externfn_impl};
|
||||
use crate::representations::interpreted::Clause;
|
||||
use crate::representations::interpreted::{Clause, ExprInst};
|
||||
|
||||
/// CharAt function
|
||||
///
|
||||
@@ -13,35 +12,31 @@ use crate::representations::interpreted::Clause;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CharAt2;
|
||||
externfn_impl!(CharAt2, |_: &Self, c: Clause| {Ok(CharAt1{c})});
|
||||
externfn_impl!(CharAt2, |_: &Self, x: ExprInst| {Ok(CharAt1{x})});
|
||||
|
||||
/// Partially applied CharAt function
|
||||
///
|
||||
/// Prev state: [CharAt2]; Next state: [CharAt0]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct CharAt1{ c: Clause }
|
||||
atomic_redirect!(CharAt1, c);
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CharAt1{ x: ExprInst }
|
||||
atomic_redirect!(CharAt1, x);
|
||||
atomic_impl!(CharAt1);
|
||||
externfn_impl!(CharAt1, |this: &Self, c: Clause| {
|
||||
let s = if let Ok(Literal::Str(s)) = this.c.clone().try_into() {s}
|
||||
else {AssertionError::fail(this.c.clone(), "a string")?};
|
||||
Ok(CharAt0{ s, c })
|
||||
externfn_impl!(CharAt1, |this: &Self, x: ExprInst| {
|
||||
with_str(&this.x, |s| Ok(CharAt0{ s: s.clone(), x }))
|
||||
});
|
||||
|
||||
/// Fully applied CharAt function.
|
||||
///
|
||||
/// Prev state: [CharAt1]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct CharAt0 { s: String, c: Clause }
|
||||
atomic_redirect!(CharAt0, c);
|
||||
atomic_impl!(CharAt0, |Self{ s, c }: &Self| {
|
||||
let i = if let Ok(Literal::Uint(i)) = c.clone().try_into() {i}
|
||||
else {AssertionError::fail(c.clone(), "an uint")?};
|
||||
if let Some(c) = s.chars().nth(i as usize) {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CharAt0 { s: String, x: ExprInst }
|
||||
atomic_redirect!(CharAt0, x);
|
||||
atomic_impl!(CharAt0, |Self{ s, x }: &Self| {
|
||||
with_uint(x, |i| if let Some(c) = s.chars().nth(i as usize) {
|
||||
Ok(Clause::P(Primitive::Literal(Literal::Char(c))))
|
||||
} else {
|
||||
RuntimeError::fail("Character index out of bounds".to_string(), "indexing string")?
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user