From 516848e214e7672adbcc1991d60a2df89f4fc0a4 Mon Sep 17 00:00:00 2001 From: Skgland Date: Thu, 20 Nov 2025 00:48:04 +0100 Subject: [PATCH] wrap resource_error into an error/2 functor and don't wrap it into a syntax_error functor --- src/machine/heap.rs | 13 +++++++------ src/machine/machine_errors.rs | 25 ++++++++++++++++++++++--- src/read.rs | 14 +++----------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/machine/heap.rs b/src/machine/heap.rs index 964d983e..3c92f29b 100644 --- a/src/machine/heap.rs +++ b/src/machine/heap.rs @@ -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); diff --git a/src/machine/machine_errors.rs b/src/machine/machine_errors.rs index 4df767bf..fa6822d0 100644 --- a/src/machine/machine_errors.rs +++ b/src/machine/machine_errors.rs @@ -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 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")) } } } diff --git a/src/read.rs b/src/read.rs index b8817aa6..fe709c7e 100644 --- a/src/read.rs +++ b/src/read.rs @@ -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()?; -- 2.54.0