write_fn_step convenience macro

This commit is contained in:
2023-06-01 19:10:07 +01:00
parent 6e545be8a7
commit 01aa37b27d
9 changed files with 154 additions and 39 deletions

View File

@@ -1,38 +1,17 @@
use std::fmt::Debug;
use super::super::litconv::{with_str, with_uint};
use super::super::runtime_error::RuntimeError;
use crate::representations::interpreted::{Clause, ExprInst};
use crate::representations::{Literal, Primitive};
use crate::{atomic_impl, atomic_redirect, externfn_impl};
use crate::interpreted::Clause;
use crate::{write_fn_step, Literal, Primitive};
/// Takes an uint and a string, finds the char in a string at a 0-based index
///
/// Next state: [CharAt1]
#[derive(Clone)]
pub struct CharAt2;
externfn_impl!(CharAt2, |_: &Self, x: ExprInst| Ok(CharAt1 { x }));
/// Prev state: [CharAt2]; Next state: [CharAt0]
#[derive(Debug, Clone)]
pub struct CharAt1 {
x: ExprInst,
}
atomic_redirect!(CharAt1, x);
atomic_impl!(CharAt1);
externfn_impl!(CharAt1, |this: &Self, x: ExprInst| {
with_str(&this.x, |s| Ok(CharAt0 { s: s.clone(), x }))
});
/// Prev state: [CharAt1]
#[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| {
write_fn_step!(pub CharAt2 > CharAt1);
write_fn_step!(
CharAt1 {}
CharAt0 where s = |x| with_str(x, |s| Ok(s.clone()))
);
write_fn_step!(
CharAt0 { s: String }
i = |x| with_uint(x, Ok)
=> {
if let Some(c) = s.chars().nth(i as usize) {
Ok(Clause::P(Primitive::Literal(Literal::Char(c))))
} else {
@@ -41,5 +20,5 @@ atomic_impl!(CharAt0, |Self { s, x }: &Self, _| {
"indexing string",
)?
}
})
});
}
);