Class: Piggly::Compiler::TraceCompiler

Inherits:
Object
  • Object
show all
Includes:
Util::Cacheable
Defined in:
lib/piggly/compiler/trace_compiler.rb

Overview

Walks the parse tree, attaching Tag values and rewriting source code to ping them.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Cacheable

#cache_path

Constructor Details

#initialize(config) ⇒ TraceCompiler

Returns a new instance of TraceCompiler.



10
11
12
# File 'lib/piggly/compiler/trace_compiler.rb', line 10

def initialize(config)
  @config = config
end

Class Method Details

.cache_sourcesObject

Each of these files’ mtimes are used to determine when another file is stale



109
110
111
112
113
# File 'lib/piggly/compiler/trace_compiler.rb', line 109

def cache_sources
  [Parser.grammar_path,
   Parser.parser_path,
   Parser.nodes_path]
end

Instance Method Details

#compile(procedure) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/piggly/compiler/trace_compiler.rb', line 21

def compile(procedure)
  cache = CacheDir.new(cache_path(procedure.source_path(@config)))

  if stale?(procedure)
    begin
    $stdout.puts "Compiling #{procedure.name}"
    tree = Parser.parse(IO.read(procedure.source_path(@config)))
    tree = tree.force! if tree.respond_to?(:thunk?)

    tags = []
    code = traverse(tree, procedure.oid, tags)

    cache.replace(:tree => tree, :code => code, :tags => tags)
    rescue RuntimeError => e
      $stdout.puts "      ****\n      Error compiling procedure \#{procedure.name}\n      Source: \#{procedure.source_path(@config)}\n      Exception Message:\n      \#{e.message}\n      ****\n      EXMSG\n    end\n\n  end\n\n  cache\nend\n"

#stale?(procedure) ⇒ Boolean

Is the cache_path is older than its source path or the other files?

Returns:



15
16
17
18
19
# File 'lib/piggly/compiler/trace_compiler.rb', line 15

def stale?(procedure)
  Util::File.stale?(cache_path(procedure.source_path(@config)),
                    procedure.source_path(@config),
                    *self.class.cache_sources)
end