postparse.icl 72 KB
Newer Older
Martin Wierich's avatar
Martin Wierich committed
1
2
3
implementation module postparse

import StdEnv
4
import syntax, parse, utilities, StdCompare
5
//import RWSDebug
Martin Wierich's avatar
Martin Wierich committed
6
7
8
9
10
11
12
13
14
15
16

:: *CollectAdmin =
	{	ca_error		:: !*ParseErrorAdmin
	,	ca_fun_count	:: !Int
	,	ca_rev_fun_defs	:: ![FunDef]
	,	ca_hash_table	:: !*HashTable
	}

cIsAGlobalDef		:== True
cIsNotAGlobalDef	:== False

17
18
19
predef_ident_expr :: Int -> ParsedExpr
predef_ident_expr index
	=	PE_Ident predefined_idents.[index]
Martin Wierich's avatar
Martin Wierich committed
20
21
22
23
24
25
26
27
28
29
30
31

optGuardedAltToRhs :: OptGuardedAlts -> Rhs
optGuardedAltToRhs optGuardedAlt
	=	{	rhs_alts	= optGuardedAlt
		,	rhs_locals	= LocalParsedDefs []
		}

exprToRhs expr 
	:==	{	rhs_alts	= UnGuardedExpr
 						{	ewl_nodes	= []
						,	ewl_expr	= expr
						,	ewl_locals = LocalParsedDefs []
32
						,	ewl_position= NoPos
Martin Wierich's avatar
Martin Wierich committed
33
34
35
36
37
38
						}
		,	rhs_locals	= LocalParsedDefs []
		}

prefixAndPositionToIdent :: !String !LineAndColumn !*CollectAdmin -> (!Ident, !*CollectAdmin)
prefixAndPositionToIdent prefix {lc_line, lc_column} ca=:{ca_hash_table}
39
40
41
42
43
44
45
	# ({boxed_ident=ident}, ca_hash_table) = putIdentInHashTable (prefix +++ ";" +++ toString lc_line +++ ";" +++ toString lc_column) IC_Expression ca_hash_table
	= (ident, { ca & ca_hash_table = ca_hash_table } )

prefixAndPositionToIdentExp :: !String !LineAndColumn !*CollectAdmin -> (!ParsedExpr, !*CollectAdmin)
prefixAndPositionToIdentExp prefix {lc_line, lc_column} ca=:{ca_hash_table}
	# ({boxed_ident=ident}, ca_hash_table) = putIdentInHashTable (prefix +++ ";" +++ toString lc_line +++ ";" +++ toString lc_column) IC_Expression ca_hash_table
	= (PE_Ident ident, { ca & ca_hash_table = ca_hash_table } )
Martin Wierich's avatar
Martin Wierich committed
46
47
48

(`) infixl 9
(`) f a
49
	:== apply f (toParsedExpr a)
Martin Wierich's avatar
Martin Wierich committed
50
51
52
53
54
55
56
57
58

// apply :: ParsedExpr ParsedExpr -> ParsedExpr

apply :: ParsedExpr ParsedExpr -> ParsedExpr
apply (PE_List application) a
	=	PE_List (application ++ [a])
apply f a
	=	PE_List [f, a]

59
class toParsedExpr a :: !a -> ParsedExpr
Martin Wierich's avatar
Martin Wierich committed
60
61
62

instance toParsedExpr [a] | toParsedExpr a where
	toParsedExpr []
63
		=	predef_ident_expr PD_NilSymbol
Martin Wierich's avatar
Martin Wierich committed
64
	toParsedExpr [hd:tl]
65
		=	predef_ident_expr PD_ConsSymbol ` hd ` tl
Martin Wierich's avatar
Martin Wierich committed
66
67
68

instance toParsedExpr ParsedExpr where
	toParsedExpr x
69
		=	x
Martin Wierich's avatar
Martin Wierich committed
70
71
72

instance toParsedExpr Int where
	toParsedExpr x
73
		=	PE_Basic (BVInt x)
Martin Wierich's avatar
Martin Wierich committed
74
75
76
77

