]> Repositorios git - scryer-prolog.git/commitdiff
address issues #104, #105, #103
authorMark Thom <[email protected]>
Sat, 20 Apr 2019 23:13:19 +0000 (20:13 -0300)
committerMark Thom <[email protected]>
Sat, 20 Apr 2019 23:13:19 +0000 (20:13 -0300)
Cargo.toml
src/prolog/machine/machine_state.rs
src/prolog/machine/system_calls.rs
src/prolog/read.rs
src/prolog/toplevel.pl

index 070107a1bb805114fe1a896173550bdf9985a5f2..cdf33442f1ab39b01b4f7b8dcd22b2761e26e398 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "scryer-prolog"
-version = "0.8.56"
+version = "0.8.57"
 authors = ["Mark Thom <[email protected]>"]
 repository = "https://github.com/mthom/scryer-prolog"
 description = "A modern Prolog implementation written mostly in Rust."
@@ -15,7 +15,7 @@ downcast = "0.10.0"
 num = "0.2"
 ordered-float = "0.5.0"
 prolog_parser = "0.8.19"
-readline_rs_compat = { version = "0.1.8", optional = true }
+readline_rs_compat = { version = "0.1.9", optional = true }
 ref_thread_local = "0.0.0"
 
 [dependencies.termion]
index 8fcac8d1a3a6532f1cfc0c83a4f35148422f4776..05ac8513971cdb3d7450c454f6e63ad001367fbf 100644 (file)
@@ -12,7 +12,7 @@ use prolog::machine::machine_indices::*;
 use prolog::machine::modules::*;
 use prolog::machine::or_stack::*;
 use prolog::num::{BigInt, BigUint, Zero, One};
-use prolog::read::{PrologStream, readline};
+use prolog::read::PrologStream;
 
 use downcast::Any;
 
