1 require 'voodoo/config'
4 # Module for selecting code generators.
6 # The code generation API is described in Voodoo::CommonCodeGenerator.
8 # Hash using target architectures as keys.
9 # Each entry is a hash mapping output format to generator class.
14 # Registers a code generator.
16 # Voodoo::CodeGenerator.register_generator I386NasmGenerator,
17 # :architecture => :i386,
19 def register_generator klass, params
20 if params.has_key?(:architecture) && params.has_key?(:format)
21 arch = params[:architecture].to_sym
22 format = params[:format].to_sym
23 format_hash = @@generators[arch] || {}
24 format_hash[format] = klass
25 @@generators[arch] = format_hash
27 raise ArgumentError, "Need to specify :architecture and :format"
31 # Gets a code generator for the specified parameters.
32 def get_generator params = {}
33 params[:architecture] = Config.default_architecture \
34 unless params[:architecture]
35 params[:format] = Config.default_format unless params[:format]
36 arch = params[:architecture].to_sym
37 format = params[:format].to_sym
38 format_hash = @@generators[arch]
39 klass = format_hash ? format_hash[format] : nil
41 return klass.new params
43 raise NotImplementedError, "No code generator for architecture #{arch} and format #{format}"
47 # Tests if a given architecture is supported.
48 def architecture_supported? arch
49 @@generators.has_key? arch.to_sym
52 # Gets an array of supported architectures.
57 # Tests if a given format is supported for a given architecture.
58 def format_supported? arch, format
59 architecture_supported?(arch.to_sym) &&
60 @@generators[arch.to_sym].has_key?(format.to_sym)
63 # Gets an array of supported formats for a given architecture.
64 def formats architecture
65 @@generators[architecture.to_sym].keys
71 Dir.glob(File.join(File.dirname(__FILE__), 'generators', '*.rb')).each do |file|
72 name = file.sub(/.*(voodoo\/generators\/.*)\.rb/, "\\1")