]> Repositorios git - scryer-prolog.git/commitdiff
fixes to binding.
authorMark Thom <[email protected]>
Mon, 31 Oct 2016 03:49:27 +0000 (21:49 -0600)
committerMark Thom <[email protected]>
Mon, 31 Oct 2016 03:49:27 +0000 (21:49 -0600)
src/l0/ast.rs
src/l0/codegen.rs
src/l0/machine.rs

index f88af86f18ab00a5a77d06af5d63caf6ed93500a..ee08b35a1bbbe50c57d4bc2c44b29cc5166ac06b 100644 (file)
@@ -29,17 +29,8 @@ pub enum MachineInstruction {
 
 pub type Program = Vec<MachineInstruction>;
 
-#[derive(Clone, Copy, PartialEq)]    
+#[derive(Clone, Copy, PartialEq)]
 pub enum Addr {
     HeapCell(usize),
     RegNum(usize)
 }
-
-impl Addr {
-    pub fn heap_offset(&self) -> usize {
-        match self {
-            &Addr::HeapCell(hc) => hc,
-            &Addr::RegNum(reg)  => reg
-        }
-    }
-}
index c5a643bd9995ece0c68772dedf4a4090f90299f8..8f50e7b8f2fa0f4123fe58b28e75a34c37f89884 100644 (file)
@@ -147,6 +147,7 @@ pub fn compile_fact<'a>(t: &'a Term) -> Program {
                     if let &Term::Var(ref var) = t.as_ref() {
                         if !variable_allocs.contains_key(var) {
                             variable_allocs.insert(var, counter);
+
                             fact.push(MachineInstruction::UnifyVariable(counter));
                             counter += 1;
                         } else {
index e87c04eeda5ac77e173a648ba32a465d4cf945fe..e9146f9f19143b2ad2cef1b2d0bb737bab5ff8c3 100644 (file)
@@ -6,7 +6,7 @@ use std::vec::{Vec};
 #[derive(Clone)]
 enum HeapCell {
     NamedStr(usize, Atom),
-    Ref(usize), // these offsets are always in reference to cells on the Heap!
+    Ref(usize),
     Str(usize),
 }
 
@@ -53,11 +53,11 @@ impl MachineState {
 
         loop {
             if let &HeapCell::Ref(value) = self.lookup(a) {
-                if value != a.heap_offset() {
-                    a = Addr::HeapCell(value);
-                    continue;
-                } else {
-                    return a;
+                if let Addr::HeapCell(av) = a {
+                    if value != av {
+                        a = Addr::HeapCell(value);
+                        continue;
+                    }
                 }
             }
 
@@ -65,10 +65,10 @@ impl MachineState {
         };
     }
 
-    fn bind(&mut self, a: Addr, val: Addr) {
+    fn bind(&mut self, a: Addr, val: usize) {
         match a {
-            Addr::RegNum(reg)  => self.registers[reg] = HeapCell::Ref(val.heap_offset()),
-            Addr::HeapCell(hc) => self.heap[hc] = HeapCell::Ref(val.heap_offset()),
+            Addr::RegNum(reg)  => self.registers[reg] = HeapCell::Ref(val),
+            Addr::HeapCell(hc) => self.heap[hc] = HeapCell::Ref(val),
         };
     }
 
@@ -83,8 +83,10 @@ impl MachineState {
 
             if d1 != d2 {
                 match (self.lookup(d1), self.lookup(d2)) {
-                    (&HeapCell::Ref(_), _) | (_, &HeapCell::Ref(_)) =>
-                        self.bind(d1, d2),
+                    (&HeapCell::Ref(hc), _) =>
+                        self.bind(d2, hc),
+                    (_, &HeapCell::Ref(hc)) =>
+                        self.bind(d1, hc),
                     (&HeapCell::Str(a1), &HeapCell::Str(a2)) => {
                         let r1 = &self.heap[a1];
                         let r2 = &self.heap[a2];
@@ -134,7 +136,7 @@ impl MachineState {
 
                         let h = self.h;
 
-                        self.bind(Addr::RegNum(reg), Addr::HeapCell(h));
+                        self.bind(Addr::RegNum(reg), h);
 
                         self.h += 2;
                         self.mode = MachineMode::Write;