git @ Cat's Eye Technologies
Attempts at intepreter and Fibonacci, dug out of the crawlspace. catseye 9 years ago
2 changed file(s) with 111 addition(s) and 0 deletion(s).
 0 // Despite much work, this is still such garbage. 1 2 class Fibonacci(Print,Input,Stop,FibLhs,FibRhs) extends Program { 3 FibLhs fl; 4 Print p; 5 Input i; 6 method continue(Passive accumulator) { 7 p = new Print(Continuation); 8 p.next = new Stop(); 9 fl = new FibLhs(FibLhs,FibRhs); 10 fl.next = p 11 i = new Input(Continuation); 12 i.next = fl; 13 goto i.continue(accumulator); 14 } 15 } 16 17 class FibLhs(FibLhs,FibRhs) extends Continuation { 18 Continuation next; 19 FibLhs fl; 20 FibRhs fr; 21 22 method continue(Passive accumulator) { 23 i = new IfOneOrZero() 24 cw = new ContinueWithOne() 25 cw.next = next 26 i.trueBranch = cw 27 i.falseBranch = 28 fl = new FibLhs(FibLhs,FibRhs) 29 fr = new FibRhs(FibLhs,FibRhs) 30 fr.value = n - 2 31 fr.next = next 32 fl.next = fr 33 goto f.continue(n - 1) 34 } 35 } 36 37 class FibRhs(Continuation,Passive,Add,Fib) extends Continuation { 38 Continuation next; 39 Passive arg; 40 method continue(Passive accumulator) { 41 a = new Add(~,~) 42 a.next = next 43 a.lhs = accumulator 44 f = new Fib(~,~,~,~) 45 f.next = a 46 goto f.continue(arg); 47 } 48 } 49 50 class ContinueWithOne() extends Continuation { 51 Continuation next; 52 method continue(Passive accumulator) { 53 goto next.continue(1); 54 } 55 }
 0 # -*- coding: utf-8 -*- 1 2 """ 3 Interpreter for the Unlikely programming language. XXX incomplete! 4 \$Id: interpreter.py 509 2010-04-27 20:15:32Z cpressey \$ 5 """ 6 7 import ast 8 9 class Instance(object): 10 """ 11 Represents an instance of an Unlikely class, at runtime. 12 """ 13 14 def __init__(self, class_defn): 15 self.class_defn = class_defn 16 self.props = {} 17 18 def set(self, prop_name, value): 19 self.props[prop_name] = value 20 21 def get(self, prop_name): 22 return self.props[prop_name] 23 24 def execute(self, method_name, args): 25 """ 26 Executes a method name in this instance. 27 Returns a new instance and method name to be executed next. 28 """ 29 for arg in args: 30 self.set(arg, args[arg]) 31 method_defn = self.class_defn.lookup_method_defn(method_name) 32 for assignment in method_defn.assignments: 33 self.assign(assignment.lhs, assignment.rhs) 34 # apply args in method_defn.continue_ 35 # get instance from method_defn.continue_ 36 # get method name from method_defn.continue_ 37 return (instance, method_name) 38 39 def assign(self, lhs, rhs): 40 (instance, prop_name) = self.resolve_qual_name(lhs) 41 if isinstance(rhs, Construction): 42 value = self.eval_construction(rhs) 43 else: 44 (src_instance, src_prop_name) = self.resolve_qual_name(rhs) 45 value = src_instance.get(src_prop_name) 46 instance.set(prop_name, value) 47 48 def resolve_qual_name(self, qual_name, pos = 0): 49 prop_defn = qual_name.get_prop_defn_by_index(pos) 50 prop_name = prop_defn.name 51 if pos == qual_name.length(): 52 return (self, prop_name) 53 instance = self.get(prop_name) 54 return instance.resolve_qual_name(qual_name, pos + 1)