Permalink
Browse files

added support for custom unicode operators

  • Loading branch information...
hartsantler committed Oct 17, 2013
1 parent 7960023 commit d5608a37dae95f7e8d4a72f6431d8bb780f74421
Showing with 65 additions and 8 deletions.
  1. +22 −8 pythonscript/python_to_pythonjs.py
  2. +6 −0 pythonscript/pythonjs.py
  3. +37 −0 tests/test_custom_unicode_operators.html
@@ -74,7 +74,8 @@ def _write(self, code):
if not code == 'pass':
code = """JS('''%s''')"""%code
s = '%s%s\n' % (indentation, code)
self.output.write(s.encode('utf-8'))
#self.output.write(s.encode('utf-8'))
self.output.write(s)
def getvalue(self):
s = self.output.getvalue()
@@ -190,15 +191,16 @@ def preprocess_custom_operators(self, data):
if line.strip().startswith('@custom_operator'):
l = line.replace('"', "'")
a,b,c = l.split("'")
self._custom_operators[ b ] = None
op = b.decode('utf-8')
self._custom_operators[ op ] = None
else:
for op in self._custom_operators:
line = line.replace(op, '|u"%s"|'%op)
op = op.encode('utf-8')
line = line.replace(op, '|"%s"|'%op)
code.append( line )
data = '\n'.join( code )
print( data )
return data
def setup_builtins(self):
@@ -465,8 +467,8 @@ def visit_BinOp(self, node):
op,left_operand = self._custom_op_hack
right_operand = self.visit(node.right)
#return '%s( %s, %s )' %(op, left_operand, right_operand)
if op in self._custom_operators: ## swap name to python function
op = self._custom_operators[ op ]
if op.decode('utf-8') in self._custom_operators: ## swap name to python function
op = self._custom_operators[ op.decode('utf-8') ]
return '%s( [%s, %s] )' %(op, left_operand, right_operand)
if isinstance(node.left, Name):
@@ -529,6 +531,9 @@ def visit_Compare(self, node):
comparator = self.visit(node.comparators[0])
return '%s %s %s' % (left, ops, comparator)
def visit_Or(self, node):
return ' or '
def visit_Not(self, node):
return ' not '
@@ -845,8 +850,9 @@ def visit_FunctionDef(self, node):
elif isinstance(decorator, Call) and decorator.func.id == 'custom_operator':
assert len(decorator.args) == 1
assert isinstance( decorator.args[0], Str )
op = decorator.args[0].s
assert op in self._custom_operators
op = decorator.args[0].s.decode('utf-8')
if op not in self._custom_operators:
raise RuntimeError( op, self._custom_operators )
self._custom_operators[ op ] = node.name
else:
@@ -964,6 +970,11 @@ def visit_FunctionDef(self, node):
assert not self._with_js
writer.write('%s = %s(create_array(%s))' % (node.name, self.visit(decorator), node.name))
def visit_Continue(self, node):
#return 'continue'
writer.write('continue')
return ''
def visit_For(self, node):
if self._with_js:
writer.write('for %s in %s:' %(self.visit(node.target),self.visit(node.iter)))
@@ -1013,6 +1024,9 @@ def command():
module = module_path = None
data = sys.stdin.read()
#data = data.decode('utf-8')
#open('/tmp/testunicode.txt', 'wb').write(data.encode('utf-8'))
if data.startswith('#!'):
header = data[ 2 : data.index('\n') ]
data = data[ data.index('\n')+1 : ]
View
@@ -269,6 +269,9 @@ def visit_Compare(self, node):
comparator = self.visit(node.comparators[0])
return '%s %s %s' % (left, ops, comparator)
def visit_Or(self, node):
return ' || '
def visit_Not(self, node):
return '!'
@@ -300,6 +303,9 @@ def visit_For(self, node):
for_block = init_iter + 'for (var %s=0; %s < iter.length; %s++) {\n%s}\n' % (target, target, target, body)
return for_block
def visit_Continue(self, node):
return 'continue'
def main(script):
input = parse(script)
@@ -0,0 +1,37 @@
<html>
<head>
<script src="pythonscript.js"></script>
<script type="text/python">
@custom_operator( '⊖' )
def symmetric_difference(a,b):
d = []
for v in a:
if b.count(v): pass
elif d.count(v): pass
else: d.append( v )
for v in b:
#if a.count(v): continue ## TODO fix continue
if a.count(v): pass
elif d.count(v): pass
else: d.append( v )
return d
def test():
sdiff = [1,2,3] ⊖ [2,3,4]
print sdiff
for v in sdiff:
print v
</script>
</head>
<body>
<button onclick="test()">click me</button>
</body>
</html>

0 comments on commit d5608a3

Please sign in to comment.