postParseError :: Position {#Char} *CollectAdmin -> *CollectAdmin
postParseError pos msg ps=:{ca_error={pea_file}}
	# (filename, line, funname) = get_file_and_line_nr pos
Martin Wierich's avatar
Martin Wierich committed
78
	  pea_file	= pea_file <<< "Error [" <<< filename <<< "," <<< line
Martin Wierich's avatar
Martin Wierich committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	  pea_file	= case funname of
	  				Yes name	-> pea_file <<< "," <<< name
	  				No			-> pea_file
	  pea_file	= pea_file <<< "]: " <<< msg <<< ".\n"
	= {ps & ca_error = { pea_file = pea_file, pea_ok = False }}
where
	get_file_and_line_nr :: Position -> (FileName, LineNr, Optional FunctName)
	get_file_and_line_nr (FunPos filename linenr funname)
		= (filename, linenr, Yes funname)
	get_file_and_line_nr (LinePos filename linenr)
		= (filename, linenr, No)

addFunctionsRange :: [FunDef] *CollectAdmin -> (IndexRange, *CollectAdmin)
addFunctionsRange fun_defs ca
	# (frm, ca)
	  	=	ca!ca_fun_count
	  ca
		=	foldSt add_function fun_defs ca
	  (to, ca)
	  	=	ca!ca_fun_count
	=	({ir_from = frm, ir_to = to}, ca)
	where
		add_function :: FunDef !*CollectAdmin -> !*CollectAdmin
		add_function fun_def ca=:{ca_fun_count, ca_rev_fun_defs}
			=	{ca & ca_fun_count = ca.ca_fun_count + 1
					, ca_rev_fun_defs = [fun_def : ca.ca_rev_fun_defs]
				}

107
108
MakeNewImpOrDefFunction name arity body kind prio opt_type pos
	:== { fun_symb = name, fun_arity = arity, fun_priority = prio, fun_type = opt_type, fun_kind = kind,
109
		  fun_body = ParsedBody body, fun_pos = pos, fun_lifted = 0, fun_info = EmptyFunInfo }
110

111
112
class collectFunctions a :: a Bool !*CollectAdmin -> (a, !*CollectAdmin)

Martin Wierich's avatar
Martin Wierich committed
113
114
instance collectFunctions ParsedExpr
where
115
116
	collectFunctions (PE_List exprs) icl_module ca
		# (exprs, ca) = collectFunctions exprs icl_module ca
Martin Wierich's avatar
Martin Wierich committed
117
		= (PE_List exprs, ca)
118
119
	collectFunctions (PE_Bound bound_expr) icl_module ca
		# (bound_expr, ca) = collectFunctions bound_expr icl_module ca
Martin Wierich's avatar
Martin Wierich committed
120
		= (PE_Bound bound_expr, ca)
121
122
	collectFunctions (PE_Lambda lam_ident args res pos) icl_module ca
		# ((args,res), ca) = collectFunctions (args,res) icl_module ca
123
124
		# (range, ca) = addFunctionsRange [transformLambda lam_ident args res pos] ca
		= (PE_Let cIsStrict (CollectedLocalDefs { loc_functions = range, loc_nodes = [], loc_in_icl_module=icl_module })
Martin Wierich's avatar
Martin Wierich committed
125
				(PE_Ident lam_ident), ca)
126
127
	collectFunctions (PE_Record rec_expr type_name fields) icl_module ca
		# ((rec_expr,fields), ca) = collectFunctions (rec_expr,fields) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
128
		= (PE_Record rec_expr type_name fields, ca)
129
130
	collectFunctions (PE_Tuple exprs) icl_module ca
		# (exprs, ca) = collectFunctions exprs icl_module ca
Martin Wierich's avatar
Martin Wierich committed
131
		= (PE_Tuple exprs, ca)
132
133
	collectFunctions (PE_Selection is_unique expr selectors) icl_module ca
		# ((expr, selectors), ca) = collectFunctions (expr, selectors) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
134
		= (PE_Selection is_unique expr selectors, ca)
135
136
	collectFunctions (PE_Update expr1 updates expr2) icl_module ca
		# ((expr1, (updates, expr2)), ca) = collectFunctions (expr1, (updates, expr2)) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
137
		= (PE_Update expr1 updates expr2, ca)
138
139
	collectFunctions (PE_Case case_ident pattern_expr case_alts) icl_module ca
		# ((pattern_expr,case_alts), ca) = collectFunctions (pattern_expr,case_alts) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
140
		= (PE_Case case_ident pattern_expr case_alts, ca)
141
	collectFunctions (PE_If if_ident c t e) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
142
143
144
145
146
		# true_pattern		= PE_Basic (BVB True)
		  false_pattern		= PE_WildCard // PE_Basic (BVB False)
		= collectFunctions (PE_Case if_ident c
							[ {calt_pattern = true_pattern , calt_rhs = exprToRhs t}
							, {calt_pattern = false_pattern, calt_rhs = exprToRhs e}
147
148
149
							]) icl_module ca
	collectFunctions (PE_Let strict locals in_expr) icl_module ca
		# ((node_defs,in_expr), ca) = collectFunctions (locals,in_expr) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
150
		= (PE_Let strict node_defs in_expr, ca)
151
152
153
154
155
	collectFunctions (PE_ListCompr predef_cons_index predef_nil_index expr qualifiers) icl_module ca
		# (compr, ca) = transformListComprehension predef_cons_index predef_nil_index expr qualifiers ca
		= collectFunctions compr icl_module ca
	collectFunctions (PE_ArrayCompr expr qualifiers) icl_module ca
		# (compr, ca) = transformArrayComprehension expr qualifiers ca
156
157
		=	collectFunctions compr icl_module ca
	collectFunctions (PE_UpdateComprehension expr updateExpr identExpr qualifiers) icl_module ca
158
159
		# (compr, ca) = transformUpdateComprehension [expr] [updateExpr] [identExpr] identExpr qualifiers ca
		= collectFunctions compr icl_module ca
160
161
162
163
	collectFunctions (PE_Sequ sequence) icl_module ca
		= collectFunctions (transformSequence sequence) icl_module ca
	collectFunctions (PE_ArrayDenot exprs) icl_module ca
		= collectFunctions (transformArrayDenot exprs) icl_module ca
164
165
	collectFunctions (PE_Dynamic exprs opt_dyn_type) icl_module ca
		# (exprs, ca) = collectFunctions exprs icl_module ca
Martin Wierich's avatar
Martin Wierich committed
166
		= (PE_Dynamic exprs opt_dyn_type, ca)
167
	collectFunctions expr icl_module ca
Martin Wierich's avatar
Martin Wierich committed
168
169
170
171
		= (expr, ca)

instance collectFunctions [a] | collectFunctions a
where
172
173
174
175
176
177
178
179
180
181
182
	collectFunctions l icl_module ca
//		=	mapSt collectFunctions l icl_module ca
		= map_st l ca
		where
			map_st [x : xs] s
			 	# (x, s) = collectFunctions x icl_module s
				  (xs, s) = map_st xs s
				#! s = s
				= ([x : xs], s)
			map_st [] s
			 	= ([], s)
Martin Wierich's avatar
Martin Wierich committed
183
184
185

instance collectFunctions (a,b) | collectFunctions a & collectFunctions b
where
186
187
188
	collectFunctions (x,y) icl_module ca
		# (x, ca) = collectFunctions x icl_module ca
		  (y, ca) = collectFunctions y icl_module ca
Martin Wierich's avatar
Martin Wierich committed
189
190
191
192
		= ((x,y), ca)

instance collectFunctions Qualifier
where
193
194
	collectFunctions qual=:{qual_generators, qual_filter} icl_module ca
		# ((qual_generators, qual_filter), ca) = collectFunctions (qual_generators, qual_filter) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
195
196
197
198
		= ({ qual & qual_generators = qual_generators, qual_filter = qual_filter }, ca) 

instance collectFunctions Generator
where
199
200
	collectFunctions gen=:{gen_pattern,gen_expr} icl_module ca
		# ((gen_pattern,gen_expr), ca) = collectFunctions (gen_pattern,gen_expr) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
201
202
203
204
		= ({gen & gen_pattern = gen_pattern, gen_expr = gen_expr}, ca)

instance collectFunctions (Optional a) | collectFunctions a
where
205
206
	collectFunctions (Yes expr) icl_module ca
		# (expr, ca) = collectFunctions expr icl_module ca
Martin Wierich's avatar
Martin Wierich committed
207
		= (Yes expr, ca) 
208
	collectFunctions No icl_module ca
Martin Wierich's avatar
Martin Wierich committed
209
210
211
212
		= (No, ca) 

instance collectFunctions ParsedSelection
where
213
214
	collectFunctions (PS_Array index_expr) icl_module ca
		# (index_expr, ca) = collectFunctions index_expr icl_module ca
Martin Wierich's avatar
Martin Wierich committed
215
		= (PS_Array index_expr, ca)
216
	collectFunctions expr icl_module ca
Martin Wierich's avatar
Martin Wierich committed
217
218
219
220
		= (expr, ca)

instance collectFunctions CaseAlt
where
221
222
	collectFunctions calt=:{calt_pattern,calt_rhs} icl_module ca
		# ((calt_pattern,calt_rhs), ca) = collectFunctions (calt_pattern,calt_rhs) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
223
224
225
226
		= ({calt & calt_pattern = calt_pattern, calt_rhs = calt_rhs}, ca) 

instance collectFunctions (Bind a b) | collectFunctions a & collectFunctions b
where
227
228
	collectFunctions bind=:{bind_src,bind_dst} icl_module ca
		# ((bind_src,bind_dst), ca) = collectFunctions (bind_src,bind_dst) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
229
230
231
232
		= ({bind & bind_src = bind_src, bind_dst = bind_dst }, ca)

instance collectFunctions OptGuardedAlts
where
233
234
	collectFunctions (GuardedAlts guarded_exprs (Yes def_expr)) icl_module ca
		# ((guarded_exprs, def_expr), ca) = collectFunctions (guarded_exprs, def_expr) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
235
		= (GuardedAlts guarded_exprs (Yes def_expr), ca)
236
237
	collectFunctions (GuardedAlts guarded_exprs No) icl_module ca
		# (guarded_exprs, ca) = collectFunctions guarded_exprs icl_module ca
Martin Wierich's avatar
Martin Wierich committed
238
		= (GuardedAlts guarded_exprs No, ca)
239
240
	collectFunctions (UnGuardedExpr unguarded_expr) icl_module ca
		# (unguarded_expr, ca) = collectFunctions unguarded_expr icl_module ca
Martin Wierich's avatar
Martin Wierich committed
241
242
243
244
		= (UnGuardedExpr unguarded_expr, ca)

instance collectFunctions GuardedExpr
where
245
	collectFunctions alt=:{alt_nodes,alt_guard,alt_expr} icl_module ca
Martin Wierich's avatar
Martin Wierich committed
246
		# ((alt_nodes, (alt_guard, alt_expr)), ca) =
247
				collectFunctions (alt_nodes, (alt_guard, alt_expr)) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
248
249
250
251
		= ({alt & alt_nodes = alt_nodes, alt_guard = alt_guard, alt_expr = alt_expr}, ca)

instance collectFunctions ExprWithLocalDefs
where
252
253
	collectFunctions expr=:{ewl_nodes, ewl_expr,ewl_locals} icl_module ca
		# ((ewl_nodes, (ewl_expr, ewl_locals)), ca) = collectFunctions (ewl_nodes, (ewl_expr, ewl_locals)) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
254
255
256
257
		= ({expr & ewl_nodes = ewl_nodes, ewl_expr = ewl_expr, ewl_locals = ewl_locals}, ca)

instance collectFunctions NodeDefWithLocals
where
258
259
	collectFunctions node_def=:{ndwl_def, ndwl_locals} icl_module ca
		# (( ndwl_def, ndwl_locals), ca) = collectFunctions (ndwl_def, ndwl_locals) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
260
261
262
263
		= ({node_def & ndwl_def = ndwl_def, ndwl_locals = ndwl_locals}, ca)

instance collectFunctions Rhs
where
264
265
	collectFunctions {rhs_alts, rhs_locals} icl_module ca
		# ((rhs_alts, rhs_locals), ca) = collectFunctions (rhs_alts, rhs_locals) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
266
267
268
269
		= ({rhs_alts = rhs_alts, rhs_locals = rhs_locals}, ca)

instance collectFunctions LocalDefs
where
270
	collectFunctions (LocalParsedDefs locals) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
271
272
		# (fun_defs, node_defs, ca) = reorganiseLocalDefinitions locals ca
		  (node_defs, ca) = collect_functions_in_node_defs node_defs ca
273
		  (fun_defs, ca) = collectFunctions fun_defs icl_module ca
Martin Wierich's avatar
Martin Wierich committed
274
		  (range, ca) = addFunctionsRange fun_defs ca
275
		= (CollectedLocalDefs { loc_functions = range, loc_nodes = node_defs, loc_in_icl_module=icl_module }, ca)
Martin Wierich's avatar
Martin Wierich committed
276
	where
277
		reorganiseLocalDefinitions :: [ParsedDefinition] *CollectAdmin -> ([FunDef],[NodeDef ParsedExpr],*CollectAdmin)
278
279
		reorganiseLocalDefinitions [PD_NodeDef pos pattern {rhs_alts,rhs_locals} : defs] ca
			# (fun_defs, node_defs, ca) = reorganiseLocalDefinitions defs ca
280
			= (fun_defs, [{ nd_dst = pattern, nd_alts = rhs_alts, nd_locals = rhs_locals, nd_position = pos } : node_defs], ca)
281
282
283
284
285
		reorganiseLocalDefinitions [PD_Function pos name is_infix args rhs fun_kind : defs] ca
			# prio = if is_infix (Prio NoAssoc 9) NoPrio
			  fun_arity = length args
			  (bodies, fun_kind, defs, ca) = collectFunctionBodies name fun_arity prio fun_kind defs ca
			  (fun_defs, node_defs, ca) = reorganiseLocalDefinitions defs ca
286
			  fun = MakeNewImpOrDefFunction name fun_arity [{ pb_args = args, pb_rhs = rhs, pb_position = pos } : bodies ] fun_kind prio No pos
287
288
289
			= ([ fun : fun_defs ], node_defs, ca)
		reorganiseLocalDefinitions [PD_TypeSpec pos1 name1 prio type specials : defs] ca
			= case defs of
290
291
292
293
				[PD_Function pos name is_infix args rhs fun_kind : othe] // PK ..
					| fun_kind == FK_Caf 
						# ca = postParseError pos "No typespecification for local graph definitions allowed" ca // .. PK
						-> reorganiseLocalDefinitions (tl defs) ca
294
295
296
297
					| belongsToTypeSpec name1 prio name is_infix
						# fun_arity = determineArity args type
						# (bodies, fun_kind, defs, ca) = collectFunctionBodies name1 fun_arity prio fun_kind defs ca
						  (fun_defs, node_defs, ca) = reorganiseLocalDefinitions defs ca
298
						  fun = MakeNewImpOrDefFunction name fun_arity bodies fun_kind prio type pos1
299
300
						-> ([fun : fun_defs], node_defs, ca)
						-> reorganiseLocalDefinitions defs (postParseError pos "function body expected" ca)
301
302
				[PD_NodeDef pos pattern=:(PE_Ident id) rhs : defs]
					| not (belongsToTypeSpec name1 prio id False)
303
						-> reorganiseLocalDefinitions defs (postParseError pos "function body expected" ca)
304
305
306
					| arity type<>0
						-> reorganiseLocalDefinitions defs (postParseError pos "this alternative has not enough arguments" ca)
					# (fun_defs, node_defs, ca) = reorganiseLocalDefinitions defs ca
307
					  fun = MakeNewImpOrDefFunction id 0 
308
					  				[{ pb_args = [], pb_rhs = rhs, pb_position = pos }]
309
					  				(FK_Function cNameNotLocationDependent) prio type pos1
310
					-> ([fun : fun_defs], node_defs, ca)
311
312
				_
					-> reorganiseLocalDefinitions defs (postParseError pos1 "function body expected" ca)
313
314
315
		  where
			arity (Yes {st_arity}) = st_arity
			arity No = 2 // it was specified as infix
316
317
318
		reorganiseLocalDefinitions [] ca
			= ([], [], ca)

319
320
		collect_functions_in_node_defs :: [NodeDef ParsedExpr] *CollectAdmin -> ([NodeDef ParsedExpr],*CollectAdmin)
		collect_functions_in_node_defs [ bind : node_defs ] ca
321
			# (bind, ca) = collectFunctions bind icl_module ca
Martin Wierich's avatar
Martin Wierich committed
322
			  (node_defs, ca) = collect_functions_in_node_defs node_defs ca
323
			= ([bind:node_defs], ca)
Martin Wierich's avatar
Martin Wierich committed
324
325
326
		collect_functions_in_node_defs [] ca
			= ([], ca)
// RWS ... +++ remove recollection
327
	collectFunctions locals icl_module ca
Martin Wierich's avatar
Martin Wierich committed
328
329
330
331
332
		=	(locals, ca)
// ... RWS

instance collectFunctions (NodeDef a) | collectFunctions a
where
333
334
	collectFunctions node_def=:{nd_dst,nd_alts,nd_locals} icl_module ca
		# ((nd_dst,(nd_alts,nd_locals)), ca) = collectFunctions (nd_dst,(nd_alts,nd_locals)) icl_module ca
Martin Wierich's avatar
Martin Wierich committed
335
336
337
338
		= ({ node_def & nd_dst = nd_dst, nd_alts = nd_alts, nd_locals = nd_locals }, ca)

instance collectFunctions Ident
where
339
	collectFunctions e icl_module ca
Martin Wierich's avatar
Martin Wierich committed
340
341
		= (e, ca)

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
instance collectFunctions (ParsedInstance a) | collectFunctions a where
	collectFunctions inst=:{pi_members} icl_module ca
		# (pi_members, ca) = collectFunctions pi_members icl_module ca
		= ({inst & pi_members = pi_members }, ca)

instance collectFunctions FunDef where
	collectFunctions fun_def=:{fun_body = ParsedBody bodies} icl_module ca
		# (bodies, ca) = collectFunctions bodies icl_module ca
		= ({fun_def & fun_body = ParsedBody bodies}, ca)

instance collectFunctions ParsedBody where
	collectFunctions pb=:{pb_rhs} icl_module ca
		# (pb_rhs, ca) = collectFunctions pb_rhs icl_module ca
		= ({ pb & pb_rhs = pb_rhs }, ca)

357
NoCollectedLocalDefs :== CollectedLocalDefs { loc_functions = { ir_from = 0, ir_to = 0 }, loc_nodes = [], loc_in_icl_module=True }
Martin Wierich's avatar
Martin Wierich committed
358

359
360
transformLambda :: Ident [ParsedExpr] ParsedExpr Position -> FunDef
transformLambda lam_ident args result pos
361
362
	# lam_rhs = { rhs_alts = UnGuardedExpr { ewl_nodes = [], ewl_expr = result, ewl_locals = NoCollectedLocalDefs, ewl_position = NoPos },
	  					rhs_locals = NoCollectedLocalDefs }
