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

@@ -8,11 +8,13 @@ use crate::representations::interpreted::ExprInst;
/// hold.
#[derive(Clone)]
pub struct AssertionError {
pub value: ExprInst,
pub assertion: &'static str,
value: ExprInst,
assertion: &'static str,
}
impl AssertionError {
/// Construct, upcast and wrap in a Result that never succeeds for easy
/// short-circuiting
pub fn fail<T>(
value: ExprInst,
assertion: &'static str,
@@ -20,6 +22,7 @@ impl AssertionError {
return Err(Self { value, assertion }.into_extern());
}
/// Construct and upcast to [ExternError]
pub fn ext(value: ExprInst, assertion: &'static str) -> Rc<dyn ExternError> {
return Self { value, assertion }.into_extern();
}

View File

@@ -1,3 +1,5 @@
//! Utility functions that operate on literals. Because of the parallel locked
//! nature of [ExprInst], returning a reference to [Literal] is not possible.
use std::rc::Rc;
use super::assertion_error::AssertionError;

View File

@@ -3,14 +3,16 @@ mod assertion_error;
mod bool;
mod conv;
mod cpsio;
mod litconv;
pub mod litconv;
mod mk_stl;
mod num;
mod runtime_error;
mod str;
pub use assertion_error::AssertionError;
pub use cpsio::{handle as handleIO, IO};
pub use mk_stl::mk_stl;
pub use runtime_error::RuntimeError;
pub use self::bool::Boolean;
pub use self::num::Numeric;

View File

@@ -11,6 +11,8 @@ pub struct RuntimeError {
}
impl RuntimeError {
/// Construct, upcast and wrap in a Result that never succeeds for easy
/// short-circuiting
pub fn fail<T>(
message: String,
operation: &'static str,
@@ -18,6 +20,7 @@ impl RuntimeError {
return Err(Self { message, operation }.into_extern());
}
/// Construct and upcast to [ExternError]
pub fn ext(message: String, operation: &'static str) -> Rc<dyn ExternError> {
return Self { message, operation }.into_extern();
}

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",
)?
}
})
});
}
);