Skip to content

Commit 8580b0d

Browse files
committed
Close #14 and #15
1 parent 2542f77 commit 8580b0d

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

logicode.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
rInput = re.compile(r"\binput\b")
2020
rScope = re.compile(r"\b__scope__\b")
2121
rInfix = re.compile(r"[&|]")
22-
rPrefix = re.compile(r"!")
23-
rPostfix = re.compile(r"\[[ht]\]")
22+
rPrefix = re.compile(r"[!$]")
23+
rPostfix = re.compile(r"[<>]")
2424
rOpenParenthesis = re.compile(r"\(")
2525
rCloseParenthesis = re.compile(r"\)")
2626
rOpenBracket = re.compile(r"\[")
@@ -39,6 +39,17 @@
3939
rLinestart = re.compile("^", re.M)
4040
rGetParentFunctionName = re.compile("<function ([^.]+)")
4141

42+
# Utility functions
43+
44+
def Binarify(number):
45+
if not number:
46+
return [0]
47+
result = []
48+
while number:
49+
result = [number % 2] + result
50+
number //= 2
51+
return result
52+
4253
# Parser functions
4354

4455
def Noop(argument):
@@ -111,16 +122,18 @@ def Expression(result):
111122
if isinstance(operator, basestring) and rPrefix.match(operator):
112123
if operator == "!":
113124
return lambda scope: list(map(int, map(op.not_, result[1](scope))))
125+
if operator == "$":
126+
return lambda scope: Binarify(len(result[1](scope)))
114127
if isinstance(result[1], list):
115128
operators = result[1]
116129
start_index = 0
117130
head = False
118131
for wrapped_operator in operators:
119132
operator = wrapped_operator[0]
120-
if operator == "[h]":
133+
if operator == "<":
121134
head = True
122135
break
123-
if operator == "[t]":
136+
if operator == ">":
124137
start_index += 1
125138
if head:
126139
return lambda scope: [result[0](scope)[start_index]]

test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ def test_not(self):
1919
self.assertEqual(Run("!1"), [0])
2020
self.assertEqual(Run("!0"), [1])
2121

22+
def test_length(self):
23+
self.assertEqual(Run("$1000"), [1, 0, 0])
24+
self.assertEqual(Run("$1"), [1])
25+
self.assertEqual(Run("$1111111111"), [1, 0, 1, 0])
26+
2227
def test_plus(self):
2328
self.assertEqual(Run("1+1"), [1, 1])
2429
self.assertEqual(Run("1&1&1&1+1&1&1&1"), [1, 1])
2530

2631
def test_heads_tails(self):
27-
self.assertEqual(Run("100[t][t]"), [0])
28-
self.assertEqual(Run("100[t]"), [0, 0])
29-
self.assertEqual(Run("100[h]"), [1])
32+
self.assertEqual(Run("100>>"), [0])
33+
self.assertEqual(Run("100>"), [0, 0])
34+
self.assertEqual(Run("100<"), [1])
3035

3136
def test_parens(self):
3237
self.assertEqual(Run("((1))"), [1])

0 commit comments

Comments
 (0)