From b41d091cac4237d5c016c0ccb5d0741fca12c199 Mon Sep 17 00:00:00 2001 From: lyken Date: Tue, 20 Aug 2024 12:10:50 +0800 Subject: [PATCH] core/model: add util::gen_for_model --- nac3core/src/codegen/model/mod.rs | 1 + nac3core/src/codegen/model/util.rs | 41 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 nac3core/src/codegen/model/util.rs diff --git a/nac3core/src/codegen/model/mod.rs b/nac3core/src/codegen/model/mod.rs index 22bb3332..4256e84e 100644 --- a/nac3core/src/codegen/model/mod.rs +++ b/nac3core/src/codegen/model/mod.rs @@ -6,6 +6,7 @@ pub mod function; mod int; mod ptr; mod structure; +pub mod util; pub use any::*; pub use array::*; diff --git a/nac3core/src/codegen/model/util.rs b/nac3core/src/codegen/model/util.rs new file mode 100644 index 00000000..cbff14c1 --- /dev/null +++ b/nac3core/src/codegen/model/util.rs @@ -0,0 +1,41 @@ +use super::*; +use crate::codegen::{ + stmt::{gen_for_callback_incrementing, BreakContinueHooks}, + CodeGenContext, CodeGenerator, +}; + +/// Like [`gen_for_callback_incrementing`] with [`Model`] abstractions. +/// +/// The value for `stop` is exclusive. +pub fn gen_for_model<'ctx, 'a, G, F, N>( + generator: &mut G, + ctx: &mut CodeGenContext<'ctx, 'a>, + start: Instance<'ctx, Int>, + stop: Instance<'ctx, Int>, + step: Instance<'ctx, Int>, + body: F, +) -> Result<(), String> +where + G: CodeGenerator + ?Sized, + F: FnOnce( + &mut G, + &mut CodeGenContext<'ctx, 'a>, + BreakContinueHooks<'ctx>, + Instance<'ctx, Int>, + ) -> 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 = unsafe { int_model.believe_value(i) }; + body(g, ctx, hooks, i) + }, + step.value, + ) +}