1 require 'voodoo/parser'
4 # Voodoo compiler driver.
5 # The compiler driver reads input from a parser (see
6 # Voodoo::Parser), feeds it to a code generator (see
7 # Voodoo::CommonCodeGenerator), and writes the generated code.
9 # An example of its usage can be found on the main page for the
12 class Error < StandardError
21 @errors.each {|e| msg << e.message << "\n"}
26 # Initialize a compiler.
29 # [parser] the parser to be used (see Voodoo::Parser)
30 # [code_generator] the code generator to be used
31 # (see Voodoo::CommonCodeGenerator)
32 # [output] an IO object. The generated code will be written to it
33 def initialize parser, code_generator, output
35 @generator = code_generator
39 # Perform the compilation.
45 statement = @parser.parse_top_level
47 break if statement == nil
48 next if statement.empty?
52 section = statement[1]
54 @generator.add section, statement
57 rescue Parser::MultipleErrors => e
58 errors.concat e.errors
60 rescue Parser::ParseError => e
64 if errors.length >= 100
65 # Too many errors, give up.
66 raise Error.new(errors)
71 @generator.write @output
73 raise Error.new(errors)