363
	  lam_body = [{pb_args = args, pb_rhs = lam_rhs, pb_position = pos }]
364
	= MakeNewImpOrDefFunction lam_ident (length args) lam_body (FK_Function cNameLocationDependent) NoPrio No pos
Martin Wierich's avatar
Martin Wierich committed
365

366
makeConsExpressionForGenerator :: GeneratorKind ParsedExpr ParsedExpr *CollectAdmin -> (ParsedExpr,*CollectAdmin)
367
368
makeConsExpressionForGenerator gen_kind a1 a2 ca
	#! cons_id = predefined_idents.[case gen_kind of IsListGenerator -> PD_ConsSymbol ; _ -> PD_OverloadedConsSymbol]
369
370
371
	= (PE_List [PE_Ident cons_id, a1, a2], ca)

makeNilExpression :: Int *CollectAdmin -> (ParsedExpr,*CollectAdmin)
372
373
makeNilExpression predef_nil_index ca
	#! nil_id = predefined_idents.[predef_nil_index]
374
	= (PE_Ident nil_id, ca)
Martin Wierich's avatar
Martin Wierich committed
375

376
makeConsExpression :: Int ParsedExpr ParsedExpr *CollectAdmin -> (ParsedExpr,*CollectAdmin)
377
378
makeConsExpression predef_cons_index a1 a2 ca
	#! cons_id = predefined_idents.[predef_cons_index]
Martin Wierich's avatar
Martin Wierich committed
379
380
	= (PE_List [PE_Ident cons_id, a1, a2], ca)

381
382
383
//get_predef_id :: Int -> Ident
get_predef_id predef_index :== predefined_idents.[predef_index]

Martin Wierich's avatar
Martin Wierich committed
384
385
// +++ change to accessor functions
:: TransformedGenerator =
386
387
	{	tg_expr :: ([ParsedDefinition],[ParsedExpr])
	,	tg_lhs_arg :: [ParsedExpr]
Martin Wierich's avatar
Martin Wierich committed
388
389
390
	,	tg_case_end_expr :: ParsedExpr
	,	tg_case_end_pattern :: ParsedExpr
	,	tg_element :: ParsedExpr
391
	,	tg_element_is_uselect :: !Bool
Martin Wierich's avatar
Martin Wierich committed
392
	,	tg_pattern :: ParsedExpr
393
	,	tg_rhs_continuation :: [ParsedExpr]
Martin Wierich's avatar
Martin Wierich committed
394
395
396
397
	,	tg_case1 :: Ident
	,	tg_case2 :: Ident
	}

398
399
:: IndexGenerator :== Optional (ParsedExpr,[([ParsedDefinition],ParsedExpr,ParsedExpr)])

400
401
402
403
is_zero_expression (PE_Basic (BVI "0")) = True
is_zero_expression (PE_Basic (BVInt 0)) = True
is_zero_expression _ = False

404
transformGenerator :: Generator String IndexGenerator *CollectAdmin -> (!TransformedGenerator,!IndexGenerator,!Int,!*CollectAdmin)
405
406
407
408
409
410
411
transformGenerator {gen_kind=IsArrayGenerator, gen_expr, gen_pattern, gen_position} qual_filename index_generator ca
	# (array, ca) = prefixAndPositionToIdentExp "g_a" gen_position ca
	  (n, ca) = prefixAndPositionToIdentExp "g_s" gen_position ca
	  (n2, ca) = prefixAndPositionToIdentExp "g_s2" gen_position ca
	  (a2, ca) = prefixAndPositionToIdentExp "g_a2" gen_position ca
	  (gen_var_case1, ca) = prefixAndPositionToIdent "g_c1" gen_position ca
	  (gen_var_case2, ca) = prefixAndPositionToIdent "g_c2" gen_position ca
