Debian Codegraph @

core/miku/primitive.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require_relative 'atom'
3 require_relative 'error'
4 require_relative 'macro'
5 require_relative 'to_ruby'
6
7 module MIKU
8 class Primitive
9 include Atom
10 include ToRuby
11
12 def initialize(func)
13 @func = func.to_sym
14 end
15
16 def call(*args)
17 send(@func, *args)
18 end
19
20 def self.injecting_method(name, method = nil)
21 method = name unless method
22 define_method(name){ |symtable, *objects|
23 unless objects.empty?
24 first, *rest = *objects
25 rest.inject(eval(symtable, first)){|a, b|
26 a.__send__(method, eval(symtable, b)) } end } end
27
28 def self.consing_method(name, method = nil)
29 method = name unless method
30 define_method(name){ |symtable, *objects|
31 unless objects.empty?
32 objects.map{ |x| eval(symtable, x) }.enum_for(:each_cons, 2).all?{ |a|
33 a[0].__send__(method, a[1]) } end } end
34
35 injecting_method(:+)
36 injecting_method(:-)
37 injecting_method(:*)
38 injecting_method(:/)
39
40 consing_method(:<)
41 consing_method(:>)
42 consing_method(:<=)
43 consing_method(:>=)
44 consing_method(:eq, :equal?)
45 consing_method(:eql, :==)
46 consing_method(:equal, :===)
47
48 def backquote(symtable, val)
49 result = []
50 val.each{|n|
51 if not n.is_a?(List) then
52 result << n
53 elsif n.car == :comma then
54 result << eval(symtable, n[1])
55 elsif n.car == :comma_at then
56 list = eval(symtable, n[1])
57 raise ExceptionDelegator.new(',@がリスト以外に対して適用されました', ArgumentError) if not list.is_a?(List)
58 result.concat(list.to_a) if list
59 else
60 result << backquote(symtable, n)
61 end
62 }
63 result
64 end
65
66 def cons(symtable, head, tail)
67 Cons.new(eval(symtable, head), eval(symtable, tail))
68 end
69
70 def eval(symtable, node)
71 miku_eval_another(symtable, node)
72 end
73
74 def _not(symtable, sexp)
75 not eval(symtable, sexp) end
76
77 def if(symtable, condition, true_case, false_case = nil)
78 if(eval(symtable, condition)) then
79 eval(symtable, true_case)
80 else
81 eval(symtable, false_case)
82 end
83 end
84
85 def list(symtable, *args)
86 args.map{|n| eval(symtable, n) }.to_cons
87 end
88
89 def listp(symtable, val)
90 eval(symtable, val).is_a?(List)
91 end
92
93 def quote(symtable, val)
94 val
95 end
96
97 def set(symtable, key, val, *args)
98 raise ExceptionDelegator.new('setに与える引数は偶数個にして下さい', ArgumentError) if args.size == 1
99 key = eval(symtable, key)
100 val = eval(symtable, val)
101 symtable.set(key, val)
102 return val if args.empty?
103 set(symtable, *args)
104 end
105
106 def defun(symtable, key, val, *args)
107 raise ExceptionDelegator.new('defunに与える引数は偶数個にして下さい', ArgumentError) if args.size == 1
108 key = eval(symtable, key)
109 val = eval(symtable, val)
110 symtable.defun(key, val)
111 return val if args.empty?
112 defun(symtable, *args)
113 end
114
115 def function(symtable, symbol)
116 if symbol.is_a? Symbol
117 symtable[symbol].cdr
118 else
119 symbol end end
120
121 def macro(symtable, alist, *body)
122 Macro.new(alist, body)
123 end
124
125 def macro_expand_all(symtable, sexp)
126 macro_expand_all_ne(symtable, eval(symtable, sexp))
127 end
128
129 def macro_expand_all_ne(symtable, sexp)
130 if sexp.is_a?(List)
131 expanded = macro_expand_ne(symtable, sexp)
132 case expanded
133 in [Symbol => macro, *] if symtable[macro].cdr.is_a?(Macro)
134 macro_expand_all_ne(symtable, expanded)
135 in List
136 expanded.map(&method(:macro_expand_all_ne).curry.(symtable))
137 else
138 expanded
139 end
140 else
141 sexp
142 end
143 end
144
145 def macro_expand_ne(symtable, sexp)
146 if sexp.is_a? List
147 macro = if(sexp.car.is_a? Symbol)
148 symtable[sexp.car].cdr
149 else
150 eval(symtable, sexp.car) end
151 if macro.is_a?(Macro)
152 macro.macro_expand(*sexp.cdr)
153 else
154 sexp end
155 else
156 sexp end end
157
158 def to_ruby(symtable, sexp)
159 MIKU::ToRuby.to_ruby(macro_expand_all(symtable, sexp))
160 end
161
162 def to_ruby_ne(symtable, sexp)
163 MIKU::ToRuby.to_ruby(macro_expand_all_ne(symtable, sexp))
164 end
165
166 def macro_expand(symtable, sexp)
167 macro_expand_ne(symtable, eval(symtable, sexp))
168 end
169
170 def negi(parenttable, alist, *body)
171 # body = body.map{ |node| macro_expand(parenttable, node) }
172 lambda{ |*args|
173 symtable = parenttable.miracle_binding(alist, args)
174 body.inject(nil){ |last, operator|
175 eval(symtable, operator) } } end
176
177 def require_runtime_library(symtable, filename)
178 require eval(symtable, filename)
179 end
180
181 end
182 end