@@ -589,8 +589,6 @@ pub(crate) trait CallPolicy: Any {
                 return_from_clause!(machine_st.last_call, machine_st)
             },
             &BuiltInClauseType::Read => {
-                readline::toggle_prompt(false);
-
                 match machine_st.read(parsing_stream, indices.atom_tbl.clone(), &indices.op_dir) {
                     Ok(offset) => {
                         let addr = machine_st[temp_v!(1)].clone();
index 8c2a55579a2f647d38db21282574b8afada933d2..31ecdb6840ebe587ce77c7537fa58f8d2040dfb9 100644 (file)
@@ -310,6 +310,20 @@ impl MachineState {
         }
     }
 
+    fn int_to_char_code(&mut self, n: Rc<BigInt>, stub: &'static str, arity: usize)
+                        -> Result<u8, MachineStub>
+    {
+        if let Some(c) = n.to_u8() {
+            Ok(c)
+        } else {
+            let stub = MachineError::functor_stub(clause_name!(stub), arity);
+            let err = MachineError::representation_error(RepFlag::CharacterCode);
+            let err = self.error_form(err, stub);
+
+            Err(err)
+        }
+    }
+    
     pub(super) fn system_call(&mut self,
                               ct: &SystemClauseType,
                               indices: &mut IndexStore,
@@ -444,6 +458,10 @@ impl MachineState {
 
                                 for addr in addrs.iter() {
                                     match addr {
+                                        &Addr::Con(Constant::Number(Number::Integer(ref n))) => {
+                                            let c = self.int_to_char_code(n.clone(), "atom_codes", 2)?;
+                                            chars.push(c as char);
+                                        },
                                         &Addr::Con(Constant::CharCode(c)) =>
                                             chars.push(c as char),
                                         _ => {
@@ -516,16 +534,10 @@ impl MachineState {
                         match self.store(self.deref(a2)) {
                             Addr::Con(Constant::CharCode(code)) =>
                                 self.unify(Addr::Con(Constant::Char(code as char)), addr.clone()),
-                            Addr::Con(Constant::Number(Number::Integer(n))) =>
-                                if let Some(c) = n.to_u8() {
-                                    self.unify(Addr::Con(Constant::Char(c as char)), addr.clone());
-                                } else {
-                                    let stub = MachineError::functor_stub(clause_name!("char_code"), 2);
-                                    let err = MachineError::representation_error(RepFlag::CharacterCode);
-                                    let err = self.error_form(err, stub);
-
-                                    return Err(err);
-                                },
+                            Addr::Con(Constant::Number(Number::Integer(n))) => {
+                                let c = self.int_to_char_code(n, "char_code", 2)?;
+                                self.unify(Addr::Con(Constant::Char(c as char)), addr.clone());
+                            },
                             _ => self.fail = true
                         };
                     },
@@ -556,8 +568,6 @@ impl MachineState {
                 };
             },
             &SystemClauseType::GetChar => {
-                readline::toggle_prompt(false);
-
                 let result = parsing_stream.next();
                 let a1 = self[temp_v!(1)].clone();
 
@@ -1408,8 +1418,6 @@ impl MachineState {
                 self.install_new_block(temp_v!(1));
             },
             &SystemClauseType::ReadTerm => {
-                readline::toggle_prompt(true);
-
                 match self.read(parsing_stream, indices.atom_tbl.clone(), &indices.op_dir) {
                     Ok(term_write_result) => {
                         let a1 = self[temp_v!(1)].clone();
index 8808713679ed954b19aa55c62c565024bd7ede6b..0ad08597e45ab80e495d254ed675941bc0fe9512 100644 (file)
@@ -85,27 +85,16 @@ pub mod readline
     impl Read for ReadlineStream {
         fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
             if self.pending_input.is_empty() {
-                let prompt = unsafe {
-                    if PRINT_PROMPT { "?- " } else { "" }
-                };
-
-                self.call_readline(prompt, buf)
+                self.call_readline("", buf)
             } else {
                 Ok(self.write_to_buf(buf))
             }
         }
     }
 
-    static mut PRINT_PROMPT: bool = true;
     static mut LINE_MODE: LineMode = LineMode::Single;
     static mut END_OF_LINE: bool = false;
 
-    pub fn toggle_prompt(on_or_off: bool) {
-        unsafe {
-            PRINT_PROMPT = on_or_off;
-        }
-    }
-
     pub fn set_line_mode(mode: LineMode) {
         unsafe {
             LINE_MODE = mode;
@@ -141,6 +130,7 @@ pub mod readline
             panic!("initialize_rl() failed with return code {}", rc);
         }
 
+        bind_key_rl('\t' as i32, rl_insert); // just insert tabs when typed.
         bind_key_rl('\n' as i32, bind_cr);
         bind_key_rl('\r' as i32, bind_cr);
         bind_keyseq_rl("\\C-d", bind_end_chord);
@@ -164,27 +154,14 @@ pub mod readline
 pub mod readline
 {
     use prolog_parser::ast::*;
-    use std::io::{BufReader, Read, Stdin, Write, stdin, stdout};
-
-    static mut PRINT_PROMPT: bool = false;
+    use std::io::{BufReader, Read, Stdin, stdin};
 
     struct StdinWrapper {
         buf: BufReader<Stdin>
     }
 
-    fn print_prompt() {
-        unsafe {
-            if PRINT_PROMPT {
-                print!("?- ");
-                stdout().flush().unwrap();
-                PRINT_PROMPT = false;
-            }
-        }
-    }
-
     impl Read for StdinWrapper {
         fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
-            print_prompt();
             self.buf.read(buf)
         }
     }
@@ -203,18 +180,9 @@ pub mod readline
 
     #[inline]
     pub fn input_stream() -> ::PrologStream {
-        print_prompt();
-
         let reader: Box<Read> = Box::new(StdinWrapper { buf: BufReader::new(stdin()) });
         parsing_stream(reader)
     }
-
-
-    pub fn toggle_prompt(on_or_off: bool) {
-        unsafe {
-            PRINT_PROMPT = on_or_off;
-        }
-    }
 }
 
 impl MachineState {
index 55a044d7ee2d57895a3f252d27ad840f5a50236d..b8c5f9a8f9b9808cf51df567cbdd8a7c32ec9566 100644 (file)
@@ -4,6 +4,7 @@ repl :-
 repl :- repl.
     
 read_and_match :-
+    write_term('?- ', [quoted(false)]),
     read_term(Term, [variable_names(VarList)]),
     '$instruction_match'(Term, VarList).