Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions logicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
rWhitespace = re.compile(r"[ \t]+", re.M)
rCommandSeparator = re.compile(r"[\r\n;]+", re.M)
rBits = re.compile(r"[01]+")
rName = re.compile(r"(?!\binput\b|\b__scope__\b)[a-zA-Z_$]+")
rName = re.compile(r"(?!\b[ab]inp\b|\b__scope__\b)[a-zA-Z0-9_$]+")
rRandom = re.compile(r"\?")
rInput = re.compile(r"\binput\b")
rInput = re.compile(r"\b[ab]inp\b")
rScope = re.compile(r"\b__scope__\b")
rInfix = re.compile(r"[&|]")
rPrefix = re.compile(r"[!$~]")
rPrefix = re.compile(r"[!~@]")
rPostfix = re.compile(r"[<>]")
rOpenParenthesis = re.compile(r"\(")
rCloseParenthesis = re.compile(r"\)")
Expand All @@ -29,6 +29,7 @@
rVariable = re.compile(r"\bvar\b")
rCondition = re.compile(r"\bcond\b")
rOut = re.compile(r"\bout\b")
rReturn = re.compile(r"\bret\b")
rComment = re.compile(r"#.*")
rLambda = re.compile(r"->")
rOr = re.compile(r"/")
Expand All @@ -41,15 +42,6 @@

# Utility functions

def Binarify(number):
if not number:
return [0]
result = []
while number:
result = [number % 2] + result
number //= 2
return result

def And(left, right):
length = max(len(left), len(right))
return list(map(op.and_, [0] * (length - len(left)) + left, [0] * (length - len(right)) + right))
Expand Down Expand Up @@ -82,7 +74,7 @@ def Random(result):


def Input(result):
return lambda scope: GetInput(scope)
return lambda scope: GetInput(scope, result[0][0])


def ScopeTransform(result):
Expand Down Expand Up @@ -130,10 +122,10 @@ def Expression(result):
if isinstance(operator, basestring) and rPrefix.match(operator):
if operator == "!":
return lambda scope: list(map(int, map(op.not_, result[1](scope))))
if operator == "$":
return lambda scope: Binarify(len(result[1](scope)))
if operator == "~":
return lambda scope: list(result[1](scope)[::-1])
if operator == "@":
return lambda scope: list(chr(int("".join(str(x) for x in result[1](scope)), 2) % 256))
if isinstance(result[1], list):
operators = result[1]
start_index = 0
Expand Down Expand Up @@ -176,32 +168,44 @@ def Condition(result):
condition = result[1]
if_true = result[3]
if_false = result[5]
return lambda scope: (if_true(scope) if condition(scope)[0] else if_false(scope))
return lambda scope: if_true(scope) if 1 in condition(scope) else if_false(scope)


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


def GetInput(scope):
def GetInput(scope, inputarg):
if not len(scope["input"]):
scope["input"] = [list(map(int, filter(lambda c: c == "0" or c == "1", raw_input("Input: "))))]
if inputarg == "a":
temp = list(x for x in raw_input("Input: ") if ord(x) < 256)
for a in range(len(temp)):
temp[a] = bin(ord(temp[a]))[2:]
while len(temp[a]) < 8:
temp[a] = "0" + temp[a]
temp = "".join(temp)
scope["input"] = [[int(x) for x in temp]]
if inputarg == "b":
scope["input"] = [list(map(int, filter(lambda c: c == "0" or c == "1", raw_input("Input: "))))]
return scope["input"].pop()


def Print(result):
if result:
print("".join(list(map(str, result))))



# Scope stuff

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


def islambda(v):
LAMBDA = lambda:0
return isinstance(v, type(LAMBDA)) and v.__name__ == LAMBDA.__name__


class Scope:
def __init__(self, parent={}):
self.parent = parent
Expand Down Expand Up @@ -256,7 +260,7 @@ def set(self, key, value):

def delete(self, key):
del self[key]


# Dictionaries:

Expand Down Expand Up @@ -324,7 +328,11 @@ def delete(self, key):
"Variable",
"Out",
"Condition",
["1", rOpenBracket, ["+", ["|", "CommandSeparator", "Variable", "Out", "Condition", "TopLevelExpression"]], rCloseBracket]
[
"1", rOpenBracket,
["+", ["|", "CommandSeparator", "Variable", "Out", "Condition", "TopLevelExpression"]],
rCloseBracket
]
]
],
"Variable": [rVariable, rName, rEquals, "TopLevelExpression"],
Expand Down Expand Up @@ -467,7 +475,8 @@ def Run(code="", input="", astify=False, grammar="Program", repl=False, scope=No
Print(Run(raw_input("Logicode> "), scope=scope))
except (KeyboardInterrupt, EOFError):
return
scope["input"] = list(map(lambda i: list(map(int, filter(lambda c: c == "0" or c == "1", i))), filter(None, input.split("\n")[::-1])))
scope["input"] = list(map(lambda i: list(map(int, filter(lambda c: c == "0" or c == "1", i))),
filter(None, input.split("\n")[::-1])))
if astify:
result = Get(code, grammar, NoTransform)[0]
print(Astify(result))
Expand Down
9 changes: 4 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ def test_not(self):
def test_plus(self):
self.assertEqual(Run("1+1"), [1, 1])
self.assertEqual(Run("1&1&1&1+1&1&1&1"), [1, 1])

def test_length(self):
self.assertEqual(Run("$1000"), [1, 0, 0])
self.assertEqual(Run("$1"), [1])
self.assertEqual(Run("$1111111111"), [1, 0, 1, 0])

def test_ascii(self):
self.assertEqual(Run("@1001000"), ["H"])
self.assertEqual(Run("@111111+@111111"), ["?", "?"])

def test_reverse(self):
self.assertEqual(Run("~1"), [1])
Expand Down