Class: RedShift::Expr

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

Overview

Kinda funny…

Direct Known Subclasses

ResetExpr

Constant Summary collapse

@@serial =
Hash.new(0)

Constants inherited from Flow

Flow::CT_STRUCT_NAME

Instance Attribute Summary collapse

Attributes inherited from Flow

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

Instance Method Summary collapse

Methods inherited from Flow

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

Constructor Details

#initialize(f, type = "double") ⇒ Expr

Returns a new instance of Expr.



59
60
61
62
# File 'lib/redshift/target/c/flow/expr.rb', line 59

def initialize f, type = "double"
  super nil, f
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



57
58
59
# File 'lib/redshift/target/c/flow/expr.rb', line 57

def type
  @type
end

Instance Method Details

#make_generator(cl) ⇒ Object



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

def make_generator cl
  @fname = "expr_#{CGenerator.make_c_name cl.name}_#{@@serial[cl]}"
  @@serial[cl] += 1
  @inspect_str = "#{cl.name}: #{formula}"
  
  @generator = proc do
    sl = cl.shadow_library
    ssn = cl.shadow_struct_name
    cont_state_ssn = cl.cont_state_class.shadow_struct_name

    sl.init_library_function.body \
      "s_init_expr(#{fname}, #{fname.inspect}, #{inspect_str.inspect});"

    include_file, source_file = sl.add_file fname
    
    # We need the struct
    source_file.include(cl.shadow_library_include_file)
    
    expr = self
    source_file.define(fname).instance_eval do
      arguments "ComponentShadow *comp_shdw"
      scope:extern
      return_type expr.type
      declare :shadow => %{
        struct #{ssn} *shadow;
        struct #{cont_state_ssn} *cont_state;
        ContVar  *var;
      }
      setup :shadow => %{
        shadow = (#{ssn} *)comp_shdw;
        cont_state = (struct #{cont_state_ssn} *)shadow->cont_state;
      }
      declare :result => "#{expr.type} result"
      translation = expr.translate(self, "result", cl, 0)
      body %{
        #{translation.join("
        ")};
        return result;
      }
    end
  end
end