Class: Trith::Compiler::Ruby

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

Overview

A Trith-to-Ruby compiler.

Constant Summary

SHEBANG =
"#!/usr/bin/env ruby -rubygems"

Instance Method Summary (collapse)

Methods inherited from

compile, #compile, compile_files, #compile_operand, for

Constructor Details

- (Ruby) initialize(options = {})

A new instance of Ruby

Parameters:

  • (Hash{Symbol => Object}) options (defaults to: {})


9
10
11
12
13
# File 'lib/trith/compiler/ruby.rb', line 9

def initialize(options = {})
  super(options)
  @options[:shebang] ||= SHEBANG
  @output = StringIO.new
end

Instance Method Details

- emit(line) (protected)

This method returns an undefined value.

Parameters:

  • (String, #to_s) line


88
89
90
# File 'lib/trith/compiler/ruby.rb', line 88

def emit(line)
  @output.puts("  " << line.to_s)
end

- (String) to_s

Returns:

  • (String)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trith/compiler/ruby.rb', line 17

def to_s
  output = StringIO.new
  output.puts @options[:shebang]
  output.puts "require 'rubygems'" if @options[:rubygems]
  output.puts "require 'trith'"
  output.puts
  output.puts "Trith::Machine.run(ARGV) do"
  output.write @output.string
  output.puts "end"
  output.string
end