mirror of
https://github.com/lloeki/wasp.git
synced 2025-12-06 10:44:39 +01:00
cruft
This commit is contained in:
parent
854433b3db
commit
39b561bbb4
3 changed files with 0 additions and 89 deletions
13
factorial.py
13
factorial.py
|
|
@ -1,13 +0,0 @@
|
||||||
from lysssp import _eval, globs
|
|
||||||
|
|
||||||
_eval(['setq', 'factorial', ['lambda', ['x'],
|
|
||||||
['cond', [
|
|
||||||
[ ['equal?', 'x', 0], 1 ],
|
|
||||||
[ True, [ '*', 'x', ['factorial', ['-', 'x', 1]]]]]]]], globs)
|
|
||||||
|
|
||||||
def test():
|
|
||||||
print _eval(['factorial', 10], globs)
|
|
||||||
|
|
||||||
import timeit
|
|
||||||
|
|
||||||
print timeit.Timer(test).timeit(1)
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
from lysssp import _eval, globs
|
|
||||||
|
|
||||||
print _eval(["apply", ["quote", ["lambda", [], 10]], ["quote", [20]]], globs)
|
|
||||||
73
lysssp.py
73
lysssp.py
|
|
@ -1,73 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
import inspect
|
|
||||||
import re
|
|
||||||
|
|
||||||
globs = {}
|
|
||||||
|
|
||||||
def isprim(name):
|
|
||||||
return inspect.isfunction(globs.get(name, None))
|
|
||||||
|
|
||||||
def islazy(name):
|
|
||||||
if isprim(name): return name in ['cond', 'quote', 'setq']
|
|
||||||
return globs.get(name, [None])[0] == 'macro'
|
|
||||||
|
|
||||||
def isatom(name):
|
|
||||||
return not (type(name) == list or type(name) == dict)
|
|
||||||
|
|
||||||
def setq(sexpr, context):
|
|
||||||
globs[sexpr[0]] = sexpr[1]
|
|
||||||
return sexpr[1]
|
|
||||||
|
|
||||||
def _apply(fn, args, context):
|
|
||||||
if isprim(fn): return globs[fn](args, context)
|
|
||||||
context = dict(zip(globs[fn][1], args))
|
|
||||||
return _eval(globs[fn][2], context)
|
|
||||||
|
|
||||||
def _eval(sexpr, context):
|
|
||||||
if isatom(sexpr):
|
|
||||||
if sexpr in context:
|
|
||||||
return context[sexpr]
|
|
||||||
return sexpr
|
|
||||||
|
|
||||||
fn = sexpr[0]
|
|
||||||
args = sexpr[1:]
|
|
||||||
|
|
||||||
if not islazy(fn):
|
|
||||||
args = map(lambda n: _eval(n, context), args)
|
|
||||||
return _apply(fn, args, context)
|
|
||||||
|
|
||||||
def _cond(sexpr, context):
|
|
||||||
for elem in sexpr[0]:
|
|
||||||
if _eval(elem[0], context): return _eval(elem[1], context)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _read(sexpr):
|
|
||||||
grammar = r"(\()|(\))|([^()\s]+)|(\s+)"
|
|
||||||
|
|
||||||
def sequenceBuilder(match):
|
|
||||||
leftbracket, rightbracket, atom, whitespace = match.groups()
|
|
||||||
if(leftbracket): return '['
|
|
||||||
elif(rightbracket): return ']'
|
|
||||||
elif(atom): return '"' + atom + '"'
|
|
||||||
elif(whitespace): return ','
|
|
||||||
return eval(re.sub(grammar, sequenceBuilder, sexpr), None, None)
|
|
||||||
|
|
||||||
globs['setq'] = setq
|
|
||||||
globs['cond'] = _cond
|
|
||||||
globs['car'] = lambda sexpr, context: sexpr[0][0]
|
|
||||||
globs['cdr'] = lambda sexpr, context: sexpr[0][1:]
|
|
||||||
globs['quote'] = lambda sexpr, context: sexpr[0]
|
|
||||||
globs["apply"] = lambda sexpr, context: _apply(sexpr[0], sexpr[1], context)
|
|
||||||
globs['+'] = lambda sexpr, context: sexpr[0] + sexpr[1]
|
|
||||||
globs['-'] = lambda sexpr, context: sexpr[0] - sexpr[1]
|
|
||||||
globs['*'] = lambda sexpr, context: sexpr[0] * sexpr[1]
|
|
||||||
globs['/'] = lambda sexpr, context: sexpr[0] / sexpr[1]
|
|
||||||
globs['equal?'] = lambda sexpr, context: sexpr[0] == sexpr[1]
|
|
||||||
|
|
||||||
# FIXME: apply, eval the first arg
|
|
||||||
# TODO: define lambda func
|
|
||||||
# FIXME: sexpr=>lstruct
|
|
||||||
# TODO: _read, sexpr=>lstruct
|
|
||||||
# TODO: REPL while 1: print _eval(_read())
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue