Class: RedShift::AlgebraicFlow

Inherits:
Flow show all
Defined in:
lib/redshift/component.rb,
lib/redshift/target/c/flow/algebraic.rb

Constant Summary

Constants inherited from Flow

Flow::CT_STRUCT_NAME

Instance Attribute Summary

Attributes inherited from Flow

#fname, #formula, #generator, #inspect_str, #strict, #var

Instance Method Summary collapse

Methods inherited from Flow

#become_generatable, #external_constant?, #generate, #initialize, #inspect, #make_ct_struct, #translate, #translate_link, #wrapper

Constructor Details

This class inherits a constructor from RedShift::Flow

Instance Method Details

#make_generator(cl, state) ⇒ Object



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
# File 'lib/redshift/target/c/flow/algebraic.rb', line 2

def make_generator cl, state
  @fname = "flow_#{CGenerator.make_c_name cl.name}_#{var}_#{state}"
  @inspect_str = "#{cl.name}:#{state}: #{var} = #{formula}"
  
  @generator = proc do
    sl = cl.shadow_library
    ssn = cl.shadow_struct_name
    cont_state_ssn = cl.cont_state_class.shadow_struct_name
    
    ## use an accumulator for these
    sl.init_library_function.body \
      "s_init_flow(#{fname}, #{fname.inspect}, #{inspect_str.inspect}, ALGEBRAIC);"

    include_file, source_file = sl.add_file fname
    
    # We need the struct
    source_file.include(cl.shadow_library_include_file)
    
    flow = self
    var_name = @var
    source_file.define(fname).instance_eval do
      arguments "ComponentShadow *comp_shdw"
      scope :extern
      declare :shadow => %{
        struct #{ssn} *shadow;
        struct #{cont_state_ssn} *cont_state;
        ContVar  *var;
      }
      exc = declare_class CircularDefinitionError
      msg = "Circularity in algebraic formula for #{var_name} in state " +
            "#{state} of class #{cl.name}."
      ## note that state may not be the same as the object's state, due
      ## to flow wrapper caching
      
      setup :shadow => %{
        shadow = (#{ssn} *)comp_shdw;
        cont_state = (struct #{cont_state_ssn} *)shadow->cont_state;
        var = &cont_state->#{var_name};
        assert(var->algebraic);
        if (shadow->world->alg_nest > shadow->world->alg_depth_limit) {
          shadow->world->alg_nest = 0;
          rs_raise(#{exc}, shadow->self, #{msg.inspect});
        }
        shadow->world->alg_nest++;
      }
      
      body %{
        #{flow.translate(self,
            "var->value[shadow->world->rk_level]", cl) {|strict|
          flow.instance_eval {@strict = strict}
        }.join("
        ")};

        switch (shadow->world->rk_level) {
        case 0:
          var->d_tick = shadow->world->d_tick;
          break;
          
        case 1:
        case 2:
        case 3:
          var->rk_level = shadow->world->rk_level;
          break;
          
        default:
          rb_raise(#{declare_class RuntimeError},
            "Bad rk_level, %ld!", shadow->world->rk_level);
        }
        
        shadow->world->alg_nest--;
      }
    end
    # Case 0 applies during discrete update.
    # Alg flows are lazy.
    # Note that @strict does not need to propagate to wrapper
    # (as it does in CexprGuard) since checking is done statically
    # in Component.define_flow.
  end
  
  return self
end