summaryrefslogtreecommitdiff
blob: 92e55274a0cace0da045d96a94a3d1c618e01918 (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
module Tonkadur.Compute exposing (compute)

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

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

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

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

address : (
      Tonkadur.Types.State ->
      Tonkadur.Types.Computation ->
      Tonkadur.Types.Value
   )
address state param =
   case (compute state param) of
      (PointerValue address) -> (PointerValue address)
      (StringValue singleton) -> (PointerValue (List.singleton singleton))
      _ -> (PointerValue (List.empty))

unsupported_cast : String -> String -> Tonkadur.Types.Value
unsupported_cast from to =
   (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
      (BoolValue bool) ->
         case to of
            "string" ->
               if bool
               then (StringValue "true")
               else (StringValue "false")

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

            "bool" -> (BoolValue bool)
            _ -> (unsupported_cast from to)

      (FloatValue float) ->
         case to of
            "string" -> (StringValue (String.fromFloat float))
            "text" -> (TextValue (StringText (String.fromFloat float)))
            "int" -> (IntValue (Math.floor float))
            "float" -> (FloatValue float)
            _ -> (unsupported_cast from to)

      (IntValue int) ->
         case to of
            "string" -> (StringValue (String.fromInt int))
            "text" -> (TextValue (StringText (String.fromInt int)))
            "float" -> (FloatValue (Math.toFloat int))
            "int" -> (IntValue int)
            _ -> (unsupported_cast from to)

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

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

            "text" -> (TextValue text)
            _ -> (unsupported_cast from to)

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

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

            "text" -> (TextValue (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 =
   (cast state "string" target_type as_string)

extra_computation : (
      Tonkadur.Types.State ->
      String ->
      (List.List Tonkadur.Types.Computation) ->
      Tonkadur.Types.Value
   )
extra_computation state name parameters =
   case name of
      _ -> (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 (WyrdType.to_boolean (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 = (IntValue state.last_choice_index)

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

next_allocable_address : Tonkadur.Types.State -> Tonkadur.Types.Value
next_allocable_address state = (IntValue state.next_allocable_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
            (IntValue val) ->
               (IntValue (val // (Tonkadur.Types.value_to_int value1)))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            (PointerValue ptr) ->
               (BoolValue
                  (
                     (Tonkadur.Types.compare_pointers
                        ptr
                        (Tonadur.Wyrd.value_to_dict value1)
                     )
                     > 0
                  )
               )

      "equals" -> (value0 == value1)

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

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


text : (
      Tonkadur.Types.State ->
      (List.List Tonkadur.Types.Computation) ->
      Tonkadur.Types.Value
   )
text state content =
   (List.foldl
      (\addition result ->
         (TextValue
            (Tonkadur.Types.append_text_content
               (Tonkadur.Types.value_to_text result)
               (Tonkadur.Types.value_to_text (compute state addition))
            )
         )
      )
      (TextValue (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
            Nothing -> (StringValue "Segmentation Fault (incorrect address)")
            (Just value) -> value
      )
      (StructureValue state.memory)
      (Tonkadur.Types.value_to_list (compute state computation))
   )

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

      (Address param) -> (address state param)
      (Cast from to value) -> (cast state from to value)
      (Constant true_type as_string) -> (constant state true_type as_string)
      (ExtraComputation name parameters) ->
         (extra_computation state name parameters)

      (IfElse condition if_true if_false) ->
         (if_else state condition if_true if_false)

      LastChoiceIndex -> (last_choice_index state)
      Newline -> (newline state)
      NextAllocableAddress -> (next_allocable_address state)
      (Operation name arg_0 arg_1) -> (operation state name arg_0 arg_1)
      (RelativeAddress base extra) -> (relative_address state base extra)
      (Size value) -> (size state value)
      (Text content) -> (text state content)
      (ValueOf address) -> (value_of state address)