Class: Nudge::ValuePoint

Inherits:
ProgramPoint show all
Defined in:
lib/interpreter/programPoints.rb

Defined Under Namespace

Classes: UnassignedValuePointError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ProgramPoint

#each

Constructor Details

#initialize(type, representation = nil) ⇒ ValuePoint

Returns a new instance of ValuePoint.

Raises:

  • (ArgumentError)


92
93
94
95
96
# File 'lib/interpreter/programPoints.rb', line 92

def initialize(type,representation=nil)
  raise(ArgumentError, "Type must be a symbol or string") unless [Symbol,String].include?(type.class)
  @type = type.to_sym
  @raw = representation.nil? ? nil : representation.to_s
end

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



89
90
91
# File 'lib/interpreter/programPoints.rb', line 89

def raw
  @raw
end

#typeObject

Returns the value of attribute type.



89
90
91
# File 'lib/interpreter/programPoints.rb', line 89

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



90
91
92
# File 'lib/interpreter/programPoints.rb', line 90

def value
  @value
end

Instance Method Details

#blueprintObject



151
152
153
154
# File 'lib/interpreter/programPoints.rb', line 151

def blueprint
  pts = self.blueprint_parts
  return "#{pts[0]} \n#{pts[1]}"
end

#blueprint_partsObject



146
147
148
149
# File 'lib/interpreter/programPoints.rb', line 146

def blueprint_parts
  fn = "«#{self.type}» #{self.raw}".strip
  return [self.tidy, fn]
end

#cloneObject



111
112
113
# File 'lib/interpreter/programPoints.rb', line 111

def clone
  ValuePoint.new(@type, @raw.dup)
end

#go(context) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/interpreter/programPoints.rb', line 98

def go(context)
  raise(UnassignedValuePointError, "#{self.type} point has no value string") if self.value == nil
  context.stacks[self.type].push(self)
  rescue UnassignedValuePointError => exc
    msg = ValuePoint.new("error", exc.message)
    context.stacks[:error].push msg
end

#nudgetype(typename = @type) ⇒ Object



120
121
122
# File 'lib/interpreter/programPoints.rb', line 120

def nudgetype(typename=@type)
  "#{typename.to_s.camelize}Type"
end

#nudgetype_defined?(typename = @type) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/interpreter/programPoints.rb', line 124

def nudgetype_defined?(typename=@type)
  Nudge.const_defined?(nudgetype(typename).to_sym)
end

#pointsObject



116
117
118
# File 'lib/interpreter/programPoints.rb', line 116

def points
  1
end

#randomize(context) ⇒ Object



140
141
142
143
144
# File 'lib/interpreter/programPoints.rb', line 140

def randomize(context)
  newType = context.types.sample
  @type = newType.to_nudgecode
  @raw = newType.any_value
end

#tidy(level = 1) ⇒ Object



136
137
138
# File 'lib/interpreter/programPoints.rb', line 136

def tidy(level=1)
  "value «" + @type.to_s + "»"
end