Module: Kapusta::Compiler

Defined in:
lib/kapusta/compiler.rb,
lib/kapusta/compiler/emitter.rb,
lib/kapusta/compiler/language.rb,
lib/kapusta/compiler/lua_compat.rb,
lib/kapusta/compiler/normalizer.rb,
lib/kapusta/compiler/macro_gensym.rb,
lib/kapusta/compiler/macro_lowerer.rb,
lib/kapusta/compiler/macro_expander.rb,
lib/kapusta/compiler/macro_importer.rb,
lib/kapusta/compiler/emitter/interop.rb,
lib/kapusta/compiler/emitter/support.rb,
lib/kapusta/compiler/emitter/bindings.rb,
lib/kapusta/compiler/emitter/patterns.rb,
lib/kapusta/compiler/emitter/collections.rb,
lib/kapusta/compiler/emitter/expressions.rb,
lib/kapusta/compiler/emitter/control_flow.rb,
lib/kapusta/compiler/emitter/simple_expression.rb

Defined Under Namespace

Modules: EmitterModules, Language, LuaCompat, MacroGensym Classes: Emitter, Error, MacroExpander, MacroImporter, MacroLowerer, Normalizer

Constant Summary collapse

CORE_SPECIAL_FORMS =
Language::CORE_SPECIAL_FORMS
SPECIAL_FORMS =
Language::SPECIAL_FORMS

Class Method Summary collapse

Class Method Details

.compile(source, path: '(kapusta)', target: nil) ⇒ Object



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

def self.compile(source, path: '(kapusta)', target: nil)
  forms = Reader.read_all(source)
  expanded = MacroExpander.new(path:).expand_all(forms)
  compile_forms(expanded, path:, target:)
rescue Kapusta::Error => e
  raise e.with_defaults(path:)
end

.compile_forms(forms, path: '(kapusta)', target: nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/kapusta/compiler.rb', line 24

def self.compile_forms(forms, path: '(kapusta)', target: nil)
  normalized = Normalizer.new.normalize_all(forms)
  Emitter.new(path:, target: normalize_target(target)).emit_file(normalized)
rescue Kapusta::Error => e
  raise e.with_defaults(path:)
end

.normalize_target(target) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/kapusta/compiler.rb', line 40

def self.normalize_target(target)
  case target
  when nil then nil
  when :mruby3, 'mruby3', :mruby, 'mruby' then :mruby3
  else
    raise Error, Kapusta::Errors.format(:unknown_target, target: target.inspect)
  end
end

.run(source, path: '(kapusta)') ⇒ Object



31
32
33
34
# File 'lib/kapusta/compiler.rb', line 31

def self.run(source, path: '(kapusta)')
  ruby = compile(source, path:)
  TOPLEVEL_BINDING.eval(ruby, path, 1)
end

.run_file(path) ⇒ Object



36
37
38
# File 'lib/kapusta/compiler.rb', line 36

def self.run_file(path)
  run(File.read(path), path:)
end