]> Repositorios git - scryer-prolog.git/commitdiff
Draft of an implementation of char_type
authornotoria <[email protected]>
Tue, 21 Apr 2020 22:46:54 +0000 (00:46 +0200)
committernotoria <[email protected]>
Wed, 22 Apr 2020 10:57:18 +0000 (12:57 +0200)
src/prolog/clause_types.rs
src/prolog/lib/charsio.pl
src/prolog/machine/system_calls.rs

index c3d79b6313778ab577a8930f0b0f882256e8620a..53d0f8e1da6aacb26735270cee33802f1a1c96f1 100644 (file)
@@ -158,6 +158,7 @@ pub enum SystemClauseType {
     BindFromRegister,
     CallContinuation,
     CharCode,
+    CharType,
     CharsToNumber,
     ClearAttributeGoals,
     CloneAttributeGoals,
@@ -278,6 +279,7 @@ impl SystemClauseType {
             &SystemClauseType::BindFromRegister => clause_name!("$bind_from_register"),
             &SystemClauseType::CallContinuation => clause_name!("$call_continuation"),
             &SystemClauseType::CharCode => clause_name!("$char_code"),
+            &SystemClauseType::CharType => clause_name!("$char_type"),
             &SystemClauseType::CharsToNumber => clause_name!("$chars_to_number"),
             &SystemClauseType::CheckCutPoint => clause_name!("$check_cp"),
             &SystemClauseType::ClearAttributeGoals => clause_name!("$clear_attribute_goals"),
@@ -432,6 +434,7 @@ impl SystemClauseType {
             ("$assertz", 4) => Some(SystemClauseType::AssertDynamicPredicateToBack),
             ("$call_continuation", 1) => Some(SystemClauseType::CallContinuation),
             ("$char_code", 2) => Some(SystemClauseType::CharCode),
+            ("$char_type", 2) => Some(SystemClauseType::CharType),
             ("$chars_to_number", 2) => Some(SystemClauseType::CharsToNumber),
             ("$clear_attribute_goals", 0) => Some(SystemClauseType::ClearAttributeGoals),
             ("$clone_attribute_goals", 1) => Some(SystemClauseType::CloneAttributeGoals),
index 717bbaaa3027bcba70a49ed2a5aa74fc8c5a250c..0281e83261b8ec3db7a8e2e905212a7053c6d173 100644 (file)
@@ -1,4 +1,4 @@
-:- module(charsio, [get_single_char/1,
+:- module(charsio, [char_type/2, get_single_char/1,
                     read_term_from_chars/2,
                     write_term_to_chars/3]).
 
@@ -57,6 +57,13 @@ extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :-
     ).
 
 
+char_type(C, T) :-
+    (   var(C) -> throw(error(instantiation_error, char_type/2))
+    ;   atom_length(C, 1) -> '$char_type'(C, T)
+    ;   throw(error(type_error(in_character, C), char_type/2))
+    ).
+
+
 get_single_char(C) :-
     (  var(C) -> '$get_single_char'(C)
     ;  atom_length(C, 1) -> '$get_single_char'(C)
index 63bb8c79dc1d3b71d882b8c4ebc1f88c12f2b4d5..e07c9eb77e33853486852581fa565ff71a7b79a3 100644 (file)
@@ -1337,6 +1337,71 @@ impl MachineState {
                     }
                 };
             }
+            &SystemClauseType::CharType => {
+                let a1 = self.store(self.deref(self[temp_v!(1)]));
+                let a2 = self.store(self.deref(self[temp_v!(2)]));
+
+                let c = match a1 {
+                    Addr::Char(c) => c,
+                    Addr::Con(h) if self.heap.atom_at(h) => {
+                        if let HeapCellValue::Atom(name, _) = &self.heap[h] {
+                            name.as_str().chars().next().unwrap()
+                        }
+                        else {
+                            unreachable!()
+                        }
+                    }
+                    _ => unreachable!()
+                };
+                macro_rules! call {
+                    ($id:ident, $name:tt) => {
+                if $id!(c) {
+                    // let string = $name;
+                    // let chars =
+                    //     clause_name!(string.to_string(), indices.atom_tbl);
+                    // let atom = self
+                    //     .heap
+                    //     .to_unifiable(HeapCellValue::Atom(chars, None));
+                    let atom = self.heap.to_unifiable(
+                        HeapCellValue::Atom(clause_name!($name.to_string(), indices.atom_tbl), None)
+                    );
+
+                    self.unify(atom, a2);
+                }
+                    }
+                }
+                call!(symbolic_control_char, "symbolic_control");
+                call!(space_char, "space");
+                call!(layout_char, "layout");
+                call!(symbolic_hexadecimal_char, "symbolic_hexadecimal");
+                call!(octal_digit_char, "octal_digit");
+                call!(binary_digit_char, "binary_digit");
+                call!(hexadecimal_digit_char, "hexadecimal_digit");
+                call!(exponent_char, "exponent");
+                call!(sign_char, "sign");
+                //call!(new_line_char, "new_line");
+                //call!(comment_1_char, "comment_1");
+                //call!(comment_2_char, "comment_2");
+                //call!(capital_letter_char, "upper");
+                //call!(small_letter_char, "lower");
+                //call!(variable_indicator_char, "variable_indicator");
+                //call!(graphic_char, "graphic");
+                //call!(graphic_token_char, "graphic_token");
+                //call!(alpha_char, "alpha");
+                //call!(decimal_digit_char, "decimal_digit");
+                //call!(decimal_point_char, "decimal_point");
+                call!(alpha_numeric_char, "alnum");
+                //call!(cut_char, "cut");
+                //call!(semicolon_char, "semicolon");
+                //call!(backslash_char, "backslash");
+                //call!(single_quote_char, "single_quote");
+                //call!(double_quote_char, "double_quote");
+                //call!(back_quote_char, "back_quote");
+                //call!(meta_char, "meta");
+                //call!(solo_char, "solo");
+                //call!(prolog_char, "prolog");
+                //self.fail = true;
+            }
             &SystemClauseType::CheckCutPoint => {
                 let addr = self.store(self.deref(self[temp_v!(1)]));