| code tree old new |
code := 'sampleCode
| a |
a := ''This is a pen''.
^a'.
old := 'a'. new := 'b'.
tree := Parser new parse: code class: UndefinedObject.
(tree instVarNamed: #temporaries) do: [:each |
each name = old ifTrue: [each key: new; name: new]].
^tree decompileString "=> 'sampleCode
| b |
b := ''This is a pen''.
^ b' "
sumim
#6155()
[
Smalltalk
]
Rating1/1=1.00
Squeak Smalltalk で。
ふつう、こういうことはしないのですが、リフレクション機能を試す問題として捉え、システム組み込みのパーサに委譲するかたちで実現してみました。
| code tree old new | code := 'sampleCode | a | a := ''This is a pen''. ^a'. old := 'a'. new := 'b'. tree := Parser new parse: code class: UndefinedObject. (tree instVarNamed: #temporaries) do: [:each | each name = old ifTrue: [each key: new; name: new]]. ^tree decompileString "=> 'sampleCode | b | b := ''This is a pen''. ^ b' "Rating1/1=1.00-0+
[ reply ]