Class: Lignite::Variables

Inherits:
Object
  • Object
show all
Includes:
Bytes
Defined in:
lib/lignite/variables.rb

Overview

Allocate local or global variables

Instance Method Summary collapse

Methods included from Bytes

#bin_to_hex, #f32, #hex_to_bin, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8

Constructor Details

#initializeVariables

Returns a new instance of Variables.



6
7
8
9
10
11
12
13
# File 'lib/lignite/variables.rb', line 6

def initialize
  @offset = 0
  # for proper decoding of direct replies according to declared types
  @unpacker = ""
  @vars = {}
  @param_count = 0
  @param_decl_bytes = ""
end

Instance Method Details

#add(id, size, count, unpacker) ⇒ Object

Declare a variable/parameter id

Parameters:

  • id (Symbol)
  • size (Integer)

    byte size of one element of the data

  • count (Integer)

    1 for scalars, more for arrays

  • unpacker (String)

    for String#unpack for direct commands



20
21
22
23
24
25
26
# File 'lib/lignite/variables.rb', line 20

def add(id, size, count, unpacker)
  raise "Duplicate variable #{id}" if @vars.key?(id)
  @offset = align(@offset, size)
  @vars[id] = { offset: @offset, size: size * count }
  @offset += size * count
  @unpacker += unpacker
end

#bytesizeObject

compile



52
53
54
# File 'lib/lignite/variables.rb', line 52

def bytesize
  @offset
end

#key?(sym) ⇒ Boolean

use

Returns:

  • (Boolean)


42
43
44
# File 'lib/lignite/variables.rb', line 42

def key?(sym)
  @vars.key?(sym)
end

#offset(sym) ⇒ Object

use



47
48
49
# File 'lib/lignite/variables.rb', line 47

def offset(sym)
  @vars[sym][:offset]
end

#param(name, size, count, size_code, direction) ⇒ Object

declare a subroutine parameter



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lignite/variables.rb', line 29

def param(name, size, count, size_code, direction)
  raise "Duplicate parameter #{name}" if @vars.key?(name)
  unless @offset == align(@offset, size)
    raise "Misaligned parameter #{name} of size #{size} at #{@offset}"
  end
  nonsense_unpacker = "," # FIXME: better
  add(name, size, count, nonsense_unpacker)

  @param_count += 1
  @param_decl_bytes += u8(size_code | direction)
end

#param_decl_headerObject



56
57
58
# File 'lib/lignite/variables.rb', line 56

def param_decl_header
  u8(@param_count) + @param_decl_bytes
end

#unpack(buf) ⇒ Object

decode reply



61
62
63
64
# File 'lib/lignite/variables.rb', line 61

def unpack(buf)
  values = buf.unpack(@unpacker)
  values.size == 1 ? values.first : values
end