use crate::atom_table::*;
use crate::functor_macro::*;
-use crate::machine::machine_errors::CompilationError;
use crate::machine::{ArenaHeaderTag, Fixnum, Integer};
use crate::types::*;
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()
}
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);
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();
InvalidRuleHead,
InvalidUseModuleDecl,
InvalidModuleResolution(Atom),
- FiniteMemoryInHeap(usize),
+ FiniteMemoryInHeap(AllocError),
+}
+
+impl From<AllocError> for CompilationError {
+ fn from(value: AllocError) -> Self {
+ Self::FiniteMemoryInHeap(value)
+ }
}
#[derive(Debug)]
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"))
}
}
}
#[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 {
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)?;
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()?;