-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ls
More file actions
226 lines (204 loc) · 4.43 KB
/
index.ls
File metadata and controls
226 lines (204 loc) · 4.43 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
if location.hostname is \localhost
delete window.livescript
window.eval await (await fetch \./livescript.js)text!
if localStorage.livescript2
opts = JSON.parse localStorage.livescript2
else
opts =
code: ""
compile:
bare: no
header: yes
const: no
json: no
org: no
parse: \ast
saveOpts = !->
localStorage.livescript2 = JSON.stringify opts
onchangeOpts = (event) !->
{target} = event
opts[target.name] = target.checked
saveOpts!
compile!
onchangeOptsCompile = (event) !->
{target} = event
opts.compile[target.name] = target.checked
saveOpts!
compile!
onchangeOptsParse = (event) !->
{target} = event
if target.checked
opts.parse = target.id
saveOpts!
compile!
editor = CodeMirror.fromTextArea editorEl,
mode: \livescript
keyMap: \sublime
theme: \monokai
tabSize: 3
indentUnit: 3
indentWithTabs: no
lineWrapping: yes
lineNumbers: yes
showCursorWhenSelecting: yes
flattenSpans: no
autoCloseBrackets: yes
styleActiveLine: yes
extraKeys:
"Tab": !~>
if editor.somethingSelected!
editor.execCommand \indentMore
else
editor.execCommand \insertSoftTab
"Shift-Tab": !~>
editor.execCommand \indentLess
editor.setValue opts.code
addEventListener \keydown (event) !->
switch event.code
| \F1
event.preventDefault!
method = opts.org and livescriptOrg or livescript
try
method.run opts.code, opts.compile
catch
console.error e
editor.on \change (cm, change) !~>
opts.code = cm.getValue!
saveOpts!
compile!
result = CodeMirror.fromTextArea resultEl,
mode: \javascript
theme: \monokai
tabSize: 3
lineWrapping: yes
lineNumbers: yes
readOnly: yes
undoDepth: 1
# result2 = CodeMirror.fromTextArea result2El,
# mode: \javascript
# theme: \monokai
# tabSize: 2
# lineWrapping: yes
# lineNumbers: yes
# readOnly: yes
# undoDepth: 1
# parser = CodeMirror.fromTextArea parserEl,
# mode: \application/json
# theme: \monokai
# tabSize: 2
# lineWrapping: yes
# lineNumbers: yes
# readOnly: yes
# undoDepth: 1
bare.checked = opts.compile.bare
bare.onchange = onchangeOptsCompile
header.checked = opts.compile.header
header.onchange = onchangeOptsCompile
const2.checked = opts.compile.const
const2.onchange = onchangeOptsCompile
json.checked = opts.compile.json
json.onchange = onchangeOptsCompile
org.checked = opts.org
org.onchange = onchangeOpts
# lex.checked = opts.parse is \lex
# lex.onchange = onchangeOptsParse
# tokens.checked = opts.parse is \tokens
# tokens.onchange = onchangeOptsParse
# ast.checked = opts.parse is \ast
# ast.onchange = onchangeOptsParse
compile2 = (tokens, opts) ->
code = ""
ifc = 0
ifb = 0
scopes = [
vrs: {}
]
scopei = 0
scope = scopes.0
for token in tokens
[id, va, row, col] = token
{eol, spaced, then: thenn} = token
switch id
| \IF
code += \if(
ifc++
| \INDENT
if ifc
ifc--
ifb++
code += \){
| \DEDENT
if ifb
ifb--
code += \}
| \ID
code += va
| \+-
code += va
| \ASSIGN
{logic} = va
op = va + ""
if op in <[= += -= *= /= %= %%= ^= **=]>
code += op
| \CREMENT
code += va
| \STRNUM
code += va
| \NEWLINE
code += va
code
do compile = !->
method = opts.org and livescriptOrg or livescript
try
code = method.compile opts.code, {...opts.compile}
catch
code = e + ""
result.setValue code
# try
# tokens = method[opts.parse] opts.code
# if opts.parse is \ast
# texts = tokens + ""
# else
# texts = []
# for token in tokens
# text = []
# for k, val of token
# str = (val + "")replace /\n/g \\\n
# if k < 2
# if k is \0
# str .= padEnd 8 " "
# else
# if typeof val is \string
# str = "'#str'"
# else if val instanceof String
# str = str.constructor.name.substring(0 3) + "'#str'"
# for k2, val2 of val
# if isNaN k2
# if val2 is yes
# str += " #k2"
# else if typeof val2 is \string
# str += " #k2:'#val2'"
# else
# str += " #k2:#val2"
# str .= padEnd 18 " "
# text.push str
# else if k > 1
# str .= padEnd 2 " "
# text.push str
# else
# if val is yes
# text.push k
# else if typeof val2 is \string
# text.push "#k:'#val'"
# else
# text.push "#k:#val"
# texts.push text * " "
# texts *= \\n
# parser.setValue texts
# catch
# parser.setValue e + ""
# try
# code = compile2 tokens, {...opts.compile}
# catch
# code = e + ""
# result2.setValue code