From af3e72d442d1822e97fa4af92e0ef89e3939a45a Mon Sep 17 00:00:00 2001 From: Ryan R Date: Wed, 15 Feb 2017 23:59:01 -0600 Subject: [PATCH] implement #79 --- docs/commands.txt | 6 +++--- seriously/SeriouslyCommands.py | 2 +- seriously/seriously.py | 23 ++++++++++++++------ seriously/test/tests.py | 38 ++++++++++++++++++---------------- setup.py | 2 +- 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/docs/commands.txt b/docs/commands.txt index b954025..ba8bb67 100755 --- a/docs/commands.txt +++ b/docs/commands.txt @@ -94,7 +94,7 @@ 5D (]): end list literal 5E (^): pop a,b: push a XOR b 5F (_): pop a: push ln(a) -60 (`): function literal delimiter, pushes function whose body contains all of the commands until the next `. An implied ` is present at EOF if needed. +60 (`): prefix for function literal of length 1 (`p is the same as ⌠p⌡) 61 (a): invert the stack ([a,b,c,d] -> [d,c,b,a]) 62 (b): pop a: push 0 if a==0 else 1; pop "a" or [a]: push 0 if len(a)==0 else 1; pop f: push 0 if len(f)==0 else 1 63 (c): pop a: push character at ordinal a%256; pop [a],b: push [a].count(b); pop "a","b": push "a".count("b") @@ -242,8 +242,8 @@ F0 (≡): pop a: push eval(a) (Python-style evaluation) F1 (±): pop a: push -a (unary negate) F2 (≥): pop a,b: push a>=b F3 (≤): pop a,b: push a<=b -F4 (⌠): -F5 (⌡): +F4 (⌠): begin function literal +F5 (⌡): end function literal F6 (÷): pop a: push a list of all positive divisors of a that are less than or equal to a F7 (≈): pop a: push int(a) F8 (°): pop a: push radians(a) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index de35cc9..41e1185 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -154,7 +154,7 @@ def __str__(self): return '{}'.format(self.code) def __repr__(self): - return '`{}`'.format(self.code) + return '⌠{}⌡'.format(self.code) def __len__(self): return len(self.code) diff --git a/seriously/seriously.py b/seriously/seriously.py index 8fd8b72..353d8ca 100755 --- a/seriously/seriously.py +++ b/seriously/seriously.py @@ -170,15 +170,26 @@ def eval(self, code): if self.debug_mode: print("list: [{}]".format(l)) print(self.stack) - elif c == '`': - f = '' + elif c == '⌠': + fn = '' i += 1 - while i < len(code) and code[i] != '`': - f += code[i] + nest = 1 + while i < len(code): + if code[i] == '⌠': + nest += 1 + elif code[i] == '⌡': + nest -= 1 + if nest == 0: + break + fn += code[i] i += 1 + self.push(SeriouslyCommands.SeriousFunction(fn)) if self.debug_mode: - print('fn: {}'.format(f)) - self.push(SeriouslyCommands.SeriousFunction(f)) + print("fn: {}".format(fn)) + print(self.stack) + elif c == '`': + i += 1 + self.push(SeriouslyCommands.SeriousFunction(code[i])) elif ord(c) in range(48, 58): self.push(int(c)) elif ord_cp437(c) == 0x0B: diff --git a/seriously/test/tests.py b/seriously/test/tests.py index 5301741..d817444 100755 --- a/seriously/test/tests.py +++ b/seriously/test/tests.py @@ -140,8 +140,10 @@ def test_numerics(self): self.assert_serious(':+', [0]) def test_functions(self): - self.assert_serious("`foo`", [SeriousFunction("foo")]) - self.assert_serious("`foo`$", ["foo"]) + self.assert_serious("`f", [SeriousFunction("f")]) + self.assert_serious("⌠foo⌡", [SeriousFunction("foo")]) + self.assert_serious("⌠⌠foo⌡⌡", [SeriousFunction("⌠foo⌡")]), + self.assert_serious("⌠foo⌡$", ["foo"]) def test_eval(self): self.assert_serious('"len(set([1,2,2,3]))"{}'.format(chr_cp437(0xF0)), @@ -252,7 +254,7 @@ def test_arithmetic(self): self.assert_serious(':1+2j'+chr_cp437(0xD7), [2, 1]) self.assert_serious('6:21▲', [42]) # weird prime bug test - self.assert_serious('9uyX9uR`p`░', [[2, 3, 5, 7]]) + self.assert_serious('9uyX9uR`p░', [[2, 3, 5, 7]]) def test_lists(self): self.assert_serious('[1][1,2]-', [[2]]) @@ -390,7 +392,7 @@ def test_string_methods(self): self.assert_serious('"abcd"'+chr_cp437(0x8A), ["'abcd'"]) self.assert_serious(':123.45'+chr_cp437(0x8A), ['123.45']) self.assert_serious(':1+2i'+chr_cp437(0x8A), ['(1+2j)']) - self.assert_serious('`foo`'+chr_cp437(0x8A), ['`foo`']) + self.assert_serious('⌠foo⌡'+chr_cp437(0x8A), ['⌠foo⌡']) self.assert_serious('"1.23"i', [1.23]) self.assert_serious('"123"R', ["321"]) self.assert_serious('"abc"3*', ['abcabcabc']) @@ -467,8 +469,8 @@ def test_list_methods(self): self.assert_serious('3R#', [[1,2,3]]) self.assert_serious('[1,2,3][0,1]'+chr_cp437(0xB0), [[2]]) self.assert_serious('3R2r'+chr_cp437(0xB0), [[2]]) - self.assert_serious('[1,2,3]`2>`'+chr_cp437(0xB0), [[1]]) - self.assert_serious('3R`2>`'+chr_cp437(0xB0), [[1]]) + self.assert_serious('[1,2,3]⌠2>⌡'+chr_cp437(0xB0), [[1]]) + self.assert_serious('3R⌠2>⌡'+chr_cp437(0xB0), [[1]]) self.assert_serious('[1,2,3]N', [3]) self.assert_serious('3RN', [3]) self.assert_serious('[1,2,3]F', [1]) @@ -499,22 +501,22 @@ def test_bases(self): class FunctionTests(SeriousTest): def test_function_methods(self): - self.assert_serious('`foo`'+chr_cp437(0x9C), ['foo']) + self.assert_serious('⌠foo⌡'+chr_cp437(0x9C), ['foo']) self.assert_serious('"foo"'+chr_cp437(0x9C), [SeriousFunction('foo')]) self.assert_serious('5'+chr_cp437(0x9C), [5]) - self.assert_serious('`foo`l', [3]) - self.assert_serious('`bar``foo`+', [SeriousFunction('foobar')]) - self.assert_serious('`foo`3*', [SeriousFunction('foofoofoo')]) - self.assert_serious('["oo"]`f%s`%', [SeriousFunction('foo')]) - self.assert_serious('`foo`"foo"=', [1]) - self.assert_serious('`foo``bar`=', [0]) - self.assert_serious('`foo`3=', [3, SeriousFunction('foo')]) - self.assert_serious('[1,2,3]`++`R', [[6]]) - self.assert_serious('3`1`n', [1,1,1]) - self.assert_serious('5`2@%Y`'+chr_cp437(0xD6), [[0,2,4,6,8]]) + self.assert_serious('⌠foo⌡l', [3]) + self.assert_serious('⌠bar⌡⌠foo⌡+', [SeriousFunction('foobar')]) + self.assert_serious('⌠foo⌡3*', [SeriousFunction('foofoofoo')]) + self.assert_serious('["oo"]⌠f%s⌡%', [SeriousFunction('foo')]) + self.assert_serious('⌠foo⌡"foo"=', [1]) + self.assert_serious('⌠foo⌡⌠bar⌡=', [0]) + self.assert_serious('⌠foo⌡3=', [3, SeriousFunction('foo')]) + self.assert_serious('[1,2,3]⌠++⌡R', [[6]]) + self.assert_serious('3`1n', [1,1,1]) + self.assert_serious('5⌠2@%Y⌡'+chr_cp437(0xD6), [[0,2,4,6,8]]) def test_combinators(self): - self.assert_serious('3`1kMD`Y', [0]) + self.assert_serious('3⌠1kMD⌡Y', [0]) class RandomTests(SeriousTest): def test_random(self): diff --git a/setup.py b/setup.py index 1bd0b65..bdfdd47 100755 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setup( name='seriously', - version='2.0.70', + version='2.1.0', description='A Python-based golfing language', long_description='Seriously is a Python-based golfing language. See the GitHub page for more details.',