Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of functions with very basic scripting

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:

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:

f0(x, y, z):=ax^by^cz

...in a shell that runs a script that runs this module (silly, eh?)

# 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) > 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())

a sample script:

mov 1
put 104.101.108.108.111.10
mov 0
ref 1
clr 0

(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.) I intend to award the best answer in a few days. I hope that that information fails to dissuade potential contributors from submitting anything they feel to be helpful for this sort of coding problem.

like image 387
motoku Avatar asked Jun 17 '11 10:06

motoku


People also ask

What are scripting functions?

A function is a named set of statements that perform a certain task. Functions have unique names. A function can take a set of arguments on which to operate on and return a value that represents the result of the task it has performed.

How is the functions implemented in a programming language?

Functions are implemented in programming languages by translating the definition of the function that you provide in your program text into a more machine-friendly form, which can either be directly executed by the machine or interpreted by another program that runs on the machine.

How do you implement a function in Python?

Basic Syntax for Defining a Function in Python In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon. The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you.


1 Answers

I am struggling a bit to understand what you are asking. Where is your function definition to be given? In the script handler or in the script?

If it is in the script handler, the obvious solution would be to use the lambda expression. Using the example you used in the question f0(x, y, z):=x^2 would translate in:

>>> f0 = lambda x, y, z : x**2
>>> f0(2,3,4)
4

If the function definitions are to be placed in the script itself, you could get away with a combination of lambda and eval expressions. Here's a quick example that I just hammered together to illustrate the idea.

class ScriptParser(object):

    # See 'to_python' to check out what this does
    mapping = {'^':'**', '!':' not ', '&':' and '}

    def to_python(self, calc):
        '''
        Parse the calculation syntax from the script grammar to the python one.
        This could be grown to a more complex parser, if needed. For now it will
        simply assume any operator as defined in the grammar used for the script
        has an equivalent in python.
        '''
        for k, v in self.mapping.items():
            calc = calc.replace(k, v)
        return calc

    def feed(self, lfs):
        '''
        Parse a line of the script containing a function defintion
        '''
        signature, calc = lfs.split(':=')
        funcname, variables = [s.strip() for s in signature.split('(')]
        # as we stripped the strings, it's now safe to do...'
        variables = variables[:-1]
        setattr(self, funcname,
                eval('lambda ' + variables + ' : ' + self.to_python(calc)))

def main():
    lines = ['f0(x, y, z) := x^2',
             'f1(x) := x**2 + x**3 + x*1000']
    sp = ScriptParser()
    for line in lines:
        sp.feed(line)
        print('Script definition  : %s' % line)
    for i in range(5):
        res0 = sp.f0(i, None, None)
        res1 = sp.f1(i)
        print('f0(%d) = %d' % (i, res0))
        print('f1(%d) = %d' % (i, res1))
        print('--------')

if __name__ == '__main__':
    main()

Running this program outputs:

Script definition  : f0(x, y, z) := x^2
Script definition  : f1(x) := x**2 + x**3 + x*1000
f0(0) = 0
f1(0) = 0
--------
f0(1) = 1
f1(1) = 1002
--------
f0(2) = 4
f1(2) = 2012
--------
f0(3) = 9
f1(3) = 3036
--------
f0(4) = 16
f1(4) = 4080
--------

Keep in mind though that:

  1. Using eval has security implications that you should be aware of.
  2. Writing your own grammar parser is a truly cool learning experience!! :)

HTH, Mac.

like image 138
mac Avatar answered Oct 21 '22 11:10

mac