]> Repositorios git - scryer-prolog.git/commitdiff
throw a representation error if max arity is exceeded (#1483)
authorMark Thom <[email protected]>
Sun, 22 May 2022 06:03:55 +0000 (00:03 -0600)
committerMark Thom <[email protected]>
Sun, 22 May 2022 06:03:55 +0000 (00:03 -0600)
src/codegen.rs
src/machine/dispatch.rs
src/machine/machine_errors.rs
src/machine/machine_state.rs
src/machine/mock_wam.rs
src/parser/ast.rs
src/read.rs

index 840c078728a60924a956cdbdc1c0e4ead9197a26..69edb0d0e696b5b415f0c7ddfe77e7a8025cd489 100644 (file)
@@ -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::<FactInstruction, _>(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);
index 9d031c71db3d3f6364a44d4a180ec74fd842cc4c..8f3d1e5d05cc1c767f76d83382e0e8978f791230 100644 (file)
@@ -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);
             }
index ddf553b8b954e689161bd464ae9866f28ebf451d..7ea753158e079aa475c748cca388da9709c9c404 100644 (file)
@@ -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,
index d19242f2491531ff33c9291f737c32a4c96d58cc..17b8eec5781124370a739a4bf9ebf32a06112c50 100644 (file)
@@ -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,
index 667097cdb6b2cc08d0b76e7387abbc590ff8a10e..d647f75671841b3f0ee26b86ff9cdf419a380ced 100644 (file)
@@ -39,14 +39,14 @@ impl MockWAM {
     pub fn write_parsed_term_to_heap(
         &mut self,
         input_stream: Stream,
-    ) -> Result<TermWriteResult, ParserError> {
+    ) -> Result<TermWriteResult, CompilationError> {
         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<TermWriteResult, ParserError> {
+    ) -> Result<TermWriteResult, CompilationError> {
         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<String, ParserError> {
+    ) -> Result<String, CompilationError> {
         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<TermWriteResult, ParserError> {
+) -> Result<TermWriteResult, CompilationError> {
     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<TermWriteResult, ParserError> {
+) -> Result<TermWriteResult, CompilationError> {
     let stream = Stream::from_static_string(term_string, &mut machine_st.arena);
     write_parsed_term_to_heap(machine_st, stream, op_dir)
 }
index aa3322e0ca28c3af30bcd43bc8ae1950a1d88574..0a14145384b2c0e26ed6e434c8316eed616330ed 100644 (file)
@@ -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"),
index 0c6c95785a9b53952548797584c7f6c536308209..3f8282c02a026be2232467947b785adc34be6de9 100644 (file)
@@ -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<TermWriteResult, ParserError> {
+    ) -> Result<TermWriteResult, CompilationError> {
         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<TermWriteResult, ParserError> {
+) -> Result<TermWriteResult, CompilationError> {
     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<TermWriteResult, ParserError> {
+    fn write_term_to_heap(mut self, term: &'a Term) -> Result<TermWriteResult, CompilationError> {
         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 {