Class: RVM::Languages::Language

Inherits:
Object
  • Object
show all
Extended by:
Plugin
Defined in:
lib/rvm/languages.rb

Overview

The Compiler class, it holds the compiler. As those grow big I suggest to split the actuall code into multiple fils. Have a look at the MUSHCode compile for an example.

Direct Known Subclasses

Brainfuck, ECMA, Math

Instance Method Summary collapse

Methods included from Plugin

helper, included, plugin_host, plugin_id, register_for

Constructor Details

#initialize(*args) ⇒ Language

Returns a new instance of Language.



33
34
35
36
37
# File 'lib/rvm/languages.rb', line 33

def initialize *args
  @compiler = self.class::Compiler.new(*args)
  @core_libs = nil
  core_libs
end

Instance Method Details

#compile(code, *args) ⇒ Object

Compiles a code with the generated compiler.



76
77
78
# File 'lib/rvm/languages.rb', line 76

def compile code, *args
  @compiler.compile code, *args
end

#core_libs(force_reload = false, additional_core_libs = []) ⇒ Object

Loads the core libaries for a language, they have to be in the directory rvm/langauges/LANGUAGE_ID/ directory and have to be named core-* to be recongnized ase core libraries. For speed reasons the core libraries will be cahced, to force them to be recompiled use the force_reload parameter as true.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rvm/languages.rb', line 44

def core_libs force_reload=false, additional_core_libs=[]
  if not @core_libs or force_reload
    @core_libs = additional_core_libs
    Dir[File.join(File.dirname(__FILE__), 'languages', self.class.plugin_id.to_s, 'core-*')].each do |f|
      begin
        @core_libs << compile(File.read(f),true)
      rescue Exception => e
        puts "Failed to compile core library: #{f}."
        raise e
      end
    end
  end
  @core_libs
end

#env(force_reload = false, additional_core_libs = [], core_env = nil) ⇒ Object

This method returns a environment that has the needed core libraries loaded.

When your langauge uses core libraries to publish RVM functions to the program this should be used. The first parameter force_reload can be passed to forcefully reload the core libraries. The second parameter can be passed to add the core libraries to an existing environment.



67
68
69
70
71
72
73
# File 'lib/rvm/languages.rb', line 67

def env force_reload=false, additional_core_libs=[], core_env=nil
  core_env ||= RVM::Interpreter.env
  core_libs(force_reload, additional_core_libs).each do |lib|
    lib.execute(core_env)
  end
  core_env
end