Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: nooodl/gs2
base: f1ea247eaf
...
head fork: nooodl/gs2
compare: 251681d2d1
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 72 additions and 18 deletions.
  1. +64 −10 gs2.py
  2. +8 −8 gs2c.py
View
74 gs2.py
@@ -2,6 +2,7 @@
# (c) nooodl 2014
import inspect
+import itertools as it
import math
import random
import re
@@ -15,7 +16,7 @@
Block = namedtuple('Block', 'code')
-DEBUG = True
+DEBUG = False
def log(x):
if not DEBUG: return
@@ -46,8 +47,9 @@ def tokenize(prog):
log(tokens[i:])
if t == '\x08': # open block
blocks.append(Block([]))
+ final.append('')
elif t == '\x09': # close block
- blocks[-2].code.append(blocks.pop())
+ blocks[-2].code.append(blocks.pop() + final.pop())
elif t == '\x4d': # open 1-token block
blocks[-1].code.append(Block([tokens[i + 1]]))
i += 1
@@ -59,18 +61,18 @@ def tokenize(prog):
blocks[-1].code.append(Block([tokens[i + 1], tokens[i + 2]]))
blocks[-1].code.append('\x34')
i += 2
- elif t == '\x5e': # open 1-token filter
+ elif t == '\x5c': # open 1-token filter
blocks[-1].code.append(Block([tokens[i + 1]]))
blocks[-1].code.append('\x35')
i += 1
- elif t == '\x5f': # open 2-token filter
+ elif t == '\x5d': # open 2-token filter
blocks[-1].code.append(Block([tokens[i + 1], tokens[i + 2]]))
blocks[-1].code.append('\x35')
i += 2
- elif t == '\x6e': # open rest-of-program map
+ elif t == '\x5e': # open rest-of-program map
blocks.append(Block([]))
final.append('\x34')
- elif t == '\x6f': # open rest-of-program filter
+ elif t == '\x5f': # open rest-of-program filter
blocks.append(Block([]))
final.append('\x35')
else:
@@ -162,6 +164,58 @@ def set_or(a, b):
def set_xor(a, b):
return set_diff(a, b) + set_diff(b, a)
+# prime number stuff
+prime_list = []
+sieved = 2
+composite = set([1])
+
+def sieve(limit):
+ global prime_list
+ global sieved
+ global composite
+ if limit <= sieved: return
+
+ prime_list = []
+ for i in range(2, limit):
+ if i in composite: continue
+ for j in range(i*2, limit, i):
+ composite.add(j)
+ prime_list.append(i)
+ sieved = limit
+
+sieve(1000)
+
+def is_prime(n):
+ global prime_list
+ sieve(n+1)
+ return n not in composite
+
+def nth_prime(n):
+ global prime_list
+ sieve(int(math.log(n) * n) + 100)
+ return prime_list[n-1]
+
+def n_primes(n):
+ global prime_list
+ sieve(int(math.log(n) * n) + 100)
+ return prime_list[:n]
+
+def primes_below(n):
+ global prime_list
+ sieve(n+1)
+ return list(it.takewhile(lambda x: x < n, prime_list))
+
+def next_prime(n):
+ n += 1
+ while not is_prime(n): n += 1
+ return n
+
+def totient(n):
+ count = 0
+ for i in xrange(1, n+1):
+ if gcd(n, i) == 1: count += 1
+ return count
+
class GS2(object):
def __init__(self, code, stdin=''):
self.code = code
@@ -323,14 +377,14 @@ def evaluate(self, block):
else:
raise TypeError('sign / min')
- elif t == '\x29': # parity / max
+ elif t == '\x29': # thousand / max
x = self.stack.pop()
if is_num(x):
- self.stack.append(x & 1)
+ self.stack.append(x * 1000)
elif is_list(x):
self.stack.append(max(x))
else:
- raise TypeError('parity / max')
+ raise TypeError('thousand / max')
elif t == '\x2a': # double / lines
x = self.stack.pop()
@@ -694,7 +748,7 @@ def f(z):
x = self.stack.pop()
self.stack.append(to_gs(', '.join(map(show, x)).join('[]')))
- # 5e 5f are special
+ # 5c 5d 5e 5f are special
elif t == '\x60': # logical and
y = self.stack.pop()
x = self.stack.pop()
View
16 gs2c.py
@@ -16,12 +16,12 @@
'{': '\x08', # open block
'}': '\x09', # close block
"'": '\x4d', # open 1-token block
- '*': '\x4e', # open 1-token map
- '**': '\x4f', # open 2-token map
- '?': '\x5e', # open 1-token filter
- '??': '\x5f', # open 2-token filter
- '*:': '\x6e', # open rest-of-program map
- '?:': '\x6f', # open rest-of-program filter
+ 'm': '\x4e', # open 1-token map
+ 'mm': '\x4f', # open 2-token map
+ 'f': '\x5c', # open 1-token filter
+ 'ff': '\x5d', # open 2-token filter
+ 'm:': '\x5e', # open rest-of-program map
+ 'f:': '\x5f', # open rest-of-program filter
'new-line': '\x0a',
'empty-list': '\x0b',
@@ -38,7 +38,7 @@
'dec left-uncons': '\x26',
'inc right-uncons': '\x27',
'sign min': '\x28',
- 'parity max': '\x29',
+ 'thousand max': '\x29',
'double lines': '\x2a',
'half unlines': '\x2b',
'square words': '\x2c',
@@ -102,7 +102,7 @@
mnemonics = {w: v for k,v in mnemonics.items() for w in k.split()}
-for i, c in enumerate('abcd', 1):
+for i, c in enumerate('abcd'):
mnemonics['save-%s' % c] = chr(0xF0 + i)
mnemonics['put-%s' % c] = chr(0xF4 + i)
mnemonics['get-%s' % c] = chr(0xF8 + i)

No commit comments for this range

Something went wrong with that request. Please try again.