Class: Creamerscript::Compiler
- Inherits:
-
Object
- Object
- Creamerscript::Compiler
- 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.
Instance Attribute Summary collapse
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #compile ⇒ Object
-
#initialize(source) ⇒ Compiler
constructor
A new instance of Compiler.
- #pattern ⇒ Object
- #substitute ⇒ Object
- #transform(source) ⇒ Object
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
#source ⇒ Object
Returns the value of attribute source.
19 20 21 |
# File 'lib/creamerscript/compiler.rb', line 19 def source @source end |
Instance Method Details
#compile ⇒ Object
25 26 27 28 |
# File 'lib/creamerscript/compiler.rb', line 25 def compile substitute source.tap { transform(source) } end |
#pattern ⇒ Object
47 48 49 |
# File 'lib/creamerscript/compiler.rb', line 47 def pattern /_____CREAMER_([A-Z_]+)_(\d+)_____/ end |
#substitute ⇒ Object
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 |