Skip to content

Commit

Permalink
functions work!
Browse files Browse the repository at this point in the history
  • Loading branch information
Mego committed Nov 10, 2015
1 parent 9dad41e commit 8745d7c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
52 changes: 47 additions & 5 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def __getattr__(self, fn):

math = Math()

class SeriousFunction(object):
def __init__(self, code):
self.code = code
def __call__(self,srs):
srs.eval(self.code,print_at_end=False)
def __str__(self):
return '`%s`'%self.code
__repr__ = __str__
def __len__(self):
return len(self.code)

def is_prime(x):
global primes
if x in primes:
Expand Down Expand Up @@ -186,7 +197,7 @@ def dupe_each_fn(srs):
tmp.append(a)
srs.stack=tmp[:]

def r_fn(srs):
def lr_fn(srs):
a=srs.pop()
if type(a) is StringType:
map(srs.push,a.split('')[::-1])
Expand All @@ -213,6 +224,35 @@ def comp_fn(srs):
else:
srs.push(a)

def map_fn(srs):
f,l=srs.pop(),srs.pop()
res=[]
for x in l:
s = srs.make_new(x)
f(s)
res+=s.stack
srs.push(res)

def r_fn(srs):
a=srs.pop()
if isinstance(a,SeriousFunction):
b=srs.pop()
s=srs.make_new(*b)
a(s)
srs.push(s.stack)
elif type(a) in [StringType,ListType]:
srs.push(a[::-1])
else:
srs.push(range(1,a+1))

def n_fn(srs):
a,b=srs.pop(),srs.pop()
for i in range(b):
if isinstance(a, SeriousFunction):
a(srs)
else:
srs.push(a)

fn_table={32:lambda x:x.push(len(x.stack)),
33:lambda x:x.push(math.factorial(x.pop())),
36:lambda x:x.push(str(x.pop())),
Expand All @@ -224,7 +264,7 @@ def comp_fn(srs):
43:lambda x:x.push(x.pop()+x.pop()),
44:lambda x:x.push(input()),
45:lambda x:x.push(x.pop()-x.pop()),
46:lambda x:print(x.pop()),
46:lambda x:(lambda y:print(y) if not isinstance(y,SeriousFunction) else y(x) or print(x.pop()))(x.pop()),
47:div_fn,
59:dupe_fn,
60:lambda x:x.push(int(x.pop()<x.pop())),
Expand All @@ -240,9 +280,10 @@ def comp_fn(srs):
73:if_fn,
75:lambda x:x.push(ceil(x.pop())),
76:lambda x:x.push(floor(x.pop())),
77:map_fn,
79:lambda x:map(lambda y:map(x.push,map(ord,y)[::-1]),x.pop()[::-1]),
80:lambda x:x.push(nth_prime(x.pop())),
82:lambda x:x.push(range(1,x.pop()+1)),
82:r_fn,
83:lambda x:x.push(math.sin(x.pop())),
84:lambda x:x.push(math.tan(x.pop())),
85:lambda x:x.push(list(set(x.pop()).union(x.pop()))),
Expand All @@ -263,11 +304,11 @@ def comp_fn(srs):
107:to_list_fn,
108:lambda x:x.push(len(x.pop())),
109:lambda x:map(x.push,math.modf(x.pop())),
110:lambda x:map(x.push,(lambda y,z:[y for _ in range(z)])(x.pop(),x.pop())),
110:n_fn,
111:psh_fn,
112:p_fn,
113:enq_fn,
114:r_fn,
114:lr_fn,
115:lambda x:x.push(math.sgn(x.pop())),
116:flat_explode_fn,
117:lambda x:x.push(x.pop()+1),
Expand All @@ -293,6 +334,7 @@ def comp_fn(srs):
144:lambda x:x.push(math.tanh(x.pop())),
155:lambda x:x.push(math.copysign(x.pop(),x.pop())),
158:lambda x:x.push(cmath.phase(x.pop())),
159:lambda x:x.pop()(x),
160:lambda x:x.push((lambda z:complex(z.real,-z.imag))(x.pop())),
166:lambda x:x.push(x.pop()**2),
167:lambda x:x.push(math.degrees(x.pop())),
Expand Down
4 changes: 2 additions & 2 deletions commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ When possible, the character at each code point is listed in parentheses followi
74 (J):
75 (K): pop a: push ceil(a)
76 (L): pop a: push floor(a)
77 (M): pop �: pop [a] n times where n is the arity of �, execute R n times using � and each [a], push each [a] (similar to map(�,[a]))
77 (M): pop �,[a], execute � for each element in [a], using the element as a temporary stack, push [a] (similar to map(�,[a]))
78 (N): if stack is empty: print the lyrics to "99 Bottles of Beer" to stdout
79 (O): pop "a" or [a]: push ord(c) for each c in "a" or [a], starting from the end. If a list is popped and it contains strings of length > 1, the strings are exploded in-place (["ABC"] -> [65,66,67], [["A","B","C"]] -> [65,66,67])
79 (O): pop "a" or [a]: push ord(c) for each c in "a" or [a], starting from the end. If a list is popped and it contains strings of length > 1, the strings are exploded in-place (["ABC"] -> [65,66,67], [["A","B","CD"]] -> [65,66,67,68])
80 (P): pop a: push the a-th prime (zero-indexed)
81 (Q): if stack is empty: print the program's source code to stdout
82 (R): pop �,[a]: call �, using [a] as a temporary stack, push [a] (similar to reduce(�,[a]))
Expand Down
27 changes: 10 additions & 17 deletions seriously.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python

import sys, math, cmath, itertools, functools
import sys, math, cmath, itertools, functools, traceback
from types import *
import commands

Expand All @@ -17,20 +17,12 @@ def NinetyNineBottles():
print(y + '\n' + w)
i += 1

class SeriousFunction(object):
def __init__(self, code, srs):
self.srs = srs
self.code = code
def __call__(self):
self.srs.eval(self.code)
def __str__(self):
return '`%s`'%code
def __repr__(self):
self.__call__()
def __len__(self):
return len(str(self))

class Seriously(object):
@classmethod
def make_new(cls,*stack):
res = cls()
res.stack=list(stack)
return res
def __init__(self):
self.stack = []
self.repl_mode = False
Expand All @@ -41,7 +33,7 @@ def pop(self):
return self.stack.pop(0)
def append(self, val):
self.stack+=[val]
def eval(self, code):
def eval(self, code, print_at_end=True):
i=0
while i < len(code):
c = code[i]
Expand Down Expand Up @@ -89,7 +81,7 @@ def eval(self, code):
while i<len(code) and code[i]!='`':
f+=code[i]
i+=1
self.push(SeriousFunction(f))
self.push(commands.SeriousFunction(f))
elif c == 'Q' and len(self.stack) == 0:
print code
elif ord(c) in range(48,58):
Expand All @@ -105,9 +97,10 @@ def eval(self, code):
try:
self.fn_table.get(ord(c), lambda x:x)(self)
except:
traceback.print_exc()
self.stack = old_stack[:]
i+=1
if not self.repl_mode:
if not self.repl_mode and print_at_end:
while len(self.stack) > 0:
print self.pop()

Expand Down

0 comments on commit 8745d7c

Please sign in to comment.