predef.icl 15.4 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
implementation module predef

import syntax, hashtable

::	PredefinedSymbols	:== {# PredefinedSymbol}

::	PredefinedSymbol =
	{	pds_ident	:: !Ident
	,	pds_module	:: !Index
	,	pds_def		:: !Index
	}

/* identifiers not present the hastable */


PD_PredefinedModule			:== 0

PD_StringType				:== 1
PD_ListType					:== 2
PD_Arity2TupleType			:== 3
PD_Arity32TupleType			:== 33

PD_LazyArrayType			:== 34
PD_StrictArrayType			:== 35
PD_UnboxedArrayType			:== 36

PD_ConsSymbol				:== 37
PD_NilSymbol				:== 38
PD_Arity2TupleSymbol		:== 39
PD_Arity32TupleSymbol		:== 69

PD_TypeVar_a0				:== 70
PD_TypeVar_a31				:== 101

35
36
/* Dynamics */

37
38
39
40
41
42
PD_TypeCodeMember			:== 102
// MV ...
PD_DynamicTemp				:== 103
PD_DynamicValue				:== 104
PD_DynamicType				:== 105
// ... MV
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
43
44
45

/* identifiers present in the hastable */

46
47
48
PD_StdArray					:== 106
PD_StdEnum					:== 107
PD_StdBool					:== 108
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
49

50
51
PD_AndOp					:== 109
PD_OrOp						:== 110
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
52
53
54
55


/* Array functions */

56
PD_ArrayClass				:== 111
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
57

58
59
60
61
62
63
64
65
PD_CreateArrayFun			:== 112
PD__CreateArrayFun			:== 113
PD_ArraySelectFun			:== 114
PD_UnqArraySelectFun		:== 115
PD_ArrayUpdateFun			:== 116
PD_ArrayReplaceFun			:== 117
PD_ArraySizeFun				:== 118
PD_UnqArraySizeFun			:== 119
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
66
67
68

/* Enum/Comprehension functions */

69
70
71
72
73
74
PD_SmallerFun				:== 120
PD_IncFun					:== 121
PD_From						:== 122
PD_FromThen					:== 123
PD_FromTo					:== 124
PD_FromThenTo				:== 125
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
75
76
77

/* Dynamics */

78
PD_TypeCodeClass			:== 126
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
79

80
81
82
PD_TypeObjectType			:== 127
PD_TypeConsSymbol			:== 128
PD_unify					:== 129
83
// MV ..
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
PD_coerce					:== 130
PD_variablePlaceholder		:== 131
PD_StdDynamics				:== 132
PD_undo_indirections		:== 133

/* Generics */
PD_StdGeneric				:== 134
PD_TypeISO					:== 135
PD_ConsISO					:== 136
PD_iso_to					:== 137
PD_iso_from					:== 138

PD_TypeUNIT					:== 139
PD_ConsUNIT					:== 140
PD_TypeEITHER				:== 141
PD_ConsLEFT					:== 142
PD_ConsRIGHT				:== 143
PD_TypePAIR					:== 144
PD_ConsPAIR					:== 145
PD_TypeARROW				:== 146
PD_ConsARROW				:== 147

106
107
108
109
110
111
112
113
PD_TypeCONSInfo				:== 148 
PD_ConsCONSInfo				:== 149
PD_cons_info				:== 150
PD_TypeCONS					:== 151
PD_ConsCONS					:== 152

PD_isomap_ARROW_			:== 153
PD_isomap_ID				:== 154
114
115

/* StdMisc */
116
117
118
PD_StdMisc					:== 155
PD_abort					:== 156
PD_undef					:== 157
119

120
PD_Start					:== 158
clean's avatar
clean committed
121

122
// MW..
123
PD_DummyForStrictAliasFun	:== 159
124

125
PD_NrOfPredefSymbols		:== 160
126
// ..MW
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
127
128
129
130
131
132
133
134
135
136
137


(<<=) infixl
(<<=) state val
	:==	let (array, symbol_table) = state
			(name, index) = val
			(id_info, new_symbol_table) = newPtr EmptySymbolTableEntry symbol_table
	  	in 	({ array & [index] = { pds_ident = { id_name = name, id_info = id_info }, pds_module = NoIndex, pds_def = NoIndex } }, new_symbol_table)

(<<-) infixl
(<<-) (array, hash_table) (name, table_kind, index)
138
139
//	# (id, hash_table) = putIdentInHashTable name table_kind hash_table
	# ({boxed_ident=id}, hash_table) = putIdentInHashTable name table_kind hash_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	= ({ array & [index] = { pds_ident = id, pds_module = NoIndex, pds_def = NoIndex } }, hash_table)
	
GetTupleConsIndex tup_arity :== PD_Arity2TupleSymbol + tup_arity - 2
GetTupleTypeIndex tup_arity :== PD_Arity2TupleType + tup_arity - 2

buildPredefinedSymbols :: !*HashTable -> (!.PredefinedSymbols,!*HashTable)
buildPredefinedSymbols hash_table=:{hte_symbol_heap}
	# predef_symbol_table = createArray PD_NrOfPredefSymbols { pds_ident = { id_name = "", id_info = nilPtr }, pds_module = NoIndex, pds_def = NoIndex }
	  (predef_symbol_table, hte_symbol_heap) = fill_table_without_hashing (predef_symbol_table, hte_symbol_heap)
	= fill_table_with_hashing (predef_symbol_table, { hash_table & hte_symbol_heap = hte_symbol_heap })
where	
	fill_table_without_hashing tables
		= build_variables 0 32 (build_tuples 2 32 tables)
			<<= ("_predefined", PD_PredefinedModule)
			<<= ("_string", PD_StringType)
			<<= ("_list", PD_ListType) <<= ("_cons", PD_ConsSymbol) <<= ("_nil", PD_NilSymbol)
			<<= ("_array", PD_LazyArrayType) <<= ("_!array", PD_StrictArrayType) <<= ("_#array", PD_UnboxedArrayType)
			<<= ("_type_code", PD_TypeCodeMember)
158
			<<= ("_dummyForStrictAlias", PD_DummyForStrictAliasFun) // MW++
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	where

		build_tuples tup_arity max_arity tables
			| tup_arity > max_arity
				= tables
				# tup_name = "_tuple" +++ toString tup_arity
				= build_tuples (inc tup_arity) max_arity (tables <<= (tup_name, GetTupleTypeIndex tup_arity)
						<<= (tup_name, GetTupleConsIndex tup_arity))

		build_variables var_number max_arity tables
			| var_number == max_arity
				= tables
				# var_name = "a" +++ toString var_number
				= build_variables (inc var_number) max_arity (tables <<= (var_name, PD_TypeVar_a0 + var_number))

	fill_table_with_hashing tables
175
		# (predefs, hash_table) = tables	
176
					<<- ("StdArray", IC_Module, PD_StdArray) <<- ("StdEnum", IC_Module, PD_StdEnum) <<- ("StdBool", IC_Module, PD_StdBool)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
177
178
179
180
181
182
183
184
					<<- ("&&", IC_Expression, PD_AndOp) <<- ("||", IC_Expression, PD_OrOp)
					<<- ("Array", IC_Class, PD_ArrayClass)
					<<- ("createArray", IC_Expression, PD_CreateArrayFun)
					<<- ("_createArray", IC_Expression, PD__CreateArrayFun)
					<<- ("select", IC_Expression, PD_ArraySelectFun)
					<<- ("uselect", IC_Expression, PD_UnqArraySelectFun) <<- ("update", IC_Expression, PD_ArrayUpdateFun)
					<<- ("replace", IC_Expression, PD_ArrayReplaceFun) <<- ("size", IC_Expression, PD_ArraySizeFun)
					<<- ("usize", IC_Expression, PD_UnqArraySizeFun)
185
186
187
// RWS ...					<<- ("_smaller", IC_Expression, PD_SmallerFun) <<- ("_inc", IC_Expression, PD_IncFun)
					<<- ("<", IC_Expression, PD_SmallerFun) <<- ("inc", IC_Expression, PD_IncFun)
// ... RWS
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
188
189
190
191
192
193
194
195
					<<- ("_from", IC_Expression, PD_From) <<- ("_from_then", IC_Expression, PD_FromThen)
					<<- ("_from_to", IC_Expression, PD_FromTo) <<- ("_from_then_to", IC_Expression, PD_FromThenTo)
					
					<<- ("TC", 					IC_Class, PD_TypeCodeClass)
					<<- ("T_ypeObjectType",		IC_Type, PD_TypeObjectType)
					<<- ("T_ypeConsSymbol",		IC_Expression, PD_TypeConsSymbol)
					<<- ("P_laceholder",		IC_Expression, PD_variablePlaceholder)
					<<- ("_unify",				IC_Expression, PD_unify)
196
					<<-	("_coerce",				IC_Expression, PD_coerce) /* MV */
197
					<<- ("StdDynamic",			IC_Module, PD_StdDynamics)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
198
					<<- ("_undo_indirections",	IC_Expression, PD_undo_indirections)
199
// MV..
200
					<<- ("DynamicTemp",			IC_Type, PD_DynamicTemp)
201
// ..MV
202
					
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// AA..
					<<- ("StdGeneric",			IC_Module, 		PD_StdGeneric)
					<<- ("ISO",					IC_Type, 		PD_TypeISO)
					<<- ("_ISO",				IC_Expression, 	PD_ConsISO)					
					//<<- ("iso_from",			IC_Field {id_name="", id_info=nilPtr}, PD_iso_from)
					//<<- ("iso_to",			IC_Field {id_name="", id_info=nilPtr}, PD_iso_to)					
					<<- ("UNIT",				IC_Type, 		PD_TypeUNIT)
					<<- ("UNIT",				IC_Expression,	PD_ConsUNIT)
					<<- ("EITHER",				IC_Type, 		PD_TypeEITHER)
					<<- ("LEFT",				IC_Expression,	PD_ConsLEFT)
					<<- ("RIGHT",				IC_Expression,	PD_ConsRIGHT)
					<<- ("PAIR",				IC_Type, 		PD_TypePAIR)					
					<<- ("PAIR",				IC_Expression,	PD_ConsPAIR)					
					<<- ("ARROW",				IC_Type, 		PD_TypeARROW)
					<<- ("ARROW",				IC_Expression, 	PD_ConsARROW)										
					<<- ("isomap_ARROW_",		IC_Expression, 	PD_isomap_ARROW_)										
					<<- ("isomap_ID",			IC_Expression, 	PD_isomap_ID)										
220
221
222
223
224
					<<- ("CONSInfo",			IC_Type, 		PD_TypeCONSInfo)					
					<<- ("_CONSInfo",			IC_Expression,	PD_ConsCONSInfo)					
					<<- ("CONS",				IC_Type, 		PD_TypeCONS)					
					<<- ("CONS",				IC_Expression,	PD_ConsCONS)					
					<<- ("_cons_info",			IC_Expression, 	PD_cons_info)										
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

					<<- ("StdMisc",				IC_Module, 		PD_StdMisc)
					<<- ("abort",				IC_Expression, 	PD_abort)
					<<- ("undef",				IC_Expression, 	PD_undef)					
// ..AA					
					
					<<- ("Start",				IC_Expression, PD_Start)

		# ({pds_ident}, predefs) = predefs![PD_TypeISO]
		# (predefs, hash_table)= (predefs, hash_table) 
			<<- ("iso_from", 			IC_Field pds_ident, PD_iso_from)
			<<- ("iso_to", 				IC_Field pds_ident, PD_iso_to)

		# ({pds_ident}, predefs) = predefs![PD_DynamicTemp]
		# (predefs, hash_table)= (predefs, hash_table) 
			<<- ("type",				IC_Field pds_ident, PD_DynamicType)
			<<- ("value",				IC_Field pds_ident, PD_DynamicValue)
			<<- ("Start",				IC_Expression, PD_Start)
		= (predefs, hash_table)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

MakeTupleConsSymbIndex arity 	:== arity - 2 + cArity2TupleConsSymbIndex
MakeTupleTypeSymbIndex arity 	:== arity - 2 + cArity2TupleTypeSymbIndex

MakeNilExpression pre_def_symbols			:== PE_List [PE_Ident pre_def_symbols.[PD_NilSymbol]]
MakeConsExpression a1 a2 pre_def_symbols	:== PE_List [PE_Ident pre_def_symbols.[PD_ConsSymbol], a1, a2]

MaxTupleArity				:== 32

cLazyArray			:== 0
cStrictArray		:== 1
cUnboxedArray		:== 2

cConsSymbIndex				:== 0
cNilSymbIndex				:== 1
cArity2TupleConsSymbIndex	:== 2
//Arity32TupleConsSymbIndex	:== 32

cListTypeSymbIndex			:== 0
cArity2TupleTypeSymbIndex	:== 1
//Arity32TupleTypeSymbIndex	:== 31
cLazyArraySymbIndex			:== 32
cStrictArraySymbIndex		:== 33
cUnboxedArraySymbIndex		:== 34

cLastPredefinedConstructor	:== 32
cLastPredefinedType			:== 34

cTCClassSymbIndex			:== 0

cTCMemberSymbIndex			:== 0

cTCInstanceSymbIndex		:== 0

buildPredefinedModule :: !*PredefinedSymbols -> (!ScannedModule, !.PredefinedSymbols)
buildPredefinedModule pre_def_symbols 
	# (type_var_id, pre_def_symbols)	= pre_def_symbols![PD_TypeVar_a0]
	  (cons_id, pre_def_symbols)		= pre_def_symbols![PD_ConsSymbol]
	  (nil_id, pre_def_symbols)			= pre_def_symbols![PD_NilSymbol]
	  (string_id, pre_def_symbols)		= pre_def_symbols![PD_StringType]
	  (list_id, pre_def_symbols)		= pre_def_symbols![PD_ListType]
	  (unb_array_id, pre_def_symbols)	= pre_def_symbols![PD_UnboxedArrayType]
	  (pre_mod_symb, pre_def_symbols)	= pre_def_symbols![PD_PredefinedModule]
287
	  (alias_dummy_symb, pre_def_symbols)	= pre_def_symbols![PD_DummyForStrictAliasFun] // MW++
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
	  (cons_symb, pre_def_symbols)		= new_defined_symbol PD_ConsSymbol 2 cConsSymbIndex pre_def_symbols
	  (nil_symb, pre_def_symbols)		= new_defined_symbol PD_NilSymbol 0 cNilSymbIndex pre_def_symbols
	  pre_mod_id						= pre_mod_symb.pds_ident

	  type_var						= MakeTypeVar type_var_id.pds_ident
	  type_var_with_attr			= MakeAttributedType (TV type_var)
	  list_type						= MakeAttributedType (TA (MakeNewTypeSymbIdent list_id.pds_ident 1) [type_var_with_attr])
	  unb_arr_of_char_type			= MakeAttributedType (TA (MakeNewTypeSymbIdent unb_array_id.pds_ident 1) [MakeAttributedType (TB BT_Char)])

	  (string_def, pre_def_symbols)	= make_type_def PD_StringType [] (SynType unb_arr_of_char_type) pre_def_symbols
	  (list_def, pre_def_symbols)	= make_type_def PD_ListType [type_var] (AlgType [cons_symb,nil_symb]) pre_def_symbols
	
	  cons_def			= { pc_cons_name = cons_id.pds_ident, pc_cons_arity = 2, pc_arg_types = [type_var_with_attr, list_type],
	  						pc_cons_prio =  NoPrio, pc_exi_vars = [], pc_cons_pos = PreDefPos pre_mod_id}
	  nil_def			= { pc_cons_name = nil_id.pds_ident, pc_cons_arity = 0, pc_arg_types = [],
	  						pc_cons_prio =  NoPrio, pc_exi_vars = [], pc_cons_pos = PreDefPos pre_mod_id}

	  (array_def, pre_def_symbols)		= make_type_def PD_LazyArrayType [type_var] (AbstractType cAllBitsClear) pre_def_symbols
	  (strict_def, pre_def_symbols)		= make_type_def PD_StrictArrayType [type_var] (AbstractType cIsHyperStrict) pre_def_symbols
	  (unboxed_def, pre_def_symbols)	= make_type_def PD_UnboxedArrayType [type_var] (AbstractType cIsHyperStrict) pre_def_symbols

	  (type_defs, cons_defs, pre_def_symbols)	= add_tuple_defs pre_mod_id MaxTupleArity [array_def,strict_def,unboxed_def] [] pre_def_symbols
310
	  alias_dummy_type = make_identity_fun_type alias_dummy_symb.pds_ident type_var // MW++
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
311
312
313
	  (class_def, member_def, pre_def_symbols) = make_TC_class_def pre_def_symbols
	= ({ mod_name = pre_mod_id, mod_type = MK_System, mod_imports = [],  mod_imported_objects = [],
		 mod_defs = {
314
315
			def_types = [string_def, list_def : type_defs], def_constructors
						= [ParsedConstructorToConsDef cons_def, ParsedConstructorToConsDef nil_def : cons_defs], def_selectors = [], def_classes = [class_def],
316
			def_macros = { ir_from = 0, ir_to = 0 }, def_members = [member_def], def_funtypes = [alias_dummy_type], def_instances = [], /* AA */ def_generics = [] }}, pre_def_symbols)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
317
318
319
320
321
322
323
324
325
326
where
	add_tuple_defs pre_mod_id tup_arity type_defs cons_defs pre_def_symbols
		| tup_arity >= 2
			# (type_vars, pre_def_symbols)		= make_type_vars tup_arity [] pre_def_symbols
			  (tuple_id, pre_def_symbols)		= pre_def_symbols![GetTupleConsIndex tup_arity]
			  tuple_cons_symb					= { ds_ident = tuple_id.pds_ident, ds_index = MakeTupleConsSymbIndex tup_arity, ds_arity = tup_arity }
			  
			  (tuple_type_def, pre_def_symbols)	= make_type_def (GetTupleTypeIndex tup_arity) type_vars (AlgType [tuple_cons_symb]) pre_def_symbols
			  tuple_cons_def	= { pc_cons_name = tuple_id.pds_ident, pc_cons_arity = tup_arity, pc_cons_pos = PreDefPos pre_mod_id,
			  						pc_arg_types = [ MakeAttributedType (TV tv) \\ tv <- type_vars], pc_cons_prio =  NoPrio, pc_exi_vars = []}
327
			= add_tuple_defs pre_mod_id (dec tup_arity) [tuple_type_def : type_defs] [ParsedConstructorToConsDef tuple_cons_def : cons_defs] pre_def_symbols
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
328
329
330
331
332
333
			= (type_defs, cons_defs, pre_def_symbols)
	where
		make_type_vars nr_of_vars type_vars pre_def_symbols
			| nr_of_vars == 0
				= (type_vars, pre_def_symbols)
				# nr_of_vars = dec nr_of_vars
334
				# (var_id, pre_def_symbols) = pre_def_symbols![PD_TypeVar_a0 + nr_of_vars]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
335
336
337
				= make_type_vars nr_of_vars [MakeTypeVar var_id.pds_ident : type_vars] pre_def_symbols

	new_defined_symbol symbol_index arity ds_index pre_def_symbols
338
		# (ds_ident, pre_def_symbols) = pre_def_symbols![symbol_index]
339
		= ({ ds_ident = ds_ident.pds_ident, ds_arity = arity/*AA: was 2*/, ds_index = ds_index }, pre_def_symbols)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
340
341
	
	make_type_def type_cons_index type_vars type_rhs pre_def_symbols
342
		# (type_ident, pre_def_symbols) = pre_def_symbols![type_cons_index]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
		= (MakeTypeDef type_ident.pds_ident (map (\tv -> MakeAttributedTypeVar tv) type_vars) type_rhs TA_None [] NoPos, pre_def_symbols)
	
	make_TC_class_def pre_def_symbols
		# (tc_class_name, pre_def_symbols)		= pre_def_symbols![PD_TypeCodeClass]
		  (type_var_id, pre_def_symbols)		= pre_def_symbols![PD_TypeVar_a0]
		  (tc_member_name, pre_def_symbols)		= pre_def_symbols![PD_TypeCodeMember]
		
		  class_var = MakeTypeVar type_var_id.pds_ident

		  me_type = { st_vars = [], st_args = [], st_arity = 0,
					  st_result = { at_attribute = TA_None, at_annotation = AN_None, at_type = TV class_var },
					  st_context = [ {tc_class = {glob_module = NoIndex, glob_object = {ds_ident = tc_class_name.pds_ident, ds_arity = 1, ds_index = NoIndex }},
					   				tc_types = [ TV class_var ], tc_var = nilPtr }],
					  st_attr_vars = [], st_attr_env = [] }

		  member_def = { me_symb = tc_member_name.pds_ident, me_type = me_type, me_pos = NoPos, me_priority = NoPrio,
						 me_offset = NoIndex, me_class_vars = [], me_class = { glob_module = NoIndex, glob_object = NoIndex}, me_type_ptr = nilPtr }
		
		  class_def = { class_name = tc_class_name.pds_ident, class_arity = 1, class_args = [class_var], class_context = [],
		  				class_members = {{ds_ident = tc_member_name.pds_ident, ds_index = cTCMemberSymbIndex, ds_arity = 0 }}, class_cons_vars = 0,
						class_dictionary = { ds_ident = { tc_class_name.pds_ident & id_info = nilPtr }, ds_arity = 0, ds_index = NoIndex }, class_pos = NoPos }

		= (class_def, member_def, pre_def_symbols)

367
368
369
370
371
372
373
374
// MW..
	make_identity_fun_type alias_dummy_id type_var
		# a = { at_attribute = TA_Anonymous, at_annotation = AN_Strict, at_type = TV type_var }
		  id_symbol_type = { st_vars = [], st_args = [a], st_arity = 1, st_result = a, st_context = [], 
							st_attr_vars = [], st_attr_env = [] } // !.a -> .a
		= { ft_symb = alias_dummy_id, ft_arity = 1, ft_priority = NoPrio, ft_type = id_symbol_type, ft_pos = NoPos,
			ft_specials = SP_None, ft_type_ptr = nilPtr }
// ..MW