sumim #4771(2007/12/12 03:16 GMT) [ Smalltalk ] Rating1/1=1.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
!OMeta methodsFor: 'calc'! num ::= <digit>+:xs => [Integer readFrom: xs readStream]! ! !OMeta methodsFor: 'calc'! mul ::= <mul>:x ( $* <num>:y => [x * y] | $/ <num>:y => [x / y] ) | <num>! ! !OMeta methodsFor: 'calc'! add ::= <add>:x ( $+ <mul>:y => [x + y] | $- <mul>:y => [x - y] ) | <mul>! ! !OMeta methodsFor: 'calc'! exp ::= <add>! ! World findATranscript: nil. #('' '+' '-' '*' '/') asDigitsToPower: 8 do: [:comb | | expStr result | expStr := '1{1}2{2}3{3}4{4}5{5}6{6}7{7}8{8}9' format: comb. result := (OMeta onTree: nil) apply: #exp withArguments: expStr. result = 100 ifTrue: [Transcript cr; show: expStr]]
Rating1/1=1.00-0+
[ reply ]
sumim
#4771()
[
Smalltalk
]
Rating1/1=1.00
eval(Compiler class>>#evaluate:)を使いたかったのですが、Smalltalk には四則演算で乗除の優先がないので断念(^_^;)。Squeak OMeta でパーサー&インタープリタを書いて、それに eval 相当の仕事を肩代わりさせています。
!OMeta methodsFor: 'calc'! num ::= <digit>+:xs => [Integer readFrom: xs readStream]! ! !OMeta methodsFor: 'calc'! mul ::= <mul>:x ( $* <num>:y => [x * y] | $/ <num>:y => [x / y] ) | <num>! ! !OMeta methodsFor: 'calc'! add ::= <add>:x ( $+ <mul>:y => [x + y] | $- <mul>:y => [x - y] ) | <mul>! ! !OMeta methodsFor: 'calc'! exp ::= <add>! ! World findATranscript: nil. #('' '+' '-' '*' '/') asDigitsToPower: 8 do: [:comb | | expStr result | expStr := '1{1}2{2}3{3}4{4}5{5}6{6}7{7}8{8}9' format: comb. result := (OMeta onTree: nil) apply: #exp withArguments: expStr. result = 100 ifTrue: [Transcript cr; show: expStr]]Rating1/1=1.00-0+
[ reply ]