412
413
414
415
	  less_or_equal = get_predef_id PD_LessOrEqualFun
	  sub = get_predef_id PD_SubFun
	  usize = get_predef_id PD_UnqArraySizeFun
	  uselect = get_predef_id PD_UnqArraySelectFun
416
417
418
419
	  pattern = PE_Tuple [gen_pattern, array]
	= case index_generator of
		No
			# (i, ca) = prefixAndPositionToIdentExp "g_i" gen_position ca
420
			  inc = get_predef_id PD_IncFun
421
			# dec_n = PE_List [n,PE_Ident sub,PE_Basic (BVInt 1)]
422
423
			# transformed_generator
			  	=	{	tg_expr =	([PD_NodeDef (LinePos qual_filename gen_position.lc_line) (PE_Tuple [n,a2]) (exprToRhs (PE_List [PE_Ident usize, gen_expr]))],
424
				  					[PE_Basic (BVInt 0),dec_n,a2])
425
426
427
428
429
430
431
432
433
434
435
			  		,	tg_lhs_arg = [i, n, array]
			  		,	tg_case_end_expr = PE_List [i,PE_Ident less_or_equal, n]
			  		,	tg_case_end_pattern = PE_Basic (BVB True)
					,	tg_element = PE_List [PE_Ident uselect, array, i]
					,	tg_element_is_uselect=True
					,	tg_pattern = pattern
					,	tg_rhs_continuation = [PE_List [PE_Ident inc, i], n, array]
					,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
					}
			-> (transformed_generator,Yes (i,[([],dec_n,n2)]),2,ca)
		Yes (i,[])
436
			# inc = get_predef_id PD_IncFun
437
			# dec_n = PE_List [n,PE_Ident sub,PE_Basic (BVInt 1)]
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
			# transformed_generator
			  	=	{	tg_expr =	([PD_NodeDef (LinePos qual_filename gen_position.lc_line) (PE_Tuple [n,a2]) (exprToRhs (PE_List [PE_Ident usize, gen_expr]))],
				  					[dec_n,a2])
			  		,	tg_lhs_arg = [n,array]
			  		,	tg_case_end_expr = PE_List [i,PE_Ident less_or_equal, n]
			  		,	tg_case_end_pattern = PE_Basic (BVB True)
					,	tg_element = PE_List [PE_Ident uselect, array, i]
					,	tg_element_is_uselect=True
					,	tg_pattern = pattern
					,	tg_rhs_continuation = [n,array]
					,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
					}
			-> (transformed_generator,Yes (i,[([],dec_n,n2)]),1,ca)
		Yes (i,size_expressions)
			# transformed_generator
			  	=	{	tg_expr = ([],[a2])
					,	tg_lhs_arg = [array]
			  		,	tg_case_end_expr = PE_Empty
			  		,	tg_case_end_pattern = PE_Empty
					,	tg_element = PE_List [PE_Ident uselect, array, i]
					,	tg_element_is_uselect=True
					,	tg_pattern = pattern
					,	tg_rhs_continuation = [array]
					,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
					}
			# size_expression
				=([PD_NodeDef (LinePos qual_filename gen_position.lc_line) (PE_Tuple [n,a2]) (exprToRhs (PE_List [PE_Ident usize, gen_expr]))],
465
				  					(PE_List [n,PE_Ident sub,PE_Basic (BVInt 1)]),n2)
466
467
			-> (transformed_generator,Yes (i,[size_expression:size_expressions]),0,ca)
transformGenerator {gen_kind, gen_expr=PE_Sequ (SQ_FromTo from_exp to_exp), gen_pattern, gen_position} qual_filename index_generator ca
468
469
470
	# (n, ca) = prefixAndPositionToIdentExp "g_s" gen_position ca
	  (gen_var_case1, ca) = prefixAndPositionToIdent "g_c1" gen_position ca
	  (gen_var_case2, ca) = prefixAndPositionToIdent "g_c2" gen_position ca
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
	| is_zero_expression from_exp
		= case index_generator of
			No
				# (i, ca) = prefixAndPositionToIdentExp "g_i" gen_position ca
				# inc = get_predef_id PD_IncFun
				  less_or_equal = get_predef_id PD_LessOrEqualFun
				# transformed_generator
				  	=	{	tg_expr = ([],[from_exp,to_exp])
				  		,	tg_lhs_arg = [i,n]
				  		,	tg_case_end_expr = PE_List [i,PE_Ident less_or_equal,n]
				  		,	tg_case_end_pattern = PE_Basic (BVB True)
						,	tg_element = i
						,	tg_element_is_uselect=False
						,	tg_pattern = gen_pattern
						,	tg_rhs_continuation = [PE_List [PE_Ident inc, i], n]
						,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
						}
				-> (transformed_generator,Yes (i,[([],to_exp,n)]),2,ca)
			Yes (i,[])
				# inc = get_predef_id PD_IncFun
				  less_or_equal = get_predef_id PD_LessOrEqualFun
				# transformed_generator
				  	=	{	tg_expr = ([],[to_exp])
				  		,	tg_lhs_arg = [n]
				  		,	tg_case_end_expr = PE_List [i,PE_Ident less_or_equal,n]
				  		,	tg_case_end_pattern = PE_Basic (BVB True)
						,	tg_element = i
						,	tg_element_is_uselect=False
						,	tg_pattern = gen_pattern
						,	tg_rhs_continuation = [n]
						,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
						}
				-> (transformed_generator,Yes (i,[([],to_exp,n)]),1,ca)
			Yes (i,size_expressions)
				# transformed_generator
				  	=	{	tg_expr = ([],[])
				  		,	tg_lhs_arg = []
				  		,	tg_case_end_expr = PE_Empty
				  		,	tg_case_end_pattern = PE_Empty
						,	tg_element = i
						,	tg_element_is_uselect=False
						,	tg_pattern = gen_pattern
						,	tg_rhs_continuation = []
						,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
						}
				-> (transformed_generator,Yes (i,[([],to_exp,n):size_expressions]),0,ca)
		# (i, ca) = prefixAndPositionToIdentExp "g_i" gen_position ca
		# inc = get_predef_id PD_IncFun
		  less_or_equal = get_predef_id PD_LessOrEqualFun
		# transformed_generator
		  	=	{	tg_expr = ([],[from_exp,to_exp])
		  		,	tg_lhs_arg = [i,n]
		  		,	tg_case_end_expr = PE_List [i,PE_Ident less_or_equal,n]
		  		,	tg_case_end_pattern = PE_Basic (BVB True)
				,	tg_element = i
				,	tg_element_is_uselect=False
				,	tg_pattern = gen_pattern
				,	tg_rhs_continuation = [PE_List [PE_Ident inc, i], n]
				,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
				}
		= (transformed_generator,index_generator,0,ca)
532
transformGenerator {gen_kind, gen_expr=PE_Sequ (SQ_From from_exp), gen_pattern, gen_position} qual_filename index_generator ca
533
534
	# (gen_var_case1, ca) = prefixAndPositionToIdent "g_c1" gen_position ca
	  (gen_var_case2, ca) = prefixAndPositionToIdent "g_c2" gen_position ca
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
	| is_zero_expression from_exp
		= case index_generator of
			No
				# (i, ca) = prefixAndPositionToIdentExp "g_i" gen_position ca
				# inc = get_predef_id PD_IncFun
				# transformed_generator
				  	=	{	tg_expr = ([],[from_exp])
				  		,	tg_lhs_arg = [i]
				  		,	tg_case_end_expr = PE_Empty
				  		,	tg_case_end_pattern = PE_Empty
						,	tg_element = i
						,	tg_element_is_uselect=False
						,	tg_pattern = gen_pattern
						,	tg_rhs_continuation = [PE_List [PE_Ident inc, i]]
						,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
						}
				-> (transformed_generator,Yes (i,[]),0,ca)
			Yes (i,size_expressions)
				# transformed_generator
				  	=	{	tg_expr = ([],[])
				  		,	tg_lhs_arg = []
				  		,	tg_case_end_expr = PE_Empty
				  		,	tg_case_end_pattern = PE_Empty
						,	tg_element = i
						,	tg_element_is_uselect=False
						,	tg_pattern = gen_pattern
						,	tg_rhs_continuation = []
						,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
						}
				-> (transformed_generator,index_generator,0,ca)
		# (i, ca) = prefixAndPositionToIdentExp "g_i" gen_position ca
		# inc = get_predef_id PD_IncFun
		# transformed_generator
		  	=	{	tg_expr = ([],[from_exp])
		  		,	tg_lhs_arg = [i]
		  		,	tg_case_end_expr = PE_Empty
		  		,	tg_case_end_pattern = PE_Empty
				,	tg_element = i
				,	tg_element_is_uselect=False
				,	tg_pattern = gen_pattern
				,	tg_rhs_continuation = [PE_List [PE_Ident inc, i]]
				,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
				}
		= (transformed_generator,index_generator,0,ca)
