view interps/lambda/lambda.py @ 6934:04f9cc15cad5

<hppavilion[1]> le/rn last-class function/A last-class function is a function that cannot be passed as an argument, accept a function as an argument, be returned by a function, return a function, set to a variable other than its initial name, or be called
author HackBot
date Tue, 23 Feb 2016 00:02:33 +0000
parents 859f9b4339e6
children
line wrap: on
line source

#!/usr/bin/python

import parser, tokenizer, evaluator

import sys, pickle
sys.setrecursionlimit(65535) # not too big, but hopefully big enough for some stuff


try:
    line = raw_input()
except EOFError:
    raise SystemExit

# tokenize
try:
    tokens = tokenizer.tokenize(line)
except Exception, line:
    print "Tokenizer error:", line
    raise SystemExit

# Get a pickled environment
try:
    env = pickle.load(open('lambda/defs.pickle'))
except:
    env = {}

# parse
try:
    exp, defs = parser.parse(tokens, env)
except Exception, line:
    print "Parser error:", line
    raise SystemExit

env.update(defs)

# evaluate

try:
    if exp != None:
        evaluator.eval(exp, env)
except Exception, line:
    print "Run-time error:", line
    raise SystemExit

# finally, save the definitions

f = open('lambda/defs.pickle', 'w')
pickle.dump(env, f)