Class: Builder::Rlang::Compiler

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/builder/rlang/compiler.rb

Constant Summary collapse

WAT_FRAME =

WAT source frame

%q{
;; Generated by Rlang compiler version %{version} on %{time}\n"

%{code}

}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

included, logger, #logger, logger=

Constructor Details

#initialize(source, target, options = {}) ⇒ Compiler

source: Path to Rlang file (.rb) target: Path to Wat file (.wat) options: Rlang parser options (parser.config)



28
29
30
31
32
33
34
35
36
37
# File 'lib/builder/rlang/compiler.rb', line 28

def initialize(source, target, options={})
  @source = source # Path to Rlang file (.rb)
  @target = target # Path to Wat file (.wat)
  @options = options # Rlang parser options (parser.config)
  @temp_target = target.nil?
  # Initialize parser and WAT code generator
  @parser = Rlang::Parser::Parser.new(nil, @options)
  @wgenerator = Rlang::Parser::WGenerator.new(@parser)
  @parser.wgenerator = @wgenerator
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



15
16
17
# File 'lib/builder/rlang/compiler.rb', line 15

def target
  @target
end

Instance Method Details

#cleanupObject



60
61
62
# File 'lib/builder/rlang/compiler.rb', line 60

def cleanup
  File.unlink(@tf.path) if @temp_target
end

#compileObject

return true if everything went well, false otherwise



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/builder/rlang/compiler.rb', line 40

def compile
  @parser.parse_file(@source)
  # Write generated WAT code in a temp file if
  # target file not given
  # Do not delete temp file when closing
  if @target
    @tf = File.open(@target, 'r')
  else
    @tf = Tempfile.new([File.basename(@source), '.wat'])
    @tf.persist!
    @target = @tf.path
  end
  @tf.write(WAT_FRAME % {version: Rlang::VERSION,
                      time: Time.now,
                      code: @wgenerator.root.transpile
                    })
  @tf.close
  true
end