Singular
https://www.singular.uni-kl.de/forum/

reconstruct function foo from string(foo)
https://www.singular.uni-kl.de/forum/viewtopic.php?f=10&t=2369
Page 1 of 1

Author:  jack [ Thu Mar 27, 2014 8:40 pm ]
Post subject:  reconstruct function foo from string(foo)

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

Author:  malex [ Fri Mar 28, 2014 11:06 am ]
Post subject:  Re: reconstruct function foo from string(foo)

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...

Author:  Guest [ Tue Apr 01, 2014 10:44 am ]
Post subject:  Re: reconstruct function foo from string(foo)

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?

Author:  malex [ Wed Apr 09, 2014 4:34 pm ]
Post subject:  Re: reconstruct function foo from string(foo)

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!

Author:  jack [ Thu Apr 10, 2014 3:45 pm ]
Post subject:  Re: reconstruct function foo from string(foo)

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

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/