]> Repositorios git - scryer-prolog.git/commitdiff
Associated functions for creating PrologTerm
authorbakaq <[email protected]>
Mon, 30 Sep 2024 01:30:24 +0000 (22:30 -0300)
committerbakaq <[email protected]>
Sun, 8 Dec 2024 23:18:06 +0000 (20:18 -0300)
src/machine/parsed_results.rs

index 561311cef510ded3226c85a9e6a44f80ac4a8edf..6b7e3fc7f61c96af5a3e78f169e46288a51638b7 100644 (file)
@@ -33,10 +33,57 @@ pub enum PrologTerm {
     Atom(String),
     String(String),
     List(Vec<PrologTerm>),
-    Structure(String, Vec<PrologTerm>),
+    Compound(String, Vec<PrologTerm>),
     Var(String),
 }
 
+impl PrologTerm {
+    /// Creates an integer term.
+    pub fn integer(value: impl Into<Integer>) -> Self {
+        PrologTerm::Integer(value.into())
+    }
+
+    /// Creates a rational term.
+    pub fn rational(value: impl Into<Rational>) -> Self {
+        PrologTerm::Rational(value.into())
+    }
+
+    /// Creates a float term.
+    pub fn float(value: impl Into<OrderedFloat<f64>>) -> Self {
+        PrologTerm::Float(value.into())
+    }
+
+    /// Creates an atom term.
+    pub fn atom(value: impl Into<String>) -> Self {
+        PrologTerm::Atom(value.into())
+    }
+
+    /// Creates a string term.
+    ///
+    /// In specific, this represents a list of chars in Prolog.
+    pub fn string(value: impl Into<String>) -> Self {
+        PrologTerm::String(value.into())
+    }
+
+    /// Creates a list term.
+    pub fn list(value: impl IntoIterator<Item = PrologTerm>) -> Self {
+        PrologTerm::List(value.into_iter().collect())
+    }
+
+    /// Creates a compound term.
+    pub fn compound(
+        functor: impl Into<String>,
+        args: impl IntoIterator<Item = PrologTerm>,
+    ) -> Self {
+        PrologTerm::Compound(functor.into(), args.into_iter().collect())
+    }
+
+    /// Creates a variable.
+    pub fn variable(value: impl Into<String>) -> Self {
+        PrologTerm::Var(value.into())
+    }
+}
+
 /// This is an auxiliary function to turn a count into names of anonymous variables like _A, _B,
 /// _AB, etc...
 fn count_to_letter_code(mut count: usize) -> String {
@@ -124,7 +171,7 @@ impl PrologTerm {
                             }
                         },
                         _ => {
-                            PrologTerm::Structure(".".into(), vec![head, tail])
+                            PrologTerm::Compound(".".into(), vec![head, tail])
                         }
                     };
                     term_stack.push(list);
@@ -218,7 +265,7 @@ impl PrologTerm {
                             .drain(term_stack.len() - arity ..)
                             .collect();
 
-                        term_stack.push(PrologTerm::Structure(name.as_str().to_string(), subterms));
+                        term_stack.push(PrologTerm::Compound(name.as_str().to_string(), subterms));
                     }
                 }
                 (HeapCellValueTag::PStr, atom) => {
@@ -248,7 +295,7 @@ impl PrologTerm {
                                 .map(|x| PrologTerm::Atom(x.to_string()))
                                 .collect();
 
-                            let mut partial_list = PrologTerm::Structure(
+                            let mut partial_list = PrologTerm::Compound(
                                 ".".into(),
                                 vec![
                                     list.pop().unwrap(),
@@ -257,7 +304,7 @@ impl PrologTerm {
                             );
 
                             while let Some(last) = list.pop() {
-                                partial_list = PrologTerm::Structure(
+                                partial_list = PrologTerm::Compound(
                                     ".".into(),
                                     vec![
                                         last,