Class: Tuga::Transpiler

Inherits:
Object
  • Object
show all
Defined in:
lib/tuga/transpiler.rb

Overview

Transpiles Tuga code to Ruby

Constant Summary collapse

ENCODING =

The encoding used to allow Portuguese characters

"# encoding: utf-8\n"
CORE_EXTENSIONS =

Aliases common methods from the standard library

"require \"tuga/core_ext\"\n"
INITIAL_CODE =

Every transpilled program will be encoded

"#{ENCODING}#{CORE_EXTENSIONS}".freeze

Instance Method Summary collapse

Constructor Details

#initializeTranspiler

Returns a new instance of Transpiler.



19
20
21
22
# File 'lib/tuga/transpiler.rb', line 19

def initialize
  @ruby_2_ruby = Ruby2Ruby.new
  @ruby_parser = RubyParser.new
end

Instance Method Details

#to_ruby(tuga_code, require_core_ext: true) ⇒ String

Converts Tuga code to Ruby

Parameters:

  • tuga_code (String)

    The Tuga code to be converted

  • require_core_ext (Boolean) (defaults to: true)

    Whether Tuga’s core extensions should be loaded or not

Returns:



31
32
33
34
35
36
# File 'lib/tuga/transpiler.rb', line 31

def to_ruby(tuga_code, require_core_ext: true)
  sexp = ruby_parser.process("# encoding: utf-8\n#{tuga_code}")
  ruby_code = ruby_2_ruby.process(sexp)

  require_core_ext ? "#{INITIAL_CODE}#{ruby_code}" : "#{ENCODING}#{ruby_code}"
end