www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Extract code of function

reply Andrey <saasecondbox yandex.ru> writes:
Hello,
Is it possible to extract code of some function into string 
variable using CT reflextion?
For example:
 int test(bool flag)
 {
     return flag ? 100 : getRandom();
 }

 enum string code = GetFunctionCode!test; // "return flag ? 100 
 : getRandom();"
May 26 2019
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2019-05-26 19:46, Andrey wrote:
 Hello,
 Is it possible to extract code of some function into string variable 
 using CT reflextion?
 For example:
 int test(bool flag)
 {
     return flag ? 100 : getRandom();
 }

 enum string code = GetFunctionCode!test; // "return flag ? 100 : 
 getRandom();"
No, that's not possible. -- /Jacob Carlborg
May 26 2019
parent reply Dennis <dkorpel gmail.com> writes:
On Sunday, 26 May 2019 at 18:14:23 UTC, Jacob Carlborg wrote:
 No, that's not possible.
Some hacky solutions are possible by importing a source file as a string and parsing it manually. dglsl actually extracts D function code to put into glsl shaders. Here's the snippet. See: https://github.com/icecocoa6/dglsl/blob/master/source/dglsl/translator.d#L140
May 26 2019
parent Andrey <saasecondbox yandex.ru> writes:
On Sunday, 26 May 2019 at 18:21:23 UTC, Dennis wrote:
 On Sunday, 26 May 2019 at 18:14:23 UTC, Jacob Carlborg wrote:
 No, that's not possible.
Some hacky solutions are possible by importing a source file as a string and parsing it manually. dglsl actually extracts D function code to put into glsl shaders. Here's the snippet. See: https://github.com/icecocoa6/dglsl/blob/master/source/dglsl/translator.d#L140
Interesting solution... Thanks for a hint.
May 26 2019
prev sibling parent Aphex <Aphex mail.com> writes:
On Sunday, 26 May 2019 at 17:46:35 UTC, Andrey wrote:
 Hello,
 Is it possible to extract code of some function into string 
 variable using CT reflextion?
 For example:
 int test(bool flag)
 {
     return flag ? 100 : getRandom();
 }

 enum string code = GetFunctionCode!test; // "return flag ? 100 
 : getRandom();"
You can hack this by using import(filename) and importing the file. If you had a good D parser you could potentially do this safely. You must use the -J switch though.
May 26 2019