1414rWhitespace = re .compile (r"[ \t]+" , re .M )
1515rCommandSeparator = re .compile (r"[\r\n;]+" , re .M )
1616rBits = re .compile (r"[01]+" )
17- rName = re .compile (r"(?!\binput \b|\b__scope__\b)[a-zA-Z_ $]+" )
17+ rName = re .compile (r"(?!\binp \b|\b__scope__\b)[a-zA-Z0-9_ $]+" )
1818rRandom = re .compile (r"\?" )
19- rInput = re .compile (r"\binput \b" )
19+ rInput = re .compile (r"\binp \b" )
2020rScope = re .compile (r"\b__scope__\b" )
2121rInfix = re .compile (r"[&|]" )
22- rPrefix = re .compile (r"[!$~ ]" )
22+ rPrefix = re .compile (r"[!~@ ]" )
2323rPostfix = re .compile (r"[<>]" )
2424rOpenParenthesis = re .compile (r"\(" )
2525rCloseParenthesis = re .compile (r"\)" )
2929rVariable = re .compile (r"\bvar\b" )
3030rCondition = re .compile (r"\bcond\b" )
3131rOut = re .compile (r"\bout\b" )
32+ rReturn = re .compile (r"\bret\b" )
3233rComment = re .compile (r"#.*" )
3334rLambda = re .compile (r"->" )
3435rOr = re .compile (r"/" )
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-
5345def 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 ))
@@ -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,7 +168,7 @@ 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
182174def Out (result ):
@@ -192,16 +184,19 @@ def GetInput(scope):
192184def Print (result ):
193185 if result :
194186 print ("" .join (list (map (str , result ))))
195-
187+
188+
196189# Scope stuff
197190
198191def getParentFunctionName (lambda_function ):
199192 return rGetParentFunctionName .match (repr (lambda_function )).group (1 )
200193
194+
201195def islambda (v ):
202196 LAMBDA = lambda :0
203197 return isinstance (v , type (LAMBDA )) and v .__name__ == LAMBDA .__name__
204198
199+
205200class Scope :
206201 def __init__ (self , parent = {}):
207202 self .parent = parent
@@ -256,7 +251,7 @@ def set(self, key, value):
256251
257252 def delete (self , key ):
258253 del self [key ]
259-
254+
260255
261256# Dictionaries:
262257
@@ -324,7 +319,11 @@ def delete(self, key):
324319 "Variable" ,
325320 "Out" ,
326321 "Condition" ,
327- ["1" , rOpenBracket , ["+" , ["|" , "CommandSeparator" , "Variable" , "Out" , "Condition" , "TopLevelExpression" ]], rCloseBracket ]
322+ [
323+ "1" , rOpenBracket ,
324+ ["+" , ["|" , "CommandSeparator" , "Variable" , "Out" , "Condition" , "TopLevelExpression" ]],
325+ rCloseBracket
326+ ]
328327 ]
329328 ],
330329 "Variable" : [rVariable , rName , rEquals , "TopLevelExpression" ],
@@ -467,7 +466,8 @@ def Run(code="", input="", astify=False, grammar="Program", repl=False, scope=No
467466 Print (Run (raw_input ("Logicode> " ), scope = scope ))
468467 except (KeyboardInterrupt , EOFError ):
469468 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 ])))
469+ scope ["input" ] = list (map (lambda i : list (map (int , filter (lambda c : c == "0" or c == "1" , i ))),
470+ filter (None , input .split ("\n " )[::- 1 ])))
471471 if astify :
472472 result = Get (code , grammar , NoTransform )[0 ]
473473 print (Astify (result ))
0 commit comments