]> Repositorios git - scryer-prolog.git/commitdiff
consume whitespace before reading a term in loader.pl, modify prolog_load_context...
authorMark Thom <[email protected]>
Mon, 22 Feb 2021 20:23:22 +0000 (13:23 -0700)
committerMark Thom <[email protected]>
Mon, 22 Feb 2021 20:23:22 +0000 (13:23 -0700)
src/clause_types.rs
src/loader.pl
src/machine/loader.rs
src/machine/streams.rs
src/machine/system_calls.rs
src/read.rs

index 707362821babdc5e35633a9447b05ffc803ae8b4..ecfd0cbf7a1d1f8d122c2991e020b12de768a668 100644 (file)
@@ -306,6 +306,7 @@ pub enum SystemClauseType {
     SetEnv,
     UnsetEnv,
     CharsBase64,
+    DevourWhitespace,
 }
 
 impl SystemClauseType {
@@ -594,6 +595,7 @@ impl SystemClauseType {
             &SystemClauseType::UnsetEnv => clause_name!("$unsetenv"),
             &SystemClauseType::CharsBase64 => clause_name!("$chars_base64"),
             &SystemClauseType::LoadLibraryAsStream => clause_name!("$load_library_as_stream"),
+            &SystemClauseType::DevourWhitespace => clause_name!("$devour_whitespace"),
         }
     }
 
@@ -841,6 +843,9 @@ impl SystemClauseType {
             ("$cpp_discontiguous_property", 3) => {
                 Some(SystemClauseType::REPL(REPLCodePtr::DiscontiguousProperty))
             }
+            ("$devour_whitespace", 1) => {
+                Some(SystemClauseType::DevourWhitespace)
+            }
             _ => None,
         }
     }
index 5975dddd8d1539c7335d40ce1854fc459cf74ef5..bf6efc2666a370fa8eff501aa252e566c15ea1dd 100644 (file)
@@ -130,28 +130,26 @@ filter_anonymous_vars([VN=V | VNEqs0], VNEqs) :-
 
 warn_about_singletons([], _).
 warn_about_singletons([Singleton|Singletons], LinesRead) :-
