From: bakaq Date: Mon, 30 Sep 2024 01:30:24 +0000 (-0300) Subject: Associated functions for creating PrologTerm X-Git-Tag: v0.10.0~92^2~23 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=4480e7c0667ca813cf2d77f83741025573831d30;p=scryer-prolog.git Associated functions for creating PrologTerm --- diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 561311ce..6b7e3fc7 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -33,10 +33,57 @@ pub enum PrologTerm { Atom(String), String(String), List(Vec), - Structure(String, Vec), + Compound(String, Vec), Var(String), } +impl PrologTerm { + /// Creates an integer term. + pub fn integer(value: impl Into) -> Self { + PrologTerm::Integer(value.into()) + } + + /// Creates a rational term. + pub fn rational(value: impl Into) -> Self { + PrologTerm::Rational(value.into()) + } + + /// Creates a float term. + pub fn float(value: impl Into>) -> Self { + PrologTerm::Float(value.into()) + } + + /// Creates an atom term. + pub fn atom(value: impl Into) -> 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) -> Self { + PrologTerm::String(value.into()) + } + + /// Creates a list term. + pub fn list(value: impl IntoIterator) -> Self { + PrologTerm::List(value.into_iter().collect()) + } + + /// Creates a compound term. + pub fn compound( + functor: impl Into, + args: impl IntoIterator, + ) -> Self { + PrologTerm::Compound(functor.into(), args.into_iter().collect()) + } + + /// Creates a variable. + pub fn variable(value: impl Into) -> 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,