Read-only forum archive

reconstruct function foo from string(foo)

reconstruct function foo from string(foo)

jack · Thu Mar 27, 2014 8:40 pm

Hello,


is it possible to reconstruct a function from a string?

Code:
proc foo( int param )
{
    return(0);
}
string fooStr = string(foo);
fooStr;
// parameter int param ;
//
//     return(0);
//
//
// ;return();


if yes, is it reliable?

Thanks,

Jack

Re: reconstruct function foo from string(foo)

malex · Fri Mar 28, 2014 11:06 am

It should be possible:

Code:
> proc a()
. { return (-666); };
> a();
-666
> string (a);

return (-666);

;return();
> proc A = string (a);
> A();
-666


you can also take a look at dump and getdump, which can store procedures in a text file...

Re: reconstruct function foo from string(foo)

Guest · Tue Apr 01, 2014 10:44 am

Hello,

I also would like to compose a command to define a function as below:
Code:
proc foo ()
{
   string bar = "sdfs";
   return (0);
}
foo;

string fooStr = string(foo);
fooStr;

string cmd = "proc bar( )
{
    proc rfoo=\""+fooStr+"\";
    return (rfoo());
}";

cmd;
execute (cmd);

bar();

Now it is probably mandatory to escape 'fooStr';
How should this be done?

Re: reconstruct function foo from string(foo)

malex · Wed Apr 09, 2014 4:34 pm

Your problem can be solved by global search&replace of `"` with `\"` inside `fooStr` before you construct cmd:

Code:
> string fooStr2 = string(fooStr[1..18]) + "\\" + string(fooStr[19..23]) + "\\" + string(fooStr[24..size(fooStr)]);
> fooStr2;


   string bar = \"sdfs\";
   return (0);


;return();

> string cmd = "proc bar( )
. { proc rfoo=\""+fooStr2 +"\";  return (rfoo());
. }"; cmd;

proc bar( )
{ proc rfoo="

   string bar = \"sdfs\";
   return (0);


;return();

";  return (rfoo());
}
> execute (cmd); bar();
0



But since generating a procedure via execute can be very troublesome i would not recommend any of such string manipulations in the Singular interpreter!

Re: reconstruct function foo from string(foo)

jack · Thu Apr 10, 2014 3:45 pm

it seems that escaping quotes does not help when applying function construction from string iteratively:
we also have to escape backslashes:

Code:
LIB("findifs.lib");


proc foo ()
{
   string bar = "sdfs";
   return (0);
}
foo;

string fooStr = string(foo);
fooStr;


def fooStrMod = replace(fooStr,"\"","\\\"");

string cmd = "proc bar( )
{
    proc rfoo=\""+fooStrMod+"\";
    return (rfoo());
}";

cmd;
execute (cmd);

bar();


string barStr = string(bar);
barStr;



def barStrMod = replace(barStr,"\\","\\\\");

def barStrMod2 = replace(barStrMod,"\"","\\\"");

cmd = "proc foobar( )
{
    proc rbar=\""+barStrMod2+"\";
    return (rbar()+2);
}";

cmd;
execute (cmd);

foobar();



does the principle of dump differ?

If this isn't gonna work in a generic way (without writing a parser for the Singular language),
the closest approach to compose generator functions without parameters from other functions
would be to introduce intermediate global functions (randomly named):

github.com/Singular/Sources/issues/558