Skip to content

Commit

Permalink
new static type long that translates to using the Long API, https:/…
Browse files Browse the repository at this point in the history
  • Loading branch information
hartsantler committed Jun 7, 2014
1 parent 766c66a commit d534822
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 28 deletions.
3 changes: 2 additions & 1 deletion pythonjs/package.json
Expand Up @@ -17,7 +17,8 @@
],
"dependencies": {
"workerjs": "*",
"requirejs": "*"
"requirejs": "*",
"long" : "*"
},
"engines": {
"node": "*"
Expand Down
43 changes: 40 additions & 3 deletions pythonjs/python_to_pythonjs.py
Expand Up @@ -1168,6 +1168,20 @@ def visit_BinOp(self, node):
else:
return '[%s]' %','.join(expanded)

elif not self._with_dart and left in self._typedef_vars and self._typedef_vars[left]=='long':
if op == '*':
return '%s.multiply(%s)'%(left, right)
elif op == '+':
return '%s.add(%s)'%(left, right)
elif op == '-':
return '%s.subtract(%s)'%(left, right)
elif op == '/' or op == '//':
return '%s.div(%s)'%(left, right)
elif op == '%':
return '%s.modulo(%s)'%(left, right)
else:
raise NotImplementedError('long operator: %s'%op)

elif not self._with_dart and op == '*' and left in self._typedef_vars and self._typedef_vars[left]=='int' and isinstance(node.right, ast.Num) and node.right.n in POWER_OF_TWO:
power = POWER_OF_TWO.index( node.right.n )
return '%s << %s'%(left, power)
Expand All @@ -1188,7 +1202,7 @@ def visit_BinOp(self, node):
elif op == '+' and not self._with_dart:
if '+' in self._direct_operators:
return '%s+%s'%(left, right)
elif left in self._typedef_vars and self._typedef_vars[left] in typedpython.number_types:
elif left in self._typedef_vars and self._typedef_vars[left] in typedpython.native_number_types:
return '%s+%s'%(left, right)

elif self._with_lua or self._in_lambda or self._in_while_test:
Expand Down Expand Up @@ -1272,7 +1286,24 @@ def visit_Compare(self, node):
left = self.visit(node.left)
comp = [ left ]
for i in range( len(node.ops) ):
if isinstance(node.ops[i], ast.In) or isinstance(node.ops[i], ast.NotIn):
if i==0 and isinstance(node.left, ast.Name) and node.left.id in self._typedef_vars:
#raise RuntimeError(node)
if isinstance(node.ops[i], ast.Eq):
comp = ['%s.equals(%s)' %(left, self.visit(node.comparators[i]))]
elif isinstance(node.ops[i], ast.Lt):
comp = ['%s.lessThan(%s)' %(left, self.visit(node.comparators[i]))]
elif isinstance(node.ops[i], ast.Gt):
comp = ['%s.greaterThan(%s)' %(left, self.visit(node.comparators[i]))]

elif isinstance(node.ops[i], ast.LtE):
comp = ['%s.lessThanOrEqual(%s)' %(left, self.visit(node.comparators[i]))]
elif isinstance(node.ops[i], ast.GtE):
comp = ['%s.greaterThanOrEqual(%s)' %(left, self.visit(node.comparators[i]))]

else:
raise NotImplementedError( node.ops[i] )

elif isinstance(node.ops[i], ast.In) or isinstance(node.ops[i], ast.NotIn):
if comp[-1] == left:
comp.pop()
else:
Expand Down Expand Up @@ -1466,7 +1497,12 @@ def visit_Assign(self, node):
if isinstance(target, ast.Name) and target.id in typedpython.types:
if len(targets)==2 and isinstance(targets[1], ast.Name):
self._typedef_vars[ targets[1].id ] = target.id
targets = targets[1:]
if target.id == 'long' and isinstance(node.value, ast.Num):
## requires long library ##
writer.write('%s = long.fromString("%s")' %(targets[1].id, self.visit(node.value)))
return None
else:
targets = targets[1:]
elif len(targets)==1 and isinstance(node.value, ast.Name) and target.id in typedpython.types:
self._typedef_vars[ node.value.id ] = target.id
return None
Expand Down Expand Up @@ -2427,6 +2463,7 @@ def visit_FunctionDef(self, node):

for v in local_vars-global_vars:
if v in args: pass
elif v == 'long': writer.write('''inline("if (__NODEJS__==true) var long = require('long')")''') ## this is ugly
else: vars.append( v )

a = ','.join( vars )
Expand Down
22 changes: 11 additions & 11 deletions pythonjs/pythonjs.js
@@ -1,15 +1,15 @@
__NULL_OBJECT__ = Object.create(null);
if (( "window" ) in this && ( "document" ) in this) {
__WEBWORKER__ = false;
__NODEJS__ = false;
} else {
if (( typeof(process) ) != "undefined") {
__WEBWORKER__ = false;
__NODEJS__ = true;
} else {
__NODEJS__ = false;
__WEBWORKER__ = true;
}
__WEBWORKER__ = false;
__NODEJS__ = false;
__BROWSER__ = false;
if (( typeof(process) ) != "undefined") {
__NODEJS__ = true;
}
if (( typeof(window) ) != "undefined") {
__BROWSER__ = true;
}
if (( typeof(importScripts) ) == "function") {
__WEBWORKER__ = true;
}
__create_array__ = function() {
"Used to fix a bug/feature of Javascript where new Array(number)\n created a array with number of undefined elements which is not\n what we want";
Expand Down
20 changes: 9 additions & 11 deletions pythonjs/runtime/pythonpythonjs.py
Expand Up @@ -3,20 +3,18 @@
# License: "New BSD"

__NULL_OBJECT__ = Object.create( null )
if 'window' in this and 'document' in this:
__WEBWORKER__ = False
__NODEJS__ = False
elif typeof(process) != 'undefined':
## note, we can not test for '"process" in global'
## make sure we are really inside NodeJS by letting this fail, and halting the program.
__WEBWORKER__ = False
__WEBWORKER__ = False
__NODEJS__ = False
__BROWSER__ = False

## note browser and nodejs can both be true in the case of NodeWebkit
if typeof(process) != 'undefined': ## TODO check if this is true inside a nodejs webworker
__NODEJS__ = True
else:
__NODEJS__ = False
if typeof(window) != 'undefined':
__BROWSER__ = True
if typeof(importScripts) == 'function':
__WEBWORKER__ = True

#if __NODEJS__:
# require('requirejs')

def __create_array__(): ## DEPRECATED
"""Used to fix a bug/feature of Javascript where new Array(number)
Expand Down
7 changes: 5 additions & 2 deletions pythonjs/typedpython.py
@@ -1,6 +1,9 @@
whitespace = [' ', '\t']
number_types = ['int', 'float']
types = ['str', 'list']
native_number_types = ['int', 'float', 'double'] ## float and double are the same
number_types = ['long'] ## requires https://github.com/dcodeIO/Long.js
number_types.extend( native_number_types )

types = ['str', 'list', 'dict']
types.extend( number_types)


Expand Down
17 changes: 17 additions & 0 deletions regtests/typed/long.py
@@ -0,0 +1,17 @@
"""long static type"""

def main():
long x = 65536
long y = x * x
long z = 4294967296
TestError( y==z )

long a = z + z
long b = 8589934592
TestError( a==b )

TestError( y < b )
TestError( b > y )

TestError( y <= b )
TestError( b >= y )

0 comments on commit d534822

Please sign in to comment.