Class: Lignite::OpCompiler
- Inherits:
-
Object
- Object
- Lignite::OpCompiler
show all
- Extended by:
- Logger
- Includes:
- Bytes, Logger
- Defined in:
- lib/lignite/op_compiler.rb
Overview
Dynamically constructs methods for all the instructions in ev3.yml The methods return the ByteStrings corresponding to the ops.
Defined Under Namespace
Classes: TODO
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logger
default_logger, logger
Methods included from Bytes
#f32, #hexdump, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8
Constructor Details
#initialize(globals = nil, locals = nil) ⇒ OpCompiler
Returns a new instance of OpCompiler.
133
134
135
136
137
|
# File 'lib/lignite/op_compiler.rb', line 133
def initialize(globals = nil, locals = nil)
self.class.load_yml
@globals = globals
@locals = locals
end
|
Class Method Details
.define_multiop(oname, commands) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/lignite/op_compiler.rb', line 45
def self.define_multiop(oname, commands)
names = commands.map do |cname, cdata|
csym = cname.downcase.to_sym
cvalue = cdata["value"]
[cvalue, csym]
end.to_h
osym = oname.downcase.to_sym
define_method(osym) do |*args|
logger.debug "called #{osym} with #{args.inspect}"
cvalue = args.shift
csym = names.fetch(cvalue)
send("#{osym}_#{csym}", *args)
end
end
|
.define_op(oname, ovalue, cvalue, params) ⇒ Object
61
62
63
64
65
66
67
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
|
# File 'lib/lignite/op_compiler.rb', line 61
def self.define_op(oname, ovalue, cvalue, params)
check_arg_count = true
param_handlers = params.map do |par|
case par["type"]
when "PARLAB" raise TODO
when "PARNO" check_arg_count = false
->(x) { param_simple(x) }
when "PARS" raise TODO
when "PARV" raise TODO
when "PARVALUES"
raise TODO
when "PAR8", "PAR16", "PAR32", "PARF"
->(x) { param_simple(x) }
else
raise "Unhandled param type #{par["type"]}"
end
end
osym = oname.downcase.to_sym
define_method(osym) do |*args|
logger.debug "called #{osym} with #{args.inspect}"
if check_arg_count && args.size != param_handlers.size
raise ArgumentError, "expected #{param_handlers.size} arguments, got #{args.size}"
end
bytes = u8(ovalue)
bytes += param_simple(cvalue) unless cvalue.nil?
bytes += args.zip(param_handlers).map do |a, h|
h ||= ->(x) { param_simple(x) }
instance_exec(a, &h)
end.join("")
logger.debug "returning bytecode: #{bytes.inspect}"
bytes
end
rescue TODO
logger.debug "Could not define #{oname}"
end
|
.load_const(name, value) ⇒ Object
22
23
24
25
|
# File 'lib/lignite/op_compiler.rb', line 22
def self.load_const(name, value)
raise "duplicate constant #{name}" if Lignite.const_defined?(name)
Lignite.const_set(name, value)
end
|
.load_op(oname, odata) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/lignite/op_compiler.rb', line 27
def self.load_op(oname, odata)
ovalue = odata["value"]
oparams = odata["params"]
p1 = oparams.first
if p1 && p1["type"] == "SUBP"
commands = p1["commands"]
commands.each do |cname, cdata|
cvalue = cdata["value"]
load_const(cname, cvalue)
cparams = cdata["params"]
define_op("#{oname}_#{cname}", ovalue, cvalue, cparams)
end
define_multiop(oname, commands)
else
define_op(oname, ovalue, nil, oparams)
end
end
|
.load_yml ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/lignite/op_compiler.rb', line 107
def self.load_yml
return if @loaded
fname = File.expand_path("../../../data/ev3.yml", __FILE__)
yml = YAML.load_file(fname)
op_hash = yml["ops"]
op_hash.each do |oname, odata|
load_op(oname, odata)
end
defines = yml["defines"]
defines.each do |dname, ddata|
load_const(dname, ddata["value"])
end
enums = yml["enums"]
enums.each do |ename, edata|
edata["members"].each do |mname, mdata|
load_const(mname, mdata["value"])
end
end
@loaded = true
end
|