]> Repositorios git - scryer-prolog.git/commitdiff
implement unify_ginteger to address FIXME in skip_max_list_cycle
authorMark Thom <[email protected]>
Tue, 16 Sep 2025 04:58:38 +0000 (21:58 -0700)
committerMark Thom <[email protected]>
Tue, 16 Sep 2025 04:58:38 +0000 (21:58 -0700)
src/machine/machine_state_impl.rs
src/machine/system_calls.rs
src/machine/unify.rs
src/parser/ast.rs
src/parser/lexer.rs

index 2ef605bfedfaa91196747aa9b5e732b8015cdbe1..2432c70255dcb8bcbca865bf11b93c7234f011cf 100644 (file)
@@ -278,6 +278,11 @@ impl MachineState {
         unifier.unify_fixnum(n1, value);
     }
 
+    pub fn unify_ginteger(&mut self, n1: GInteger, value: HeapCellValue) {
+        let mut unifier = DefaultUnifier::from(self);
+        unifier.unify_ginteger(n1, value);
+    }
+
     pub fn unify_big_int(&mut self, n1: TypedArenaPtr<Integer>, value: HeapCellValue) {
         let mut unifier = DefaultUnifier::from(self);
         unifier.unify_big_integer(n1, value);
index 320c0d92380adbeee47e9b90320e7118784f8484..a2a8576b161195733cc32ee610e6ecec13499f0c 100644 (file)
@@ -744,11 +744,9 @@ impl MachineState {
         }
 
         let target_n = self.store(self.deref(self.registers[1]));
-        self.unify_fixnum(
-            /* FIXME this is not safe */
-            unsafe { Fixnum::build_with_unchecked(brent_st.num_steps() as i64) },
-            target_n,
-        );
+        let num_steps = fixnum!(GInteger, brent_st.num_steps() as i64, &mut self.arena);
+
+        self.unify_ginteger(num_steps, target_n);
 
         if !self.fail {
             unify!(self, self.registers[4], self.heap[prev_hare]);
index fdc430eeb059d27567422ea9f8371944d277c54b..e1f73eaee25ff61978eb95520368e4eb232abc68 100644 (file)
@@ -150,6 +150,13 @@ pub(crate) trait Unifier: DerefMut<Target = MachineState> {
         );
     }
 
+    fn unify_ginteger(&mut self, n: GInteger, value: HeapCellValue) {
+        match n {
+            GInteger::Integer(integer) => self.unify_big_int(integer, value),
+            GInteger::Fixnum(fixnum) => self.unify_fixnum(fixnum, value),
+        }
+    }
+
     fn unify_atom(&mut self, atom: Atom, value: HeapCellValue) {
         read_heap_cell!(value,
             (HeapCellValueTag::Atom, (name, arity)) => {
index c2a1e7f89ad83a63151d2163f20984f7d49cee21..9effb51eb83d288bca3716c71863b4c00d888415 100644 (file)
@@ -241,6 +241,22 @@ macro_rules! is_fy {
     };
 }
 
+#[derive(Debug)]
+pub enum GInteger {
+    Integer(TypedArenaPtr<Integer>),
+    Fixnum(Fixnum),
+}
+
+impl GInteger {
+    #[inline]
+    pub fn to_literal(self) -> Literal {
+        match self {
+            GInteger::Integer(integer) => Literal::Integer(integer),
+            GInteger::Fixnum(fixnum) => Literal::Fixnum(fixnum),
+        }
+    }
+}
+
 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
 pub enum RegType {
     Perm(usize),
index e1cf3b90768db6e624496d0c2155998d54b3549e..bfc3fb8f71a20c57e1e27e1c935e6bed0c9fc2bf 100644 (file)
@@ -1,4 +1,3 @@
-use crate::arena::*;
 use crate::atom_table::*;
 pub use crate::machine::machine_state::*;
 use crate::offset_table::*;
@@ -67,7 +66,7 @@ impl NumberToken {
     fn to_token(self) -> Option<Token> {
         match self {
             NumberToken::Float(offset, fl) => Some(Token::Literal(Literal::F64(offset, fl))),
-            NumberToken::Integer(GInteger::BigInt(n)) => Some(Token::Literal(Literal::Integer(n))),
+            NumberToken::Integer(GInteger::Integer(n)) => Some(Token::Literal(Literal::Integer(n))),
             NumberToken::Integer(GInteger::Fixnum(n)) => Some(Token::Literal(Literal::Fixnum(n))),
             NumberToken::Partial(_) => None,
         }
@@ -89,22 +88,6 @@ macro_rules! try_nt {
     }};
 }
 
-#[derive(Debug)]
-enum GInteger {
-    BigInt(TypedArenaPtr<Integer>),
-    Fixnum(Fixnum),
-}
-
-impl GInteger {
-    #[inline]
-    fn to_literal(self) -> Literal {
-        match self {
-            GInteger::BigInt(integer) => Literal::Integer(integer),
-            GInteger::Fixnum(fixnum) => Literal::Fixnum(fixnum),
-        }
-    }
-}
-
 pub(crate) struct Lexer<'a, R> {
     pub(crate) reader: R,
     pub(crate) machine_st: &'a mut MachineState,
@@ -716,12 +699,15 @@ impl<'a, R: CharRead> Lexer<'a, R> {
                 Fixnum::build_with_checked(n)
                     .map(GInteger::Fixnum)
                     .unwrap_or_else(|_| {
-                        GInteger::BigInt(arena_alloc!(Integer::from(n), &mut self.machine_st.arena))
+                        GInteger::Integer(arena_alloc!(
+                            Integer::from(n),
+                            &mut self.machine_st.arena
+                        ))
                     })
             })
             .or_else(|_| {
                 Integer::from_str_radix(token, radix)
-                    .map(|n| GInteger::BigInt(arena_alloc!(n, &mut self.machine_st.arena)))
+                    .map(|n| GInteger::Integer(arena_alloc!(n, &mut self.machine_st.arena)))
                     .map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
             })
     }