ID:106048
 
BYOND Version:479
Operating System:Windows XP Pro
Web Browser:Firefox 3.6.13
Applies to:DM Language
Status: Deferred

This issue may be low priority or very difficult to fix, and has been put on the back burner for the time being.
Descriptive Problem Summary:

See this post: http://www.byond.com/developer/forum/?id=768946

In short, attempts to use #define'd macros in variable declarations is failing, producing an error that makes it look like this:

#define test list
var/test/a


is expanding to this:

var/test /a


Code Snippet (if applicable) to Reproduce Problem:
#define test list

var/test/a // Syntax error
var/list /a // Same syntax error
var/list/a // Works, of course

var/test // Works
b
var/list // Note space on end. Works.
c


This may be expected behaviour - I'm seeking a clarification.

Expected Results:

#define'd macros should not have a space appended on the end - var/test/a should compile.

Actual Results:

Syntax error.

Does the problem occur:
Every time? Or how often?
Yes.
In other user accounts?
Yes, a forum thread by Ripiz brought the problem up.
On other computers?
Yes, a forum thread by Ripiz brought the problem up.

When does the problem NOT occur?
#define test list

var/test
a


works, which leads me to suspect the trailing space.

Did the problem NOT occur in any earlier versions? If so, what was the last version that worked? (Visit http://www.byond.com/download/build to download old versions for testing.)

Haven't checked, I'm afraid. But my guess is that you haven't touched the preprocessor for a while...

Workarounds:

Use of the token-pasting operator, as follows:

#define test(x) list/##x

var/test(a)


Looks a bit ugly, though, rather than the syntactical mimicry that was originally aimed for.

Appending the newline (var/test\na) isn't always possible - function argument lists don't accept a newline in the appropriate spot.
Strictly speaking I don't think this is appending a space--I think it's just not replacing in a manner that allows certain tokens to be used immediately afterward. It's well known to 4Kers for instance that you can't sub U for usr and then use U.name or something like that. Changing the #define directive's behavior is tricky though so this will have to go on the back burner.
defined.dm
//using #define to reproduce bug
#define Vector list
#define Me usr

mob/verb/try()
var/list/test = Vector(1, 2)
var/list/test2 = Vector (1, 2)
var/Vector/direction[2]
Me.name = direction[1]

/*
defined.dm:8:error: list: undefined var
defined.dm:8:error: direction: undefined var
defined.dm:9:error: .name: undefined type path
defined.dm:9:error: direction: undefined var
defined.dm:9:warning: usr: unused label
defined.dm:6:warning: test: variable defined but not used
defined.dm:7:warning: test2: variable defined but not used
defined.dm:8:warning: /: variable defined but not used
*/


undefined.dm
//replaced defined macros with their definition + space (our assumed bug)
//extra spacing forces line numbers to match


mob/verb/try()
var/list/test = list (1, 2)
var/list/test2 = list (1, 2)
var/list /direction[2]
usr .name = direction[1]

/*
undefined.dm:8:error: list: undefined var
undefined.dm:8:error: direction: undefined var
undefined.dm:9:error: .name: undefined type path
undefined.dm:9:error: direction: undefined var
undefined.dm:9:warning: usr: unused label
undefined.dm:6:warning: test: variable defined but not used
undefined.dm:7:warning: test2: variable defined but not used
undefined.dm:8:warning: /: variable defined but not used
*/



This demo produces exactly same errors. I'm pretty sure it's problem with that extra space in the end.
I even included your mentioned "#define U usr", and it matches perfectly too.
Lummox's point is more that it's the parser starting a new token, not that there's a literal space at te end inserted. Given that he's got access to the code, I'm inclined to believe him.

It probably behaves rather like there's a space at the end, though, so you can think of it like that if it makes things easier.

Ripiz wrote:
...
This demo produces exactly same errors. I'm pretty sure it's problem with that extra space in the end.
I even included your mentioned "#define U usr", and it matches perfectly too.