forked from mozart/mozart2-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunTime.oz
368 lines (321 loc) · 10.2 KB
/
RunTime.oz
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
%%%
%%% Author:
%%% Martin Henz <[email protected]>
%%% Leif Kornstaedt <[email protected]>
%%% Martin Mueller <[email protected]>
%%% Christian Schulte <[email protected]>
%%%
%%% Copyright:
%%% Leif Kornstaedt, 1997
%%%
%%% Last change:
%%% $Date$ by $Author$
%%% $Revision$
%%%
%%% This file is part of Mozart, an implementation of Oz 3:
%%% http://www.mozart-oz.org
%%%
%%% See the file "LICENSE" or
%%% http://www.mozart-oz.org/LICENSE.html
%%% for information on usage and redistribution
%%% of this file, and for a DISCLAIMER OF ALL
%%% WARRANTIES.
%%%
functor
import
Module(manager)
Core(valueNode userVariable)
Combinator('not' 'cond' 'or' 'dis')
Space('choose')
RecordC('^' tellSize)
FD(int dom sum sumC sumCN reified)
export
Literals
Tokens
Procs
ProcValues
MakeVar
require
BootRecord(test testLabel testFeature aritySublist)
at 'x-oz://boot/Record'
BootObject(ooGetLock ',' '@' '<-' ooExch)
at 'x-oz://boot/Object'
BootException('fail' raiseError:RaiseError)
at 'x-oz://boot/Exception'
BootThread(create)
at 'x-oz://boot/Thread'
BootName(newUnique: NewUniqueName)
at 'x-oz://boot/Name'
BootValue(newReadOnly:NewReadOnly bindReadOnly:BindReadOnly
dotAssign dotExchange catAccess catAssign catExchange catAccessOO catAssignOO catExchangeOO) at 'x-oz://boot/Value'
prepare
ProcValues0 = env(%% Operators
'.': Value.'.'
dotAssign: BootValue.dotAssign
dotExchange: BootValue.dotExchange
catAccess: BootValue.catAccess
catAssign: BootValue.catAssign
catExchange: BootValue.catExchange
catAccessOO: BootValue.catAccessOO
catAssignOO: BootValue.catAssignOO
catExchangeOO: BootValue.catExchangeOO
'==': Value.'=='
'=': Value.'='
'\\=': Value.'\\='
'<': Value.'<'
'=<': Value.'=<'
'>=': Value.'>='
'>': Value.'>'
'!!': Value.'!!'
'div': Int.'div'
'mod': Int.'mod'
'/': Float.'/'
'+': Number.'+'
'-': Number.'-'
'*': Number.'*'
'~': Number.'~'
%% Value
'Value.byNeed': Value.byNeed
'Value.byNeedDot': Value.byNeedDot
'Value.byNeedFuture': Value.byNeedFuture
%% Name
'Name.new': Name.new
%% Cell
'Cell.exchange': Exchange
'Cell.new': NewCell
%% List
'List.toTuple': List.toTuple
'List.toRecord': List.toRecord
'List.append': Append
%% Record
'Record.width': Record.width
'Record.test': BootRecord.test
'Record.testLabel': BootRecord.testLabel
'Record.testFeature': BootRecord.testFeature
%% Object
'Object.\'@\'': BootObject.'@'
'Object.\'<-\'': BootObject.'<-'
'Object.exchange': BootObject.ooExch
'Object.\',\'': BootObject.','
'Object.\'class\'': OoExtensions.'class'
%% Thread
%% Exception
'Exception.\'raise\'': Exception.'raise'
'Exception.raiseError': Exception.raiseError
'Exception.\'fail\'': BootException.'fail'
%% Functor
'Functor.new': Functor.new
%% Internal
'ooGetLock': BootObject.ooGetLock
'aritySublist': BootRecord.aritySublist
'Thread.create': BootThread.create)
LiteralValues = env('ooDefaultVar': {NewUniqueName 'ooDefaultVar'}
'ooFreeFlag': {NewUniqueName 'ooFreeFlag'}
'ooRequiredArg': {NewUniqueName 'ooRequiredArg'})
TokenValues = env('true': true
'false': false)
%% For loop support
local
NONE={NewName}
fun {MkOptimize} {NewCell NONE} end
fun {MkCount} {NewCell 0} end
%% Note that this new impl. for list accumulators has
%% a pleasant effect on both memory and speed. See cvs log
%% msg. for details
%%
%% First cell contains the head of list, Second
%% cell contains current unbound var at tail of list.
fun {MkList} L in {NewCell L} | {NewCell L} end
MkSum=MkCount
fun {MkMultiply} {NewCell 1} end
proc {Maximize C N} Old New in
{Exchange C Old New}
if Old==NONE orelse N>Old then New=N else New=Old end
end
proc {Minimize C N} Old New in
{Exchange C Old New}
if Old==NONE orelse N<Old then New=N else New=Old end
end
proc {Count C B}
if B then Old New in
{Exchange C Old New}
New=Old+1
end
end
proc {Sum C N} Old New in
{Exchange C Old New}
New=Old+N
end
proc {Multiply C N} Old New in
{Exchange C Old New}
New=Old*N
end
%% Bind element to tail, and create new unbound tail var
proc {Collect C X} Tail in
{Exchange C.2 X|Tail Tail}
end
proc {Postpend C X} Tail in
{Exchange C.2 {Append X Tail} Tail}
end
proc {Prepend C X} Head in
{Exchange C.1 Head {Append X Head}}
end
fun {RetIntDefault C D} V in
{Exchange C V unit}
if V==NONE then D else V end
end
fun {RetInt C} V in
{Exchange C V unit}
if V==NONE
then {RaiseError 'for'(noDefaultValue)} unit
else V end
end
fun {RetList C}
{Exchange C.2 nil unit}
{Access C.1}
end
proc {RetYield C}
{BindReadOnly {Access C} nil}
end
proc {Yield C X}
O
N={NewReadOnly}
in
{Exchange C O N}
{BindReadOnly O X|N}
{WaitNeeded N}
end
proc {MkYield C L}
{NewReadOnly L}
{NewCell L C}
{WaitNeeded L}
end
proc {YieldAppend C L}
case L
of nil then skip
[] H|T then {Yield C H} {YieldAppend C T}
end
end
in
ProcValuesFor =
env(
'For.mkoptimize' : MkOptimize
'For.mkcount' : MkCount
'For.mklist' : MkList
'For.mksum' : MkSum
'For.mkmultiply' : MkMultiply
'For.maximize' : Maximize
'For.minimize' : Minimize
'For.count' : Count
'For.sum' : Sum
'For.multiply' : Multiply
'For.collect' : Collect
'For.append' : Postpend
'For.prepend' : Prepend
'For.retintdefault' : RetIntDefault
'For.retint' : RetInt
'For.retlist' : RetList
'For.retyield' : RetYield
'For.yield' : Yield
'For.yieldAppend' : YieldAppend
'For.mkyield' : MkYield
)
end
ProcValuesAll = {Adjoin ProcValues0 ProcValuesFor}
define
fun {ApplyFunctor FileName F}
ModMan = {New Module.manager init()}
in
{ModMan apply(url: FileName F $)}
end
Literals = LiteralValues
Tokens = {Record.mapInd TokenValues
fun {$ X Value}
{New Core.valueNode init(Value unit)}
end}
proc {MakeVar X Value ?V} PrintName in
PrintName = {VirtualString.toAtom '`'#X#'`'}
V = {New Core.userVariable init(PrintName unit)}
{V valToSubst(Value)}
{V setUse(multiple)}
{V reg(~1)}
end
%--** The following built-in procedures should not need wrappers.
%--** Unfortunately, code generation crashes if we omit them.
proc {`RecordC.'^'` A B C}
{RecordC.'^' A B C}
end
proc {RecordCTellSize A B C}
{RecordC.tellSize A B C}
end
proc {CombinatorNot P}
{Combinator.'not' P}
end
proc {CombinatorCond A B}
{Combinator.'cond' A B}
end
proc {CombinatorOr A}
{Combinator.'or' A}
end
proc {CombinatorDis A}
{Combinator.'dis' A}
end
proc {SpaceChoose X Y}
{Space.choose X Y}
end
proc {FDInt A B}
{FD.int A B}
end
proc {FDDom A B}
{FD.dom A B}
end
proc {FDSum X O D}
{FD.sum X O D}
end
proc {FDSumC A X O D}
{FD.sumC A X O D}
end
proc {FDSumCN A X O D}
{FD.sumCN A X O D}
end
fun {FDReifiedInt A B}
{FD.reified.int A B}
end
fun {FDReifiedDom A B}
{FD.reified.dom A B}
end
fun {FDReifiedSum X O D}
{FD.reified.sum X O D}
end
fun {FDReifiedSumC A X O D}
{FD.reified.sumC A X O D}
end
fun {FDReifiedSumCN A X O D}
{FD.reified.sumCN A X O D}
end
ProcValues = {Adjoin ProcValuesAll
env(%% RecordC
'RecordC.\'^\'': `RecordC.'^'`
'RecordC.tellSize': RecordCTellSize
%% Functor
'ApplyFunctor': ApplyFunctor
%% Combinator
'Combinator.\'not\'': CombinatorNot
'Combinator.\'cond\'': CombinatorCond
'Combinator.\'or\'': CombinatorOr
'Combinator.\'dis\'': CombinatorDis
%% Space
'Space.choose': SpaceChoose
%% FD
'FD.int': FDInt
'FD.dom': FDDom
'FD.sum': FDSum
'FD.sumC': FDSumC
'FD.sumCN': FDSumCN
'FD.reified.int': FDReifiedInt
'FD.reified.dom': FDReifiedDom
'FD.reified.sum': FDReifiedSum
'FD.reified.sumC': FDReifiedSumC
'FD.reified.sumCN': FDReifiedSumCN)}
Procs = {Record.mapInd ProcValues MakeVar}
end