Talk:Runtime evaluation: Difference between revisions

Line 7:
:If you want to do more, you can create the page [[Eval/Tcl]] and link to it under the header on the main task page. This has been done before, but before we had support for slashes [[Polymorphism#BASIC]]. --[[User:Mwn3d|Mwn3d]] 20:54, 3 June 2009 (UTC)
::More work than I feel like tonight. :-) I'll focus on just making sure that the task itself is done for now; the fancy stuff is probably better done as another (probably pretty major) task. —[[User:Dkf|Donal Fellows]] 23:09, 3 June 2009 (UTC)
 
== Joke solution for C ==
 
Requires gdb; eval expressions as you'd type in gdb prompt; code must be compiled with -g flag and without -O.
<lang C>#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <err.h>
 
char spid[100], pname[1024];
 
#define EVAL(a) call_gdb(a, __LINE__ + 1)
void call_gdb(char * command, int lineno)
{
if (fork()) {
while (1);
return;
}
 
FILE *fp = fopen("cmds", "w");
fprintf(fp, "attach %s\nb %d\nret\n%s\n", spid, lineno, command);
fclose(fp);
 
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
 
execlp("gdb", pname, spid, "-x", "cmds", "-batch", "-q", 0);
err(1, "Exec failure %s %s", pname, spid);
}
 
int main(int argc, char **argv)
{
int a = 0, b = 1, c = 2;
sprintf(spid, "%ld", (unsigned long) getpid());
sprintf(pname, "%s", argv[0]);
 
printf("before eval: a = %d\n", a);
EVAL("set variable a = b + c");
printf("after eval: a = %d\n", a);
 
wait(0);
return 0;
}</lang>
Anonymous user