1.1 --- a/docs/concepts.txt Thu Apr 23 00:52:33 2009 +0200
1.2 +++ b/docs/concepts.txt Sun Apr 26 03:24:12 2009 +0200
1.3 @@ -189,13 +189,13 @@
1.4 certain common features and operations are supported in the same way for all
1.5 of these things. To permit this, a common data structure format is used.
1.6
1.7 - Header........................................ Attributes.................
1.8 + Header............................................................................ Attributes.................
1.9
1.10 - Identifier Identifier Address Details Object Object ...
1.11 + Identifier Identifier Address Details Flag Identifier Size Object Object ...
1.12
1.13 - 0 1 2 3 4 5 6
1.14 - classcode attrcode invocation invocation __class__ attribute ...
1.15 - reference #args, reference reference
1.16 + 0 1 2 3 4 5 6 7 8 9
1.17 + classcode attrcode invocation invocation instance funccode size __class__ attribute ...
1.18 + reference #args, status reference reference
1.19 defaults
1.20 reference
1.21
1.22 @@ -207,34 +207,34 @@
1.23
1.24 Class C:
1.25
1.26 - 0 1 2 3 4 5 6
1.27 - classcode attrcode __new__ __new__ class type attribute ...
1.28 - for C for C reference #args, reference reference
1.29 + 0 1 2 3 4 5 6 7 8 9
1.30 + classcode attrcode __new__ __new__ false size class type attribute ...
1.31 + for C for C reference #args, reference reference
1.32 defaults
1.33 reference
1.34
1.35 Instance of C:
1.36
1.37 - 0 1 2 3 4 5 6
1.38 - classcode attrcode C.__call__ C.__call__ class C attribute ...
1.39 - for C for C reference #args, reference reference
1.40 + 0 1 2 3 4 5 6 7 8 9
1.41 + classcode attrcode C.__call__ C.__call__ true size class C attribute ...
1.42 + for C for C reference #args, reference reference
1.43 (if exists) defaults
1.44 reference
1.45
1.46 Function f:
1.47
1.48 - 0 1 2 3 4 5 6
1.49 - classcode attrcode code code class attribute ...
1.50 - for for reference #args, function (default)
1.51 - function function defaults reference reference
1.52 + 0 1 2 3 4 5 6 7 8 9
1.53 + classcode attrcode code code true funccode size class attribute ...
1.54 + for for reference #args, function (default)
1.55 + function function defaults reference reference
1.56 reference
1.57
1.58 Module m:
1.59
1.60 - 0 1 2 3 4 5 6
1.61 - classcode attrcode (unused) (unused) module type attribute ...
1.62 - for m for m reference (global)
1.63 - reference
1.64 + 0 1 2 3 4 5 6 7 8 9
1.65 + classcode attrcode (unused) (unused) true module type attribute ...
1.66 + for m for m reference (global)
1.67 + reference
1.68
1.69 The __class__ Attribute
1.70 -----------------------
1.71 @@ -246,6 +246,13 @@
1.72 Function: refers to the function class
1.73 Instance: refers to the class instantiated to make the object
1.74
1.75 +Lists and Tuples
1.76 +----------------
1.77 +
1.78 +The built-in list and tuple sequences employ variable length structures using
1.79 +the attribute locations to store their elements, where each element is a
1.80 +reference to a separately stored object.
1.81 +
1.82 Testing Instance Compatibility with Classes (attrcode)
1.83 ------------------------------------------------------
1.84
2.1 --- a/lib/builtins.py Thu Apr 23 00:52:33 2009 +0200
2.2 +++ b/lib/builtins.py Sun Apr 26 03:24:12 2009 +0200
2.3 @@ -32,7 +32,7 @@
2.4 def __add__(self, other): pass
2.5 def __radd__(self, other): pass
2.6 def __mul__(self, other): pass
2.7 - def __radd__(self, other): pass
2.8 + def __rmul__(self, other): pass
2.9 def __mod__(self, other): pass
2.10 def __rmod__(self, other): pass
2.11 def __lt__(self, other): pass
2.12 @@ -266,7 +266,7 @@
2.13
2.14 # Various types.
2.15
2.16 -class ellipsis: pass
2.17 +#class ellipsis: pass
2.18 class NoneType: pass
2.19 class NotImplementedType: pass
2.20
2.21 @@ -333,13 +333,14 @@
2.22 # should be predefined constants.
2.23
2.24 function
2.25 -list
2.26 -tuple
2.27 -xrange
2.28 AttributeError
2.29 StopIteration
2.30 TypeError
2.31 -ellipsis
2.32 -bool
2.33 +
2.34 +list
2.35 +tuple
2.36 +#xrange
2.37 +#ellipsis
2.38 +#bool
2.39
2.40 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/micropython/__init__.py Thu Apr 23 00:52:33 2009 +0200
3.2 +++ b/micropython/__init__.py Sun Apr 26 03:24:12 2009 +0200
3.3 @@ -140,6 +140,11 @@
3.4 self.code += obj.attributes_as_list()
3.5 pos += len(attributes.keys())
3.6
3.7 + # Omit built-in function code where requested.
3.8 +
3.9 + if not with_builtins and module.name == "__builtins__":
3.10 + continue
3.11 +
3.12 # Generate the instantiator/initialiser.
3.13 # Append the function code to the image.
3.14
3.15 @@ -270,7 +275,7 @@
3.16
3.17 elif isinstance(item, micropython.data.Class):
3.18 assert item.instance_template_location == len(self.raw_code)
3.19 - self.raw_code += item.as_raw(objtable, paramtable)
3.20 + self.raw_code += item.as_raw(objtable, paramtable, with_builtins or item.module.name != "__builtins__")
3.21 assert item.location == len(self.raw_code) - 1
3.22
3.23 elif isinstance(item, micropython.data.Const):
3.24 @@ -378,7 +383,7 @@
3.25 "None" : None,
3.26 "True" : True,
3.27 "False" : False,
3.28 - "Ellipsis" : Ellipsis,
3.29 + #"Ellipsis" : Ellipsis,
3.30 "NotImplemented" : NotImplemented
3.31 }
3.32
4.1 --- a/micropython/data.py Thu Apr 23 00:52:33 2009 +0200
4.2 +++ b/micropython/data.py Sun Apr 26 03:24:12 2009 +0200
4.3 @@ -462,8 +462,9 @@
4.4 objtable.get_index(self.value_type_name()),
4.5 None,
4.6 None,
4.7 - 1,
4.8 - self.value_type_name()
4.9 + 1, # instance
4.10 + self.value_type_name(),
4.11 + 1 # size
4.12 )
4.13 ] + self.raw_data()
4.14
4.15 @@ -550,7 +551,7 @@
4.16 def __shortrepr__(self):
4.17 return "Class(%r, %s)" % (self.name, shortrepr(self.parent))
4.18
4.19 - def as_raw(self, objtable, paramtable):
4.20 + def as_raw(self, objtable, paramtable, with_instantiator=1):
4.21 classcode = objtable.as_list().get_code(self.full_name())
4.22 attrcode = objtable.get_index(self.full_name())
4.23
4.24 @@ -562,7 +563,10 @@
4.25
4.26 # NOTE: The instantiator code is the first block of the class.
4.27
4.28 - instantiator_code_location = self.get_instantiator().blocks[0].location
4.29 + if not with_instantiator:
4.30 + instantiator_code_location = self.full_name()
4.31 + else:
4.32 + instantiator_code_location = self.get_instantiator().blocks[0].location
4.33
4.34 return [
4.35
4.36 @@ -574,8 +578,9 @@
4.37 call_method_value and len(call_method_value.positional_names),
4.38 call_method_value and len(call_method_value.defaults)
4.39 ),
4.40 - 1,
4.41 - self.full_name()
4.42 + 1, # instance
4.43 + self.full_name(),
4.44 + len(self.instance_attributes()) + 1 # size
4.45 ),
4.46
4.47 # Class...
4.48 @@ -586,8 +591,9 @@
4.49 len(self.get_instantiator().positional_names),
4.50 len(self.get_instantiator().defaults)
4.51 ),
4.52 - 0,
4.53 - self.full_name()
4.54 + 0, # not instance
4.55 + self.full_name(),
4.56 + len(self.class_attributes()) + 1 # size
4.57 )
4.58 ]
4.59
4.60 @@ -958,7 +964,7 @@
4.61 )
4.62
4.63 def as_raw(self, objtable, paramtable):
4.64 - # NOTE: Need class and parameter details! Should arguably be types.FunctionType.
4.65 + # NOTE: Need class and parameter details! Should arguably be an instance of types.FunctionType.
4.66 return [
4.67 DataObject(
4.68 objtable.as_list().get_code("__builtins__.function"),
4.69 @@ -968,8 +974,9 @@
4.70 len(self.positional_names),
4.71 len(self.defaults)
4.72 ),
4.73 - 0,
4.74 + 1, # instance
4.75 "__builtins__.function",
4.76 + len(self.defaults) + 1, # size
4.77 paramtable.as_list().get_code(self.full_name())
4.78 )
4.79 ]
4.80 @@ -1072,7 +1079,7 @@
4.81
4.82 "Make a function from a method."
4.83
4.84 - function = Function(self.name, self.parent, self.argnames[1:], self.defaults,
4.85 + function = Function(self.name + " (function)", self.parent, self.argnames[1:], self.defaults,
4.86 self.has_star, self.has_dstar, self.module, self.astnode)
4.87 function.default_attrs = self.default_attrs
4.88 return function
4.89 @@ -1169,8 +1176,9 @@
4.90 None, # module name not used as an attribute
4.91 None,
4.92 None,
4.93 - 0,
4.94 - self.full_name()
4.95 + 1, # instance
4.96 + self.full_name(),
4.97 + len(self.module_attributes()) + 1 # size
4.98 )
4.99 ]
4.100
5.1 --- a/micropython/program.py Thu Apr 23 00:52:33 2009 +0200
5.2 +++ b/micropython/program.py Sun Apr 26 03:24:12 2009 +0200
5.3 @@ -25,16 +25,22 @@
5.4
5.5 "A representation of a raw program data object."
5.6
5.7 - def __init__(self, classcode, attrcode, codeaddr, codedetails, instance, name, funccode=None):
5.8 + def __init__(self, classcode, attrcode, codeaddr, codedetails, instance, name, size, funccode=None):
5.9 self.classcode = classcode
5.10 self.attrcode = attrcode
5.11 self.codeaddr = codeaddr
5.12 self.codedetails = codedetails
5.13 self.instance = instance
5.14 self.name = name
5.15 + self.size = size
5.16 self.funccode = funccode
5.17
5.18 + def with_size(self, size):
5.19 + return DataObject(self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance, self.name, size, self.funccode)
5.20 +
5.21 def __repr__(self):
5.22 - return "%r # %s" % ((self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance, self.funccode), self.name)
5.23 + return "%r # %s" % (
5.24 + (self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance, self.funccode, self.size), self.name
5.25 + )
5.26
5.27 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/micropython/trans.py Thu Apr 23 00:52:33 2009 +0200
6.2 +++ b/micropython/trans.py Sun Apr 26 03:24:12 2009 +0200
6.3 @@ -508,13 +508,6 @@
6.4 self.new_op(LoadContext())
6.5 self.new_op(StoreFrame(0))
6.6
6.7 - # For known instantiations, provide a new object as the first argument
6.8 - # to the __init__ method.
6.9 -
6.10 - elif isinstance(target, Class):
6.11 - self.make_object(target, len(target.instance_attributes()))
6.12 - self.new_op(StoreFrame(0))
6.13 -
6.14 # Otherwise omit the context.
6.15
6.16 else:
6.17 @@ -552,11 +545,12 @@
6.18 expect_context = 0
6.19
6.20 # Handle calls to classes.
6.21 + # The resulting target must match that used in the actual invocation.
6.22
6.23 elif isinstance(target, Class):
6.24 - ncontext = 1
6.25 + ncontext = 0
6.26 expect_context = 0
6.27 - target = target.get_init_method()
6.28 + target = target.get_instantiator()
6.29
6.30 # Method calls via classes.
6.31
6.32 @@ -771,10 +765,14 @@
6.33
6.34 "Make the invocation."
6.35
6.36 + # For classes, the target itself is used, since the instantiator will be
6.37 + # obtained via the class.
6.38 +
6.39 if isinstance(target, Class):
6.40 - self.new_op(LoadConst(target.get_init_method()))
6.41 + self.new_op(LoadConst(target))
6.42 else:
6.43 self.new_op(instruction)
6.44 +
6.45 self.new_op(LoadCallable())
6.46 self.new_op(JumpWithFrame())
6.47
6.48 @@ -789,9 +787,6 @@
6.49
6.50 "Finish the invocation and tidy up afterwards."
6.51
6.52 - if isinstance(target, Class):
6.53 - self.new_op(LoadName(target.get_init_method().all_locals()["self"])) # load the context in the invocation frame
6.54 - self.new_op(StoreResult())
6.55 self.new_op(DropFrame())
6.56 if load_result:
6.57 self.new_op(LoadResult())
7.1 --- a/rsvp.py Thu Apr 23 00:52:33 2009 +0200
7.2 +++ b/rsvp.py Sun Apr 26 03:24:12 2009 +0200
7.3 @@ -129,6 +129,8 @@
7.4 cls = objlist.access("__builtins__", "int")
7.5 self.int_class_location = cls and cls.get_value() and cls.get_value().location
7.6 self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
7.7 + cls = objlist.access("__builtins__", "list")
7.8 + self.list_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
7.9
7.10 # Debugging attributes.
7.11
7.12 @@ -338,7 +340,10 @@
7.13 self.frame_stack[frame + self.operand] = self.source
7.14
7.15 LoadTemp = LoadName
7.16 - StoreTemp = StoreName
7.17 +
7.18 + def StoreTemp(self):
7.19 + frame = self.local_sp_stack[-1]
7.20 + self.frame_stack[frame + self.operand] = self.value
7.21
7.22 def LoadAddress(self):
7.23 # Preserve context (potentially null).
7.24 @@ -616,10 +621,11 @@
7.25 return attr_index == data.attrcode
7.26
7.27 def _MakeObject(self, size, ref):
7.28 + # Load the template.
7.29 data = self.load(ref)
7.30 addr = self.new(size)
7.31 - # Set the header to resemble the class.
7.32 - self.save(addr, data)
7.33 + # Save the header, overriding the size.
7.34 + self.save(addr, data.with_size(size))
7.35 return addr
7.36
7.37 def _LoadAddressContextCond(self, context, ref, inst_context, inst_ref):
7.38 @@ -634,12 +640,6 @@
7.39
7.40 # Native function implementations.
7.41
7.42 - def builtins_object_init(self):
7.43 - pass
7.44 -
7.45 - def builtins_int_init(self):
7.46 - pass
7.47 -
7.48 def builtins_int_add(self):
7.49 frame = self.local_sp_stack[-1]
7.50
7.51 @@ -706,12 +706,25 @@
7.52 left_context, left = self.frame_stack[frame]
7.53 self.result = left, left
7.54
7.55 + def builtins_list_new(self):
7.56 + frame = self.local_sp_stack[-1]
7.57 +
7.58 + # NOTE: Specific copying of tuples/lists.
7.59 +
7.60 + args_context, args = self.frame_stack[frame]
7.61 + header = self.load(args)
7.62 +
7.63 + list = self._MakeObject(header.size, self.list_instance_location)
7.64 + for i in range(1, header.size):
7.65 + self.save(list + i, self.load(args + i))
7.66 +
7.67 + self.result = list, list
7.68 +
7.69 native_functions = {
7.70 - "__builtins__.object.__init__" : builtins_object_init,
7.71 - "__builtins__.int.__init__" : builtins_int_init,
7.72 "__builtins__.int.__add__" : builtins_int_add,
7.73 "__builtins__.int.__bool__" : builtins_int_bool,
7.74 "__builtins__.bool.__bool__" : builtins_bool_bool,
7.75 + "__builtins__.list" : builtins_list_new,
7.76 }
7.77
7.78 # vim: tabstop=4 expandtab shiftwidth=4
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/tests/empty.py Sun Apr 26 03:24:12 2009 +0200
8.3 @@ -0,0 +1,5 @@
8.4 +#!/usr/bin/env python
8.5 +
8.6 +# An empty program, not even having a docstring.
8.7 +
8.8 +# vim: tabstop=4 expandtab shiftwidth=4
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/tests/minimal.py Sun Apr 26 03:24:12 2009 +0200
9.3 @@ -0,0 +1,5 @@
9.4 +#!/usr/bin/env python
9.5 +
9.6 +"A minimal program. The docstring introduces some content."
9.7 +
9.8 +# vim: tabstop=4 expandtab shiftwidth=4
10.1 --- a/tests/op_add_call.py Thu Apr 23 00:52:33 2009 +0200
10.2 +++ b/tests/op_add_call.py Sun Apr 26 03:24:12 2009 +0200
10.3 @@ -1,11 +1,11 @@
10.4 #!/usr/bin/env python
10.5
10.6 def f(x, y):
10.7 - pass
10.8 + return x + y
10.9
10.10 a = 1
10.11 b = 2
10.12 c = 3
10.13 -f(a + b, b + c)
10.14 +d = f(a + b, b + c)
10.15
10.16 # vim: tabstop=4 expandtab shiftwidth=4