-    (  LinesRead =:= -1 ->
-       true
-    ;  filter_anonymous_vars([Singleton|Singletons], VarEqs),
+    (  filter_anonymous_vars([Singleton|Singletons], VarEqs),
        VarEqs \== [] ->
        write('Warning: singleton variables '),
        print_comma_separated_list(VarEqs),
        write(' at line '),
        write(LinesRead),
        write(' of '),
-       load_context(Module),
-       write(Module),
+       prolog_load_context(file, File),
+       write(File),
        nl
     ;  true
     ).
 
 
 load_loop(Stream, Evacuable) :-
-    (  stream_property(Stream, position(position_and_lines_read(_, LinesRead))) ->
-       true
-    ;  LinesRead = -1
+    (  '$devour_whitespace'(Stream) ->
+       stream_property(Stream, position(position_and_lines_read(_, LinesRead))),
+       read_term(Stream, Term, [singletons(Singletons)])
+    ;  Term = end_of_file
     ),
-    read_term(Stream, Term, [singletons(Singletons)]),
     (  Term == end_of_file ->
        close(Stream),
        '$conclude_load'(Evacuable)
index a9d45bcc406ce5e9feb1fbeec82d6c98056f8e3a..96c449dc939cef50cb9305ad0c680f2acc778aaa 100644 (file)
@@ -1612,20 +1612,26 @@ impl Machine {
 
     pub(crate) fn load_context_file(&mut self) {
         if let Some(load_context) = self.load_contexts.last() {
-            if let Some(file_name) = load_context.path.file_name() {
-                let file_name_str = file_name.to_str().unwrap();
-                let file_name_atom =
-                    clause_name!(file_name_str.to_string(), self.machine_st.atom_tbl);
+            match load_context.path.file_name() {
+                Some(file_name) if load_context.path.is_file() => {
+                    let file_name_str = file_name.to_str().unwrap();
+                    let file_name_atom =
+                        clause_name!(file_name_str.to_string(), self.machine_st.atom_tbl);
+
+                    let file_name_addr = Addr::Con(
+                        self.machine_st
+                            .heap
+                            .push(HeapCellValue::Atom(file_name_atom, None)),
+                    );
 
-                let file_name_addr = Addr::Con(
                     self.machine_st
-                        .heap
-                        .push(HeapCellValue::Atom(file_name_atom, None)),
-                );
+                        .unify(file_name_addr, self.machine_st[temp_v!(1)]);
 
-                self.machine_st
-                    .unify(file_name_addr, self.machine_st[temp_v!(1)]);
-                return;
+                    return;
+                }
+                _ => {
+                    return self.load_context_module();
+                }
             }
         }
 
index 4c138a4933c137320c0b5390378464ca45c40513..c08a6ae515e651fdf7d7d6e44967313b5e5befcd 100644 (file)
@@ -395,6 +395,7 @@ impl Stream {
             StreamInstance::TlsStream(..) |
             StreamInstance::ReadlineStream(..) |
             StreamInstance::StaticStr(..) |
+            StreamInstance::PausedPrologStream(..) |
             StreamInstance::Bytes(..) => Some(0),
             _ => None,
         };
index 3c30de069a4d3d394d65013b52e9f7071c781ef4..0d94fd1c94065f2c364e6a2f356dc013b0fcc3df 100644 (file)
@@ -3888,54 +3888,6 @@ impl MachineState {
 
                 self.unify(a1, a2);
             }
-            /*
-                        &SystemClauseType::GetClause => {
-                            let head = self[temp_v!(1)];
-
-                            let subsection = match self.store(self.deref(head)) {
-                                Addr::Str(s) => match &self.heap[s] {
-                                    &HeapCellValue::NamedStr(arity, ref name, ..) => {
-                                        indices.get_clause_subsection(
-                                            name.owning_module(),
-                                            name.clone(),
-                                            arity,
-                                        )
-                                    }
-                                    _ => {
-                                        unreachable!()
-                                    }
-                                },
-                                Addr::Con(h) if self.heap.atom_at(h) => {
-                                    if let &HeapCellValue::Atom(ref name, _) = &self.heap[h] {
-                                        indices.get_clause_subsection(
-                                            name.owning_module(),
-                                            name.clone(),
-                                            0,
-                                        )
-                                    } else {
-                            unreachable!()
-                                    }
-                                }
-                                _ => {
-                                    unreachable!()
-                                }
-                            };
-
-                            match subsection {
-                                Some(dynamic_predicate_info) => {
-                                    self.execute_at_index(
-                                        2,
-                                        dir_entry!(dynamic_predicate_info.clauses_subsection_p),
-                                    );
-
-                                    return Ok(());
-                                }
-                                _ => {
-                                    unreachable!()
-                                }
-                            }
-                        }
-            */
             &SystemClauseType::GetCutPoint => {
                 let a1 = self[temp_v!(1)];
                 let a2 = Addr::CutPoint(self.b0);
@@ -5496,6 +5448,18 @@ impl MachineState {
                     }
                 }
             }
+            &SystemClauseType::DevourWhitespace => {
+                let stream =
+                    self.get_stream_or_alias(self[temp_v!(1)], indices, "$devour_whitespace", 1)?;
+
+                match self.devour_whitespace(stream, self.atom_tbl.clone()) {
+                    Ok(false) => {} // not at EOF.
+                    _ => {
+                        self.fail = true;
+                        return Ok(());
+                    }
+                }
+            }
         };
 
         return_from_clause!(self.last_call, self)
index 4cb8e779cee12fd8b6251e69f98aa30b04030960..fa1584149b997e7d4e58b258654d298c28ea9be6 100644 (file)
@@ -176,6 +176,24 @@ pub mod readline {
 }
 
 impl MachineState {
+    pub fn devour_whitespace(
+        &mut self,
+        mut inner: Stream,
+        atom_tbl: TabledData<Atom>,
+    ) -> Result<bool, ParserError> {
+        let mut stream = parsing_stream(inner.clone())?;
+        let mut parser = Parser::new(&mut stream, atom_tbl, self.flags);
+
+        parser.devour_whitespace()?;
+        inner.add_lines_read(parser.num_lines_read());
+
+        let result = parser.eof();
+        let buf = stream.take_buf();
+
+        inner.pause_stream(buf)?;
+        result
+    }
+
     pub fn read(
         &mut self,
         mut inner: Stream,
@@ -191,12 +209,12 @@ impl MachineState {
             (term, parser.num_lines_read())
         };
 
+        inner.add_lines_read(num_lines_read);
+
         // 'pausing' the stream saves the pending top buffer
         // created by the parsing stream, which was created in this
         // scope and is about to be destroyed in it.
 
-        inner.add_lines_read(num_lines_read);
-
         let buf = stream.take_buf();
         inner.pause_stream(buf)?;