1.1 --- a/generator.py Sat Mar 25 23:06:55 2017 +0100
1.2 +++ b/generator.py Sat Mar 25 23:20:16 2017 +0100
1.3 @@ -42,10 +42,13 @@
1.4
1.5 # NOTE: These must be synchronised with the library.
1.6
1.7 + dict_type = "__builtins__.dict.dict"
1.8 function_type = "__builtins__.core.function"
1.9 int_type = "__builtins__.int.int"
1.10 + list_type = "__builtins__.list.list"
1.11 none_type = "__builtins__.none.NoneType"
1.12 string_type = "__builtins__.str.string"
1.13 + tuple_type = "__builtins__.tuple.tuple"
1.14 type_type = "__builtins__.core.type"
1.15 unicode_type = "__builtins__.unicode.utf8string"
1.16
1.17 @@ -58,17 +61,10 @@
1.18 ("__builtins__.notimplemented", "NotImplemented"),
1.19 )
1.20
1.21 - literal_mapping_types = (
1.22 - "__builtins__.dict.dict",
1.23 + literal_instantiator_types = (
1.24 + dict_type, list_type, tuple_type
1.25 )
1.26
1.27 - literal_sequence_types = (
1.28 - "__builtins__.list.list",
1.29 - "__builtins__.tuple.tuple",
1.30 - )
1.31 -
1.32 - literal_instantiator_types = literal_mapping_types + literal_sequence_types
1.33 -
1.34 def __init__(self, importer, optimiser, output):
1.35
1.36 """
1.37 @@ -1247,15 +1243,11 @@
1.38 # Signature: __newliteral_sequence(ARGS, NUM)
1.39
1.40 if path in self.literal_instantiator_types:
1.41 - if path in self.literal_mapping_types:
1.42 - style = "mapping"
1.43 - else:
1.44 - style = "sequence"
1.45 + style = path.rsplit(".", 1)[-1]
1.46
1.47 - print >>f_signatures, "#define %s(ARGS, NUM) (%s(__NEWINSTANCE(%s), ARGS, NUM))" % (
1.48 + print >>f_signatures, "#define %s(ARGS, NUM) (%s(ARGS, NUM))" % (
1.49 encode_literal_instantiator(path),
1.50 encode_literal_data_initialiser(style),
1.51 - encode_path(path)
1.52 )
1.53
1.54 def write_main_program(self, f_code, f_signatures):
2.1 --- a/templates/progops.c Sat Mar 25 23:06:55 2017 +0100
2.2 +++ b/templates/progops.c Sat Mar 25 23:20:16 2017 +0100
2.3 @@ -28,12 +28,17 @@
2.4
2.5 /* Generic instantiation operations, defining common members. */
2.6
2.7 +void __init(__ref obj, const __table * table, __ref cls)
2.8 +{
2.9 + obj->table = table;
2.10 + obj->pos = __INSTANCEPOS;
2.11 + __store_via_object(obj, __class__, __ATTRVALUE(cls));
2.12 +}
2.13 +
2.14 __attr __new(const __table * table, __ref cls, size_t size, int immutable)
2.15 {
2.16 __ref obj = (__ref) (immutable ? __ALLOCATEIM : __ALLOCATE)(1, size);
2.17 - obj->table = table;
2.18 - obj->pos = __INSTANCEPOS;
2.19 - __store_via_object(obj, __class__, __ATTRVALUE(cls));
2.20 + __init(obj, table, cls);
2.21 return __ATTRVALUE(obj);
2.22 }
2.23
2.24 @@ -57,11 +62,8 @@
2.25 return data;
2.26 }
2.27
2.28 -__attr __newdata_sequence(__attr self, __attr args[], unsigned int number)
2.29 +void __newdata_sequence(__attr self, __attr args[], unsigned int number, __fragment *data)
2.30 {
2.31 - /* Calculate the size of the fragment. */
2.32 -
2.33 - __fragment *data = __new_fragment(number);
2.34 __attr attr = {.seqvalue=data};
2.35 unsigned int i;
2.36
2.37 @@ -75,12 +77,35 @@
2.38 /* Store a reference to the data in the object's __data__ attribute. */
2.39
2.40 __store_via_object(__VALUE(self), __data__, attr);
2.41 +}
2.42 +
2.43 +__attr __newdata_list(__attr args[], unsigned int number)
2.44 +{
2.45 + __attr self = __NEWINSTANCE(__builtins___list_list);
2.46 + __fragment *data = __new_fragment(number);
2.47 + __newdata_sequence(self, args, number, data);
2.48 + return self;
2.49 +}
2.50 +
2.51 +__attr __newdata_tuple(__attr args[], unsigned int number)
2.52 +{
2.53 + /* Allocate the tuple and fragment together. */
2.54 +
2.55 + __ref obj = (__ref) __ALLOCATE(1, __INSTANCESIZE(__builtins___tuple_tuple) + __FRAGMENT_SIZE(number));
2.56 + __attr self = __ATTRVALUE(obj);
2.57 +
2.58 + /* Initialise the instance and fragment. */
2.59 +
2.60 + __init(obj, &__INSTANCETABLE(__builtins___tuple_tuple), &__builtins___tuple_tuple);
2.61 + __newdata_sequence(self, args, number, (__fragment *) ((void *) obj + __INSTANCESIZE(__builtins___tuple_tuple)));
2.62 return self;
2.63 }
2.64
2.65 #ifdef __HAVE___builtins___dict_dict
2.66 -__attr __newdata_mapping(__attr self, __attr args[], unsigned int number)
2.67 +__attr __newdata_dict(__attr args[], unsigned int number)
2.68 {
2.69 + __attr self = __NEWINSTANCE(__builtins___dict_dict);
2.70 +
2.71 /* Create a temporary list using the arguments. */
2.72
2.73 __attr tmp = __newliteral___builtins___list_list(args, number);
3.1 --- a/templates/progops.h Sat Mar 25 23:06:55 2017 +0100
3.2 +++ b/templates/progops.h Sat Mar 25 23:20:16 2017 +0100
3.3 @@ -32,10 +32,11 @@
3.4
3.5 __fragment *__new_fragment(unsigned int n);
3.6
3.7 -__attr __newdata_sequence(__attr self, __attr args[], unsigned int number);
3.8 +__attr __newdata_list(__attr args[], unsigned int number);
3.9 +__attr __newdata_tuple(__attr args[], unsigned int number);
3.10
3.11 #ifdef __HAVE___builtins___dict_dict
3.12 -__attr __newdata_mapping(__attr self, __attr args[], unsigned int number);
3.13 +__attr __newdata_dict(__attr args[], unsigned int number);
3.14 #endif /* __HAVE___builtins___dict_dict */
3.15
3.16 /* Helpers for raising errors within common operations. */
3.17 @@ -78,8 +79,10 @@
3.18
3.19 /* Convenience definitions. */
3.20
3.21 -#define __NEWINSTANCE(__CLS) __new(&__InstanceTable_##__CLS, &__CLS, sizeof(__obj_##__CLS), 0)
3.22 -#define __NEWINSTANCEIM(__CLS) __new(&__InstanceTable_##__CLS, &__CLS, sizeof(__obj_##__CLS), 1)
3.23 -#define __ISINSTANCE(__ATTR, __TYPE) __BOOL(__fn_native_introspection_isinstance(__NULL, __ATTR, __TYPE))
3.24 +#define __INSTANCESIZE(CLS) sizeof(__obj_##CLS)
3.25 +#define __INSTANCETABLE(CLS) (__InstanceTable_##CLS)
3.26 +#define __NEWINSTANCE(CLS) __new(&__INSTANCETABLE(CLS), &CLS, __INSTANCESIZE(CLS), 0)
3.27 +#define __NEWINSTANCEIM(CLS) __new(&__INSTANCETABLE(CLS), &CLS, __INSTANCESIZE(CLS), 1)
3.28 +#define __ISINSTANCE(ATTR, TYPE) __BOOL(__fn_native_introspection_isinstance(__NULL, ATTR, TYPE))
3.29
3.30 #endif /* __PROGOPS_H__ */