Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrblib
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 added
- 2 deleted
- 10 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/00class.rb
r331 r439 6 6 end 7 7 # 15.2.2.4.11 8 def attr(name) 9 attr_reader(name) 10 end 8 alias attr attr_reader 9 #def attr(name) 10 # attr_reader(name) 11 #end 11 12 12 13 # 15.2.2.4.27 … … 16 17 m.included(self) 17 18 end 19 self 18 20 end 19 21 … … 23 25 m.prepended(self) 24 26 end 27 self 25 28 end 26 29 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/10error.rb
r331 r439 3 3 end 4 4 5 # ISO 15.2.25 6 class LocalJumpError < S tandardError5 # ISO 15.2.25 says "LocalJumpError < StandardError" 6 class LocalJumpError < ScriptError 7 7 end 8 8 … … 52 52 end 53 53 54 class FrozenError < RuntimeError 55 end 56 54 57 class StopIteration < IndexError 55 58 attr_accessor :result -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/array.rb
r331 r439 1 # coding: utf-8 1 2 ## 2 3 # Array … … 10 11 # 11 12 # ISO 15.2.12.5.10 12 def each(&block)13 return to_enum :each unless block_given?14 15 idx = 016 while idx < length17 block.call(self[idx])18 idx += 119 end20 self21 end13 # def each(&block) 14 # return to_enum :each unless block 15 16 # idx = 0 17 # while idx < length 18 # block.call(self[idx]) 19 # idx += 1 20 # end 21 # self 22 # end 22 23 23 24 ## … … 27 28 # ISO 15.2.12.5.11 28 29 def each_index(&block) 29 return to_enum :each_index unless block _given?30 return to_enum :each_index unless block 30 31 31 32 idx = 0 … … 44 45 # ISO 15.2.12.5.7 45 46 def collect!(&block) 46 return to_enum :collect! unless block_given? 47 48 self.each_index { |idx| self[idx] = block.call(self[idx]) } 47 return to_enum :collect! unless block 48 49 idx = 0 50 len = size 51 while idx < len 52 self[idx] = block.call self[idx] 53 idx += 1 54 end 49 55 self 50 56 end … … 61 67 # ISO 15.2.12.5.15 62 68 def initialize(size=0, obj=nil, &block) 63 raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integer69 size = size.__to_int 64 70 raise ArgumentError, "negative array size" if size < 0 65 71 … … 78 84 end 79 85 80 def _inspect 81 return "[]" if self.size == 0 82 "["+self.map{|x|x.inspect}.join(", ")+"]" 86 def _inspect(recur_list) 87 size = self.size 88 return "[]" if size == 0 89 return "[...]" if recur_list[self.object_id] 90 recur_list[self.object_id] = true 91 ary=[] 92 i=0 93 while i<size 94 ary<<self[i]._inspect(recur_list) 95 i+=1 96 end 97 "["+ary.join(", ")+"]" 83 98 end 84 99 ## … … 87 102 # ISO 15.2.12.5.31 (x) 88 103 def inspect 89 begin 90 self._inspect 91 rescue SystemStackError 92 "[...]" 93 end 104 self._inspect({}) 94 105 end 95 106 # ISO 15.2.12.5.32 (x) … … 179 190 ret 180 191 end 181 182 # internal method to convert multi-value to single value183 def __svalue184 return self.first if self.size < 2185 self186 end187 192 end 188 193 … … 197 202 # elements. 198 203 def sort!(&block) 199 self.replace(self.sort(&block)) 204 stack = [ [ 0, self.size - 1 ] ] 205 until stack.empty? 206 left, mid, right = stack.pop 207 if right == nil 208 right = mid 209 # sort self[left..right] 210 if left < right 211 if left + 1 == right 212 lval = self[left] 213 rval = self[right] 214 cmp = if block then block.call(lval,rval) else lval <=> rval end 215 if cmp.nil? 216 raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed" 217 end 218 if cmp > 0 219 self[left] = rval 220 self[right] = lval 221 end 222 else 223 mid = ((left + right + 1) / 2).floor 224 stack.push [ left, mid, right ] 225 stack.push [ mid, right ] 226 stack.push [ left, (mid - 1) ] if left < mid - 1 227 end 228 end 229 else 230 lary = self[left, mid - left] 231 lsize = lary.size 232 233 # The entity sharing between lary and self may cause a large memory 234 # copy operation in the merge loop below. This harmless operation 235 # cancels the sharing and provides a huge performance gain. 236 lary[0] = lary[0] 237 238 # merge 239 lidx = 0 240 ridx = mid 241 (left..right).each { |i| 242 if lidx >= lsize 243 break 244 elsif ridx > right 245 self[i, lsize - lidx] = lary[lidx, lsize - lidx] 246 break 247 else 248 lval = lary[lidx] 249 rval = self[ridx] 250 cmp = if block then block.call(lval,rval) else lval <=> rval end 251 if cmp.nil? 252 raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed" 253 end 254 if cmp <= 0 255 self[i] = lval 256 lidx += 1 257 else 258 self[i] = rval 259 ridx += 1 260 end 261 end 262 } 263 end 264 end 265 self 266 end 267 268 def sort(&block) 269 self.dup.sort!(&block) 200 270 end 201 271 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/enum.rb
r331 r439 14 14 module Enumerable 15 15 16 NONE = Object.new 17 16 18 ## 17 19 # Call the given block for each element … … 64 66 65 67 ## 66 # Call the given block for each element67 # which is yield by +each+. Return68 # +ifnone+ if no block value was true.69 # Otherwise return the first block value70 # which had was true.68 # Return the first element for which 69 # value from the block is true. If no 70 # object matches, calls +ifnone+ and 71 # returns its result. Otherwise returns 72 # +nil+. 71 73 # 72 74 # ISO 15.3.2.2.4 73 75 def detect(ifnone=nil, &block) 74 ret = ifnone 76 return to_enum :detect, ifnone unless block 77 75 78 self.each{|*val| 76 79 if block.call(*val) 77 ret = val.__svalue 78 break 79 end 80 } 81 ret 80 return val.__svalue 81 end 82 } 83 ifnone.call unless ifnone.nil? 82 84 end 83 85 … … 283 285 # ISO 15.3.2.2.16 284 286 def partition(&block) 287 return to_enum :partition unless block 288 285 289 ary_T = [] 286 290 ary_F = [] … … 303 307 # ISO 15.3.2.2.17 304 308 def reject(&block) 309 return to_enum :reject unless block 310 305 311 ary = [] 306 312 self.each{|*val| … … 315 321 # ISO 15.3.2.2.18 316 322 alias select find_all 317 318 ##319 # TODO320 # Does this OK? Please test it.321 def __sort_sub__(sorted, work, src_ary, head, tail, &block)322 if head == tail323 sorted[head] = work[head] if src_ary == 1324 return325 end326 327 # on current step, which is a src ary?328 if src_ary == 0329 src, dst = sorted, work330 else331 src, dst = work, sorted332 end333 334 key = src[head] # key value for dividing values335 i, j = head, tail # position to store on the dst ary336 337 (head + 1).upto(tail){|idx|338 if ((block)? block.call(src[idx], key): (src[idx] <=> key)) > 0339 # larger than key340 dst[j] = src[idx]341 j -= 1342 else343 dst[i] = src[idx]344 i += 1345 end346 }347 348 sorted[i] = key349 350 # sort each sub-array351 src_ary = (src_ary + 1) % 2 # exchange a src ary352 __sort_sub__(sorted, work, src_ary, head, i - 1, &block) if i > head353 __sort_sub__(sorted, work, src_ary, i + 1, tail, &block) if i < tail354 end355 # private :__sort_sub__356 323 357 324 ## … … 365 332 # ISO 15.3.2.2.19 366 333 def sort(&block) 367 ary = [] 368 self.each{|*val| ary.push(val.__svalue)} 369 if ary.size > 1 370 __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block) 371 end 372 ary 334 self.map{|*val| val.__svalue}.sort(&block) 373 335 end 374 336 … … 384 346 i = 0 385 347 self.each do |e| 386 n = (e.hash & (0x7fffffff >> (i % 16))) << (i % 16) 387 h ^= n 348 h = __update_hash(h, i, e.hash) 388 349 i += 1 389 350 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/hash.rb
r331 r439 13 13 def ==(hash) 14 14 return true if self.equal?(hash) 15 begin 16 hash = hash.to_hash 17 rescue NoMethodError 15 unless Hash === hash 18 16 return false 19 17 end … … 33 31 def eql?(hash) 34 32 return true if self.equal?(hash) 35 begin 36 hash = hash.to_hash 37 rescue NoMethodError 33 unless Hash === hash 38 34 return false 39 35 end … … 56 52 def delete(key, &block) 57 53 if block && !self.has_key?(key) 58 block.call(key) 59 else 60 self.__delete(key) 61 end 54 return block.call(key) 55 end 56 self.__delete(key) 62 57 end 63 58 … … 85 80 # ISO 15.2.13.4.9 86 81 def each(&block) 87 return to_enum :each unless block _given?82 return to_enum :each unless block 88 83 89 84 keys = self.keys … … 118 113 # ISO 15.2.13.4.10 119 114 def each_key(&block) 120 return to_enum :each_key unless block _given?115 return to_enum :each_key unless block 121 116 122 117 self.keys.each{|k| block.call(k)} … … 144 139 # ISO 15.2.13.4.11 145 140 def each_value(&block) 146 return to_enum :each_value unless block _given?141 return to_enum :each_value unless block 147 142 148 143 self.keys.each{|k| block.call(self[k])} … … 155 150 # ISO 15.2.13.4.23 156 151 def replace(hash) 157 raise TypeError, " can't convert argument into Hash" unless hash.respond_to?(:to_hash)152 raise TypeError, "Hash required (#{hash.class} given)" unless Hash === hash 158 153 self.clear 159 hash = hash.to_hash160 154 hash.each_key{|k| 161 155 self[k] = hash[k] … … 180 174 # ISO 15.2.13.4.22 181 175 def merge(other, &block) 182 h = {} 183 raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash) 184 other = other.to_hash 185 self.each_key{|k| h[k] = self[k]} 176 raise TypeError, "Hash required (#{other.class} given)" unless Hash === other 177 h = self.dup 186 178 if block 187 179 other.each_key{|k| … … 195 187 196 188 # internal method for Hash inspection 197 def _inspect 189 def _inspect(recur_list) 198 190 return "{}" if self.size == 0 199 "{"+self.map {|k,v| 200 k._inspect + "=>" + v._inspect 201 }.join(", ")+"}" 191 return "{...}" if recur_list[self.object_id] 192 recur_list[self.object_id] = true 193 ary=[] 194 keys=self.keys 195 size=keys.size 196 i=0 197 while i<size 198 k=keys[i] 199 ary<<(k._inspect(recur_list) + "=>" + self[k]._inspect(recur_list)) 200 i+=1 201 end 202 "{"+ary.join(", ")+"}" 202 203 end 203 204 ## … … 206 207 # ISO 15.2.13.4.30 (x) 207 208 def inspect 208 begin 209 self._inspect 210 rescue SystemStackError 211 "{...}" 212 end 209 self._inspect({}) 213 210 end 214 211 # ISO 15.2.13.4.31 (x) … … 225 222 # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing. 226 223 # 227 def reject!(&b )228 return to_enum :reject! unless block _given?224 def reject!(&block) 225 return to_enum :reject! unless block 229 226 230 227 keys = [] 231 228 self.each{|k,v| 232 if b .call([k, v])229 if block.call([k, v]) 233 230 keys.push(k) 234 231 end … … 256 253 # 1.8/1.9 Hash#reject returns Hash; ISO says nothing. 257 254 # 258 def reject(&b )259 return to_enum :reject unless block _given?255 def reject(&block) 256 return to_enum :reject unless block 260 257 261 258 h = {} 262 259 self.each{|k,v| 263 unless b .call([k, v])260 unless block.call([k, v]) 264 261 h[k] = v 265 262 end … … 278 275 # 1.9 Hash#select! returns Hash; ISO says nothing. 279 276 # 280 def select!(&b )281 return to_enum :select! unless block _given?277 def select!(&block) 278 return to_enum :select! unless block 282 279 283 280 keys = [] 284 281 self.each{|k,v| 285 unless b .call([k, v])282 unless block.call([k, v]) 286 283 keys.push(k) 287 284 end … … 309 306 # 1.9 Hash#select returns Hash; ISO says nothing 310 307 # 311 def select(&b )312 return to_enum :select unless block _given?308 def select(&block) 309 return to_enum :select unless block 313 310 314 311 h = {} 315 312 self.each{|k,v| 316 if b .call([k, v])313 if block.call([k, v]) 317 314 h[k] = v 318 315 end 319 316 } 320 317 h 321 end322 323 ##324 # call-seq:325 # hsh.rehash -> hsh326 #327 # Rebuilds the hash based on the current hash values for each key. If328 # values of key objects have changed since they were inserted, this329 # method will reindex <i>hsh</i>.330 #331 # h = {"AAA" => "b"}332 # h.keys[0].chop!333 # h #=> {"AA"=>"b"}334 # h["AA"] #=> nil335 # h.rehash #=> {"AA"=>"b"}336 # h["AA"] #=> "b"337 #338 def rehash339 h = {}340 self.each{|k,v|341 h[k] = v342 }343 self.replace(h)344 end345 346 def __update(h)347 h.each_key{|k| self[k] = h[k]}348 self349 318 end 350 319 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/kernel.rb
r321 r439 7 7 # 15.3.1.2.1 Kernel.` 8 8 # provided by Kernel#` 9 # 15.3.1.3. 59 # 15.3.1.3.3 10 10 def `(s) 11 11 raise NotImplementedError.new("backquotes not implemented") … … 41 41 42 42 # internal method for inspect 43 def _inspect 43 def _inspect(_recur_list) 44 44 self.inspect 45 45 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/mrblib.rake
r321 r439 4 4 current_build_dir = "#{build_dir}/#{relative_from_root}" 5 5 6 self.libmruby << objfile("#{current_build_dir}/mrblib")6 self.libmruby_objs << objfile("#{current_build_dir}/mrblib") 7 7 8 8 file objfile("#{current_build_dir}/mrblib") => "#{current_build_dir}/mrblib.c" 9 9 file "#{current_build_dir}/mrblib.c" => [mrbcfile, __FILE__] + Dir.glob("#{current_dir}/*.rb").sort do |t| 10 10 _, _, *rbfiles = t.prerequisites 11 FileUtils.mkdir_p File.dirname(t.name)11 mkdir_p File.dirname(t.name) 12 12 open(t.name, 'w') do |f| 13 13 _pp "GEN", "*.rb", "#{t.name.relative_path}" -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/numeric.rb
r331 r439 46 46 # ISO 15.2.8.3.15 47 47 def downto(num, &block) 48 return to_enum(:downto, num) unless block _given?48 return to_enum(:downto, num) unless block 49 49 50 50 i = self.to_i … … 71 71 # ISO 15.2.8.3.22 72 72 def times &block 73 return to_enum :times unless block _given?73 return to_enum :times unless block 74 74 75 75 i = 0 … … 87 87 # ISO 15.2.8.3.27 88 88 def upto(num, &block) 89 return to_enum(:upto, num) unless block _given?89 return to_enum(:upto, num) unless block 90 90 91 91 i = self.to_i … … 103 103 def step(num=nil, step=1, &block) 104 104 raise ArgumentError, "step can't be 0" if step == 0 105 return to_enum(:step, num, step) unless block _given?105 return to_enum(:step, num, step) unless block 106 106 107 i = if num.kind_of? Float then self.to_f else self end 108 if num == nil 107 i = __coerce_step_counter(num, step) 108 if num == self || step.infinite? 109 block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) 110 elsif num == nil 109 111 while true 110 112 block.call(i) 111 i +=step113 i += step 112 114 end 113 return self 114 end 115 if step > 0 115 elsif step > 0 116 116 while i <= num 117 117 block.call(i) … … 162 162 alias truncate floor 163 163 end 164 165 ##166 # Float167 #168 # ISO 15.2.9169 class Float170 # mruby special - since mruby integers may be upgraded to floats,171 # floats should be compatible to integers.172 include Integral173 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/range.rb
r331 r439 11 11 # ISO 15.2.14.4.4 12 12 def each(&block) 13 return to_enum :each unless block _given?13 return to_enum :each unless block 14 14 15 15 val = self.first … … 27 27 end 28 28 29 if val.kind_of?(String) && last.kind_of?(String) # fixnums are special29 if val.kind_of?(String) && last.kind_of?(String) # strings are special 30 30 if val.respond_to? :upto 31 31 return val.upto(last, exclude_end?, &block) -
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/string.rb
r331 r439 4 4 # ISO 15.2.10 5 5 class String 6 # ISO 15.2.10.3 6 7 include Comparable 8 7 9 ## 8 10 # Calls the given block for each line … … 10 12 # 11 13 # ISO 15.2.10.5.15 12 def each_line(rs = "\n", &block) 13 return to_enum(:each_line, rs, &block) unless block 14 return block.call(self) if rs.nil? 15 rs = rs.to_str 16 offset = 0 17 rs_len = rs.length 18 this = dup 19 while pos = this.index(rs, offset) 20 block.call(this[offset, pos + rs_len - offset]) 21 offset = pos + rs_len 22 end 23 block.call(this[offset, this.size - offset]) if this.size > offset 14 def each_line(separator = "\n", &block) 15 return to_enum(:each_line, separator) unless block 16 17 if separator.nil? 18 block.call(self) 19 return self 20 end 21 raise TypeError unless separator.is_a?(String) 22 23 paragraph_mode = false 24 if separator.empty? 25 paragraph_mode = true 26 separator = "\n\n" 27 end 28 start = 0 29 string = dup 30 self_len = length 31 sep_len = separator.length 32 should_yield_subclass_instances = self.class != String 33 34 while (pointer = string.index(separator, start)) 35 pointer += sep_len 36 pointer += 1 while paragraph_mode && string[pointer] == "\n" 37 if should_yield_subclass_instances 38 block.call(self.class.new(string[start, pointer - start])) 39 else 40 block.call(string[start, pointer - start]) 41 end 42 start = pointer 43 end 44 return self if start == self_len 45 46 if should_yield_subclass_instances 47 block.call(self.class.new(string[start, self_len - start])) 48 else 49 block.call(string[start, self_len - start]) 50 end 24 51 self 25 52 end … … 68 95 end 69 96 if !replace.nil? || !block 70 replace = replace.to_str97 replace.__to_str 71 98 end 72 99 offset = 0 … … 97 124 # ISO 15.2.10.5.19 98 125 def gsub!(*args, &block) 99 raise RuntimeError, "can't modify frozen String" if frozen?126 raise FrozenError, "can't modify frozen String" if frozen? 100 127 return to_enum(:gsub!, *args) if args.length == 1 && !block 101 128 str = self.gsub(*args, &block) 102 return nil if str == self129 return nil unless self.index(args[0]) 103 130 self.replace(str) 104 131 end 105 132 106 ## 107 # Calls the given block for each match of +pattern+ 108 # If no block is given return an array with all 109 # matches of +pattern+. 110 # 111 # ISO 15.2.10.5.32 112 def scan(reg, &block) 113 ### *** TODO *** ### 114 unless Object.const_defined?(:Regexp) 115 raise NotImplementedError, "scan not available (yet)" 116 end 117 end 133 # ## 134 # # Calls the given block for each match of +pattern+ 135 # # If no block is given return an array with all 136 # # matches of +pattern+. 137 ## 138 # # ISO 15.2.10.5.32 139 # def scan(pattern, &block) 140 # # TODO: String#scan is not implemented yet 141 # end 118 142 119 143 ## … … 130 154 131 155 pattern, replace = *args 132 pattern = pattern.to_str156 pattern.__to_str 133 157 if args.length == 2 && block 134 158 block = nil 135 159 end 136 if !block137 replace = replace.to_str160 unless block 161 replace.__to_str 138 162 end 139 163 result = [] … … 160 184 # ISO 15.2.10.5.37 161 185 def sub!(*args, &block) 162 raise RuntimeError, "can't modify frozen String" if frozen?186 raise FrozenError, "can't modify frozen String" if frozen? 163 187 str = self.sub(*args, &block) 164 return nil if str == self188 return nil unless self.index(args[0]) 165 189 self.replace(str) 166 end167 168 ##169 # Call the given block for each character of170 # +self+.171 def each_char(&block)172 pos = 0173 while pos < self.size174 block.call(self[pos])175 pos += 1176 end177 self178 190 end 179 191 … … 181 193 # Call the given block for each byte of +self+. 182 194 def each_byte(&block) 195 return to_enum(:each_byte, &block) unless block 183 196 bytes = self.bytes 184 197 pos = 0 … … 190 203 end 191 204 192 ## 193 # Modify +self+ by replacing the content of +self+. 194 # The portion of the string affected is determined using the same criteria as +String#[]+. 195 def []=(*args) 196 anum = args.size 197 if anum == 2 198 pos, value = args 199 case pos 200 when String 201 posnum = self.index(pos) 202 if posnum 203 b = self[0, posnum.to_i] 204 a = self[(posnum + pos.length)..-1] 205 self.replace([b, value, a].join('')) 206 else 207 raise IndexError, "string not matched" 208 end 209 when Range 210 head = pos.begin 211 tail = pos.end 212 tail += self.length if tail < 0 213 unless pos.exclude_end? 214 tail += 1 215 end 216 return self[head, tail-head]=value 217 else 218 pos += self.length if pos < 0 219 if pos < 0 || pos > self.length 220 raise IndexError, "index #{args[0]} out of string" 221 end 222 b = self[0, pos.to_i] 223 a = self[pos + 1..-1] 224 self.replace([b, value, a].join('')) 225 end 226 return value 227 elsif anum == 3 228 pos, len, value = args 229 pos += self.length if pos < 0 230 if pos < 0 || pos > self.length 231 raise IndexError, "index #{args[0]} out of string" 232 end 233 if len < 0 234 raise IndexError, "negative length #{len}" 235 end 236 b = self[0, pos.to_i] 237 a = self[pos + len..-1] 238 self.replace([b, value, a].join('')) 239 return value 240 else 241 raise ArgumentError, "wrong number of arguments (#{anum} for 2..3)" 242 end 243 end 244 205 # those two methods requires Regexp that is optional in mruby 245 206 ## 246 207 # ISO 15.2.10.5.3 247 def =~(re) 248 raise TypeError, "type mismatch: String given" if re.respond_to? :to_str 249 re =~ self 250 end 208 #def =~(re) 209 # re =~ self 210 #end 251 211 252 212 ## 253 213 # ISO 15.2.10.5.27 254 def match(re, &block) 255 if re.respond_to? :to_str 256 if Object.const_defined?(:Regexp) 257 r = Regexp.new(re) 258 r.match(self, &block) 259 else 260 raise NotImplementedError, "String#match needs Regexp class" 261 end 262 else 263 re.match(self, &block) 264 end 265 end 214 #def match(re, &block) 215 # re.match(self, &block) 216 #end 266 217 end 267 268 ##269 # String is comparable270 #271 # ISO 15.2.10.3272 module Comparable; end273 class String274 include Comparable275 end
Note:
See TracChangeset
for help on using the changeset viewer.