Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:39:28+00:00 2026-05-23T09:39:28+00:00

I’ve been playing around with python for some time and decided to better my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:39:28+00:00Added an answer on May 23, 2026 at 9:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.