Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementation of functions with very basic scripting
    primarykey
    data
    text
    <p>I've been playing around with python for some time and decided to better my generalized understanding of programming languages by writing a custom script handler in python. I have so far successfully implemented a basic memory handler and hooked a memory address ordinate to printing to the screen. My question can be posed as:</p> <p>How can functions be implemented here? A goto statement is too easy, I would like to try something more difficult. (edit) Eventually i want to be able to do:</p> <pre><code>f0(x, y, z):=ax^by^cz </code></pre> <p>...in a shell that runs a script that runs this module (silly, eh?)</p> <pre><code># notes: separate addresses from data lest the loop of doom cometh class Interpreter: def __init__(self): self.memory = { } self.dictionary = {"mov" : self.mov, "put" : self.put, "add" : self.add, "sub" : self.sub, "clr" : self.clr, "cpy" : self.cpy, "ref" : self.ref } self.hooks = {self.val("0") : self.out } def interpret(self, line): x = line.split(" ") vals = tuple(self.val(y) for y in x[1:]) dereferenced = [] keys_only = tuple(key for key in self.memory) for val in vals: while val in self.memory: val = self.memory[val] dereferenced.append(val) vals = tuple(y for y in dereferenced) self.dictionary[x[0]](vals) def val(self, x): return tuple(int(y) for y in str(x).split(".")) def mov(self, value): self.ptr = value[0] def put(self, value): self.memory[self.ptr] = value[0] def clr(self, value): if self.ptr in self.hooks and self.ptr in self.memory: x = self.hooks[self.ptr] y = self.memory[self.ptr] for z in y: x(z) del self.memory[self.ptr] def add(self, values): self.put(self.mat(values, lambda x, y: x + y)) def sub(self, values): self.put(self.mat(values, lambda x, y: x - y)) def mat(self, values, op): a, b = self.memory[values[0]], self.memory[values[1]] if len(a) &gt; len(b): a, b = b, a c = [op(a[x], b[x]) for x in xrange(len(b))] + [x for x in a[len(a):]] return [tuple(x for x in c)] def cpy(self, value): self.put(value) def out(self, x): print chr(x), def ref(self, x): self.put(x) interp = Interpreter() for x in file(__file__.split('/')[-1].split(".")[-2] + ".why"): interp.interpret(x.strip()) </code></pre> <p>a sample script:</p> <pre><code>mov 1 put 104.101.108.108.111.10 mov 0 ref 1 clr 0 </code></pre> <p>(EDIT) I've made the decision to use this attempt as inspiration and start from scratch on this project. (Hopefully I'll find some real time to sit down and code before classes start up again.) <strike>I intend to award the best answer in a few days</strike>. I hope that that information fails to dissuade potential contributors from submitting anything they feel to be helpful for this sort of coding problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload