Module: Flok::UserCompilerMacro

Included in:
UserCompilerAction, UserCompilerController
Defined in:
lib/flok/user_compiler.rb

Instance Method Summary collapse

Instance Method Details

#_macro(text) ⇒ Object



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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/flok/user_compiler.rb', line 81

def _macro text
  out = StringIO.new

  text.split("\n").each do |l|
    #EMBED(vc_name, spot_name, context) macro
    if l =~ /Embed/
      l.strip!
      l.gsub!(/Embed\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      vc_name = o.shift.gsub(/"/, "")
      spot_name = o.shift.gsub(/"/, "")
      context = o.shift

      #Get the spot 
      spot_index = @controller.spots.index(spot_name)
      raise "controller #{@controller.name.inspect} attempted to embed #{spot_name.inspect} inside #{@name.inspect}, but #{spot_name.inspect} was not defined in 'spots' (#{@controller.spots.inspect})" unless spot_index

      #Calculate spot index as an offset from the base address using the index of the spot in the spots
      #address offset
      res = ""

      if @debug
        res += %{
        }
      end

      res += %{
        <% if @debug %>
          if (__base__.constructor !== Number) { throw "Embed for the controller: #{@controller.name} was not given a number for it's __base__ pointer, but of type: " + __base__.constructor + "with the value: " + __base__};
        <% end %>

        var ptr = _embed("#{vc_name}", __base__+#{spot_index}+1, #{context}, __base__);
        __info__.embeds[#{spot_index-1}].push(ptr);
      }
      out.puts res
    #Send(event_name, info)
    elsif l =~ /Send/
      l.strip!
      l.gsub!(/Send\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      event_name = o.shift.gsub(/"/, "")
      info = o.shift

      out << %{
       main_q.push([3, "if_event", __base__, "#{event_name}", #{info}])
      }
    #Raise(event_name, info)
    elsif l =~ /Raise/
      l.strip!
      l.gsub!(/Raise\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      event_name = o.shift
      info = o.shift

      out << %{
        int_event(__info__.event_gw, #{event_name}, #{info});
      }
    #Lower(spot_name, event_name, info)
    elsif l =~ /Lower/
      l.strip!
      l.gsub!(/Lower\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      spot_name = o.shift.gsub(/"/, "")
      event_name = o.shift
      info = o.shift

      #Get the spot 
      spot_index = @controller.spots.index(spot_name)
      raise "controller #{@controller.name.inspect} attempted to lower message to #{spot_name.inspect} inside #{@name.inspect}, but #{spot_name.inspect} was not defined in 'spots' (#{@controller.spots.inspect})" unless spot_index

      #Forward an event to the appropriate spot
      out << %{

        var vcs = __info__.embeds[#{spot_index-1}];
        for (var i = 0; i < vcs.length; ++i) {
          int_event(vcs[i], #{event_name}, #{info});
        }
      }
    #GOTO(action_name)
    elsif l =~ /Goto/
      l.strip!
      l.gsub!(/Goto\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      action_name = o.shift.gsub(/"/, "")

      #Switch the actions, reset embeds, and call on_entry
      res = %{
        var old_action = __info__.action;
        __info__.action = "#{action_name}";

        var __free_asap = true;
        //HOOK_ENTRY[controller_will_goto] #{{"controller_name" => @controller.name, "might_respond_to" => @ctx.might_respond_to, "actions_responds_to" => @ctx.actions_respond_to, "from_action" => @name, "to_action" => action_name, "handling_event_named" => @handling_event_named}.to_json}

        //If views are configured to not free right away, set up a new stack of views to free
        //This is usually picked up by the hook GOTO
        if (__free_asap === false) {
          var views_to_free_id = tels(1);
          views_to_free[views_to_free_id] = views_to_free[views_to_free_id] || [];
        }

        //Remove all views, we don't have to recurse because removal of a view
        //is supposed to remove *all* view controllers of that tree as well.
        var embeds = __info__.embeds;
        for (var i = 0; i < __info__.embeds.length; ++i) {
          for (var j = 0; j < __info__.embeds[i].length; ++j) {
            //Free +1 because that will be the 'main' view

            //Free if 'free_asap' is not set, this is usually configured via the 'goto' hook
            if (__free_asap === true) {
              main_q.push([1, "if_free_view", embeds[i][j]+1]);
            } else {
              views_to_free[views_to_free_id].push(embeds[i][j]+1);
            }

            //Call dealloc on the controller
            tel_deref(embeds[i][j]).cte.__dealloc__(embeds[i][j]);

            <% if @debug %>
              var vp = embeds[i][j]+1;
              //First locate spot this view belongs to in reverse hash
              var spot = debug_ui_view_to_spot[vp];

              //Find it's index in the spot
              var idx = debug_ui_spot_to_views[spot].indexOf(vp);

              //Remove it from the spot => [view]
              debug_ui_spot_to_views[spot].splice(idx, 1);

              //Remove it from the reverse hash
              delete debug_ui_view_to_spot[vp];
            <% end %>
          }
        }

        //Prep embeds array, embeds[0] refers to the spot bp+2 (bp is vc, bp+1 is main)
        __info__.embeds = [];
        for (var i = 1; i < #{@controller.spots.count}; ++i) {
          __info__.embeds.push([]);
        }

        //Call on_entry for the new action via the singleton on_entry
        //located in ctable
        __info__.cte.actions[__info__.action].on_entry(__base__)

        //HOOK_ENTRY[controller_did_goto] #{{"controller_name" => @controller.name, "might_respond_to" => @ctx.might_respond_to, "actions_responds_to" => @ctx.actions_respond_to, "from_action" => @name, "to_action" => action_name, "handling_event_named" => @handling_event_named}.to_json}
        //'choose_action' pseudo-action will be sent as 'null' as it's the initial state
        if (old_action === "choose_action") {
          old_action = null;
        }

        //Send off event for action change
        main_q.push([3, "if_event", __base__, "action", {
          from: old_action,
          to: "#{action_name}"
        }]);

      }
      out.puts res
    elsif l =~ /Push/
      l.strip!
      l.gsub!(/Push\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      action_name = o.shift.gsub(/"/, "")

      #Switch the actions, reset embeds, and call on_entry
      res = %{
        //Save state
        var old_action = __info__.action;
        var old_embeds = __info__.embeds;
        __info__.stack.push({action: old_action, embeds: old_embeds});

        __info__.action = "#{action_name}";

        //HOOK_ENTRY[controller_will_push] #{{"controller_name" => @controller.name, "might_respond_to" => @ctx.might_respond_to, "actions_responds_to" => @ctx.actions_respond_to, "from_action" => @name, "to_action" => action_name, "handling_event_named" => @handling_event_named}.to_json}


        //Prep embeds array, embeds[0] refers to the spot bp+2 (bp is vc, bp+1 is main)
        __info__.embeds = [];
        for (var i = 1; i < #{@controller.spots.count}; ++i) {
          __info__.embeds.push([]);
        }

        //Call on_entry for the new action via the singleton on_entry
        //located in ctable
        __info__.cte.actions[__info__.action].on_entry(__base__)

        //HOOK_ENTRY[controller_did_push] #{{"controller_name" => @controller.name, "might_respond_to" => @ctx.might_respond_to, "actions_responds_to" => @ctx.actions_respond_to, "from_action" => @name, "to_action" => action_name, "handling_event_named" => @handling_event_named}.to_json}

        //Send off event for action change
        main_q.push([3, "if_event", __base__, "action", {
          from: old_action,
          to: "#{action_name}"
        }]);
      }
      out.puts res
    elsif l =~ /Pop/
      l.strip!
      l.gsub!(/Pop\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}
      #Switch the actions, reset embeds, and call on_entry
      res = %{
        var restore_info = __info__.stack.pop();

        //Retrieve the original action info
        var orig_action = restore_info.action;
        var orig_embeds = restore_info.embeds;

        //Save the old action
        //var old_action = __info__.action;

        var __free_asap = true;
        //HOOK_ENTRY[controller_will_pop] #{{"controller_name" => @controller.name, "might_respond_to" => @ctx.might_respond_to, "actions_responds_to" => @ctx.actions_respond_to, "from_action" => @name, "handling_event_named" => @handling_event_named}.to_json}

        //If views are configured to not free right away, set up a new stack of views to free
        //This is usually picked up by the hook GOTO
        if (__free_asap === false) {
          var views_to_free_id = tels(1);
          views_to_free[views_to_free_id] = views_to_free[views_to_free_id] || [];
        }

        //Restore the action we pushed from
        __info__.action = orig_action;

        //Remove all views, we don't have to recurse because removal of a view
        //is supposed to remove *all* view controllers of that tree as well.
        var embeds = __info__.embeds;
        for (var i = 0; i < __info__.embeds.length; ++i) {
          for (var j = 0; j < __info__.embeds[i].length; ++j) {
            //Free +1 because that will be the 'main' view
            //Free if 'free_asap' is not set, this is usually configured via the 'goto' hook
            if (__free_asap === true) {
              main_q.push([1, "if_free_view", embeds[i][j]+1]);
            } else {
              views_to_free[views_to_free_id].push(embeds[i][j]+1);
            }

            //Call dealloc on the controller
            tel_deref(embeds[i][j]).cte.__dealloc__(embeds[i][j]);
          }
        }

        //Restore embeds
        __info__.embeds = orig_embeds;

        //HOOK_ENTRY[controller_did_pop] #{{"controller_name" => @controller.name, "might_respond_to" => @ctx.might_respond_to, "actions_responds_to" => @ctx.actions_respond_to, "from_action" => @name, "handling_event_named" => @handling_event_named}.to_json}

      }

      out.puts res
    #Request(service_instance_name, ename, info)
    elsif l =~ /Request/
      l.strip!
      l.gsub!(/Request\(/, "")
      l.gsub! /\)$/, ""
      l.gsub! /\);$/, ""
      o = l.split(",").map{|e| e.strip}

      name = o.shift.gsub(/"/, "")
      ename = o.shift.gsub(/"/, "")
      info = o.shift.gsub(/"/, "")
      raise "You tried to Request the service #{name.inspect}, but you haven't added that to your 'services' for this controller (#{@controller.name.inspect})" unless @controller._services.include? name
      out << %{
        #{name}_on_#{ename}(__base__, #{info});
      }
    #VM Page macros
    elsif l =~ /NewPage/
      le = (l.split /NewPage/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*?),(.*?)\);?/
      exp.match /\((.*)\)/ if $1 == nil


      #Get the id value the user wants, but we have to be careful
      #because if nothing is passed, then we need to set it to null
      type_var = $1
      id_var = $2

      type_var = type_var.gsub(/"/, "").strip
      id_var = (id_var || "null").strip

      raise "NewPage was not given a type" if type_var == ""
      raise "NewPage type is not valid #{type_var.inspect}" unless ["array", "hash"].include? type_var

      type_var_to_entries = {
        "array" => "[]",
        "hash" => "{}",
      }

      out << %{
        #{lvar} {
          _head: null,
          _next: null,
          entries: #{type_var_to_entries[type_var]},
          _id: #{id_var},
          _type: "#{type_var}",
        }
      }
    elsif l =~ /CopyPage/
      le = (l.split /CopyPage/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*)\);?/
      page_var = $1

      out << %{
        
        var __page__ = {
          _head: #{page_var}._head,
          _next: #{page_var}._next,
          _id: #{page_var}._id,
          _type: #{page_var}._type,
        }

        //This is a shallow clone, but we own this array
        //When a mutable entry needs to be created, an entry will be cloned
        //and swappend out
        if (#{page_var}._type === "array") {
          __page__.entries = [];
          for (var i = 0; i < #{page_var}.entries.length; ++i) {
            __page__.entries.push(#{page_var}.entries[i]);
          }
        } else if (#{page_var}._type === "hash") {
          __page__.entries = {};
          var keys = Object.keys(#{page_var}.entries);
          for (var i = 0; i < keys.length; ++i) {
            var key = keys[i];
            __page__.entries[key] = #{page_var}.entries[key];
          }
        }

        #{lvar} __page__;
      }
    elsif l =~ /EntryDel/
      le = (l.split /EntryDel/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*?),(.*)\);?/
      page_var = $1
      index_var = $2

      out << %{
        if (#{page_var}._type === "array") {
          #{page_var}.entries.splice(#{index_var}, 1);
        } else if (#{page_var}._type === "hash") {
          delete #{page_var}.entries[#{index_var}];
        }
      }

    elsif l =~ /EntryInsert/
      le = (l.split /EntryInsert/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*?),(.*),(.*)\);?/
      page_var = $1
      index_var = $2
      entry_var = $3

      page_var.strip!
      index_var.strip!
      entry_var.strip!

      out << %{

        if (#{page_var}._type === "array") {
          #{entry_var}._id = gen_id();
          #{entry_var}._sig = gen_id();
          #{page_var}.entries.splice(#{index_var}, 0, #{entry_var});
        } else if (#{page_var}._type === "hash") {
          #{entry_var}._sig = gen_id();
          #{page_var}.entries[#{index_var}] = #{entry_var};
        }

      }

    elsif l =~ /SetPageNext/
      le = (l.split /SetPageNext/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*?),(.*)\);?/
      page_var = $1
      value_var = $2

      out << %{
        #{page_var}._next = #{value_var};
      }

    elsif l =~ /SetPageHead/
      le = (l.split /SetPageHead/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*?),(.*)\);?/
      page_var = $1
      value_var = $2

      out << %{
        #{page_var}._head = #{value_var};
      }

    elsif l =~ /EntryMutable/
      le = (l.split /EntryMutable/)
      lvar = le[0].strip #Probably var x = 
      exp = le[1].strip

      #For CopyPage(original_page), page_var is original_page
      #This only supports variable names at this time
      exp.match /\((.*?),(.*)\);?/
      page_var = $1
      index_var = $2


      out << %{
        if (#{page_var}._type === "array") {
          //Duplicate entry
          #{page_var}.entries.splice(#{index_var}, 1, JSON.parse(JSON.stringify(#{page_var}.entries[#{index_var}])));

          //Here's our new entry
          var ne = #{page_var}.entries[#{index_var}];
          ne._sig = gen_id();

          #{lvar} #{page_var}.entries[#{index_var}];
        } else if (#{page_var}._type === "hash") {
          //Duplicate entry
          #{page_var}.entries[#{index_var}] = JSON.parse(JSON.stringify(#{page_var}.entries[#{index_var}]));

          //Here's our new entry
          var ne = #{page_var}.entries[#{index_var}];
          ne._sig = gen_id();

          #{lvar} #{page_var}.entries[#{index_var}];

        }
      }
    else
      out.puts l
    end
  end

  return out.string
end