2.1 --- a/micropython/syspython.py Sun Jun 30 22:46:32 2013 +0200
2.2 +++ b/micropython/syspython.py Mon Jul 01 00:47:43 2013 +0200
2.3 @@ -34,15 +34,18 @@
2.4
2.5 # Convenience definitions.
2.6
2.7 -module_attribute = compiler.ast.Getattr
2.8 +constant_attribute = compiler.ast.Getattr
2.9 special_name = compiler.ast.Name
2.10
2.11 -def quoted_ref(obj):
2.12 - return compiler.ast.CallFunc("__static__", [compiler.ast.Const(obj.full_name())])
2.13 -
2.14 def quoted_name(s):
2.15 return compiler.ast.Const(s)
2.16
2.17 +def quoted_ref(obj):
2.18 + return compiler.ast.CallFunc("__static__", [quoted_name(obj.full_name())])
2.19 +
2.20 +def module_attribute(module_name, attrname):
2.21 + return compiler.ast.Getattr(special_name(module_name), attrname)
2.22 +
2.23 # Special function names.
2.24 # NOTE: Some of the assignment operations should not be supported unless
2.25 # NOTE: attribute usage observations are being made.
2.26 @@ -244,13 +247,11 @@
2.27 else_nodes = node.else_ and self.dispatch(node.else_).nodes or []
2.28
2.29 return compiler.ast.Stmt([
2.30 - # __storetemp__(_it, __loadattr__(__builtins__, iter)(<list>))
2.31 + # __storetemp__(_it, __builtins__.iter(<list>))
2.32 compiler.ast.CallFunc(special_name("__storetemp__"), [
2.33 temp,
2.34 compiler.ast.CallFunc(
2.35 - compiler.ast.CallFunc(special_name("__loadattr__"),
2.36 - [special_name("__builtins__"), special_name("iter")]
2.37 - ),
2.38 + module_attribute("__builtins__", "iter"),
2.39 [self.dispatch(node.list)]
2.40 )
2.41 ]),
2.42 @@ -547,7 +548,7 @@
2.43 if node.attrname == "__class__":
2.44
2.45 # __storetemp__(n, <accessor>)
2.46 - # __isclass__(n) and __loadattr__(__builtins__, type) or <access>
2.47 + # __isclass__(n) and __builtins__.type or <access>
2.48
2.49 temp = quoted_name(unit.temp_usage)
2.50 unit.temp_usage += 1
2.51 @@ -560,10 +561,7 @@
2.52 special_name("__isclass__"),
2.53 [compiler.ast.CallFunc(special_name("__loadtemp__"), [temp])]
2.54 ),
2.55 - compiler.ast.CallFunc(
2.56 - special_name("__loadattr__"),
2.57 - [special_name("__builtins__"), special_name("type")]
2.58 - )
2.59 + module_attribute("__builtins__", "type")
2.60 ]),
2.61 access
2.62 ])
2.63 @@ -798,13 +796,11 @@
2.64 unit.temp_usage += 1
2.65
2.66 return compiler.ast.Stmt([
2.67 - # __storetemp__(_out, __loadattr__(__builtins__, list)())
2.68 + # __storetemp__(_out, __builtins__.list())
2.69 compiler.ast.CallFunc(special_name("__storetemp__"), [
2.70 temp,
2.71 compiler.ast.CallFunc(
2.72 - compiler.ast.CallFunc(special_name("__loadattr__"),
2.73 - [special_name("__builtins__"), special_name("list")]
2.74 - ),
2.75 + module_attribute("__builtins__", "list"),
2.76 []
2.77 )
2.78 ]),
2.79 @@ -850,13 +846,11 @@
2.80 # Wrap the above body in the loop construct.
2.81
2.82 return compiler.ast.Stmt([
2.83 - # __storetemp__(_it, __loadattr__(__builtins__, iter)(<list>))
2.84 + # __storetemp__(_it, __builtins__.iter(<list>))
2.85 compiler.ast.CallFunc(special_name("__storetemp__"), [
2.86 temp,
2.87 compiler.ast.CallFunc(
2.88 - compiler.ast.CallFunc(special_name("__loadattr__"),
2.89 - [special_name("__builtins__"), special_name("iter")]
2.90 - ),
2.91 + module_attribute("__builtins__", "iter"),
2.92 [self.dispatch(node.list)]
2.93 )
2.94 ]),
2.95 @@ -946,10 +940,13 @@
2.96 # Other attributes should already be resolved.
2.97
2.98 elif attr is not None and not isinstance(attr, Instance):
2.99 - return compiler.ast.CallFunc(
2.100 - special_name("__loadattr__"),
2.101 - [quoted_ref(attr.parent), special_name(node.name)]
2.102 - )
2.103 + if attr.is_constant():
2.104 + return constant_attribute(quoted_ref(attr.parent), node.name)
2.105 + else:
2.106 + return compiler.ast.CallFunc(
2.107 + special_name("__loadattr__"),
2.108 + [quoted_ref(attr.parent), special_name(node.name)]
2.109 + )
2.110
2.111 # Function globals are referenced via the module.
2.112