Module: ExternalVariableProcessing

Included in:
ArduinoSketch
Defined in:
lib/rad/variable_processing.rb

Instance Method Summary collapse

Instance Method Details

#c_type(typ) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rad/variable_processing.rb', line 135

def c_type(typ)
  type = 
    case typ 
    when Integer
      "int"
    when String
      "char*"
    when TrueClass
      "bool"
    when FalseClass
      "bool"
    else
      raise "Bug! Unknown type #{typ.inspect} in c_type"
    end

    type
end

#check_variable_type(type) ⇒ Object



129
130
131
132
133
# File 'lib/rad/variable_processing.rb', line 129

def check_variable_type(type)
  unless type =~ /#{C_VAR_TYPES}/
    raise ArgumentError, "the following variable types are supported \n #{C_VAR_TYPES.gsub("|",", ")} got #{type}" 
  end
end

#post_process_arrays(name, var) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/rad/variable_processing.rb', line 120

def post_process_arrays(name, var)
  type = c_type(var[0])
  $array_types[name] = type
  assignment = var.inspect.gsub("[","{").gsub("]","}")      
  c_style_array = "#{type} __#{name}[] = #{assignment};"
  $external_var_identifiers << "__#{name}" unless $external_var_identifiers.include?("__#{name}")
  $external_array_vars << c_style_array unless $external_array_vars.include?(c_style_array)
end

#post_process_vars(name, type, value = nil) ⇒ Object



114
115
116
117
118
# File 'lib/rad/variable_processing.rb', line 114

def post_process_vars(name, type, value = nil)
  value = " = #{value}" if value 
  $external_var_identifiers << "__#{name}" unless $external_var_identifiers.include?("__#{name}")
  $external_vars << "#{type} __#{name}#{value};"
end

#pre_process_vars(name, var) ⇒ Object



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
# File 'lib/rad/variable_processing.rb', line 24

def pre_process_vars(name, var)
    # puts 
    # puts
    case var
    when Integer
      # puts "pre_process: #{name}, #{var}, #{var.inspect} got #{var.class} 29"
      value = var
      type = "int"
      post_process_vars(name, type, value)
    when Float
      # puts "pre_process: #{name}, #{var}, #{var.inspect} got #{var.class} 34"
      value = var
      type = "float"
      post_process_vars(name, type, value)
    when String
      # puts "pre_process: #{name}, #{var.inspect} got #{var.class} on 39"
      if var.match(",").nil? && var =~ /long|byte|unsigned|int|short/
        # puts "pre_process #{name}, #{var.inspect} got #{var.class} level three sublevel"
        type = var
        value = nil
        post_process_vars(name, type, value)
      else
        value = var.split(",").first.lstrip
        type = var.split(",")[1].nil? ?  nil : var.split(",")[1].lstrip
        translate_variables( name , type, value )
      end
    when TrueClass
      # puts "pre_process: #{name}, #{var}, #{var.inspect} got #{var.class} on 50"
      value = 1
      type = "bool"
      post_process_vars(name, type, value)
    when FalseClass
      # puts "pre_process: #{name}, #{var}, #{var.inspect} got #{var.class} on 55"
      value = 0
      type = "bool"
      post_process_vars(name, type, value)
    when Array
      post_process_arrays(name, var)
    else
      raise ArgumentError, "not sure what to do here...  got #{name} with value #{var} which is a #{var.class}" 
    end      
end

#process_external_vars(klass) ⇒ Object

need to clean this up need to test



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rad/variable_processing.rb', line 9

def process_external_vars(klass)
  vars = eval "#{klass}.instance_variables"
  local_vars = []
  vars.each { |v| local_vars << ":#{v.gsub("@", "")}" }
  loc_vars = local_vars.join(", ")
  # add accessors 
  klass.module_eval "class << self; attr_accessor #{loc_vars} end"
  local_vars.each do |symbol|
    name = symbol.gsub(":","")
    t_var = eval "#{klass}.#{name}"
    pre_process_vars(name, t_var)
  end
end

#translate_variables(name, type = nil, value = nil) ⇒ Object



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
# File 'lib/rad/variable_processing.rb', line 68

def translate_variables(name, type = nil, value = nil)
  
  unless type.nil?
    check_variable_type(type)
  end

  # classify the values
  if value.class == Fixnum 
    # puts "translate_variables: #{name}, #{value}, #{type} is a fixnum, got #{value.class} on 74"
  elsif value.class == Float 
    # puts "translate_variables: #{name}, #{value}, #{type} is a float, got #{value.class} on 76"
  elsif value =~ /^-(\d|x)*$/ 
    value = value.to_i
    type = "int" if type.nil?
  elsif value =~ /^-(\d|\.|x)*$/ 
    value = value.to_f
    unless type.nil?
      raise ArgumentError, "#{value} should be a float got  #{type}" unless type == "float"
    end
    type = "float" if type.nil?   

  elsif value[0,1] !~ /\d/
    # puts value[0,1]
    # puts "translate_variables: #{name}, #{value}, #{type} is a number of some type, got #{value.class} on 79"
    type = "char*"
    value = "\"#{value}\""
  elsif value !~ /(\.|x)/
    # puts "translate_variables: #{name}, #{value}, #{type} is an integer, got #{value.class} on 83"
    value = value.to_i
    type = "int" if type.nil?
  elsif value =~ /(\d*\.\d*)/ # and no 
    # puts "translate_variables: #{name}, #{value}, #{type} is a float, got #{value.class} on 87"
    value = value.to_f
    type = "float"
  elsif value =~ /0x\d\d/
    # puts "translate_variables: #{name}, #{value}, #{type} is a byte, got #{value.class} on 91"
    type = "byte"
  else
    raise ArgumentError, "not sure what to do with a value of #{value} with a type like #{type}" 
  end
 
  post_process_vars(name, type, value)
end