From bef8eb538c094efe20c3ad828e74570dcc3d89af Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 22 May 2022 00:03:55 -0600 Subject: [PATCH] throw a representation error if max arity is exceeded (#1483) --- src/codegen.rs | 6 +++--- src/machine/dispatch.rs | 2 +- src/machine/machine_errors.rs | 9 +++++++-- src/machine/machine_state.rs | 2 +- src/machine/mock_wam.rs | 10 +++++----- src/parser/ast.rs | 2 -- src/read.rs | 13 ++++++++----- 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/codegen.rs b/src/codegen.rs index 840c0787..69edb0d0 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -867,7 +867,7 @@ impl<'b> CodeGenerator<'b> { self.compile_query_line(term, term_loc, code, num_perm_vars, is_exposed); if self.marker.max_reg_allocated() > MAX_ARITY { - return Err(CompilationError::from(ParserError::ExceededMaxArity)); + return Err(CompilationError::ExceededMaxArity); } } } @@ -939,7 +939,7 @@ impl<'b> CodeGenerator<'b> { let mut fact = self.compile_target::(iter, GenContext::Head, false); if self.marker.max_reg_allocated() > MAX_ARITY { - return Err(CompilationError::from(ParserError::ExceededMaxArity)); + return Err(CompilationError::ExceededMaxArity); } let mut unsafe_var_marker = UnsafeVarMarker::new(); @@ -1002,7 +1002,7 @@ impl<'b> CodeGenerator<'b> { ); if self.marker.max_reg_allocated() > MAX_ARITY { - return Err(CompilationError::from(ParserError::ExceededMaxArity)); + return Err(CompilationError::ExceededMaxArity); } self.mark_unsafe_fact_vars(&mut compiled_fact); diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index 9d031c71..8f3d1e5d 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -188,7 +188,7 @@ impl Machine { let value = self.machine_st.registers[2]; unify_fn!(&mut self.machine_st, value, heap_loc_as_cell!(offset.heap_loc)); } - Err(ParserError::UnexpectedEOF) => { + Err(CompilationError::ParserError(ParserError::UnexpectedEOF)) => { let value = self.machine_st.registers[2]; self.machine_st.unify_atom(atom!("end_of_file"), value); } diff --git a/src/machine/machine_errors.rs b/src/machine/machine_errors.rs index ddf553b8..7ea75315 100644 --- a/src/machine/machine_errors.rs +++ b/src/machine/machine_errors.rs @@ -452,6 +452,9 @@ impl MachineState { SessionError::OpIsInfixAndPostFix(op) => { self.permission_error(Permission::Create, atom!("operator"), functor!(op)) } + SessionError::CompilationError(CompilationError::ExceededMaxArity) => { + self.representation_error(RepFlag::MaxArity) + } SessionError::CompilationError(err) => self.syntax_error(err), SessionError::PredicateNotMultifileOrDiscontiguous(compilation_target, key) => { let functor_stub = functor_stub(key.0, key.1); @@ -586,6 +589,7 @@ pub enum CompilationError { Arithmetic(ArithmeticError), ParserError(ParserError), CannotParseCyclicTerm, + ExceededMaxArity, ExpectedRel, InadmissibleFact, InadmissibleQueryTerm, @@ -629,6 +633,9 @@ impl CompilationError { &CompilationError::CannotParseCyclicTerm => { functor!(atom!("cannot_parse_cyclic_term")) } + &CompilationError::ExceededMaxArity => { + functor!(atom!("exceeded_max_arity")) + } &CompilationError::ExpectedRel => { functor!(atom!("expected_relation")) } @@ -900,9 +907,7 @@ pub enum ExistenceError { pub enum SessionError { CompilationError(CompilationError), CannotOverwriteBuiltIn(PredicateKey), - // CannotOverwriteImport(Atom), ExistenceError(ExistenceError), - // InvalidFileName(Atom), ModuleDoesNotContainExport(Atom, PredicateKey), ModuleCannotImportSelf(Atom), NamelessEntry, diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index d19242f2..17b8eec5 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -602,7 +602,7 @@ impl MachineState { return Ok(unify_fn!(*self, var_names_offset, var_names_addr)); } Err(err) => { - if let ParserError::UnexpectedEOF = err { + if let CompilationError::ParserError(ParserError::UnexpectedEOF) = err { self.eof_action( self.registers[2], stream, diff --git a/src/machine/mock_wam.rs b/src/machine/mock_wam.rs index 667097cd..d647f756 100644 --- a/src/machine/mock_wam.rs +++ b/src/machine/mock_wam.rs @@ -39,14 +39,14 @@ impl MockWAM { pub fn write_parsed_term_to_heap( &mut self, input_stream: Stream, - ) -> Result { + ) -> Result { self.machine_st.read(input_stream, &self.op_dir) } pub fn parse_and_write_parsed_term_to_heap( &mut self, term_string: &'static str, - ) -> Result { + ) -> Result { let stream = Stream::from_static_string(term_string, &mut self.machine_st.arena); self.write_parsed_term_to_heap(stream) } @@ -54,7 +54,7 @@ impl MockWAM { pub fn parse_and_print_term( &mut self, term_string: &'static str, - ) -> Result { + ) -> Result { let term_write_result = self.parse_and_write_parsed_term_to_heap(term_string)?; print_heap_terms(self.machine_st.heap.iter(), term_write_result.heap_loc); @@ -200,7 +200,7 @@ pub(crate) fn write_parsed_term_to_heap( machine_st: &mut MachineState, input_stream: Stream, op_dir: &OpDir, -) -> Result { +) -> Result { machine_st.read(input_stream, op_dir) } @@ -209,7 +209,7 @@ pub(crate) fn parse_and_write_parsed_term_to_heap( machine_st: &mut MachineState, term_string: &'static str, op_dir: &OpDir, -) -> Result { +) -> Result { let stream = Stream::from_static_string(term_string, &mut machine_st.arena); write_parsed_term_to_heap(machine_st, stream, op_dir) } diff --git a/src/parser/ast.rs b/src/parser/ast.rs index aa3322e0..0a141453 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -371,7 +371,6 @@ pub enum ArithmeticError { #[derive(Debug)] pub enum ParserError { BackQuotedString(usize, usize), - ExceededMaxArity, IO(IOError), IncompleteReduction(usize, usize), InvalidSingleQuotedCharacter(char), @@ -401,7 +400,6 @@ impl ParserError { pub fn as_atom(&self) -> Atom { match self { ParserError::BackQuotedString(..) => atom!("back_quoted_string"), - ParserError::ExceededMaxArity => atom!("exceeded_max_arity"), ParserError::IncompleteReduction(..) => atom!("incomplete_reduction"), ParserError::InvalidSingleQuotedCharacter(..) => atom!("invalid_single_quoted_character"), ParserError::IO(_) => atom!("input_output_error"), diff --git a/src/read.rs b/src/read.rs index 0c6c9578..3f8282c0 100644 --- a/src/read.rs +++ b/src/read.rs @@ -5,6 +5,7 @@ use crate::atom_table::*; use crate::forms::*; use crate::iterators::*; use crate::machine::heap::*; +use crate::machine::machine_errors::*; use crate::machine::machine_indices::*; use crate::machine::machine_state::MachineState; use crate::machine::streams::*; @@ -40,14 +41,16 @@ impl MachineState { &mut self, mut inner: Stream, op_dir: &OpDir, - ) -> Result { + ) -> Result { let (term, num_lines_read) = { let prior_num_lines_read = inner.lines_read(); let mut parser = Parser::new(inner, self); parser.add_lines_read(prior_num_lines_read); - let term = parser.read_term(&CompositeOpDir::new(op_dir, None))?; + let term = parser.read_term(&CompositeOpDir::new(op_dir, None)) + .map_err(CompilationError::from)?; + (term, parser.lines_read() - prior_num_lines_read) }; @@ -245,7 +248,7 @@ pub(crate) fn write_term_to_heap( term: &Term, heap: &mut Heap, atom_tbl: &mut AtomTable, -) -> Result { +) -> Result { let term_writer = TermWriter::new(heap, atom_tbl); term_writer.write_term_to_heap(term) } @@ -311,7 +314,7 @@ impl<'a, 'b> TermWriter<'a, 'b> { } } - fn write_term_to_heap(mut self, term: &'a Term) -> Result { + fn write_term_to_heap(mut self, term: &'a Term) -> Result { let heap_loc = self.heap.len(); for term in breadth_first_iter(term, true) { @@ -335,7 +338,7 @@ impl<'a, 'b> TermWriter<'a, 'b> { } &TermRef::Clause(Level::Root, _, ref ct, subterms) => { if subterms.len() > MAX_ARITY { - return Err(ParserError::ExceededMaxArity); + return Err(CompilationError::ExceededMaxArity); } self.heap.push(if subterms.len() == 0 { -- 2.54.0