]> Repositorios git - scryer-prolog.git/commitdiff
wrap resource_error into an error/2 functor and don't wrap it into a syntax_error...
authorSkgland <[email protected]>
Wed, 19 Nov 2025 23:48:04 +0000 (00:48 +0100)
committerBennet Bleßmann <[email protected]>
Thu, 20 Nov 2025 00:22:18 +0000 (01:22 +0100)
src/machine/heap.rs
src/machine/machine_errors.rs
src/read.rs

index 964d983e90d38e7d6b67e823ac4c8a229fc3347c..3c92f29b29c9eee4b1ec34b4a915f3ad9cb611b0 100644 (file)
@@ -1,6 +1,5 @@
 use crate::atom_table::*;
 use crate::functor_macro::*;
-use crate::machine::machine_errors::CompilationError;
 use crate::machine::{ArenaHeaderTag, Fixnum, Integer};
 use crate::types::*;
 
@@ -16,10 +15,6 @@ const ALIGN: usize = Heap::heap_cell_alignment();
 pub struct AllocError;
 
 impl AllocError {
-    pub(crate) fn to_compilation_error(&self, heap: &mut Heap) -> CompilationError {
-        CompilationError::FiniteMemoryInHeap(self.resource_error_offset(heap))
-    }
-
     pub(crate) fn resource_error_offset(&self, heap: &mut Heap) -> usize {
         heap.resource_error_offset()
     }
@@ -706,7 +701,13 @@ impl Heap {
 
     pub(crate) fn store_resource_error(&mut self) {
         RESOURCE_ERROR_OFFSET_INIT.call_once(move || {
-            let stub = functor!(atom!("resource_error"), [atom_as_cell((atom!("memory")))]);
+            let stub = functor!(
+                atom!("error"),
+                [
+                    functor((atom!("resource_error")), [atom_as_cell((atom!("memory")))]),
+                    atom_as_cell((atom!("[]")))
+                ]
+            );
             self.resource_err_loc = cell_index!(self.inner.byte_len);
 
             let mut writer = Heap::functor_writer(stub);
index 4df767bf28e88a397af96891c7d8990ed7bc0f52..fa6822d0af5d7967622ac6348ac4cf1f3a07cea7 100644 (file)
@@ -646,6 +646,19 @@ impl MachineState {
             return self.directive_error(err);
         }
 
+        if let CompilationError::FiniteMemoryInHeap(err) = err {
+            // err.resource_error_offset() should be the address of the error/2 functor in the pre-allocated term error(resource_error(memory), [])
+            let err_loc = err.resource_error_offset(&mut self.heap);
+            let stub = vec![FunctorElement::AbsoluteCell(
+                // err_loc + 1 should be the functors first argument which should be a str cell pointing at the resource_error/1 functor
+                self.heap[err_loc + 1],
+            )];
+            return MachineError {
+                stub,
+                location: None,
+            };
+        }
+
         let location = err.line_and_col_num();
         let stub = err.as_functor();
 
@@ -803,7 +816,13 @@ pub enum CompilationError {
     InvalidRuleHead,
     InvalidUseModuleDecl,
     InvalidModuleResolution(Atom),
-    FiniteMemoryInHeap(usize),
+    FiniteMemoryInHeap(AllocError),
+}
+
+impl From<AllocError> for CompilationError {
+    fn from(value: AllocError) -> Self {
+        Self::FiniteMemoryInHeap(value)
+    }
 }
 
 #[derive(Debug)]
@@ -879,8 +898,8 @@ impl CompilationError {
             CompilationError::ParserError(ref err) => {
                 functor!(err.as_atom())
             }
-            CompilationError::FiniteMemoryInHeap(h) => {
-                vec![FunctorElement::AbsoluteCell(str_loc_as_cell!(*h))]
+            CompilationError::FiniteMemoryInHeap(_) => {
+                functor!(atom!("resource_error"))
             }
         }
     }
index b8817aa6aca33283b9ac0a089f9c433b9fd2ab53..fe709c7e617132820139b9bae168f3aa122b55af 100644 (file)
@@ -379,9 +379,7 @@ impl<'a> TermWriter<'a> {
 
     #[inline]
     fn push_cell(&mut self, cell: HeapCellValue) -> Result<(), CompilationError> {
-        self.heap
-            .push_cell(cell)
-            .map_err(|err| err.to_compilation_error(self.heap))
+        Ok(self.heap.push_cell(cell)?)
     }
 
     fn term_as_addr(&mut self, term: &TermRef, h: usize) -> HeapCellValue {
@@ -475,10 +473,7 @@ impl<'a> TermWriter<'a> {
                         self.push_stub_addr()?;
                     }
 
-                    let cell = self
-                        .heap
-                        .allocate_cstr(src)
-                        .map_err(|err| err.to_compilation_error(self.heap))?;
+                    let cell = self.heap.allocate_cstr(src)?;
 
                     let new_h = self.heap.cell_len();
                     self.push_cell(cell)?;
@@ -496,10 +491,7 @@ impl<'a> TermWriter<'a> {
                         self.push_stub_addr()?;
                     }
 
-                    let cell = self
-                        .heap
-                        .allocate_pstr(src)
-                        .map_err(|err| err.to_compilation_error(self.heap))?;
+                    let cell = self.heap.allocate_pstr(src)?;
 
                     let tail_h = self.heap.cell_len();
                     self.push_stub_addr()?;