Commits

catseye  committed 2b91752

Attempts at intepreter and Fibonacci, dug out of the crawlspace.

  • Participants
  • Parent commits 688070f

Comments (0)

Files changed (2)

File eg/fibonacci.unlikely

+// Despite much work, this is still such garbage.
+
+class Fibonacci(Print,Input,Stop,FibLhs,FibRhs) extends Program {
+  FibLhs fl;
+  Print p;
+  Input i;
+  method continue(Passive accumulator) {
+    p = new Print(Continuation);
+    p.next = new Stop();
+    fl = new FibLhs(FibLhs,FibRhs);
+    fl.next = p
+    i = new Input(Continuation);
+    i.next = fl;
+    goto i.continue(accumulator);
+  }
+}
+
+class FibLhs(FibLhs,FibRhs) extends Continuation {
+  Continuation next;
+  FibLhs fl;
+  FibRhs fr;
+  
+  method continue(Passive accumulator) {
+    i = new IfOneOrZero()
+    cw = new ContinueWithOne()
+    cw.next = next
+    i.trueBranch = cw
+    i.falseBranch = 
+          fl = new FibLhs(FibLhs,FibRhs)
+          fr = new FibRhs(FibLhs,FibRhs)
+          fr.value = n - 2
+          fr.next = next
+          fl.next = fr
+          goto f.continue(n - 1)
+  }
+}
+
+class FibRhs(Continuation,Passive,Add,Fib) extends Continuation {
+  Continuation next;
+  Passive arg;
+  method continue(Passive accumulator) {
+    a = new Add(~,~)
+    a.next = next
+    a.lhs = accumulator
+    f = new Fib(~,~,~,~)
+    f.next = a
+    goto f.continue(arg);
+  }
+}
+
+class ContinueWithOne() extends Continuation {
+  Continuation next;
+  method continue(Passive accumulator) {
+    goto next.continue(1);
+  }
+}

File src/unlikely/interpreter.py

+# -*- coding: utf-8 -*-
+
+"""
+Interpreter for the Unlikely programming language.  XXX incomplete!
+$Id: interpreter.py 509 2010-04-27 20:15:32Z cpressey $
+"""
+
+import ast
+
+class Instance(object):
+    """
+    Represents an instance of an Unlikely class, at runtime.
+    """
+    
+    def __init__(self, class_defn):
+        self.class_defn = class_defn
+        self.props = {}
+
+    def set(self, prop_name, value):
+        self.props[prop_name] = value
+
+    def get(self, prop_name):
+        return self.props[prop_name]
+
+    def execute(self, method_name, args):
+        """
+        Executes a method name in this instance.
+        Returns a new instance and method name to be executed next.
+        """
+        for arg in args:
+            self.set(arg, args[arg])
+        method_defn = self.class_defn.lookup_method_defn(method_name)
+        for assignment in method_defn.assignments:
+            self.assign(assignment.lhs, assignment.rhs)
+        # apply args in method_defn.continue_
+        # get instance from method_defn.continue_
+        # get method name from method_defn.continue_
+        return (instance, method_name)
+
+    def assign(self, lhs, rhs):
+        (instance, prop_name) = self.resolve_qual_name(lhs)
+        if isinstance(rhs, Construction):
+            value = self.eval_construction(rhs)
+        else:
+            (src_instance, src_prop_name) = self.resolve_qual_name(rhs)
+            value = src_instance.get(src_prop_name)
+        instance.set(prop_name, value)
+
+    def resolve_qual_name(self, qual_name, pos = 0):
+        prop_defn = qual_name.get_prop_defn_by_index(pos)
+        prop_name = prop_defn.name
+        if pos == qual_name.length():
+            return (self, prop_name)
+        instance = self.get(prop_name)
+        return instance.resolve_qual_name(qual_name, pos + 1)