Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: reconstruct function foo from string(foo)
PostPosted: 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


Report this post
Top
  
Reply with quote  
 Post subject: Re: reconstruct function foo from string(foo)
PostPosted: Fri Mar 28, 2014 11:06 am 

Joined: Tue Jun 23, 2009 10:33 pm
Posts: 51
Location: Kaiserslautern
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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: reconstruct function foo from string(foo)
PostPosted: 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?


Report this post
Top
  
Reply with quote  
 Post subject: Re: reconstruct function foo from string(foo)
PostPosted: Wed Apr 09, 2014 4:34 pm 

Joined: Tue Jun 23, 2009 10:33 pm
Posts: 51
Location: Kaiserslautern
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: reconstruct function foo from string(foo)
PostPosted: 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


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

It is currently Fri May 13, 2022 10:58 am
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group