slang-users mailing list

[2019 Date Index] [2019 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Limitations of eval()?


Am 04.11.2019 um 01:25 schrieb John E. Davis:
Bernd Eggink <bernd.eggink@xxxxxxxxxx> wrote:
I noticed that, while this works:

slsh> s = "print(\"foo\")"; eval(s);

"foo"

this doesn't:

slsh> s = "print(\"foo\nbar\")"; eval(s);

***string***:1: Expecting a quote-character: found '??'

It seems that eval() doesn't like statements with a line break in it,
but I found no such limitation mentioned in the docs.

Here is what the eval function saw:

    print("foo
    bar")

This is not a valid statement since the parsing of the string "foo..."
stopped at the newline.  You need to pass to the eval function one of
the string forms such as:

   print("foo\nbar");

   print(`foo
   bar`);

   print("foo\n\
   bar");

The above 3 forms would be coded as:

   s = "print(\"foo\\nbar\")"; eval(s);
   s = "print(`foo\nbar`)"; eval(s);
   s = "print(\"foo\\n\\\nbar\")"; eval(s);

Note that the print function will not output a physical newline
character to the terminal.  If you want that, then either use the
message function, e.g.,

  s = "message(\"foo\\nbar\")"; eval(s);

Thanks. I had already tried the backslash trick (know similar problems from shell programming), but wasn't aware of the 'message' function.

The real problem I was trying to solve is the following. I need a wrapper function which works exactly like 'printf', except that it first makes some changes to the format parameter. My first attempt was to change the corresponding stack element and then call printf() - but then printf thinks it has zero parameters and generates a stack underflow error. And as _NARGS can't be set from outside, I'm stuck.

It's not _that_ important, but I'd be interested to know if this could be solved in an elegant way, without using eval.

Kind regards,
Bernd


--
http://www.sudrala.de
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2019 date index] [2019 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]