Welcome, guest | Sign In | My Account | Store | Cart

a small language in python

Python, 123 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class Stack:
    def __init__(self):
        self.data = []
    def push(self, x):
        self.data.append(x)
    def pop(self):
        return self.data.pop()
class Interpreter:

    def __init__(self):
        self.stack = Stack()

    def process(self, word):
        if is_int(word):
            n = int(word)
            self.stack.push(n)
        elif '"' in word:
            self.stack.push(word.strip('"'))
        else:
            try:
                f = words[word]
            except KeyError:
                raise SyntaxError, 'ERRor Unknown word\n\t'+word+'!!!'

            # determine number of arguments
            n = num_args(f)

            # pop that number of args from stack
            args = reversed([self.stack.pop() for i in range(n)])

            # call f with that list
            result = f(*args)

            # take return value (a list)
            # push elements on stack
            for x in result or []:
                self.stack.push(x)

    def process_line(self, line):
        words = line.split()
        for word in words:
            self.process(word)
def _print(x):
    print x
def _pas():
    pass
def _exit():
    exit()
        
def _help():
    print '''
    words = {
    '+': lambda x, y: [x+y],
    '-': lambda x, y: [x-y],
    '*': lambda x, y: [x*y],
    '/': lambda x, y: [x/y],
    '==': lambda x, y: [x==y],
    '!=': lambda x, y: [x!=y],
    '>': lambda x, y: [x>y],
    '<': lambda x, y: [x<y],
    'dup': lambda x: [x, x],
    'drop': lambda x: [],
    'swap': lambda x, y: [y, x],
    'print': _print,
    'chr': lambda x: [chr(x)],
    'sweep': _pas,
    'scan': lambda x: [raw_input(x)],
    'exit': _exit,
    'help': _help
    }
    '''
words = {
    '+': lambda x, y: [x+y],
    '-': lambda x, y: [x-y],
    '*': lambda x, y: [x*y],
    '/': lambda x, y: [x/y],
    '==': lambda x, y: [x==y],
    '!=': lambda x, y: [x!=y],
    '>': lambda x, y: [x>y],
    '<': lambda x, y: [x<y],
    'dup': lambda x: [x, x],
    'drop': lambda x: [],
    'swap': lambda x, y: [y, x],
    'print': _print,
    'chr': lambda x: [chr(x)],
    'sweep': _pas,
    'scan': lambda x: [raw_input(x)],
    'exit': _exit,
    'help': _help,
    '.': _pas
    
}
def num_args(f):
    """ Return the number of arguments that function <f> takes. """
    return f.func_code.co_argcount

def is_int(s):
    try:
        int(s)
    except ValueError:
        return False
    else:
        return True
if __name__ == "__main__":
    import sys
    e = Interpreter()
    main = True
    print '==================================================='
    print '***************************************************'
    print 'drinkable_chicken modified version 1.0.0 6.13.2013'
    print 'A.W.T. 10 YRS. old'
    print '***************************************************'
    print 'IDE 1.0.1'
    print '==================================================='
    while main == True:
        line = raw_input('> ')
        if '.' in line:
            e.process_line(line)
            if 'sweep' in line:
                e.stack.data = []
            print "stack:", e.stack.data
        else:
            print 'ERRor command \n\tterminator not found!'
    

try adding a if statement

1 comment

Ryan Gonzalez 10 years, 8 months ago  # | flag

Not sure if it's a glitch, but I had to change:

e.process_line(line)

to:

e.process_line(line.rstrip('.'))