Class: C0::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/c0/body.rb

Overview

One unit as written: its raw value bytes (layout included) and its sub-units (␞a␟b␚␟c: b holds the sub-unit c), each a Unit with sub-units of its own (␞a␚␟b␚␚␟c: a holds b holds c).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, subs = []) ⇒ Unit

Returns a new instance of Unit.



202
203
204
205
# File 'lib/c0/body.rb', line 202

def initialize(raw, subs = [])
  @raw = raw
  @subs = subs
end

Instance Attribute Details

#raw ⇒ Object (readonly)

Returns the value of attribute raw.



200
201
202
# File 'lib/c0/body.rb', line 200

def raw
  @raw
end

#subs ⇒ Object (readonly)

Returns the value of attribute subs.



200
201
202
# File 'lib/c0/body.rb', line 200

def subs
  @subs
end

Class Method Details

.from_tree(buf, tree) ⇒ Object

A unit from the extension's [start, end, subs] tree over buf.



208
209
210
211
# File 'lib/c0/body.rb', line 208

def self.from_tree(buf, tree)
  s, e, subs = tree
  new(buf.byteslice(s, e - s), subs.map { |t| from_tree(buf, t) })
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



233
# File 'lib/c0/body.rb', line 233

def ==(other) = other.is_a?(Unit) && other.raw == @raw && other.subs == @subs

#hash ⇒ Object



237
# File 'lib/c0/body.rb', line 237

def hash = [@raw, @subs].hash

#inspect ⇒ Object



239
# File 'lib/c0/body.rb', line 239

def inspect = "#<C0::Unit #{@raw.inspect}#{@subs.empty? ? '' : " subs=#{@subs.inspect}"}>"

#level ⇒ Object

The unit as a nested level (a Body). Raises C0::Error when it is not one.



227
228
229
230
231
# File 'lib/c0/body.rb', line 227

def level
  b = C0.to_bin(@raw)
  ls, le = Ext.unit_level(b, 0, b.bytesize)
  Body.view(b, ls, le)
end

#level? ⇒ Boolean

Returns:

  • (Boolean)


219
# File 'lib/c0/body.rb', line 219

def level? = type == UnitType::LEVEL

#ref? ⇒ Boolean

Returns:

  • (Boolean)


221
# File 'lib/c0/body.rb', line 221

def ref? = type == UnitType::REF

#subs? ⇒ Boolean

Whether the unit holds sub-units.

Returns:

  • (Boolean)


224
# File 'lib/c0/body.rb', line 224

def subs? = !@subs.empty?

#type ⇒ Object

One of C0::UnitType.



217
# File 'lib/c0/body.rb', line 217

def type = Ext.unit_type(@raw, 0, @raw.bytesize)

#value ⇒ Object

The logical value. Raises C0::Error on a level or a reference.



214
# File 'lib/c0/body.rb', line 214

def value = C0.value(@raw)