1.1 --- a/micropython/syspython.py Sun Jun 30 22:46:32 2013 +0200
1.2 +++ b/micropython/syspython.py Mon Jul 01 00:47:43 2013 +0200
1.3 @@ -34,15 +34,18 @@
1.4
1.5 # Convenience definitions.
1.6
1.7 -module_attribute = compiler.ast.Getattr
1.8 +constant_attribute = compiler.ast.Getattr
1.9 special_name = compiler.ast.Name
1.10
1.11 -def quoted_ref(obj):
1.12 - return compiler.ast.CallFunc("__static__", [compiler.ast.Const(obj.full_name())])
1.13 -
1.14 def quoted_name(s):
1.15 return compiler.ast.Const(s)
1.16
1.17 +def quoted_ref(obj):
1.18 + return compiler.ast.CallFunc("__static__", [quoted_name(obj.full_name())])
1.19 +
1.20 +def module_attribute(module_name, attrname):
1.21 + return compiler.ast.Getattr(special_name(module_name), attrname)
1.22 +
1.23 # Special function names.
1.24 # NOTE: Some of the assignment operations should not be supported unless
1.25 # NOTE: attribute usage observations are being made.
1.26 @@ -244,13 +247,11 @@
1.27 else_nodes = node.else_ and self.dispatch(node.else_).nodes or []
1.28
1.29 return compiler.ast.Stmt([
1.30 - # __storetemp__(_it, __loadattr__(__builtins__, iter)(<list>))
1.31 + # __storetemp__(_it, __builtins__.iter(<list>))
1.32 compiler.ast.CallFunc(special_name("__storetemp__"), [
1.33 temp,
1.34 compiler.ast.CallFunc(
1.35 - compiler.ast.CallFunc(special_name("__loadattr__"),
1.36 - [special_name("__builtins__"), special_name("iter")]
1.37 - ),
1.38 + module_attribute("__builtins__", "iter"),
1.39 [self.dispatch(node.list)]
1.40 )
1.41 ]),
1.42 @@ -547,7 +548,7 @@
1.43 if node.attrname == "__class__":
1.44
1.45 # __storetemp__(n, <accessor>)
1.46 - # __isclass__(n) and __loadattr__(__builtins__, type) or <access>
1.47 + # __isclass__(n) and __builtins__.type or <access>
1.48
1.49 temp = quoted_name(unit.temp_usage)
1.50 unit.temp_usage += 1
1.51 @@ -560,10 +561,7 @@
1.52 special_name("__isclass__"),
1.53 [compiler.ast.CallFunc(special_name("__loadtemp__"), [temp])]
1.54 ),
1.55 - compiler.ast.CallFunc(
1.56 - special_name("__loadattr__"),
1.57 - [special_name("__builtins__"), special_name("type")]
1.58 - )
1.59 + module_attribute("__builtins__", "type")
1.60 ]),
1.61 access
1.62 ])
1.63 @@ -798,13 +796,11 @@
1.64 unit.temp_usage += 1
1.65
1.66 return compiler.ast.Stmt([
1.67 - # __storetemp__(_out, __loadattr__(__builtins__, list)())
1.68 + # __storetemp__(_out, __builtins__.list())
1.69 compiler.ast.CallFunc(special_name("__storetemp__"), [
1.70 temp,
1.71 compiler.ast.CallFunc(
1.72 - compiler.ast.CallFunc(special_name("__loadattr__"),
1.73 - [special_name("__builtins__"), special_name("list")]
1.74 - ),
1.75 + module_attribute("__builtins__", "list"),
1.76 []
1.77 )
1.78 ]),
1.79 @@ -850,13 +846,11 @@
1.80 # Wrap the above body in the loop construct.
1.81
1.82 return compiler.ast.Stmt([
1.83 - # __storetemp__(_it, __loadattr__(__builtins__, iter)(<list>))
1.84 + # __storetemp__(_it, __builtins__.iter(<list>))
1.85 compiler.ast.CallFunc(special_name("__storetemp__"), [
1.86 temp,
1.87 compiler.ast.CallFunc(
1.88 - compiler.ast.CallFunc(special_name("__loadattr__"),
1.89 - [special_name("__builtins__"), special_name("iter")]
1.90 - ),
1.91 + module_attribute("__builtins__", "iter"),
1.92 [self.dispatch(node.list)]
1.93 )
1.94 ]),
1.95 @@ -946,10 +940,13 @@
1.96 # Other attributes should already be resolved.
1.97
1.98 elif attr is not None and not isinstance(attr, Instance):
1.99 - return compiler.ast.CallFunc(
1.100 - special_name("__loadattr__"),
1.101 - [quoted_ref(attr.parent), special_name(node.name)]
1.102 - )
1.103 + if attr.is_constant():
1.104 + return constant_attribute(quoted_ref(attr.parent), node.name)
1.105 + else:
1.106 + return compiler.ast.CallFunc(
1.107 + special_name("__loadattr__"),
1.108 + [quoted_ref(attr.parent), special_name(node.name)]
1.109 + )
1.110
1.111 # Function globals are referenced via the module.
1.112