/
toy.e
350 lines (278 loc) · 10.3 KB
/
toy.e
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
# Copyright 2006 Kevin Reid, under the terms of the MIT X license
# found at http://www.opensource.org/licenses/mit-license.html ................
pragma.syntax("0.9")
pragma.enable("accumulator")
def <aui> := <import:org.cubik.cle.aui.*>
def makeLamportSlot := <import:org.erights.e.elib.slot.makeLamportSlot>
def Command := <aui:command.Command>
def gatherCommands
def [=> SimpleMessageCommand,
=> makeSimpleMessageCommand,
=> makeCompleteCommand,
=> FlexArgCommand,
=> makeFlexArgCommand,
=> makeArglessMessageCommand,
=> CommandMessageDesc,
=> Zero, => One, => Many,
=> zero, => one, => many,
=> ZOM, => ZO] | _ \
:= def auiCommon := <aui:command.messyCommandDefinitions> \
| <aui:data.zeroOneMany> \
| [=> gatherCommands]
def openInSeparateWindow extends makeSimpleMessageCommand.extract(__identityFunc, "run", 1) implements Command {}
bind gatherCommands(object) :vow[List[Command]] {
# XXX we need general architecture for collecting 1-arg commands and filling them into the list
def baseCommands := [makeCompleteCommand(openInSeparateWindow, [object])]
return when (def allegedType := object <- __getAllegedType()) -> {
accum baseCommands for desc ? (desc.getVerb().indexOf1("()"[0]) == -1) in allegedType.getMessageTypes() {
_.with(makeArglessMessageCommand(object, desc))
}
}
}
def presentDefault := <aui:present.makeDefaultPresenter>(auiCommon)
# ------------------------------------------------------------------------------
# "backend" name borrowed from McCLIM
if (currentVat.getRunnerKind() != "awt") {
interp.waitAtTop(currentVat.morphInto("awt"))
}
def backend := <aui:swing.makeSwingBackend>(<awt>, <swing>, presentDefault, <aui:present.makeDefaultIconPresenter>(), auiCommon, stdout)
def rootsFlex := [].asMap().diverge()
# ------------------------------------------------------------------------------
def parse := e__quasiParser
def borderFactory := <swing:makeBorderFactory>
def awtDropTarget := <awt:dnd.makeDropTarget>
def presentCaplet(capletFile) {
def capletAuthor := parse(capletFile.getTwine()).eval(safeScope)
def capletName := E.toString(capletFile)
return def presenter(document, context) {
def container := JPanel``
var takeContainer := fn { container }
def makeCapletFrame {
to run() {
def ffOuter := takeContainer()
takeContainer := fn { throw("multiple caplet frames not implemented yet") }
ffOuter.setBorder(def tb := borderFactory.createTitledBorder(capletName))
ffOuter."add(Component)"(def userPane := JPanel``)
def capletFrame {
to setTitle(title :String) :void {tb.setTitle(`$capletName - $title`); ffOuter.revalidate()}
#to setState(state :boolean) :void {myJFrame.setState(state)}
#to setResizable(state :boolean) :void {myJFrame.setResizable(state)}
#to setJMenuBar(bar :JMenuBar) :void {myJFrame.setJMenuBar(bar)}
to setDefaultCloseOperation(op :int) :void {stderr.println("XXX need to implement setDefaultCloseOperation")}
to addWindowListener(lis) :void {stderr.println("XXX need to implement addWindowListener")}
to removeWindowListener(lis) :void {stderr.println("XXX need to implement removeWindowListener")}
#to setCursor(cursor :Cursor) :void {myJFrame.setCursor(cursor)}
to getContentPane() :near {
return userPane
}
#match [verb, []] {
# if (validNoArgVerbs.maps(verb)) {
# E.call(myJFrame, verb, [])
# }
#}
}
return capletFrame
}
}
def keys := "INITIAL_DOC_RCVRS TRACELN FRAME_MAKER DROP_TARGET".split(" ").asSet()
def powerbox {
to optCap(key) {
switch (key) {
match =="INITIAL_DOC_RCVRS" {
return [document]
}
match =="TRACELN" {
return def capletTraceln {
match [`run`, pieces] {
def prefix := `$capletFile: `
def sub := stderr.indent(" " * prefix.size())
stderr.print(prefix)
for piece in pieces {
sub.print(piece)
}
stderr.println()
null
}
}
}
match =="FRAME_MAKER" {
return makeCapletFrame
}
match =="DROP_TARGET" { return awtDropTarget }
}
}
to requestFileRcvrsVow(editable :boolean,
title :String,
justification :String,
startPathText :String,
optFilter :pbc) {
# XXX this code is variously Wrong
# also, it ignores startPathText and optFilter
def files
def fileSlot := makeLamportSlot(zero)
def fsel := backend.getRootContext().subPresentType(fileSlot, context.kit().getObjectSelectorPresenter(), false)
def run
def ui := JPanel`
${context.kit().text(`Justification: "$justification"`)}
$fsel >
${JPanel``}.X ${context.kit().button("Run", run)}
`
def context := backend.getRootContext()
def frame := backend.openFrame(`CapDesk - $capletName requests ${editable.pick("editable", "read-only")} file: $title`,
ui,
container)
bind run() {
bind files := [fileSlot.getValue().getValue()]
frame.hide()
}
return files
}
match [`get@{key ? (keys.contains(key))}`, []] { key }
}
capletAuthor(powerbox)
return container
}
}
# ------------------------------------------------------------------------------
# File-browser application example
def openCommand
def presentDirEntry(name) {
return def present(file, context) {
return context.kit().plabel(name, null, fn { makeCompleteCommand(openCommand, [file]) })
}
}
def presentDirectory(dir, context) {
def container := E.call(context.kit(), "y",
accum [] for name => file in dir {
_.with(context.subPresentType(file, presentDirEntry(name), true))
})
return container
}
bind openCommand implements Command {
to "&available"([_]) { return &true }
to run(file) {
return if (file.isDirectory()) {
[file, presentDirectory]
} else {
[file, presentCaplet(<file:/Stuff/e/caplets/capEdit/capEdit.caplet>)]
}
}
}
def exampleFile := <file:/>
rootsFlex["File Browser"] := fn {
[exampleFile, presentDirectory]
}
# ------------------------------------------------------------------------------
# REPL
def seqEval := {
var env := privilegedScope
def seqEval(expr) {
def [v, e] := expr.evalToPair(env)
env := e
return v
}
}
def presentSeqEval(seqEval, context) {
def box := context.kit().y(def filler := JPanel``)
filler.setPreferredSize(<awt:makeDimension>(320,320))
box."add(Component)"(context.kit()._textLineCommandField(def handle(line) {
# XXX needs cleanup.
def expr := e__quasiParser(line)
box."add(Component, int)"(
JPanel`${context.kit().text("? ")} ${context.subPresent(expr, false)} ${JPanel``}.X.Y
${JPanel``}.Y V V
`, box.getComponentCount() - 1)
box."add(Component, int)"(context.subPresent(seqEval <- (expr), true), box.getComponentCount() - 1)
box.revalidate()
}))
return box
}
rootsFlex["Repl"] := fn {
[seqEval, presentSeqEval]
}
# ------------------------------------------------------------------------------
def makeClock(timer, resolution) {
def reduce(t) { return t // resolution * resolution }
def clock {
to getValue() { return reduce(timer.now()) }
to whenUpdated(reactor) {
clock.whenUpdated(reactor, -1)
}
to whenUpdated(reactor, knownGen) {
timer.whenPast(reduce(knownGen) + resolution,
def sendClockUpdate() {
def answer := reduce(timer.now())
reactor <- reactToUpdate(answer, answer, clock)
})
}
}
return clock
}
# XXX needs a time-value type
rootsFlex["Clock"] := fn {
[makeClock(timer, 1000), null]
}
# ------------------------------------------------------------------------------
rootsFlex["Updating FlexSet"] := fn {
def makeUpdatingFlexSet := <aui:data.makeUpdatingFlexSet>
def uf := makeUpdatingFlexSet()
uf.addElement(1)
[[uf, uf], null]
}
# ------------------------------------------------------------------------------
rootsFlex["Things"] := fn {
def exampleButtonCommand := makeArglessMessageCommand(def tk := <awt:makeToolkit>.getDefaultToolkit(), tk.__getAllegedType().getMessageTypes()["beep/0"])
[[
=> exampleButtonCommand,
=> Zero, => One, => Many,
=> zero, => one, => many,
=> ZOM, => ZO,
=> backend,
=> privilegedScope,
=> safeScope,
], null]
}
rootsFlex["Expression"] := fn {
def expr := e`
pragma.enable("easy-return")
pragma.disable("explicit-result-guard")
def object extends 1.0 implements Example {
to makeCounter() {
return { var x := 0; [fn { x += 1 }, &x] }
}
to "exit"() {
try { throw("biff") } catch p { throw(p) } finally { notice('q') }
}
to patterns() {
def [_, a, var b, &c, d] + g := data # , via (e) f
}
to "meta"() {
[meta.getState(),
meta.context()]
}
to plumbing() { def drain match water { flush(water) } }
}
`
[expr, null]
}
# ------------------------------------------------------------------------------
def roots := rootsFlex.snapshot()
def presentNamedRootMaker(name) {
def invokeCommand implements Command {
to "&available"([_]) { return &true }
to run(f) {
return f()
}
}
return def namePresent(object, context) {
return context.kit().plabel(name, null, fn { makeCompleteCommand(invokeCommand, [object]) })
}
}
def presentRoots(map, context) {
def container := E.call(context.kit(), "y",
accum [] for name => maker in map {
_.with(context.subPresentType(maker, presentNamedRootMaker(name), true))
})
return container
}
backend.openFrame("Roots", backend.getRootContext().subPresentType(roots, presentRoots, false), null)