source: asp3_wo_tecs/trunk/cfg/common/StrVal.rb@ 304

Last change on this file since 304 was 304, checked in by ertl-honda, 7 years ago

コンフィギュレータをruby版に変更

File size: 9.9 KB
Line 
1#!ruby -Ku
2#
3# TOPPERS Configurator by Ruby
4#
5# Copyright (C) 2015,2016 by Embedded and Real-Time Systems Laboratory
6# Graduate School of Information Science, Nagoya Univ., JAPAN
7# Copyright (C) 2015 by FUJI SOFT INCORPORATED, JAPAN
8#
9# 上記著作権者
10は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
11# ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
12# 変・再é…
13å¸ƒï¼ˆä»¥ä¸‹ï¼Œåˆ©ç”¨ã¨å‘¼ã¶ï¼‰ã™ã‚‹ã“とを無償で許諾する.
14# (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15# 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16# スコード中に含まれていること.
17# (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18# 用できる形で再é…
19å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
20å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
21# 者
22マニュアルなど)に,上記の著作権表示,この利用条件および下記
23# の無保証規定を掲載すること.
24# (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
25# 用できない形で再é…
26å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
27# と.
28# (a) 再é…
29å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
30マニュアルなど)に,上記の著
31# 作権表示,この利用条件および下記の無保証規定を掲載すること.
32# (b) 再é…
33å¸ƒã®å½¢æ…
34‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
35# 報告すること.
36# (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
37# 害からも,上記著作権者
38およびTOPPERSプロジェクトをå…
39è²¬ã™ã‚‹ã“と.
40# また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
41# 由に基づく請求からも,上記著作権者
42およびTOPPERSプロジェクトを
43# å…
44è²¬ã™ã‚‹ã“と.
45#
46# 本ソフトウェアは,無保証で提供されているものである.上記著作権者
47お
48# よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
49# に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
50# アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
51# の責任を負わない.
52#
53# $Id: StrVal.rb 25 2016-01-29 16:48:00Z ertl-hiro $
54#
55
56######################################################################
57# 文字列+数値クラス定義
58######################################################################
59class StrVal < String
60 def initialize(sStr, nVal = (sStr =~ /^[0-9]+$/) ? sStr.to_i() : nil)
61 @nVal = nVal
62 super(sStr)
63 end
64
65 # 数値情
66報を返す
67 def val()
68 return @nVal
69 end
70
71 # イレギュラーな使用時のエラー
72 def unexpected_use(other, sExp)
73 if (!@nVal.nil?)
74 if (other.is_a?(StrVal) && other.val.nil?)
75 error_exit("`#{other}' don't have value", caller[1])
76 else
77 error_exit("`#{sExp}' can't use with `#{other.class}'", caller[1])
78 end
79 else
80 error_exit("`#{self}' don't have value", caller[1])
81 end
82 end
83
84 # 演算子オーバーライド(tfと同等の機能を実現)
85 alias_method :plus, :+
86 alias_method :asterisk, :*
87 alias_method :gt, :>
88 alias_method :lt, :<
89 alias_method :ge, :>=
90 alias_method :le, :<=
91 alias_method :eq, :==
92 alias_method :ne, :!=
93 alias_method :ls, :<<
94 def +(other)
95 if (!@nVal.nil? && other.is_a?(Integer))
96 @nVal + other
97 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
98 @nVal + other.val
99 else
100 plus(other)
101 end
102 end
103 def -(other)
104 if (!@nVal.nil? && other.is_a?(Integer))
105 @nVal - other
106 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
107 @nVal - other.val
108 else
109 unexpected_use(other, "-")
110 end
111 end
112 def *(other)
113 if (!@nVal.nil? && other.is_a?(Integer))
114 @nVal * other
115 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
116 @nVal * other.val
117 else
118 asterisk(other)
119 end
120 end
121 def /(other)
122 if (!@nVal.nil? && other.is_a?(Integer))
123 @nVal / other
124 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
125 @nVal / other.val
126 else
127 unexpected_use(other, "/")
128 end
129 end
130 def &(other)
131 if (!@nVal.nil? && other.is_a?(Integer))
132 @nVal & other
133 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
134 @nVal & other.val
135 else
136 unexpected_use(other, "&")
137 end
138 end
139 def |(other)
140 if (!@nVal.nil? && other.is_a?(Integer))
141 @nVal | other
142 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
143 @nVal | other.val
144 else
145 unexpected_use(other, "|")
146 end
147 end
148 def >(other)
149 if (!@nVal.nil? && other.is_a?(Integer))
150 @nVal > other
151 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
152 @nVal > other.val
153 else
154 gt(other)
155 end
156 end
157 def <(other)
158 if (!@nVal.nil? && other.is_a?(Integer))
159 @nVal < other
160 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
161 @nVal < other.val
162 else
163 lt(other)
164 end
165 end
166 def >=(other)
167 if (!@nVal.nil? && other.is_a?(Integer))
168 @nVal >= other
169 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
170 @nVal >= other.val
171 else
172 ge(other)
173 end
174 end
175 def <=(other)
176 if (!@nVal.nil? && other.is_a?(Integer))
177 @nVal <= other
178 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
179 @nVal <= other.val
180 else
181 le(other)
182 end
183 end
184 def ==(other)
185 if (!@nVal.nil? && other.is_a?(Integer))
186 @nVal == other
187 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
188 @nVal == other.val
189 else
190 eq(other)
191 end
192 end
193 def !=(other)
194 if (!@nVal.nil? && other.is_a?(Integer))
195 @nVal != other
196 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
197 @nVal != other.val
198 else
199 ne(other)
200 end
201 end
202 def >>(other)
203 if (!@nVal.nil? && other.is_a?(Integer))
204 @nVal >> other
205 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
206 @nVal >> other.val
207 else
208 unexpected_use(other, ">>")
209 end
210 end
211 def <<(other)
212 if (!@nVal.nil? && other.is_a?(Integer))
213 @nVal << other
214 elsif (!@nVal.nil? && other.is_a?(StrVal) && !other.val.nil?)
215 @nVal << other.val
216 else
217 ls(other)
218 end
219 end
220 def ~@
221 if (!@nVal.nil?)
222 ~@nVal
223 else
224 unexpected_use(nil, "~")
225 end
226 end
227
228 # ppデバッグ時の表示改善
229 def pretty_print(q)
230 if (!@nVal.nil?)
231 q.text("#{self}{#{@nVal}(#{sprintf("0x%x",@nVal)})}")
232 else
233 q.text("#{self}{-}")
234 end
235 end
236end
237
238######################################################################
239# 整数クラスの演算子オーバーライド(tfと同等の機能を実現)
240######################################################################
241class Fixnum
242 alias_method :plus, :+
243 alias_method :minus, :-
244 alias_method :asterisk, :*
245 alias_method :slash, :/
246 alias_method :ampersand, :&
247 alias_method :pipe, :|
248 alias_method :gt, :>
249 alias_method :lt, :<
250 alias_method :ge, :>=
251 alias_method :le, :<=
252 alias_method :eq, :==
253 alias_method :ne, :!=
254 alias_method :rs, :>>
255 alias_method :ls, :<<
256 def +(other)
257 other.is_a?(StrVal) ? plus(other.val) : plus(other)
258 end
259 def -(other)
260 other.is_a?(StrVal) ? minus(other.val) : minus(other)
261 end
262 def *(other)
263 other.is_a?(StrVal) ? asterisk(other.val) : asterisk(other)
264 end
265 def /(other)
266 other.is_a?(StrVal) ? slash(other.val) : slash(other)
267 end
268 def &(other)
269 other.is_a?(StrVal) ? ampersand(other.val) : ampersand(other)
270 end
271 def |(other)
272 other.is_a?(StrVal) ? pipe(other.val) : pipe(other)
273 end
274 def >(other)
275 other.is_a?(StrVal) ? gt(other.val) : gt(other)
276 end
277 def <(other)
278 other.is_a?(StrVal) ? lt(other.val) : lt(other)
279 end
280 def >=(other)
281 other.is_a?(StrVal) ? ge(other.val) : ge(other)
282 end
283 def <=(other)
284 other.is_a?(StrVal) ? le(other.val) : le(other)
285 end
286 def ==(other)
287 other.is_a?(StrVal) ? eq(other.val) : eq(other)
288 end
289 def !=(other)
290 other.is_a?(StrVal) ? ne(other.val) : ne(other)
291 end
292 def >>(other)
293 other.is_a?(StrVal) ? rs(other.val) : rs(other)
294 end
295 def <<(other)
296 other.is_a?(StrVal) ? ls(other.val) : ls(other)
297 end
298end
299
300class Bignum
301 alias_method :plus, :+
302 alias_method :minus, :-
303 alias_method :asterisk, :*
304 alias_method :slash, :/
305 alias_method :ampersand, :&
306 alias_method :pipe, :|
307 alias_method :gt, :>
308 alias_method :lt, :<
309 alias_method :ge, :>=
310 alias_method :le, :<=
311 alias_method :eq, :==
312 alias_method :ne, :!=
313 alias_method :rs, :>>
314 alias_method :ls, :<<
315 def +(other)
316 other.is_a?(StrVal) ? plus(other.val) : plus(other)
317 end
318 def -(other)
319 other.is_a?(StrVal) ? minus(other.val) : minus(other)
320 end
321 def *(other)
322 other.is_a?(StrVal) ? asterisk(other.val) : asterisk(other)
323 end
324 def /(other)
325 other.is_a?(StrVal) ? slash(other.val) : slash(other)
326 end
327 def &(other)
328 other.is_a?(StrVal) ? ampersand(other.val) : ampersand(other)
329 end
330 def |(other)
331 other.is_a?(StrVal) ? pipe(other.val) : pipe(other)
332 end
333 def >(other)
334 other.is_a?(StrVal) ? gt(other.val) : gt(other)
335 end
336 def <(other)
337 other.is_a?(StrVal) ? lt(other.val) : lt(other)
338 end
339 def >=(other)
340 other.is_a?(StrVal) ? ge(other.val) : ge(other)
341 end
342 def <=(other)
343 other.is_a?(StrVal) ? le(other.val) : le(other)
344 end
345 def ==(other)
346 other.is_a?(StrVal) ? eq(other.val) : eq(other)
347 end
348 def !=(other)
349 other.is_a?(StrVal) ? ne(other.val) : ne(other)
350 end
351 def >>(other)
352 other.is_a?(StrVal) ? rs(other.val) : rs(other)
353 end
354 def <<(other)
355 other.is_a?(StrVal) ? ls(other.val) : ls(other)
356 end
357end
Note: See TracBrowser for help on using the repository browser.