Class: Lignite::Variables

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

Overview

Allocate local or global variables FIXME: the user can make bad alignment, resulting in VMError at runtime bad: data8 :speed; data32 :tacho; output_read(…) good data32 :tacho; data8 :speed; output_read(…)

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.



9
10
11
12
13
14
15
16
# File 'lib/lignite/variables.rb', line 9

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, unpacker) ⇒ Object

declare



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

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

#bytesizeObject

compile



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

def bytesize
  @offset
end

#key?(sym) ⇒ Boolean

use

Returns:

  • (Boolean)


37
38
39
# File 'lib/lignite/variables.rb', line 37

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

#offset(sym) ⇒ Object

use



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

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

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

declare a subroutine parameter



27
28
29
30
31
32
33
34
# File 'lib/lignite/variables.rb', line 27

def param(name, size, size_code, direction)
  raise "Duplicate parameter #{name}" if @vars.key?(name)
  nonsense_unpacker = "," # FIXME: better
  add(name, size, nonsense_unpacker)

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

#param_decl_headerObject



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

def param_decl_header
  u8(@param_count) + @param_decl_bytes
end

#unpack(buf) ⇒ Object

decode reply



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

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