Class: Trith::Compiler Abstract

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

Overview

This class is abstract.

A base class for Trith compiler targets.

Direct Known Subclasses

Compiler::Java, Compiler::Ruby

Defined Under Namespace

Classes: Java, Ruby

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Compiler) initialize(options = {})

A new instance of Compiler

Parameters:

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


44
45
46
# File 'lib/trith/compiler.rb', line 44

def initialize(options = {})
  @options = options.to_hash.dup
end

Instance Attribute Details

- (Hash{Symbol => Object}) options (readonly)

Returns:

  • (Hash{Symbol => Object})


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

def options
  @options
end

Class Method Details

+ (Compiler) compile(program, options = {})

Parameters:

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

Returns:



38
39
40
# File 'lib/trith/compiler.rb', line 38

def self.compile(program, options = {})
  self.new(options).compile(program, options)
end

+ (Compiler) compile_files(*filenames)

Parameters:

  • (Array<String>) filenames
  • (Hash{Symbol => Object}) options

Returns:



29
30
31
32
# File 'lib/trith/compiler.rb', line 29

def self.compile_files(*filenames)
  options = filenames.last.is_a?(Hash) ? filenames.peek : {}
  self.compile(Reader.read_files(*filenames), options)
end

+ (Class) for(target = nil, &block)

Parameters:

  • (Symbol, #to_sym) target (defaults to: nil)

Returns:

  • (Class)


16
17
18
19
20
21
22
23
# File 'lib/trith/compiler.rb', line 16

def self.for(target = nil, &block)
  target = block.call if block_given?
  case target.to_sym
    when :ruby then Ruby
    when :java then Java
    else nil
  end
end

Instance Method Details

- (Compiler) compile(program, options = {})

Parameters:

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

Returns:



52
53
54
55
56
# File 'lib/trith/compiler.rb', line 52

def compile(program, options = {})
  program = program.respond_to?(:each) ? program : [program]
  program.each { |op| compile_operand(op) }
  self
end

- compile_boolean(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Boolean) operand

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/trith/compiler.rb', line 111

def compile_boolean(operand)
  raise NotImplementedError.new("#{self.class}#compile_boolean(#{operand.inspect})")
end

- compile_decimal(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (BigDecimal) operand

Raises:

  • (NotImplementedError)


154
155
156
# File 'lib/trith/compiler.rb', line 154

def compile_decimal(operand)
  raise NotImplementedError.new("#{self.class}#compile_decimal(#{operand.inspect})")
end

- compile_float(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Float) operand

Raises:

  • (NotImplementedError)


146
147
148
# File 'lib/trith/compiler.rb', line 146

def compile_float(operand)
  raise NotImplementedError.new("#{self.class}#compile_float(#{operand.inspect})")
end

- compile_integer(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Integer) operand

Raises:

  • (NotImplementedError)


138
139
140
# File 'lib/trith/compiler.rb', line 138

def compile_integer(operand)
  raise NotImplementedError.new("#{self.class}#compile_integer(#{operand.inspect})")
end

- compile_nil(operand = nil) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (NilClass) operand (defaults to: nil)

Raises:

  • (NotImplementedError)


103
104
105
# File 'lib/trith/compiler.rb', line 103

def compile_nil(operand = nil)
  raise NotImplementedError.new("#{self.class}#compile_nil")
end

- compile_number(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Numeric) operand


119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/trith/compiler.rb', line 119

def compile_number(operand)
  case operand
    when Integer
      compile_integer(operand)
    when Float
      compile_float(operand)
    when BigDecimal
      compile_decimal(operand)
    when Rational
      compile_rational(operand)
    else
      raise NotImplementedError.new("#{self.class}#compile_number(#{operand.inspect})")
  end
end

- compile_operand(operand) (protected)

This method returns an undefined value.

Parameters:

  • (Object) operand


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/trith/compiler.rb', line 63

def compile_operand(operand)
  case operand
    when Symbol
      compile_operator(operand)
    when NilClass
      compile_nil(operand)
    when TrueClass, FalseClass
      compile_boolean(operand)
    when Numeric
      compile_number(operand)
    when String
      compile_string(operand)
    when URI
      compile_reference(operand)
    when Array, SXP::List
      compile_quotation(operand)
    when Machine, Function
      compile_quotation(operand.to_a)
    else
      raise NotImplementedError.new("#{self.class}#compile_operand(#{operand.inspect})")
  end
end

- compile_operator(operator) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Symbol, #to_sym) operator


90
91
92
93
94
95
96
97
# File 'lib/trith/compiler.rb', line 90

def compile_operator(operator)
  case operator.to_sym
    when :nil   then compile_nil(nil)
    when :false then compile_boolean(false)
    when :true  then compile_boolean(true)
    else raise NotImplementedError.new("#{self.class}#compile_operator(#{operator.inspect})")
  end
end

- compile_quotation(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Array) operand

Raises:

  • (NotImplementedError)


186
187
188
# File 'lib/trith/compiler.rb', line 186

def compile_quotation(operand)
  raise NotImplementedError.new("#{self.class}#compile_quotation(#{operand.inspect})")
end

- compile_rational(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (Rational) operand

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/trith/compiler.rb', line 162

def compile_rational(operand)
  raise NotImplementedError.new("#{self.class}#compile_rational(#{operand.inspect})")
end

- compile_reference(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (URI) operand

Raises:

  • (NotImplementedError)


178
179
180
# File 'lib/trith/compiler.rb', line 178

def compile_reference(operand)
  raise NotImplementedError.new("#{self.class}#compile_reference(#{operand.inspect})")
end

- compile_string(operand) (protected)

This method is abstract.

This method returns an undefined value.

Parameters:

  • (String) operand

Raises:

  • (NotImplementedError)


170
171
172
# File 'lib/trith/compiler.rb', line 170

def compile_string(operand)
  raise NotImplementedError.new("#{self.class}#compile_string(#{operand.inspect})")
end