Skip to content

Commit 318af9a

Browse files
authored
Close #12
1 parent eff9c44 commit 318af9a

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

logicode.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
regex = re._pattern_type
1313

1414
rWhitespace = re.compile(r"[ \t]+", re.M)
15-
rNewlines = re.compile(r"[\r\n]+", re.M)
15+
rCommandSeparator = re.compile(r"[\r\n;]+", re.M)
1616
rBits = re.compile(r"[01]+")
1717
rName = re.compile(r"(?!\binput\b|\b__scope__\b)[a-zA-Z_$]+")
1818
rRandom = re.compile(r"\?")
@@ -23,6 +23,8 @@
2323
rPostfix = re.compile(r"\[[ht]\]")
2424
rOpenParenthesis = re.compile(r"\(")
2525
rCloseParenthesis = re.compile(r"\)")
26+
rOpenBracket = re.compile(r"\[")
27+
rCloseBracket = re.compile(r"\]")
2628
rCircuit = re.compile(r"\bcirc\b")
2729
rVariable = re.compile(r"\bvar\b")
2830
rCondition = re.compile(r"\bcond\b")
@@ -109,12 +111,13 @@ def Expression(result):
109111
if isinstance(operator, basestring) and rPrefix.match(operator):
110112
if operator == "!":
111113
return lambda scope: list(map(int, map(op.not_, result[1](scope))))
112-
operator = result[1]
113-
if isinstance(operator, basestring) and rPostfix.match(operator):
114-
if operator == "[h]":
115-
return lambda scope: [result[0](scope)[0]]
116-
if operator == "[t]":
117-
return lambda scope: [result[0](scope)[-1]]
114+
if isinstance(result[1], list):
115+
operator = result[1][0][0]
116+
if isinstance(operator, basestring) and rPostfix.match(operator):
117+
if operator == "[h]":
118+
return lambda scope: [result[0](scope)[0]]
119+
if operator == "[t]":
120+
return lambda scope: [result[0](scope)[-1]]
118121
# Function call
119122
name = result[0]
120123
args = result[1]
@@ -126,8 +129,11 @@ def Expression(result):
126129
def Circuit(result):
127130
name = result[1]
128131
arguments = result[2]
129-
expression = result[4]
130-
return lambda scope: scope.set(name, lambda args: expression(Inject(Scope(scope), arguments(scope), args)))
132+
body = result[4]
133+
if isinstance(body, list):
134+
expressions = map(lambda l: l[0], body[0][1])
135+
body = lambda scope: list(filter(None, map(lambda expression: expression(scope), expressions)))[-1]
136+
return lambda scope: scope.set(name, lambda args: body(Inject(Scope(scope), arguments(scope), args)))
131137

132138

133139
def Variable(result):
@@ -224,7 +230,7 @@ def delete(self, key):
224230

225231
# Grammars
226232
grammars = {
227-
"Newlines": [rNewlines],
233+
"CommandSeparator": [rCommandSeparator],
228234
"Bits": [rBits],
229235
"Name": [rName],
230236
"Random": [rRandom],
@@ -262,7 +268,7 @@ def delete(self, key):
262268
"|",
263269
["1", "Alpha", rInfix, "Expression"],
264270
["1", rPrefix, "Term"],
265-
["1", "Alpha", rPostfix],
271+
["1", "Alpha", ["+", rPostfix]],
266272
["1", "Name", "Call Arguments"],
267273
["1", rOpenParenthesis, "Expression", rCloseParenthesis],
268274
"Literal"
@@ -275,7 +281,19 @@ def delete(self, key):
275281
["1", "Expression"]
276282
]
277283
],
278-
"Circuit": [rCircuit, rName, "Arguments", rLambda, "TopLevelExpression"],
284+
"Circuit": [
285+
rCircuit,
286+
rName,
287+
"Arguments",
288+
rLambda,
289+
[
290+
"|",
291+
"TopLevelExpression",
292+
"Variable",
293+
"Out",
294+
["1", rOpenBracket, ["+", ["|", "CommandSeparator", "Variable", "Out", "TopLevelExpression"]], rCloseBracket]
295+
]
296+
],
279297
"Variable": [rVariable, rName, rEquals, "TopLevelExpression"],
280298
"Condition": [
281299
rCondition,
@@ -290,14 +308,14 @@ def delete(self, key):
290308
"Program": [
291309
[
292310
"+",
293-
["|", "Circuit", "Variable", "Condition", "Out", "Comment", "Newlines", "TopLevelExpression"]
311+
["|", "CommandSeparator", "Comment", "Circuit", "Variable", "Condition", "Out", "TopLevelExpression"]
294312
]
295313
]
296314
}
297315

298316
# Transforming grammars to functions
299317
transform = {
300-
"Newlines": NoLambda,
318+
"CommandSeparator": NoLambda,
301319
"Bits": Bits,
302320
"Name": Name,
303321
"Random": Random,

0 commit comments

Comments
 (0)