Class: Creamerscript::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/creamerscript/compiler.rb

Overview

Compiles CreamerScript code to Coffeescript.

The Compiler itself doesn’t really do much. It’s just a structured way of parsing code and then transforming it into Coffeescript.

Sweeteners are the real workers of CreamerScript. Take a look at the the creamerscript-sweeteners project for more info.

Examples:


compiler = Creamerscript::Compiler.new "(person say:'Hello')"
compiler.compile
# => "person.say("hello")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Compiler

Returns a new instance of Compiler.



21
22
23
# File 'lib/creamerscript/compiler.rb', line 21

def initialize(source)
  @source = source.dup
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/creamerscript/compiler.rb', line 19

def source
  @source
end

Instance Method Details

#compileObject



25
26
27
28
# File 'lib/creamerscript/compiler.rb', line 25

def compile
  substitute
  source.tap { transform(source) }
end

#patternObject



47
48
49
# File 'lib/creamerscript/compiler.rb', line 47

def pattern
  /_____CREAMER_([A-Z_]+)_(\d+)_____/
end

#substituteObject



30
31
32
33
34
# File 'lib/creamerscript/compiler.rb', line 30

def substitute
  Sweeteners.each do |sweetener|
    sweetener.substitute(source) while source =~ sweetener.pattern
  end
end

#transform(source) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/creamerscript/compiler.rb', line 36

def transform(source)
  source.gsub!(pattern) do |sub|
    sweetener = Sweeteners.for($1.downcase.to_sym)
    sweetener.call($2.to_i)

    result = sweetener.to_coffee
    result = transform(result) if result =~ pattern
    result
  end
end