579
transformGenerator {gen_kind, gen_expr, gen_pattern, gen_position} qual_filename index_generator ca
580
581
582
583
584
	# (list, ca) = prefixAndPositionToIdentExp "g_l" gen_position ca
	  (hd, ca) = prefixAndPositionToIdentExp "g_h" gen_position ca
	  (tl, ca) = prefixAndPositionToIdentExp "g_t" gen_position ca
	  (gen_var_case1, ca) = prefixAndPositionToIdent "g_c1" gen_position ca
	  (gen_var_case2, ca) = prefixAndPositionToIdent "g_c2" gen_position ca
585
	  (cons, ca) = makeConsExpressionForGenerator gen_kind hd tl ca
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
	# transformed_generator
	  	=	{	tg_expr = ([],[gen_expr])
	  		,	tg_lhs_arg = [list]
	  		,	tg_case_end_expr = list
	  		,	tg_case_end_pattern = cons
			,	tg_element = hd
			,	tg_element_is_uselect=False
			,	tg_pattern = gen_pattern
			,	tg_rhs_continuation = [tl]
			,	tg_case1 = gen_var_case1, tg_case2 = gen_var_case2
			}
	= (transformed_generator,index_generator,0,ca)

transformGenerators :: [Generator] String IndexGenerator *CollectAdmin -> (![TransformedGenerator],!IndexGenerator,!*CollectAdmin)
transformGenerators [generator:generators] qual_filename index_generator ca
	# (transformed_generator,index_generator,index_argument_n,ca) = transformGenerator generator qual_filename index_generator ca
	| index_argument_n>0
		# (transformed_generators,index_generator,ca) = transformGenerators generators qual_filename index_generator ca		
		# (node_defs,size_exp,_,ca) = compute_minimum_of_sizes index_generator generator.gen_position ca
		# transformed_generator = store_minimum_of_sizes_in_generator node_defs size_exp index_argument_n transformed_generator
		= ([transformed_generator:transformed_generators],index_generator,ca)
		# (transformed_generators,index_generator,ca) = transformGenerators generators qual_filename index_generator ca
		= ([transformed_generator:transformed_generators],index_generator,ca)
transformGenerators [] qual_filename index_generator ca
	= ([],index_generator,ca)

transformGeneratorsAndReturnSize :: [Generator] String IndexGenerator ParsedExpr *CollectAdmin -> (![TransformedGenerator],!IndexGenerator,!ParsedExpr,!*CollectAdmin)
transformGeneratorsAndReturnSize [generator:generators] qual_filename index_generator size_exp ca
	# (transformed_generator,index_generator,index_argument_n,ca) = transformGenerator generator qual_filename index_generator ca
	| index_argument_n>0
		# (transformed_generators,index_generator,_,ca) = transformGeneratorsAndReturnSize generators qual_filename index_generator size_exp ca		

		# (node_defs,size_exp,ident,ca) = compute_minimum_of_sizes index_generator generator.gen_position ca
		# (node_defs,size_exp) = case size_exp of
									PE_Ident _
										-> (node_defs,size_exp)
									_
										-> (node_defs++[PD_NodeDef NoPos ident (exprToRhs size_exp)],ident)
		# transformed_generator = store_minimum_of_sizes_in_generator node_defs size_exp index_argument_n transformed_generator

626
		# inc = get_predef_id PD_IncFun
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
		# size_exp = PE_List [PE_Ident inc,size_exp]
		
		= ([transformed_generator:transformed_generators],index_generator,size_exp,ca)

		# (transformed_generators,index_generator,size_exp,ca) = transformGeneratorsAndReturnSize generators qual_filename index_generator size_exp ca
		= ([transformed_generator:transformed_generators],index_generator,size_exp,ca)
transformGeneratorsAndReturnSize [] qual_filename index_generator size_exp ca
	= ([],index_generator,size_exp,ca)

compute_minimum_of_sizes :: IndexGenerator LineAndColumn *CollectAdmin -> *(![ParsedDefinition],!ParsedExpr,!ParsedExpr,!*CollectAdmin);
compute_minimum_of_sizes (Yes (i,sizes)) gen_position ca
	= compute_minimum (reverse sizes) 1 ca
where
	compute_minimum [(node_defs,exp,ident)] n ca
		= (node_defs,exp,ident,ca)
	compute_minimum [(node_defs1,exp1,ident1):sizes] n ca
		# (node_defs2,exp2,ident2,ca) = compute_minimum sizes (n+1) ca
		# node_defs=node_defs1++node_defs2
		# (to_exp,ca)=minimum exp1 exp2 ca
			with
				minimum ident1=:(PE_Ident _) ident2=:(PE_Ident _) ca
					= minimum_of_idents ident1 ident2 ca
				minimum exp1 ident2=:(PE_Ident _) ca
					# node_def1 = PD_NodeDef NoPos ident1 (exprToRhs exp1)
					# (min_exp,ca) = minimum_of_idents ident1 ident2 ca
					= (PE_Let cIsNotStrict (LocalParsedDefs [node_def1]) min_exp,ca)
				minimum ident1=:(PE_Ident _) exp2 ca
					# node_def2 = PD_NodeDef NoPos ident2 (exprToRhs exp2)
					# (min_exp,ca) = minimum_of_idents ident1 ident2 ca
					= (PE_Let cIsNotStrict (LocalParsedDefs [node_def2]) min_exp,ca)
				minimum exp1 exp2 ca
					# node_def1 = PD_NodeDef NoPos ident1 (exprToRhs exp1)
					# node_def2 = PD_NodeDef NoPos ident2 (exprToRhs exp2)
					# (min_exp,ca) = minimum_of_idents ident1 ident2 ca
					= (PE_Let cIsNotStrict (LocalParsedDefs [node_def1,node_def2]) min_exp,ca)
				
				minimum_of_idents ident1 ident2 ca
664
					# smaller_fun = get_predef_id PD_SmallerFun
665
666
667
668
669
670
671
672
673
674
675
676
677
					# (case_ident,ca) = prefixAndPositionToIdent ("g_s"+++toString n) gen_position ca
					= (PE_Case case_ident (PE_List [ident1,PE_Ident smaller_fun,ident2])
						[{calt_pattern = PE_Basic (BVB True), calt_rhs = exprToRhs ident1},
						 {calt_pattern = PE_WildCard, calt_rhs = exprToRhs ident2}],ca)
		= (node_defs,to_exp,ident1,ca)

store_minimum_of_sizes_in_generator :: [ParsedDefinition] ParsedExpr Int TransformedGenerator -> TransformedGenerator;
store_minimum_of_sizes_in_generator node_defs size_exp index_argument_n generator=:{tg_expr=(exp_node_defs,exps)}
	# exps=replace_exp_n index_argument_n exps
		with
			replace_exp_n 1 [e:l] = [size_exp:l]
			replace_exp_n n [e:l] = [e: replace_exp_n (n-1) l]
	= {generator & tg_expr=(node_defs++exp_node_defs,exps)}
Martin Wierich's avatar
Martin Wierich committed
678
679
680
681
682
683
684
685
686
687

::	TransformedQualifier =
	{	tq_generators :: [TransformedGenerator]
	,	tq_call :: ParsedExpr
	,	tq_lhs_args :: [ParsedExpr]
	,	tq_filter :: Optional ParsedExpr
	,	tq_continue :: ParsedExpr
	,	tq_success :: ParsedExpr
	,	tq_end :: ParsedExpr
	,	tq_fun_id :: Ident
688
	,	tq_fun_pos :: !Position
Martin Wierich's avatar
Martin Wierich committed
689
690
	}

691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
rhs_continuation_args_from_generators generators
	= [arg \\ generator<-generators, arg<-generator.tg_rhs_continuation]

expr_args_from_generators generators
	= [arg \\ {tg_expr=(_,args)}<-generators, arg<-args]

lhs_args_from_generators generators
	= [arg \\ generator<-generators, arg<-generator.tg_lhs_arg]

add_node_defs_to_exp [] exp
	= exp
add_node_defs_to_exp [{tg_expr=([],_)}:generators] exp
	= add_node_defs_to_exp generators exp
add_node_defs_to_exp [{tg_expr=(node_defs,_)}:generators] exp
	= PE_Let cIsNotStrict (LocalParsedDefs node_defs) (add_node_defs_to_exp generators exp)

