Compiling a string with escape character

This snippet seems like it should execute successfully

"[\dur, 0.25]".compile.value

but it results in an error:

ERROR: Variable 'dur' not defined.
  in interpreted text

There seems to be a conflict between the dual nature of the backslash - it’s an escape character in the context of a string and symbol notation in sclang.

Is there a good way to handle that without having to stray too far from regular sclang syntax? For example escaping the backslash would work, e.g.

"[\\dur, 0.25]".compile

but I would still like to retain typical sclang syntax in this mini-dsl i’m trying to put together.

Thanks.

You could try the interpreter’s preprocessor function, which gives you access to the code string before reaching the compiler.

http://doc.sccode.org/Classes/Interpreter.html#-preProcessor

The example here is trivial, but string manipulations can get a bit intricate. I found CollStream is a really nice way to scan through a string and be able to pass the scanner’s state into various functions.

hjh

that’s what i was intending to do but i was writing and testing the code as a regular function. I’ll see if that issue is present in the preprocessor. thanks for the CollStream suggestion. i’m not looking to do anything too fancy - i just want to see if i can slim down the typography necessary do do some simple tasks

I’ll see if that issue is present in the preprocessor.

The preprocessor shouldn’t have the same issue. It gives you access to the raw string before the compiler gets it – so that you can search for problem character sequences and massage them into acceptable SC syntax. In the case you mentioned, I believe the string going into the preprocessor will be [\dur, 0.25] – the escape sequence \d is handled by the compiler, but the preprocessor is before that – and you could use the function to change \dur into \\dur. (The user never sees the change btw.)

hjh

yeah - you’re correct - with the preprocessor the string was unprocessed so extracting a portion and compiling to sclang worked as you described