Module: Kapusta::Compiler

Defined in:
lib/kapusta/compiler.rb,
lib/kapusta/compiler/emitter.rb,
lib/kapusta/compiler/lua_compat.rb,
lib/kapusta/compiler/normalizer.rb,
lib/kapusta/compiler/macro_expander.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

Defined Under Namespace

Modules: EmitterModules, LuaCompat Classes: Emitter, Error, MacroExpander, Normalizer

Constant Summary collapse

CORE_SPECIAL_FORMS =
%w[
  fn lambda λ let local var global set if when unless case match
  while for each do values
  -> ->> -?> -?>> doto
  icollect collect fcollect accumulate faccumulate
  hashfn
  . ?. :
  ..
  length
  require
  module class
  try catch finally
  raise
  ivar cvar gvar
  ruby
  tset
  and or not
  = not= < <= > >=
  + - * / %
  print
  macro macros import-macros
  quasi-sym quasi-list quasi-list-tail quasi-vec quasi-vec-tail quasi-hash quasi-gensym
].freeze
SPECIAL_FORMS =
(CORE_SPECIAL_FORMS + LuaCompat::SPECIAL_FORMS).freeze

Class Method Summary collapse

Class Method Details

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



37
38
39
40
41
42
43
# File 'lib/kapusta/compiler.rb', line 37

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

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



45
46
47
48
49
50
# File 'lib/kapusta/compiler.rb', line 45

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

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



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

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

.run_file(path) ⇒ Object



57
58
59
# File 'lib/kapusta/compiler.rb', line 57

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