Martin Wierich's avatar
Martin Wierich committed
707
transformQualifier :: Qualifier *CollectAdmin -> (TransformedQualifier, *CollectAdmin) 
Martin Wierich's avatar
Martin Wierich committed
708
transformQualifier {qual_generators, qual_filter, qual_position, qual_filename} ca
709
710
	# (transformedGenerators,index_generator,ca) = transformGenerators qual_generators qual_filename No ca
	# (qual_fun_id, ca) = prefixAndPositionToIdent "c" qual_position ca
Martin Wierich's avatar
Martin Wierich committed
711
	=	({	tq_generators = transformedGenerators
712
713
		,	tq_call = add_node_defs_to_exp transformedGenerators (PE_List [PE_Ident qual_fun_id : expr_args_from_generators transformedGenerators])
		,	tq_lhs_args = lhs_args_from_generators transformedGenerators
Martin Wierich's avatar
Martin Wierich committed
714
		,	tq_filter = qual_filter
715
		,	tq_continue = PE_List [PE_Ident qual_fun_id : rhs_continuation_args_from_generators transformedGenerators]
Martin Wierich's avatar
Martin Wierich committed
716
717
718
		,	tq_success = PE_Empty
		,	tq_end = PE_Empty
		,	tq_fun_id = qual_fun_id
719
		,	tq_fun_pos = LinePos qual_filename qual_position.lc_line
Martin Wierich's avatar
Martin Wierich committed
720
721
722
		}, ca)

// =array&callArray are misnomers (can also be records)
723
transformUpdateQualifier :: [ParsedExpr] [ParsedExpr] Qualifier *CollectAdmin -> (TransformedQualifier, *CollectAdmin) 
Martin Wierich's avatar
Martin Wierich committed
724
transformUpdateQualifier array callArray {qual_generators, qual_filter, qual_position, qual_filename} ca
725
726
727
728
729
	# (transformedGenerators,index_generator,ca) = transformGenerators qual_generators qual_filename No ca
	= CreateTransformedQualifierFromTransformedGenerators transformedGenerators array callArray qual_filter qual_position qual_filename ca
	
CreateTransformedQualifierFromTransformedGenerators transformedGenerators array callArray qual_filter qual_position qual_filename ca
	# (qual_fun_id, ca) = prefixAndPositionToIdent "cu" qual_position ca
Martin Wierich's avatar
Martin Wierich committed
730
	=	({	tq_generators = transformedGenerators
731
732
		,	tq_call = add_node_defs_to_exp transformedGenerators (PE_List [PE_Ident qual_fun_id : callArray ++ expr_args_from_generators transformedGenerators])
		,	tq_lhs_args = array ++ lhs_args_from_generators transformedGenerators
Martin Wierich's avatar
Martin Wierich committed
733
		,	tq_filter = qual_filter
734
		,	tq_continue = PE_List [PE_Ident qual_fun_id : array ++ rhs_continuation_args_from_generators transformedGenerators]
Martin Wierich's avatar
Martin Wierich committed
735
736
737
		,	tq_success = PE_Empty
		,	tq_end = PE_Empty
		,	tq_fun_id = qual_fun_id
738
		,	tq_fun_pos = LinePos qual_filename qual_position.lc_line
Martin Wierich's avatar
Martin Wierich committed
739
740
		}, ca)

741
742
transformListComprehension :: Int Int ParsedExpr [Qualifier] *CollectAdmin -> (ParsedExpr, *CollectAdmin)
transformListComprehension predef_cons_index predef_nil_index expr qualifiers ca
743
	# (transformed_qualifiers, ca) = mapSt transformQualifier qualifiers ca
744
745
	  (success, ca) = makeConsExpression predef_cons_index expr (last transformed_qualifiers).tq_continue ca
	  (nil, ca) = makeNilExpression predef_nil_index ca
Martin Wierich's avatar
Martin Wierich committed
746
747
748
749
	  transformed_qualifiers
	  	=	[	{qual & tq_success = success, tq_end = end}
	  		\\	qual <- transformed_qualifiers
	  		&	success <- [qual.tq_call \\ qual <- tl transformed_qualifiers] ++ [success]
750
751
752
	  		&	end <- [nil : [qual.tq_continue \\ qual <- transformed_qualifiers]]
	  		]
	= makeComprehensions transformed_qualifiers success [] ca
753
754
755

transformArrayComprehension :: ParsedExpr [Qualifier] *CollectAdmin -> (ParsedExpr, *CollectAdmin)
transformArrayComprehension expr qualifiers ca
756
757
758
759
	# [hd_qualifier:_] = qualifiers
	  qual_position = hd_qualifier.qual_position
	  (c_i_ident_exp, ca) = prefixAndPositionToIdentExp "c_i" qual_position ca
	  (c_a_ident_exp, ca) = prefixAndPositionToIdentExp "c_a" qual_position ca
760
	  create_array = get_predef_id PD__CreateArrayFun
761
	| same_index_for_update_and_array_generators qualifiers
762
763
		# index_range = PE_Sequ (SQ_From (PE_Basic (BVInt 0)))
		# index_generator = {gen_kind=IsListGenerator, gen_pattern=c_i_ident_exp, gen_expr=PE_Sequ (SQ_From (PE_Basic (BVInt 0))), gen_position=qual_position}
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
		# update = PE_Update c_a_ident_exp [PS_Array  c_i_ident_exp] expr
		| size_of_generators_can_be_computed_quickly qualifiers
			# {qual_generators,qual_filter,qual_position,qual_filename} = hd_qualifier
			# qual_generators = [index_generator : qual_generators]
			# (transformedGenerators,index_generator,size_exp,ca) = transformGeneratorsAndReturnSize qual_generators qual_filename No PE_Empty ca
			# new_array = PE_List [PE_Ident create_array,size_exp]
			# (transformed_qualifier,ca) = CreateTransformedQualifierFromTransformedGenerators transformedGenerators [c_a_ident_exp] [new_array] qual_filter qual_position qual_filename ca
			= makeUpdateComprehensionFromTransFormedQualifiers [update] [c_a_ident_exp] c_a_ident_exp [transformed_qualifier] ca

			# (length, ca) = computeSize qualifiers qual_position hd_qualifier.qual_filename ca
			# new_array = PE_List [PE_Ident create_array,length]
			  qualifiers = [{hd_qualifier & qual_generators = [index_generator : hd_qualifier.qual_generators] }]
			= transformUpdateComprehension [new_array] [update] [c_a_ident_exp] c_a_ident_exp qualifiers ca

		# (length, ca) = computeSize qualifiers qual_position hd_qualifier.qual_filename ca
		# new_array = PE_List [PE_Ident create_array,length]
780
		# inc = get_predef_id PD_IncFun
781
		  new_array_and_index =	[new_array,PE_Basic (BVInt 0)]
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
		  update = [PE_Update c_a_ident_exp [PS_Array  c_i_ident_exp] expr,PE_List [PE_Ident inc,c_i_ident_exp]]
		= transformUpdateComprehension new_array_and_index update [c_a_ident_exp,c_i_ident_exp] c_a_ident_exp qualifiers ca

All p l :== all l
where
	all [] = True
	all [b : tl] = p b && all tl

pattern_will_always_match (PE_Ident _)
	= True;
pattern_will_always_match (PE_Tuple tuple_args)
	= All pattern_will_always_match tuple_args
pattern_will_always_match _
	= False

patterns_in_generator_will_always_match {gen_pattern}
	= pattern_will_always_match gen_pattern

same_index_for_update_and_array_generators [{qual_generators,qual_filter=No}]
	= All patterns_in_generator_will_always_match qual_generators
same_index_for_update_and_array_generators _
	= False

transformUpdateQualifiers :: [ParsedExpr] [ParsedExpr] [Qualifier] *CollectAdmin -> *(![TransformedQualifier],!*CollectAdmin);
transformUpdateQualifiers identExprs exprs [first_qualifier:rest_qualifiers] ca
	# (transformed_first_qualifier,ca) = transformUpdateQualifier identExprs exprs first_qualifier ca
	  (transformed_rest_qualifiers,ca) = mapSt (transformUpdateQualifier identExprs identExprs) rest_qualifiers ca
	= ([transformed_first_qualifier : transformed_rest_qualifiers],ca)

makeUpdateOrSizeComprehension transformed_qualifiers success identExprs result_expr ca
	# transformed_qualifiers
	  	=	[	{qual & tq_success = success, tq_end = end}
	  		\\	qual <- transformed_qualifiers
	  		&	success <- [qual.tq_call \\ qual <- tl transformed_qualifiers] ++ [success]
	  		&	end <- [result_expr : [qual.tq_continue \\ qual <- transformed_qualifiers]]
Martin Wierich's avatar
Martin Wierich committed
817
	  		]
818
819
820
821
 	= makeComprehensions transformed_qualifiers success identExprs ca

size_of_generator_can_be_computed_quickly {gen_pattern,gen_kind=IsArrayGenerator}
	= pattern_will_always_match gen_pattern
