summaryrefslogtreecommitdiff
blob: a75ef7c6c47a24196a4c4e645cdbef6f355b3be6 (plain)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
module Tonkadur.Compute exposing (compute)

-- Elm -------------------------------------------------------------------------
import Dict
import List

-- Tonkadur --------------------------------------------------------------------
import Tonkadur.Types

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
add_text_effect : (
      Tonkadur.Types.State ->
      String ->
      (List Tonkadur.Types.Computation) ->
      (List Tonkadur.Types.Computation) ->
      Tonkadur.Types.Value
   )
add_text_effect state name parameters content =
   (Tonkadur.Types.TextValue
      (Tonkadur.Types.AugmentedText
         {
            content =
               (List.map
                  (\val ->
                     (Tonkadur.Types.value_to_text_or_string
                        (compute state val)
                     )
                  )
                  content
               ),
            effect_name = name,
            effect_parameters = (List.map (compute state) parameters)
         }
      )
   )

address : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
address state param =
   case (compute state param) of
      (Tonkadur.Types.PointerValue address_v) ->
         (Tonkadur.Types.PointerValue address_v)

      (Tonkadur.Types.StringValue singleton) ->
         (Tonkadur.Types.PointerValue (List.singleton singleton))

      _ -> (Tonkadur.Types.PointerValue [])

unsupported_cast : String -> String -> Tonkadur.Types.Value
unsupported_cast from to =
   (Tonkadur.Types.StringValue
      ("Unsupported cast from " ++ from ++ " to " ++ to ++ ".")
   )

