forked from M-Labs/nac3
1
0
Fork 0

core/model: add util::gen_for_model

This commit is contained in:
lyken 2024-08-20 12:10:50 +08:00
parent f9b8071df9
commit fd1a9f4f77
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
2 changed files with 41 additions and 0 deletions

View File

@ -6,6 +6,7 @@ pub mod function;
mod int;
mod ptr;
mod structure;
pub mod util;
pub use any::*;
pub use array::*;

View File

@ -0,0 +1,40 @@
use crate::codegen::{
stmt::{gen_for_callback_incrementing, BreakContinueHooks},
CodeGenContext, CodeGenerator,
};
use super::*;
/// Like [`gen_for_callback_incrementing`] with [`Model`] abstractions.
pub fn gen_for_model<'ctx, 'a, G, F, N>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, 'a>,
start: Instance<'ctx, Int<N>>,
stop: Instance<'ctx, Int<N>>,
step: Instance<'ctx, Int<N>>,
body: F,
) -> Result<(), String>
where
G: CodeGenerator + ?Sized,
F: FnOnce(
&mut G,
&mut CodeGenContext<'ctx, 'a>,
BreakContinueHooks<'ctx>,
Instance<'ctx, Int<N>>,
) -> Result<(), String>,
N: IntKind<'ctx> + Default,
{
let int_model = Int(N::default());
gen_for_callback_incrementing(
generator,
ctx,
None,
start.value,
(stop.value, false),
|g, ctx, hooks, i| {
let i = int_model.believe_value(i);
body(g, ctx, hooks, i)
},
step.value,
)
}