822
size_of_generator_can_be_computed_quickly {gen_pattern,gen_kind=IsListGenerator,gen_expr=PE_Sequ (SQ_FromTo (PE_Basic (BVInt 0)) to_exp)}
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
	= pattern_will_always_match gen_pattern	
size_of_generator_can_be_computed_quickly {gen_pattern,gen_kind=IsListGenerator,gen_expr=PE_Sequ (SQ_From from_exp)}
	= pattern_will_always_match gen_pattern	
size_of_generator_can_be_computed_quickly _
	= False

size_of_generators_can_be_computed_quickly qualifiers=:[qualifier=:{qual_generators,qual_filter=No}]
	= All size_of_generator_can_be_computed_quickly qual_generators && not (All is_from_generator qual_generators)
	where
		is_from_generator {gen_pattern,gen_kind=IsListGenerator,gen_expr=PE_Sequ (SQ_From from_exp)}
			= True
		is_from_generator _
			= False
size_of_generators_can_be_computed_quickly _
	= False

computeSize :: [Qualifier] LineAndColumn FileName *CollectAdmin -> (!ParsedExpr,!*CollectAdmin)
computeSize qualifiers qual_position qual_filename ca
	# (counter_ident_exp, ca) = prefixAndPositionToIdentExp "c_l_i" qual_position ca
842
	  (transformed_qualifiers,ca) = transformUpdateQualifiers [counter_ident_exp] [PE_Basic (BVInt 0)] qualifiers ca
843
	  inc = get_predef_id PD_IncFun
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
	  success = insert_inc_in_inner_loop (last transformed_qualifiers).tq_continue
				with
					insert_inc_in_inner_loop (PE_List [f, a : args])
						= PE_List [f, PE_List  [PE_Ident inc,a] : args]
	= makeUpdateOrSizeComprehension transformed_qualifiers success [counter_ident_exp] counter_ident_exp ca

transformUpdateComprehension :: [ParsedExpr] [ParsedExpr] [ParsedExpr] ParsedExpr [Qualifier] *CollectAdmin -> (!ParsedExpr,!*CollectAdmin)
transformUpdateComprehension exprs updateExprs identExprs result_expr qualifiers ca
	# (transformed_qualifiers,ca) = transformUpdateQualifiers identExprs exprs qualifiers ca
	= makeUpdateComprehensionFromTransFormedQualifiers updateExprs identExprs result_expr transformed_qualifiers ca

makeUpdateComprehensionFromTransFormedQualifiers :: [ParsedExpr] [ParsedExpr] ParsedExpr [TransformedQualifier] *CollectAdmin -> *(!ParsedExpr,!*CollectAdmin);
makeUpdateComprehensionFromTransFormedQualifiers updateExprs identExprs result_expr transformed_qualifiers ca
	# success
	  	// +++ remove hack
	  	=	this_is_definitely_a_hack (last transformed_qualifiers).tq_continue updateExprs
			with
				this_is_definitely_a_hack (PE_List [f : args]) updateExprs
					=	PE_List [f : replace_args updateExprs args]
					with
						replace_args [] args = args
						replace_args [e:l] [a:args] = [e:replace_args l args]
	= makeUpdateOrSizeComprehension transformed_qualifiers success identExprs result_expr ca
Martin Wierich's avatar
Martin Wierich committed
867
868

// +++ rewrite threading
869
makeComprehensions :: [TransformedQualifier] ParsedExpr [ParsedExpr] *CollectAdmin -> (ParsedExpr, *CollectAdmin)
Martin Wierich's avatar
Martin Wierich committed
870
871
makeComprehensions [] success _ ca
	=	(success, ca)
Martin Wierich's avatar
Martin Wierich committed
872
makeComprehensions [{tq_generators, tq_filter, tq_end, tq_call, tq_lhs_args, tq_fun_id, tq_fun_pos} : qualifiers] success threading ca
Martin Wierich's avatar
Martin Wierich committed
873
874
	# (success, ca)
		=	makeComprehensions qualifiers success threading ca
Martin Wierich's avatar
Martin Wierich committed
875
  	=	make_list_comprehension tq_generators tq_lhs_args success tq_end tq_filter tq_call tq_fun_id tq_fun_pos ca
Martin Wierich's avatar
Martin Wierich committed
876
	where
Martin Wierich's avatar
Martin Wierich committed
877
878
879
880
		make_list_comprehension :: [TransformedGenerator] [ParsedExpr] ParsedExpr ParsedExpr
									(Optional ParsedExpr) ParsedExpr Ident Position *CollectAdmin 
								 -> (ParsedExpr, *CollectAdmin)
		make_list_comprehension generators lhsArgs success end optional_filter call_comprehension fun_ident fun_pos ca
Martin Wierich's avatar
Martin Wierich committed
881
			# continue
882
				=	PE_List [PE_Ident fun_ident : threading ++ rhs_continuation_args_from_generators generators]
Martin Wierich's avatar
Martin Wierich committed
883
884
885
			  failure
				=	continue
			  rhs
886
			  	=	build_rhs generators success optional_filter failure end fun_pos
Martin Wierich's avatar
Martin Wierich committed
887
			  parsed_def
Martin Wierich's avatar
Martin Wierich committed
888
			  	=	MakeNewParsedDef fun_ident lhsArgs rhs fun_pos
Martin Wierich's avatar
Martin Wierich committed
889
890
			= (PE_Let cIsStrict (LocalParsedDefs [parsed_def]) call_comprehension, ca)

891
892
		build_rhs :: [TransformedGenerator] ParsedExpr (Optional ParsedExpr) ParsedExpr ParsedExpr Position -> Rhs
		build_rhs [generator : generators] success optional_filter failure end fun_pos
893
			=	case_with_default generator.tg_case1 generator.tg_case_end_expr False generator.tg_case_end_pattern
Martin Wierich's avatar
Martin Wierich committed
894
					(foldr (case_end end)
895
896
897
						(case_with_default generator.tg_case2 generator.tg_element generator.tg_element_is_uselect generator.tg_pattern
							(foldr (case_pattern failure) rhs generators)
							failure)
Martin Wierich's avatar
Martin Wierich committed
898
899
900
901
902
903
904
905
						generators)
					end
			where
				rhs
					=	case optional_filter of
							Yes filter
								->	optGuardedAltToRhs (GuardedAlts [
										{alt_nodes = [], alt_guard = filter, alt_expr = UnGuardedExpr
906
907
908
												{ewl_nodes	= [], ewl_expr	= success, ewl_locals	= LocalParsedDefs [], ewl_position = NoPos },
											alt_ident = { id_name ="_f;" +++ toString line_nr +++ ";", id_info = nilPtr },
											alt_position = NoPos}] No)
Martin Wierich's avatar
Martin Wierich committed
909
910
							No
								->	exprToRhs success
911
				(LinePos _ line_nr) = fun_pos
Martin Wierich's avatar
Martin Wierich committed
912
913
914
915
916
917
918
919
920
921
922
923
924

	/* +++ remove code duplication (bug in 2.0 with nested cases)
		case_end :: TransformedGenerator Rhs -> Rhs
		case_end {tg_case1, tg_case_end_expr, tg_case_end_pattern} rhs
			=	single_case tg_case1 tg_case_end_expr tg_case_end_pattern rhs
	
		case_pattern :: TransformedGenerator Rhs -> Rhs
		case_pattern {tg_case2, tg_element, tg_pattern} rhs
			=	single_case tg_case2 tg_element tg_pattern rhs
	*/

		case_end :: ParsedExpr TransformedGenerator Rhs -> Rhs
		case_end end {tg_case1, tg_case_end_expr, tg_case_end_pattern} rhs
925
			=	case_with_default tg_case1 tg_case_end_expr False tg_case_end_pattern rhs end
Martin Wierich's avatar
Martin Wierich committed
926
927
	
		case_pattern :: ParsedExpr TransformedGenerator Rhs -> Rhs
928
929
930
		case_pattern failure {tg_case2, tg_element,tg_element_is_uselect, tg_pattern} rhs
			=	case_with_default tg_case2 tg_element tg_element_is_uselect tg_pattern rhs failure

Martin Wierich's avatar
Martin Wierich committed
931
932
933
934
935
		single_case :: Ident ParsedExpr ParsedExpr Rhs -> Rhs
		single_case case_ident expr pattern rhs
			=	exprToRhs (PE_Case case_ident expr
					[	{calt_pattern = pattern, calt_rhs = rhs}
					])
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960

		case_with_default :: Ident ParsedExpr Bool ParsedExpr Rhs ParsedExpr -> Rhs
		case_with_default case_ident expr expr_is_uselect pattern=:(PE_Ident ident) rhs=:{rhs_alts=UnGuardedExpr ung_exp=:{ewl_nodes,ewl_expr,ewl_locals=LocalParsedDefs [],ewl_position},rhs_locals=LocalParsedDefs []} default_rhs
			# new_node={ndwl_strict=False,ndwl_def={bind_src=expr,bind_dst=pattern},ndwl_locals=LocalParsedDefs [],ndwl_position=ewl_position}
			= {rhs & rhs_alts=UnGuardedExpr {ung_exp & ewl_nodes=[new_node:ewl_nodes]}}
