-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cene
More file actions
266 lines (242 loc) · 8.83 KB
/
Copy pathmain.cene
File metadata and controls
266 lines (242 loc) · 8.83 KB
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
\= main.cene (part of Cene Scaffold)
\= Copyright 2016-2017 Rocketnia. Released under the MIT License.
(defn js-client-main client
(let tvary
tvary.\;qq[
The clock struck [cycle twelve|three|six|nine], and the mouse
[proceed ran up the clock|ran down the clock|was not impressed].
]
/run-js-builder client
/then-js-builder (js-box-new/table-empty/nil) /fn tvary-state-box
/then-js-builder (dom-tag str.div) /fn display-div
/then-js-builder
(dom-transform (dom-tag str.div) /list
(domt-child/dom-transform (dom-tag str.p) /list
(domt-child/dom-text str.\;qq[Hello, client!]))
(domt-child/dom-transform (dom-tag str.button) /list
(domt-child/dom-text
str.\;qq[Append another paragraph below.])
(domt-on str.click /fn event
(run-js-builder client
/then-js-builder js-box-get.tvary-state-box /fn tvary-state
/js-builder/fn client
/later-js-effects/fn js-effects-definer
/c (proj1 tvaryrun tvary) tvary-state /fn result
/cast result folding tvary-state result
err.\;qq[Internal error]
/definer-define js-effects-definer (dex-give-up/nil)
/run-js-builder client
/then-js-builder (js-box-put tvary-state-box tvary-state)
/fn -
/then-js-builder
(dom-transform (dom-tag str.p) /list
(domt-child/dom-text result))
/fn paragraph
/dom-append-child display-div paragraph)))
(domt-child/done-js-builder display-div))
/fn dom
/js-builder/fn client
/call-js-effects-1 client dom
js.\;qq;yp[return function ( client, dom ) {
var document = client.getTopLevelVar( "document" );
document.body.appendChild( dom );
return client.done( client.wrap( null ) );
};]))
(defn js-server-main client
/run-give-unwrapped-js-effects client js.\;qq;yp/
return function ( client ) {
/*
var require = process.mainModule.require;
var __filename = process.mainModule.filename;
var express = process.mainModule.require( "express" );
var $path = process.mainModule.require( "path" );
var __dirname = $path.dirname( __filename );
*/
var require = client.getTopLevelVar( "require" );
var __dirname = client.getTopLevelVar( "__dirname" );
var express = require( "express" );
var $path = require( "path" );
var app = express();
app.use( express.static( $path.resolve( __dirname, "static" ) ) );
app.listen( 8080, function () {
console.log( "Hello, port 8080!" );
} );
return client.done( client.wrap( null ) );
};)
(def-macro go home-scope caller-scope my-stx-details args then
(basic-nil-macro home-scope caller-scope my-stx-details then
/fn caller-scope s mac
/c istr.\;qq[build] /fn place
/c istr.\;qq[Hello \;uq;ls`place`.\n] /fn hello-place
/get-mode/fn mode
/join-effects
(output-path-blob-utf-8
(output-path-get cli-output-directory.mode
str.\;qq[hello-build.txt])
hello-place)
/join-effects
(copy-paths mode
(input-path-get
(input-path-get cli-input-directory.mode str.src)
str.raw)
cli-output-directory.mode)
/join-effects
(make-js-program mode home-scope
(output-path-get
(output-path-get cli-output-directory.mode str.\;qq[static])
str.\;qq[client.js])
str.js-client-main
/list str.document)
/join-effects
(make-js-program mode home-scope
(output-path-get cli-output-directory.mode str.\;qq[server.js])
str.js-server-main
/list str.require str.__dirname)
/no-effects/nil))
(go)
\= Text variations ===================================================
\= Inform 7 and Inkle implement a system they call text variations.
\= Each time a piece of code is run, it generates a different string
\= by cycling or proceeding through several options. This requires
\= keeping track of state that's unique to that section of the code,
\= but shared between all invocations of that section of the code.
\= (They allow some randomness too, but we don't deal with that here.)
\=
\= In Cene, we can obtain a name with the right scope of uniqueness by
\= way of a macro call's caller scope's unique namespace. Then we can
\= use that to generate a similarly unique name at run time. To
\= illustrate, we implement an expression `(uniq-name)` that results
\= in a name that's unique up to the lexical location the expression
\= appears in. (See chops.cene to follow along with these
\= implementation notes.)
\=
\= Then we use this name as the key in a table, and we code in the
\= style of a state monad where that table is the state. Using a
\= generic markup language called Chops, we build a `tvary` sugar.
\= This way, we have a verbose and programmable way to create text
\= variations, and we have a concise way reminiscent of Inform 7 and
\= Ink.
\=
\=
\= Inform 7:
\=
\= "The clock struck [one of]twelve[or]three[or]six[or]nine[cycling],
\= and the mouse [one of]ran up the clock[or]ran down the clock[or]was
\= not impressed[stopping]."
\=
\=
\= Ink:
\=
\= The clock struck {&twelve|three|six|nine}, and the mouse
\= {ran up the clock|ran down the clock|was not impressed}.
\=
\=
\= Cene:
\=
\= tvary.\;qq[
\= The clock struck [cycle twelve|three|six|nine], and the mouse
\= [proceed ran up the clock|ran down the clock|was not impressed].]
\=
\=
\= First six consecutive outputs:
\=
\= The clock struck twelve, and the mouse ran up the clock.
\= The clock struck three, and the mouse ran down the clock.
\= The clock struck six, and the mouse was not impressed.
\= The clock struck nine, and the mouse was not impressed.
\= The clock struck twelve, and the mouse was not impressed.
\= The clock struck three, and the mouse was not impressed.
\=
\=
\= This sample is executed whenever this project is built, and the
\= sample outputs are written to dist/tvary-demo.txt.
\= TODO: See if we should factor this section out into a separate
\= codebase.
(defn tvary-demo-later then
(let tvary
\= NOTE: The `tvary` macro and the `cycle` and `proceed` Chops
\= macros are defined in chops.cene.
tvary.\;qq[
The clock struck [cycle twelve|three|six|nine], and the mouse
[proceed ran up the clock|ran down the clock|was not impressed].
]
\= These are some alternate, more verbose versions.
\;rm[
tvary.\;qq[
The clock struck
\;uq;ls[tvary-cycling (uniq-name) /list
tvary.\;qq[twelve]
tvary.\;qq[three]
tvary.\;qq[six]
tvary.\;qq[nine]
],
and the mouse
\;uq;ls[tvary-proceeding (uniq-name) /list
tvary.\;qq[ran up the clock]
tvary.\;qq[ran down the clock]
tvary.\;qq[was not impressed]
].]
]
\;rm
(pass-to (tvary-then) /qdo/^/qq/-/tvary-later-done
istr.\;qq[
The clock struck
\;uq;ls[^/uq/-/tvary-cycling (uniq-name) /list
(tvary-done str.\;qq[twelve])
(tvary-done str.\;qq[three])
(tvary-done str.\;qq[six])
(tvary-done str.\;qq[nine])
],
and the mouse
\;uq;ls[^/uq/-/tvary-proceeding (uniq-name) /list
(tvary-done str.\;qq[ran up the clock])
(tvary-done str.\;qq[ran down the clock])
(tvary-done str.\;qq[was not impressed])
].])
\;rm
(tvary-then
(tvary-cycling (uniq-name) /list
(tvary-done str.\;qq[twelve])
(tvary-done str.\;qq[three])
(tvary-done str.\;qq[six])
(tvary-done str.\;qq[nine]))
/fn hour
/tvary-then
(tvary-proceeding (uniq-name) /list
(tvary-done str.\;qq[ran up the clock])
(tvary-done str.\;qq[ran down the clock])
(tvary-done str.\;qq[was not impressed]))
/fn action
/tvary-later-done
istr.\;qq[
The clock struck \;uq;ls`hour`, and the mouse
\;uq;ls`action`.])
/foldl-later (pair (nil) (table-empty/nil))
(list (nil) (nil) (nil) (nil) (nil) (nil))
(fn loop-state - then
(cast loop-state pair rev-results tvary-state
err.\;qq[Internal error]
/c (proj1 tvaryrun tvary) tvary-state /fn result
/cast result folding tvary-state result
err.\;qq[Internal error]
/c then /pair (cons result rev-results) tvary-state))
/fn loop-state
/cast loop-state pair rev-results tvary-state
err.\;qq[Internal error]
/foldl-later str.\;qq[] rev-results
(fn rest entry then
(c istr.\;qq[\;uq;ls`entry`\n \;uq;ls`rest`] then))
/fn results
/c then results))
(def-macro tvary-go home-scope caller-scope my-stx-details args then
(basic-nil-macro home-scope caller-scope my-stx-details then
/fn caller-scope s mac
/tvary-demo-later/fn demo-results
/get-mode/fn mode
/join-effects
(output-path-blob-utf-8
(output-path-get cli-output-directory.mode
str.\;qq[tvary-demo.txt])
demo-results)
/no-effects/nil))
(tvary-go)