Skip to content

Commit c5a7c82

Browse files
author
Derp McDerpson
authored
Merge pull request #21 from LogicodeLang/Changes!
Added `@`, removed `$`
2 parents def7cfd + 3f1ea38 commit c5a7c82

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

logicode.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
rWhitespace = re.compile(r"[ \t]+", re.M)
1515
rCommandSeparator = re.compile(r"[\r\n;]+", re.M)
1616
rBits = re.compile(r"[01]+")
17-
rName = re.compile(r"(?!\binput\b|\b__scope__\b)[a-zA-Z_$]+")
17+
rName = re.compile(r"(?!\b[ab]inp\b|\b__scope__\b)[a-zA-Z0-9_$]+")
1818
rRandom = re.compile(r"\?")
19-
rInput = re.compile(r"\binput\b")
19+
rInput = re.compile(r"\b[ab]inp\b")
2020
rScope = re.compile(r"\b__scope__\b")
2121
rInfix = re.compile(r"[&|]")
22-
rPrefix = re.compile(r"[!$~]")
22+
rPrefix = re.compile(r"[!~@]")
2323
rPostfix = re.compile(r"[<>]")
2424
rOpenParenthesis = re.compile(r"\(")
2525
rCloseParenthesis = re.compile(r"\)")
@@ -29,6 +29,7 @@
2929
rVariable = re.compile(r"\bvar\b")
3030
rCondition = re.compile(r"\bcond\b")
3131
rOut = re.compile(r"\bout\b")
32+
rReturn = re.compile(r"\bret\b")
3233
rComment = re.compile(r"#.*")
3334
rLambda = re.compile(r"->")
3435
rOr = re.compile(r"/")
@@ -41,15 +42,6 @@
4142

4243
# Utility functions
4344

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-
5345
def And(left, right):
5446
length = max(len(left), len(right))
5547
return list(map(op.and_, [0] * (length - len(left)) + left, [0] * (length - len(right)) + right))
@@ -82,7 +74,7 @@ def Random(result):
8274

8375

8476
def Input(result):
85-
return lambda scope: GetInput(scope)
77+
return lambda scope: GetInput(scope, result[0][0])
8678

8779

8880
def ScopeTransform(result):
@@ -130,10 +122,10 @@ def Expression(result):
130122
if isinstance(operator, basestring) and rPrefix.match(operator):
131123
if operator == "!":
132124
return lambda scope: list(map(int, map(op.not_, result[1](scope))))
133-
if operator == "$":
134-
return lambda scope: Binarify(len(result[1](scope)))
135125
if operator == "~":
136126
return lambda scope: list(result[1](scope)[::-1])
127+
if operator == "@":
128+
return lambda scope: list(chr(int("".join(str(x) for x in result[1](scope)), 2) % 256))
137129
if isinstance(result[1], list):
138130
operators = result[1]
139131
start_index = 0
@@ -176,32 +168,44 @@ def Condition(result):
176168
condition = result[1]
177169
if_true = result[3]
178170
if_false = result[5]
179-
return lambda scope: (if_true(scope) if condition(scope)[0] else if_false(scope))
171+
return lambda scope: if_true(scope) if 1 in condition(scope) else if_false(scope)
180172

181173

182174
def Out(result):
183175
return lambda scope: Print(result[1](scope))
184176

185177

186-
def GetInput(scope):
178+
def GetInput(scope, inputarg):
187179
if not len(scope["input"]):
188-
scope["input"] = [list(map(int, filter(lambda c: c == "0" or c == "1", raw_input("Input: "))))]
180+
if inputarg == "a":
181+
temp = list(x for x in raw_input("Input: ") if ord(x) < 256)
182+
for a in range(len(temp)):
183+
temp[a] = bin(ord(temp[a]))[2:]
184+
while len(temp[a]) < 8:
185+
temp[a] = "0" + temp[a]
186+
temp = "".join(temp)
187+
scope["input"] = [[int(x) for x in temp]]
188+
if inputarg == "b":
189+
scope["input"] = [list(map(int, filter(lambda c: c == "0" or c == "1", raw_input("Input: "))))]
189190
return scope["input"].pop()
190191

191192

192193
def Print(result):
193194
if result:
194195
print("".join(list(map(str, result))))
195-
196+
197+
196198
# Scope stuff
197199

198200
def getParentFunctionName(lambda_function):
199201
return rGetParentFunctionName.match(repr(lambda_function)).group(1)
200202

203+
201204
def islambda(v):
202205
LAMBDA = lambda:0
203206
return isinstance(v, type(LAMBDA)) and v.__name__ == LAMBDA.__name__
204207

208+
205209
class Scope:
206210
def __init__(self, parent={}):
207211
self.parent = parent
@@ -256,7 +260,7 @@ def set(self, key, value):
256260

257261
def delete(self, key):
258262
del self[key]
259-
263+
260264

261265
# Dictionaries:
262266

@@ -324,7 +328,11 @@ def delete(self, key):
324328
"Variable",
325329
"Out",
326330
"Condition",
327-
["1", rOpenBracket, ["+", ["|", "CommandSeparator", "Variable", "Out", "Condition", "TopLevelExpression"]], rCloseBracket]
331+
[
332+
"1", rOpenBracket,
333+
["+", ["|", "CommandSeparator", "Variable", "Out", "Condition", "TopLevelExpression"]],
334+
rCloseBracket
335+
]
328336
]
329337
],
330338
"Variable": [rVariable, rName, rEquals, "TopLevelExpression"],
@@ -467,7 +475,8 @@ def Run(code="", input="", astify=False, grammar="Program", repl=False, scope=No
467475
Print(Run(raw_input("Logicode> "), scope=scope))
468476
except (KeyboardInterrupt, EOFError):
469477
return
470-
scope["input"] = list(map(lambda i: list(map(int, filter(lambda c: c == "0" or c == "1", i))), filter(None, input.split("\n")[::-1])))
478+
scope["input"] = list(map(lambda i: list(map(int, filter(lambda c: c == "0" or c == "1", i))),
479+
filter(None, input.split("\n")[::-1])))
471480
if astify:
472481
result = Get(code, grammar, NoTransform)[0]
473482
print(Astify(result))

test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ def test_not(self):
2222
def test_plus(self):
2323
self.assertEqual(Run("1+1"), [1, 1])
2424
self.assertEqual(Run("1&1&1&1+1&1&1&1"), [1, 1])
25-
26-
def test_length(self):
27-
self.assertEqual(Run("$1000"), [1, 0, 0])
28-
self.assertEqual(Run("$1"), [1])
29-
self.assertEqual(Run("$1111111111"), [1, 0, 1, 0])
25+
26+
def test_ascii(self):
27+
self.assertEqual(Run("@1001000"), ["H"])
28+
self.assertEqual(Run("@111111+@111111"), ["?", "?"])
3029

3130
def test_reverse(self):
3231
self.assertEqual(Run("~1"), [1])

0 commit comments

Comments
 (0)