class MIKU::Primitive

Public Class Methods

consing_method(name, method = nil) click to toggle source
# File core/miku/primitive.rb, line 27
def self.consing_method(name, method = nil)
  method = name unless method
  define_method(name){ |symtable, *objects|
    unless objects.empty?
      objects.map{ |x| eval(symtable, x) }.enum_for(:each_cons, 2).all?{ |a|
        a[0].__send__(method, a[1]) } end } end
injecting_method(name, method = nil) click to toggle source
# File core/miku/primitive.rb, line 19
def self.injecting_method(name, method = nil)
  method = name unless method
  define_method(name){ |symtable, *objects|
    unless objects.empty?
      first, *rest = *objects
      rest.inject(eval(symtable, first)){|a, b|
        a.__send__(method, eval(symtable, b)) } end } end
new(func) click to toggle source
# File core/miku/primitive.rb, line 11
def initialize(func)
  @func = func.to_sym
end

Public Instance Methods

_not(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 73
def _not(symtable, sexp)
  not eval(symtable, sexp) end
backquote(symtable, val) click to toggle source
# File core/miku/primitive.rb, line 47
def backquote(symtable, val)
  result = []
  val.each{|n|
    if not n.is_a?(List) then
      result << n
    elsif n.car == :comma then
      result << eval(symtable, n[1])
    elsif n.car == :comma_at then
      list = eval(symtable, n[1])
      raise ExceptionDelegator.new(',@がリスト以外に対して適用されました', ArgumentError) if not list.is_a?(List)
      result.concat(list.to_a) if list
    else
      result << backquote(symtable, n)
    end
  }
  result
end
call(*args) click to toggle source
# File core/miku/primitive.rb, line 15
def call(*args)
  send(@func, *args)
end
cons(symtable, head, tail) click to toggle source
# File core/miku/primitive.rb, line 65
def cons(symtable, head, tail)
  Cons.new(eval(symtable, head), eval(symtable, tail))
end
defun(symtable, key, val, *args) click to toggle source
# File core/miku/primitive.rb, line 105
def defun(symtable, key, val, *args)
  raise ExceptionDelegator.new('defunに与える引数は偶数個にして下さい', ArgumentError) if args.size == 1
  key = eval(symtable, key)
  val = eval(symtable, val)
  symtable.defun(key, val)
  return val if args.empty?
  defun(symtable, *args)
end
eval(symtable, node) click to toggle source
# File core/miku/primitive.rb, line 69
def eval(symtable, node)
  miku_eval_another(symtable, node)
end
function(symtable, symbol) click to toggle source
# File core/miku/primitive.rb, line 114
def function(symtable, symbol)
  if symbol.is_a? Symbol
    symtable[symbol].cdr
  else
    symbol end end
if(symtable, condition, true_case, false_case = nil) click to toggle source
# File core/miku/primitive.rb, line 76
def if(symtable, condition, true_case, false_case = nil)
  if(eval(symtable, condition)) then
    eval(symtable, true_case)
  else
    eval(symtable, false_case)
  end
end
list(symtable, *args) click to toggle source
# File core/miku/primitive.rb, line 84
def list(symtable, *args)
  args.map{|n| eval(symtable, n) }.to_cons
end
listp(symtable, val) click to toggle source
# File core/miku/primitive.rb, line 88
def listp(symtable, val)
  eval(symtable, val).is_a?(List)
end
macro(symtable, alist, *body) click to toggle source
# File core/miku/primitive.rb, line 120
def macro(symtable, alist, *body)
  Macro.new(alist, body)
end
macro_expand(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 168
def macro_expand(symtable, sexp)
  macro_expand_ne(symtable, eval(symtable, sexp))
end
macro_expand_all(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 124
def macro_expand_all(symtable, sexp)
  sexp = eval(symtable, sexp)
  if sexp.is_a? List
    expanded = macro_expand_ne(symtable, sexp)
    if expanded.is_a? List
      expanded.map{|node|
        macro_expand_all_ne(symtable, node) }
    else
      expanded end
  else
    sexp end end
macro_expand_all_ne(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 136
def macro_expand_all_ne(symtable, sexp)
  if sexp.is_a? List
    expanded = macro_expand_ne(symtable, sexp)
    if expanded.is_a? List
      expanded.map{|node|
        macro_expand_all_ne(symtable, node) }
    else
      expanded end
  else
    sexp end end
macro_expand_ne(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 147
def macro_expand_ne(symtable, sexp)
  if sexp.is_a? List
    macro = if(sexp.car.is_a? Symbol)
              symtable[sexp.car].cdr
            else
              eval(symtable, sexp.car) end
    if macro.is_a?(Macro)
      macro.macro_expand(*sexp.cdr.to_a)
    else
      sexp end
  else
    sexp end end
negi(parenttable, alist, *body) click to toggle source
# File core/miku/primitive.rb, line 172
def negi(parenttable, alist, *body)
  # body = body.map{ |node| macro_expand(parenttable, node) }
  lambda{ |*args|
    symtable = parenttable.miracle_binding(alist, args)
    body.inject(nil){ |last, operator|
      eval(symtable, operator) } } end
quote(symtable, val) click to toggle source
# File core/miku/primitive.rb, line 92
def quote(symtable, val)
  val
end
require_runtime_library(symtable, filename) click to toggle source
# File core/miku/primitive.rb, line 179
def require_runtime_library(symtable, filename)
  require eval(symtable, filename)
end
set(symtable, key, val, *args) click to toggle source
# File core/miku/primitive.rb, line 96
def set(symtable, key, val, *args)
  raise ExceptionDelegator.new('setに与える引数は偶数個にして下さい', ArgumentError) if args.size == 1
  key = eval(symtable, key)
  val = eval(symtable, val)
  symtable.set(key, val)
  return val if args.empty?
  set(symtable, *args)
end
to_ruby(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 160
def to_ruby(symtable, sexp)
  MIKU::ToRuby.to_ruby(macro_expand_all(symtable, sexp))
end
to_ruby_ne(symtable, sexp) click to toggle source
# File core/miku/primitive.rb, line 164
def to_ruby_ne(symtable, sexp)
  MIKU::ToRuby.to_ruby(macro_expand_all_ne(symtable, sexp))
end