add assoc.pl, add max evaluable functor

This commit is contained in:
Mark Thom
2019-03-08 22:31:12 -07:00
parent 089428b8c4
commit da0e1b9436
8 changed files with 523 additions and 10 deletions

View File

@@ -423,8 +423,11 @@ impl ListingCompiler {
let idx = code_dir.entry((name.clone(), arity)).or_insert(CodeIndex::default());
set_code_index!(idx, IndexPtr::Index(p), self.get_module_name());
self.localize_self_calls(name, arity, &mut decl_code, p);
/*
println!("{}/{}:", name.as_str(), arity);
print_code(&decl_code);
*/
self.localize_self_calls(name, arity, &mut decl_code, p);
code.extend(decl_code.into_iter());
}

View File

@@ -536,11 +536,14 @@ pub(crate) trait CallPolicy: Any {
let a2 = machine_st[temp_v!(2)].clone();
let a3 = machine_st[temp_v!(3)].clone();
let c = Addr::Con(match machine_st.compare_term_test(&a2, &a3) {
Ordering::Greater => atom!(">"),
Ordering::Equal => atom!("="),
Ordering::Less => atom!("<")
});
let c = match machine_st.compare_term_test(&a2, &a3) {
Ordering::Greater => Addr::Con(Constant::Atom(clause_name!(">"),
Some((700, XFX)))),
Ordering::Equal => Addr::Con(Constant::Atom(clause_name!("="),
Some((700, XFX)))),
Ordering::Less => Addr::Con(Constant::Atom(clause_name!("<"),
Some((700, XFX))))
};
machine_st.unify(a1, c);
return_from_clause!(machine_st.last_call, machine_st)

View File

@@ -620,7 +620,7 @@ impl MachineState {
for heap_val in self.post_order_iter(a) {
match heap_val {
HeapCellValue::NamedStr(2, name, Some(_)) => {
HeapCellValue::NamedStr(2, name, _) => {
let a2 = interms.pop().unwrap();
let a1 = interms.pop().unwrap();
@@ -628,8 +628,9 @@ impl MachineState {
"+" => interms.push(a1 + a2),
"-" => interms.push(a1 - a2),
"*" => interms.push(a1 * a2),
"/" => interms.push(self.div(a1, a2)?),
"/" => interms.push(self.div(a1, a2)?),
"**" => interms.push(self.pow(a1, a2)?),
"max" => interms.push(self.max(a1, a2)?),
"rdiv" => {
let r1 = self.get_rational(&ArithmeticTerm::Number(a1), &caller)?;
let r2 = self.get_rational(&ArithmeticTerm::Number(a2), &caller)?;
@@ -650,7 +651,7 @@ impl MachineState {
caller))
}
},
HeapCellValue::NamedStr(1, name, Some(_)) => {
HeapCellValue::NamedStr(1, name, _) => {
let a1 = interms.pop().unwrap();
match name.as_str() {
@@ -849,6 +850,10 @@ impl MachineState {
}
}
fn max(&self, n1: Number, n2: Number) -> Result<Number, MachineStub> {
Ok(max(n1, n2))
}
fn remainder(&self, n1: Number, n2: Number) -> Result<Rc<BigInt>, MachineStub>
{
let stub = MachineError::functor_stub(clause_name!("(rem)"), 2);
@@ -913,6 +918,13 @@ impl MachineState {
self.interms[t - 1] = n1 * n2;
self.p += 1;
},
&ArithmeticInstruction::Max(ref a1, ref a2, t) => {
let n1 = try_or_fail!(self, self.get_number(a1));
let n2 = try_or_fail!(self, self.get_number(a2));
self.interms[t - 1] = try_or_fail!(self, self.max(n1, n2));
self.p += 1;
},
&ArithmeticInstruction::Pow(ref a1, ref a2, t) => {
let n1 = try_or_fail!(self, self.get_number(a1));
let n2 = try_or_fail!(self, self.get_number(a2));

View File

@@ -154,6 +154,7 @@ static ATTS: &str = include_str!("../lib/atts.pl");
static DIF: &str = include_str!("../lib/dif.pl");
static FREEZE: &str = include_str!("../lib/freeze.pl");
static REIF: &str = include_str!("../lib/reif.pl");
static ASSOC: &str = include_str!("../lib/assoc.pl");
impl Machine {
fn compile_special_forms(&mut self) {
@@ -185,6 +186,7 @@ impl Machine {
compile_user_module(self, DIF.as_bytes());
compile_user_module(self, FREEZE.as_bytes());
compile_user_module(self, REIF.as_bytes());
compile_user_module(self, ASSOC.as_bytes());
}
pub fn new() -> Self {