-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.elm
436 lines (388 loc) · 10.7 KB
/
main.elm
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import Html.App as App
import Html exposing (div, Attribute, Html)
import Html.Attributes exposing (style)
import Html.Events exposing (onInput, onClick)
import Array
import String
import Time exposing (Time, second)
type Msg
= InstructionUpdated (Int, String)
| Play
| Debug
| Reset
| Tick Time
type alias Node = {
instructions : List String
, bak : Int
, acc : Int
, left : Int
, right : Int
, activeInstruction : Int
, inputRead: Bool
, outputWritten: Bool
}
type alias Model = {
node : Node
, inputs : List Int
, activeInput : Int
, expectedOutputs : List Int
, actualOutputs : List Int
, autoRun : Bool
}
instruction : String
instruction =
""
computeNode : Node
computeNode =
Node [ instruction
, instruction
, instruction
, instruction
, instruction
, instruction
, instruction
, instruction
, instruction
] 0 0 0 0 0 False False
inputs2 : List Int
inputs2 =
[1, 2, 3, 12, 18, 41, 16, 9, 11, 8, 12, 23]
expectedOutputs2 : List Int
expectedOutputs2 =
[2, 4, 6, 24, 36, 82, 32, 18, 22, 16, 24, 46]
inputs1 : List Int
inputs1 =
[1,2,3,4]
expectedOutputs1 : List Int
expectedOutputs1 =
[1,2,3,4]
init : Model
init =
Model computeNode inputs2 0 expectedOutputs2 [] False
nodeStyle : Attribute msg
nodeStyle =
style [ ("padding", "10px")
, ("margin", "40px")
, ("border", "1px solid white")
, ("align-self", "flex-start")
]
instructionStyle : Int -> Int -> Attribute msg
instructionStyle active current =
let
color = if active == current then
"1px dotted red"
else
"1px solid white"
in
style [ ("width", "310px")
, ("font-size", "15px")
, ("margin", "1px")
, ("margin-left", "7px")
, ("background-color", "black")
, ("border", color)
, ("color", "white")
]
instructionSetStyle : Attribute msg
instructionSetStyle =
style [("padding-top", "10px")]
viewNode : Node -> Html Msg
viewNode node =
Html.div [ nodeStyle
]
[ viewInstructions node
, viewLoc "ACC " node.acc
, viewLoc "BAK " node.bak
, viewLoc "LEFT " node.left
, viewLoc "RIGHT " node.right
]
viewLoc : String -> Int -> Html Msg
viewLoc name location =
Html.div []
[ Html.text name
, Html.text (toString location)
]
viewInstructions : Node -> Html Msg
viewInstructions node =
Html.div [instructionSetStyle]
(List.indexedMap (viewInstruction node.activeInstruction) node.instructions)
viewInstruction : Int -> Int -> String -> Html Msg
viewInstruction active id instruction =
Html.div [] [ Html.text (toString id)
, Html.input [(instructionStyle active id), onInput (\text -> InstructionUpdated (id, text))] []
]
viewList : String -> Int -> List Int -> Html Msg
viewList title active list =
Html.div [style
[ ("padding", "10px")
, ("margin", "40px")
, ("border", "1px solid white")
, ("align-self", "flex-start")
]
]
[ Html.text title
, Html.ul [] (List.indexedMap (viewItem active) list)
]
viewItem : Int -> Int -> Int -> Html Msg
viewItem active id item =
let
color = if id == active then
"green"
else
"white"
in
Html.li [style [("color", color)]] [Html.text (toString item)]
viewButtons : Model -> Html Msg
viewButtons model =
Html.div []
[ buttonViewer Reset Play "RESET" "PLAY" (model.autoRun)
, buttonViewer Debug Reset "STEP" "RESET" (moreToRead model)
]
buttonViewer : Msg -> Msg -> String -> String -> Bool -> Html Msg
buttonViewer yes no yesd nod on =
if on then
Html.div [buttonStyle, onClick yes] [ Html.text yesd]
else
Html.div [buttonStyle, onClick no] [ Html.text nod]
buttonStyle : Attribute msg
buttonStyle =
style
[ ("padding", "10px")
, ("margin", "40px")
, ("border", "1px solid white")
, ("align-self", "flex-start")
]
view : Model -> Html Msg
view model =
Html.div [style
[ ("height", "100%")
, ("padding", "0")
, ("margin", "0")
, ("display", "-webkit-box")
, ("display", "-moz-box")
, ("display", "-ms-flexbox")
, ("display", "-webkit-flex")
, ("display", "flex")
, ("align-items", "center")
, ("justify-content", "center")
, ("background-color", "black")
, ("color", "white")
]
]
[ viewList "INPUTS" model.activeInput model.inputs
, viewNode model.node
, viewList "EXPECTED" -1 model.expectedOutputs
, viewList "ACTUAL" -1 model.actualOutputs
, viewButtons model
]
moreToRead : Model -> Bool
moreToRead model =
model.activeInput < List.length model.inputs
update : Msg -> Model -> Model
update msg model =
case msg of
InstructionUpdated (id, text) ->
let
node = model.node
updatedInstructions = (Array.set id text (Array.fromList node.instructions))
newNode = { node | instructions = (Array.toList updatedInstructions) }
in
{ model | node = newNode }
Play ->
{ model | autoRun = not model.autoRun }
Debug ->
runInstruction model
Reset ->
resetModel model
Tick time ->
if model.autoRun && moreToRead model then
runInstruction model
else
model
resetModel : Model -> Model
resetModel model =
let
node = model.node
resetNode = { node |
bak = 0
, acc = 0
, left = 0
, right = 0
, activeInstruction = 0
, inputRead = False
, outputWritten = False
}
in
{ model |
activeInput = 0
, actualOutputs = []
, node = resetNode
, autoRun = False
}
runInstruction : Model -> Model
runInstruction model =
let
curInput = case (Array.get model.activeInput (Array.fromList model.inputs)) of
Just input ->
input
Nothing ->
10000
node = model.node
preNode = { node |
left = curInput
, inputRead = False
, right = 0
, outputWritten = False
}
newNode = runNode preNode
nextInput = if newNode.inputRead then
model.activeInput + 1
else
model.activeInput
output = if newNode.outputWritten then
model.actualOutputs ++ [newNode.right]
else
model.actualOutputs
in
{ model |
node = newNode
, activeInput = nextInput
, actualOutputs = output
}
loopingNext : List a -> Int -> Int
loopingNext list index =
if (List.length list) - 1 <= index then
0
else
index + 1
runNode : Node -> Node
runNode node =
let
curInstruction = case (Array.get node.activeInstruction (Array.fromList node.instructions)) of
Just instruction ->
instruction
Nothing ->
"skip"
in
applyInstruction curInstruction node
getOutput : List String -> Int -> Node -> Node
getOutput instructions input node =
let
nodeWithInput = { node | left = input }
in
List.foldl applyInstruction nodeWithInput instructions
applyInstruction : String -> Node -> Node
applyInstruction instruction node =
case (tokenize instruction) of
"MOV"::rest ->
mov node rest
"INC"::_ ->
inc node
"SWP"::_ ->
swp node
"ADD"::rest ->
add node rest
_ -> {node | activeInstruction = loopingNext node.instructions node.activeInstruction }
add : Node -> List String -> Node
add node locations =
case locations of
"LEFT"::_ ->
let
newAcc = node.acc + node.left
nextInstruction = loopingNext node.instructions node.activeInstruction
in
{ node | acc = newAcc, left = 0, inputRead = True, activeInstruction = nextInstruction }
"ACC"::_ ->
let
newAcc = node.acc + node.acc
in
{ node | acc = newAcc, left = 0, activeInstruction = node.activeInstruction + 1 }
_ -> node
mov : Node -> List String -> Node
mov node locations =
let
-- TODO this is the granularity we need to refresh at for `step`
x = Debug.log "node" node
y = Debug.log "node" locations
nextInstruction = loopingNext node.instructions node.activeInstruction
in
case locations of
"LEFT"::"RIGHT"::_ ->
{ node |
right = node.left
, inputRead = True
, outputWritten = True
, activeInstruction = nextInstruction
}
"LEFT"::"ACC"::_ ->
let
left = node.left
in
{ node |
acc = left
, inputRead = True
, activeInstruction = nextInstruction
}
"ACC"::"RIGHT"::_ ->
{ node |
right = node.acc
, inputRead = False
, outputWritten = True
, activeInstruction = nextInstruction
}
from::"ACC"::_ ->
case String.toInt(from) of
Ok val ->
{ node |
acc = val
, activeInstruction = nextInstruction
}
Err _ ->
node
from::"RIGHT"::_ ->
case String.toInt(from) of
Ok val ->
{ node |
right = val
, outputWritten = True
, activeInstruction = nextInstruction
}
Err _ ->
node
_ -> node
inc : Node -> Node
inc node =
let
newAcc = node.acc + 1
nextInstruction = loopingNext node.instructions node.activeInstruction
in
{ node |
acc = newAcc
, activeInstruction = nextInstruction
}
swp : Node -> Node
swp node =
let
acc = node.acc
bak = node.bak
nextInstruction = loopingNext node.instructions node.activeInstruction
in
{ node |
acc = bak
, bak = acc
, activeInstruction = nextInstruction
}
tokenize : String -> List String
tokenize string =
String.split " " string
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[Time.every Time.second Tick]
main : Program Never
main =
App.program
{ init = (init, Cmd.none)
, update = \msg model -> (update msg model, Cmd.none)
, view = view
, subscriptions = subscriptions
}