/* Clean 2.0 generates incorrect code for:
		case_with_default case_ident expr expr_is_uselect=:True pattern=:(PE_Tuple [PE_Ident ident1,ident2_exp=:PE_Ident ident2]) rhs=:{rhs_alts=UnGuardedExpr ung_exp=:{ewl_nodes,ewl_expr,ewl_locals=LocalParsedDefs [],ewl_position},rhs_locals=LocalParsedDefs []} default_rhs
			# new_node1={ndwl_strict=False,ndwl_def={bind_src=expr,bind_dst=pattern},ndwl_locals=LocalParsedDefs [],ndwl_position=ewl_position}
			# new_node2={ndwl_strict=True,ndwl_def={bind_src=ident2_exp,bind_dst=ident2_exp},ndwl_locals=LocalParsedDefs [],ndwl_position=ewl_position}
			= {rhs & rhs_alts=UnGuardedExpr {ung_exp & ewl_nodes=[new_node1,new_node2:ewl_nodes]}}
		we therefore use:
*/
		case_with_default case_ident expr expr_is_uselect pattern=:(PE_Tuple [PE_Ident ident1,ident2_exp=:PE_Ident ident2]) rhs=:{rhs_alts=UnGuardedExpr ung_exp=:{ewl_nodes,ewl_expr,ewl_locals=LocalParsedDefs [],ewl_position},rhs_locals=LocalParsedDefs []} default_rhs
			| expr_is_uselect
				# new_node1={ndwl_strict=False,ndwl_def={bind_src=expr,bind_dst=pattern},ndwl_locals=LocalParsedDefs [],ndwl_position=ewl_position}
				# new_node2={ndwl_strict=True,ndwl_def={bind_src=ident2_exp,bind_dst=ident2_exp},ndwl_locals=LocalParsedDefs [],ndwl_position=ewl_position}
				= {rhs & rhs_alts=UnGuardedExpr {ung_exp & ewl_nodes=[new_node1,new_node2:ewl_nodes]}}
				= exprToRhs (PE_Case case_ident expr
					[	{calt_pattern = pattern, calt_rhs = rhs}
					,	{calt_pattern = PE_WildCard, calt_rhs = exprToRhs default_rhs}
					])
/**/
		case_with_default case_ident expr expr_is_uselect PE_Empty rhs default_rhs
			= rhs
		case_with_default case_ident expr expr_is_uselect pattern rhs default_rhs
Martin Wierich's avatar
Martin Wierich committed
961
962
963
964
965
			=	exprToRhs (PE_Case case_ident expr
					[	{calt_pattern = pattern, calt_rhs = rhs}
					,	{calt_pattern = PE_WildCard, calt_rhs = exprToRhs default_rhs}
					])

966
transformSequence :: Sequence -> ParsedExpr
Martin Wierich's avatar
Martin Wierich committed
967
transformSequence (SQ_FromThen frm then)
968
	=	predef_ident_expr PD_FromThen ` frm ` then
Martin Wierich's avatar
Martin Wierich committed
969
transformSequence (SQ_FromThenTo frm then to)
970
	=	predef_ident_expr PD_FromThenTo ` frm ` then ` to
Martin Wierich's avatar
Martin Wierich committed
971
transformSequence (SQ_From frm)
972
	=	predef_ident_expr PD_From ` frm
Martin Wierich's avatar
Martin Wierich committed
973
transformSequence (SQ_FromTo frm to)
974
	=	predef_ident_expr PD_FromTo ` frm ` to
Martin Wierich's avatar
Martin Wierich committed
975

976
977
978
transformArrayUpdate :: ParsedExpr [Bind ParsedExpr ParsedExpr] -> ParsedExpr
transformArrayUpdate expr updates
	=	foldr (update (predef_ident_expr PD_ArrayUpdateFun)) expr updates
Martin Wierich's avatar
Martin Wierich committed
979
	where
980
981
982
		update :: ParsedExpr (Bind ParsedExpr ParsedExpr) ParsedExpr -> ParsedExpr
		update updateIdent {bind_src=value, bind_dst=index} expr
			=	updateIdent ` expr ` index ` value
Martin Wierich's avatar
Martin Wierich committed
983

984
985
transformArrayDenot :: [ParsedExpr] -> ParsedExpr
transformArrayDenot exprs
Martin Wierich's avatar
Martin Wierich committed
986
	=	transformArrayUpdate
987
988
			(predef_ident_expr PD__CreateArrayFun ` length exprs)
			[{bind_dst=toParsedExpr i, bind_src=expr} \\ expr <- exprs & i <- [0..]]
Martin Wierich's avatar
Martin Wierich committed
989

990
991
992
scanModules :: [ParsedImport] [ScannedModule] [Ident] SearchPaths Bool (ModTimeFunction *Files) *Files *CollectAdmin -> (Bool, [ScannedModule],*Files, *CollectAdmin)
scanModules [] parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
	= (True, parsed_modules,files, ca)
993
scanModules [{import_module,import_symbols,import_file_position} : mods] parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
994
	| in_cache import_module cached_modules
995
		= scanModules mods parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
996
997
998
999
1000
	# (found_module,mod_type) = try_to_find import_module parsed_modules
	| found_module
		= case mod_type of
			MK_NoMainDcl
				# ca = postParseError import_file_position ("main module \'"+++import_module.id_name+++"\' does not have a definition module") ca
1001
				# (_,parsed_modules,files,ca) = scanModules mods parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
1002
1003
				-> (False,parsed_modules,files,ca)
			_
1004
				-> scanModules mods parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
1005
		# (succ, parsed_modules,files, ca)
1006
				= parseAndScanDclModule import_module import_file_position parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
1007
		  (mods_succ, parsed_modules,files, ca)
1008
		  		= scanModules mods parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
1009
		= (succ && mods_succ, parsed_modules,files, ca)
Martin Wierich's avatar
Martin Wierich committed
1010
where
1011
1012
1013
1014
1015
1016
1017
	in_cache mod_id []
		= False
	in_cache mod_id [cached_module_ident : pmods]
		| mod_id==cached_module_ident
			=True
			= in_cache mod_id pmods

1018
 	try_to_find :: Ident [ScannedModule] -> (Bool,ModuleKind)
Martin Wierich's avatar
Martin Wierich committed
1019
	try_to_find mod_id []
1020
		= (False,MK_None)
Martin Wierich's avatar
Martin Wierich committed
1021
1022
	try_to_find mod_id [pmod : pmods]
		| mod_id == pmod.mod_name
1023
			= (True,pmod.mod_type)
Martin Wierich's avatar
Martin Wierich committed
1024
1025
			= try_to_find mod_id pmods

1026
MakeEmptyModule name mod_type
1027
	:== { mod_name = name, mod_modification_time = "", mod_type = mod_type, mod_imports = [], mod_imported_objects = [], mod_defs =
1028
1029
				{	def_types = [], def_constructors = [], def_selectors = [], def_classes = [], def_macro_indices={ir_from=0,ir_to=0},
					def_macros=[],def_members = [], def_funtypes = [], def_instances = [], def_generics = [] } }
Martin Wierich's avatar
Martin Wierich committed
1030

1031
parseAndScanDclModule :: !Ident !Position ![ScannedModule] ![Ident] !SearchPaths !Bool (ModTimeFunction *Files) !*Files !*CollectAdmin
1032
	-> *(!Bool, ![ScannedModule],!*Files, !*CollectAdmin)
1033
parseAndScanDclModule dcl_module import_file_position parsed_modules cached_modules searchPaths support_generics modtimefunction files ca
1034
1035
1036
	# {ca_error, ca_hash_table} = ca
	# (parse_ok, mod, ca_hash_table, err_file, files) = wantModule cWantDclFile dcl_module import_file_position support_generics ca_hash_table ca_error.pea_file searchPaths modtimefunction files
	# ca = {ca & ca_hash_table=ca_hash_table, ca_error={pea_file=err_file,pea_ok=True} }
Martin Wierich's avatar
Martin Wierich committed
1037
	| parse_ok
1038
		= scan_dcl_module mod parsed_modules searchPaths modtimefunction files ca
1039
		= (False, [MakeEmptyModule mod.mod_name MK_None: parsed_modules],files, ca)
Martin Wierich's avatar
Martin Wierich committed
1040
where
1041
	scan_dcl_module :: ParsedModule [ScannedModule] !SearchPaths (ModTimeFunction *Files) *Files *CollectAdmin -> (Bool, [ScannedModule],*Files, *CollectAdmin)
1042
	scan_dcl_module mod=:{mod_defs = pdefs} parsed_modules searchPaths modtimefunction files ca