|
14 | 14 | rWhitespace = re.compile(r"[ \t]+", re.M) |
15 | 15 | rCommandSeparator = re.compile(r"[\r\n;]+", re.M) |
16 | 16 | 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_$]+") |
18 | 18 | rRandom = re.compile(r"\?") |
19 | | -rInput = re.compile(r"\binput\b") |
| 19 | +rInput = re.compile(r"\b[ab]inp\b") |
20 | 20 | rScope = re.compile(r"\b__scope__\b") |
21 | 21 | rInfix = re.compile(r"[&|]") |
22 | | -rPrefix = re.compile(r"[!$~]") |
| 22 | +rPrefix = re.compile(r"[!~@]") |
23 | 23 | rPostfix = re.compile(r"[<>]") |
24 | 24 | rOpenParenthesis = re.compile(r"\(") |
25 | 25 | rCloseParenthesis = re.compile(r"\)") |
|
29 | 29 | rVariable = re.compile(r"\bvar\b") |
30 | 30 | rCondition = re.compile(r"\bcond\b") |
31 | 31 | rOut = re.compile(r"\bout\b") |
| 32 | +rReturn = re.compile(r"\bret\b") |
32 | 33 | rComment = re.compile(r"#.*") |
33 | 34 | rLambda = re.compile(r"->") |
34 | 35 | rOr = re.compile(r"/") |
|
41 | 42 |
|
42 | 43 | # Utility functions |
43 | 44 |
|
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 | | - |
53 | 45 | def And(left, right): |
54 | 46 | length = max(len(left), len(right)) |
55 | 47 | return list(map(op.and_, [0] * (length - len(left)) + left, [0] * (length - len(right)) + right)) |
@@ -82,7 +74,7 @@ def Random(result): |
82 | 74 |
|
83 | 75 |
|
84 | 76 | def Input(result): |
85 | | - return lambda scope: GetInput(scope) |
| 77 | + return lambda scope: GetInput(scope, result[0][0]) |
86 | 78 |
|
87 | 79 |
|
88 | 80 | def ScopeTransform(result): |
@@ -130,10 +122,10 @@ def Expression(result): |
130 | 122 | if isinstance(operator, basestring) and rPrefix.match(operator): |
131 | 123 | if operator == "!": |
132 | 124 | return lambda scope: list(map(int, map(op.not_, result[1](scope)))) |
133 | | - if operator == "$": |
134 | | - return lambda scope: Binarify(len(result[1](scope))) |
135 | 125 | if operator == "~": |
136 | 126 | 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)) |
137 | 129 | if isinstance(result[1], list): |
138 | 130 | operators = result[1] |
139 | 131 | start_index = 0 |
@@ -176,32 +168,44 @@ def Condition(result): |
176 | 168 | condition = result[1] |
177 | 169 | if_true = result[3] |
178 | 170 | 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) |
180 | 172 |
|
181 | 173 |
|
182 | 174 | def Out(result): |
183 | 175 | return lambda scope: Print(result[1](scope)) |
184 | 176 |
|
185 | 177 |
|
186 | | -def GetInput(scope): |
| 178 | +def GetInput(scope, inputarg): |
187 | 179 | 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: "))))] |
189 | 190 | return scope["input"].pop() |
190 | 191 |
|
191 | 192 |
|
192 | 193 | def Print(result): |
193 | 194 | if result: |
194 | 195 | print("".join(list(map(str, result)))) |
195 | | - |
| 196 | + |
| 197 | + |
196 | 198 | # Scope stuff |
197 | 199 |
|
198 | 200 | def getParentFunctionName(lambda_function): |
199 | 201 | return rGetParentFunctionName.match(repr(lambda_function)).group(1) |
200 | 202 |
|
| 203 | + |
201 | 204 | def islambda(v): |
202 | 205 | LAMBDA = lambda:0 |
203 | 206 | return isinstance(v, type(LAMBDA)) and v.__name__ == LAMBDA.__name__ |
204 | 207 |
|
| 208 | + |
205 | 209 | class Scope: |
206 | 210 | def __init__(self, parent={}): |
207 | 211 | self.parent = parent |
@@ -256,7 +260,7 @@ def set(self, key, value): |
256 | 260 |
|
257 | 261 | def delete(self, key): |
258 | 262 | del self[key] |
259 | | - |
| 263 | + |
260 | 264 |
|
261 | 265 | # Dictionaries: |
262 | 266 |
|
@@ -324,7 +328,11 @@ def delete(self, key): |
324 | 328 | "Variable", |
325 | 329 | "Out", |
326 | 330 | "Condition", |
327 | | - ["1", rOpenBracket, ["+", ["|", "CommandSeparator", "Variable", "Out", "Condition", "TopLevelExpression"]], rCloseBracket] |
| 331 | + [ |
| 332 | + "1", rOpenBracket, |
| 333 | + ["+", ["|", "CommandSeparator", "Variable", "Out", "Condition", "TopLevelExpression"]], |
| 334 | + rCloseBracket |
| 335 | + ] |
328 | 336 | ] |
329 | 337 | ], |
330 | 338 | "Variable": [rVariable, rName, rEquals, "TopLevelExpression"], |
@@ -467,7 +475,8 @@ def Run(code="", input="", astify=False, grammar="Program", repl=False, scope=No |
467 | 475 | Print(Run(raw_input("Logicode> "), scope=scope)) |
468 | 476 | except (KeyboardInterrupt, EOFError): |
469 | 477 | 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]))) |
471 | 480 | if astify: |
472 | 481 | result = Get(code, grammar, NoTransform)[0] |
473 | 482 | print(Astify(result)) |
|
0 commit comments