cast : (
      Tonkadur.Types.State ->
      String ->
      String ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
cast state from to param =
   case (compute state param) of
      (Tonkadur.Types.BoolValue bool) ->
         case to of
            "string" ->
               if bool
               then (Tonkadur.Types.StringValue "true")
               else (Tonkadur.Types.StringValue "false")

            "text" ->
               if bool
               then
                  (Tonkadur.Types.TextValue (Tonkadur.Types.StringText "true"))
               else
                  (Tonkadur.Types.TextValue (Tonkadur.Types.StringText "false"))

            "bool" -> (Tonkadur.Types.BoolValue bool)
            _ -> (unsupported_cast from to)

      (Tonkadur.Types.FloatValue float) ->
         case to of
            "string" -> (Tonkadur.Types.StringValue (String.fromFloat float))
            "text" ->
               (Tonkadur.Types.TextValue
                  (Tonkadur.Types.StringText (String.fromFloat float))
               )

            "int" -> (Tonkadur.Types.IntValue (floor float))
            "float" -> (Tonkadur.Types.FloatValue float)
            _ -> (unsupported_cast from to)

      (Tonkadur.Types.IntValue int) ->
         case to of
            "string" -> (Tonkadur.Types.StringValue (String.fromInt int))
            "text" ->
               (Tonkadur.Types.TextValue
                  (Tonkadur.Types.StringText (String.fromInt int))
               )

            "float" -> (Tonkadur.Types.FloatValue (toFloat int))
            "bool" -> (Tonkadur.Types.BoolValue (not (int == 0)))
            "int" -> (Tonkadur.Types.IntValue int)
            _ -> (unsupported_cast from to)

      (Tonkadur.Types.TextValue text_v) ->
         let
            as_string =
               (Tonkadur.Types.value_to_string
                  (Tonkadur.Types.TextValue text_v)
               )
         in
         case to of
            "string" -> (Tonkadur.Types.StringValue as_string)
            "float" ->
               case (String.toFloat as_string) of
                  Nothing -> (unsupported_cast from to)
                  (Just result) -> (Tonkadur.Types.FloatValue result)

            "int" ->
               case (String.toInt as_string) of
                  Nothing -> (unsupported_cast from to)
                  (Just result) -> (Tonkadur.Types.IntValue result)

            "bool" ->
               (Tonkadur.Types.BoolValue
                  ((String.toLower (String.trim as_string)) == "true")
               )

            "text" -> (Tonkadur.Types.TextValue text_v)
            _ -> (unsupported_cast from to)

      (Tonkadur.Types.StringValue string) ->
         case to of
            "string" -> (Tonkadur.Types.StringValue string)
            "float" ->
               case (String.toFloat string) of
                  Nothing -> (unsupported_cast from to)
                  (Just result) -> (Tonkadur.Types.FloatValue result)

            "int" ->
               case (String.toInt string) of
                  Nothing -> (unsupported_cast from to)
                  (Just result) -> (Tonkadur.Types.IntValue result)

            "bool" ->
               (Tonkadur.Types.BoolValue
                  ((String.toLower (String.trim string)) == "true")
               )

            "text" ->
               (Tonkadur.Types.TextValue (Tonkadur.Types.StringText string))

            _ -> (unsupported_cast from to)

      _ -> (unsupported_cast from to)

constant : (
      Tonkadur.Types.State ->
      String ->
      String ->
      Tonkadur.Types.Value
   )
constant state target_type as_string =
   case target_type of
      "string" -> (Tonkadur.Types.StringValue as_string)
      "float" ->
         case (String.toFloat as_string) of
            Nothing -> (unsupported_cast as_string target_type)
            (Just result) -> (Tonkadur.Types.FloatValue result)

      "int" ->
         case (String.toInt as_string) of
            Nothing -> (unsupported_cast as_string target_type)
            (Just result) -> (Tonkadur.Types.IntValue result)

      "text" ->
         (Tonkadur.Types.TextValue (Tonkadur.Types.StringText as_string))

      _ -> (unsupported_cast as_string target_type)

extra_computation : (
      Tonkadur.Types.State ->
      String ->
      (List Tonkadur.Types.Computation) ->
      Tonkadur.Types.Value
   )
extra_computation state name parameters =
   case name of
      _ ->
         (Tonkadur.Types.StringValue
            ("Unsupported extra computation '" ++ name ++ "'")
         )

if_else : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
if_else state condition if_true if_false =
   if (Tonkadur.Types.value_to_bool (compute state condition))
   then (compute state if_true)
   else (compute state if_false)

last_choice_index : Tonkadur.Types.State -> Tonkadur.Types.Value
last_choice_index state = (Tonkadur.Types.IntValue state.last_choice_index)

newline : Tonkadur.Types.State -> Tonkadur.Types.Value
newline state = (Tonkadur.Types.TextValue Tonkadur.Types.NewlineText)

next_allocable_address : Tonkadur.Types.State -> Tonkadur.Types.Value
next_allocable_address state =
   case state.freed_addresses of
      [] ->
         (Tonkadur.Types.PointerValue
            [(".alloc." ++ (String.fromInt state.allocated_data))]
         )

      (available_address :: _) ->
         (Tonkadur.Types.PointerValue [available_address])

operation : (
      Tonkadur.Types.State ->
      String ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
operation state name param0 param1 =
   let
      value0 = (compute state param0)
      value1 = (compute state param1)
   in
   case name of
      "divide" ->
         case value0 of
            (Tonkadur.Types.IntValue val) ->
               (Tonkadur.Types.IntValue
                  (val // (Tonkadur.Types.value_to_int value1))
               )

            _ ->
               (Tonkadur.Types.FloatValue
                  (
                     (Tonkadur.Types.value_to_float value0)
                     / (Tonkadur.Types.value_to_float value1)
                  )
               )

      "minus" ->
         case value0 of
            (Tonkadur.Types.IntValue val) ->
               (Tonkadur.Types.IntValue
                  (val - (Tonkadur.Types.value_to_int value1))
               )

            _ ->
               (Tonkadur.Types.FloatValue
                  (
                     (Tonkadur.Types.value_to_float value0)
                     - (Tonkadur.Types.value_to_float value1)
                  )
               )

      "modulo" ->
         (Tonkadur.Types.IntValue
            (modBy
               (Tonkadur.Types.value_to_int value0)
               (Tonkadur.Types.value_to_int value1)
            )
         )

      "plus" ->
         case value0 of
            (Tonkadur.Types.IntValue val) ->
               (Tonkadur.Types.IntValue
                  (val + (Tonkadur.Types.value_to_int value1))
               )

            _ ->
               (Tonkadur.Types.FloatValue
                  (
                     (Tonkadur.Types.value_to_float value0)
                     + (Tonkadur.Types.value_to_float value1)
                  )
               )

      "power" ->
         case value0 of
            (Tonkadur.Types.IntValue val) ->
               (Tonkadur.Types.IntValue
                  (val ^ (Tonkadur.Types.value_to_int value1))
               )

            _ ->
               (Tonkadur.Types.FloatValue
                  (
                     (Tonkadur.Types.value_to_float value0)
                     ^ (Tonkadur.Types.value_to_float value1)
                  )
               )

      "times" ->
         case value0 of
            (Tonkadur.Types.IntValue val) ->
               (Tonkadur.Types.IntValue
                  (val * (Tonkadur.Types.value_to_int value1))
               )

            _ ->
               (Tonkadur.Types.FloatValue
                  (
                     (Tonkadur.Types.value_to_float value0)
                     * (Tonkadur.Types.value_to_float value1)
                  )
               )

      "and" ->
         (Tonkadur.Types.BoolValue
            (
               (Tonkadur.Types.value_to_bool value0)
               && (Tonkadur.Types.value_to_bool value1)
            )
         )

      "not" ->
         (Tonkadur.Types.BoolValue (not (Tonkadur.Types.value_to_bool value0)))

      "less_than" ->
         case value0 of
            (Tonkadur.Types.BoolValue bool) ->
               (Tonkadur.Types.BoolValue
                  ((Tonkadur.Types.value_to_bool value1) && (not bool))
               )

            (Tonkadur.Types.FloatValue float) ->
               (Tonkadur.Types.BoolValue
                  (float < (Tonkadur.Types.value_to_float value1))
               )

            (Tonkadur.Types.IntValue int) ->
               (Tonkadur.Types.BoolValue
                  (int < (Tonkadur.Types.value_to_int value1))
               )

            (Tonkadur.Types.StringValue str) ->
               (Tonkadur.Types.BoolValue
                  (str < (Tonkadur.Types.value_to_string value1))
               )

            (Tonkadur.Types.PointerValue ptr) ->
               (Tonkadur.Types.BoolValue
                  (
                     (Tonkadur.Types.compare_pointers
                        ptr
                        (Tonkadur.Types.value_to_address value1)
                     )
                     > 0
                  )
               )

            _ -> (Tonkadur.Types.StringValue ("Not a comparable type."))

      "equals" -> (Tonkadur.Types.BoolValue (value0 == value1))

      other ->
         (Tonkadur.Types.StringValue
            ("Unknown operation '" ++ other ++ "'")
         )

relative_address : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
relative_address state base extra =
   (Tonkadur.Types.PointerValue
      (List.append
         (Tonkadur.Types.value_to_address (compute state base))
         (Tonkadur.Types.value_to_address (compute state extra))
      )
   )

size : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
size state computation =
   (Tonkadur.Types.IntValue
      (Dict.size (Tonkadur.Types.value_to_dict (compute state computation)))
   )


text : (
      Tonkadur.Types.State ->
      (List Tonkadur.Types.Computation) ->
      Tonkadur.Types.Value
   )
text state content =
   (List.foldl
      (\addition result ->
         (Tonkadur.Types.TextValue
            (Tonkadur.Types.append_text_content
               (Tonkadur.Types.value_to_text_or_string result)
               (Tonkadur.Types.value_to_text_or_string (compute state addition))
            )
         )
      )
      (Tonkadur.Types.TextValue
         (Tonkadur.Types.AugmentedText (Tonkadur.Types.default_text_data))
      )
      content
   )

value_of : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
value_of state computation =
   (List.foldl
      (\next_step object ->
         case (Dict.get next_step (Tonkadur.Types.value_to_dict object)) of
            (Just value) -> value
            Nothing ->
               (Tonkadur.Types.StringValue
                  "Segmentation Fault (incorrect address)"
               )
      )
      (Tonkadur.Types.StructureValue state.memory)
      (Tonkadur.Types.value_to_address (compute state computation))
   )

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
compute : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
compute state computation =
   case computation of
      (Tonkadur.Types.AddTextEffect effect_name effect_parameters content) ->
         (add_text_effect state effect_name effect_parameters content)

      (Tonkadur.Types.Address param) -> (address state param)
      (Tonkadur.Types.Cast from to value) -> (cast state from to value)
      (Tonkadur.Types.Constant true_type as_string) ->
         (constant state true_type as_string)

      (Tonkadur.Types.ExtraComputation name parameters) ->
         (extra_computation state name parameters)

      (Tonkadur.Types.IfElse condition if_true if_false) ->
         (if_else state condition if_true if_false)

      Tonkadur.Types.LastChoiceIndex -> (last_choice_index state)
      Tonkadur.Types.Newline -> (newline state)
      Tonkadur.Types.NextAllocableAddress -> (next_allocable_address state)
      (Tonkadur.Types.Operation name arg_0 arg_1) ->
         (operation state name arg_0 arg_1)
      (Tonkadur.Types.RelativeAddress base extra) ->
         (relative_address state base extra)

      (Tonkadur.Types.Size value) -> (size state value)
      (Tonkadur.Types.Text content) -> (text state content)
      (Tonkadur.Types.ValueOf address_c) -> (value_of state address_c)