Compiler and duplicate variable statement (POL 097 RC5)

Report core bugs regarding the Ultima Online Emulator Core release (version 097). You can attach your Core Dump. One bug per post.
Locked
User avatar
ncrsn
Grandmaster Poster
Posts: 255
Joined: Fri Feb 10, 2006 12:15 am

Compiler and duplicate variable statement (POL 097 RC5)

Post by ncrsn »

Example code to make the problem clear.

Code: Select all

// Yarr, eternal loop ahead!
var thing := 0;
	
while (thing == 0)

    var thing := 1; // Notice the var.
    print(thing); // Output: 1

endwhile
	
print(thing); // Output: 0
The bug is that compiler compiles this script though two variables share the same name. This is little yet annoying feature. Sometimes script changes its form during development and one var in a wrong place really messes things up.

EScript Compiler's version is 1.08.
User avatar
OldnGrey
POL Expert
Posts: 657
Joined: Sat Feb 04, 2006 6:26 pm

Post by OldnGrey »

Don't you get a compiler warning?
User avatar
ncrsn
Grandmaster Poster
Posts: 255
Joined: Fri Feb 10, 2006 12:15 am

Post by ncrsn »

ecompile.cfg

Code: Select all

DisplayWarnings   1
Many other warnings I do get, but code in the first post doesn't trigger one.

Have I overlooked something?
User avatar
OldnGrey
POL Expert
Posts: 657
Joined: Sat Feb 04, 2006 6:26 pm

Post by OldnGrey »

You are absolutely right. No warning for trying to declare a variable a second time.

This gives a warning:

Code: Select all

var thing := 0; 
program textcmd_thingy()
	while ( thing == 0 )
    		var thing := 1;
    		print(thing);
	endwhile 
	print(thing);
endprogram
Changing to if ... endif made no difference.
Changing the variable name to something else made no difference.

I think I need to be very careful with my variables from now on. I did rely on the warnings until now.

I imagine you aren't the first one to find this though.
Last edited by OldnGrey on Mon Sep 17, 2007 6:09 pm, edited 1 time in total.
User avatar
ncrsn
Grandmaster Poster
Posts: 255
Joined: Fri Feb 10, 2006 12:15 am

Post by ncrsn »

Thanks for testing.
OldnGrey wrote:I imagine you aren't the first one to find this though.
I think so too. May be one of those bugs some are aware of but no-one reports.
User avatar
MontuZ
Forum Regular
Posts: 338
Joined: Fri Feb 10, 2006 8:08 am

Post by MontuZ »

I don't think this is a bug and I really hope it's not because I've done a lot of code with duplicate vars, each duplicate var with it's own purpose of course, so they're not really duplicate.

You'll get an compiling error if you do something like this;

Code: Select all

program TextCMD(mobile)
    var x := 1;

    while ( x <= 1 )
        var y := 2;
        x += 1;
        y += 1;
    endwhile

    print(y); // You'll get the error here,

endprogram
Because the variable y is reserved for the while() statement. And it's the same reason why the compiler doesn't throw a warning when you use two vars with the same name as long as one is inside a statement.

I wish I could explain that a little better.
User avatar
ncrsn
Grandmaster Poster
Posts: 255
Joined: Fri Feb 10, 2006 12:15 am

Post by ncrsn »

I don't understand why you would want to use same variable name twice. Is it not confusing? Bad coding style, anyone?

Code: Select all

program use_item(who, item)

   while (who.connected)
       var item := Target(who);
       // Do stuff with targeted item
   endwhile
   
   // Do stuff with used item.
   return 1;
endprogram
I bet reader gets confused after the while-loop, if one would code something like that. Oh, what was that item again?. Now, at least I would be. Of course that script is a rude example, using i or holder or something like that in other purposes wouldn't be that bad. Yet, isn't that still unnecessary - we do have so many letters and words to use instead?
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

It's almost like the compiler sees the 'while' loop as a seperate function apart from the main program atleast when dealing with new variable declarations anyway.

Though I can see why this might be appealing as a "feature" I tend to agree with ncrsn that this could get confusing when trying to understand and debug someone else's code. It almost reminds me of my assembly language days when we used to write self-modifying code. It made for some neat tricks but boy could it create some nasty issues when it came to debugging.

I have always understood variable declarations to require unique names within a function or procedure but then again POL has some strange quirks with variable declarations and the various 'for' statement types too. The basic 'for' statement requires one to declare a variable using the 'var' statement and yet 'foreach' does not for the first variable in the statement.

Ex. foreach blah in speech
The variable "blah" does not require a seperate 'var' statement.

So to find another quirk in POL is not too surprising.

Maybe the developers can enlighten us more on this one.
User avatar
OldnGrey
POL Expert
Posts: 657
Joined: Sat Feb 04, 2006 6:26 pm

Post by OldnGrey »

It's not just in while loops.
I declared the variable inside and if ... endif block with no error or warning.
User avatar
MontuZ
Forum Regular
Posts: 338
Joined: Fri Feb 10, 2006 8:08 am

Post by MontuZ »

It's not only the fact that we're using the same variable name twice. It's the fact that any variable used inside of a if, for, foreach, while, do, repeat, etc can't be used outside of the statement. It doesn't matter if it does have the same name, it only exists inside of one of those statements and if the reader gets confused then he's just not reading the code right. Which is exactly how I learned about it, lol.
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

Is this "feature" mentioned or documented in the eScript "manual" or is this a standard feature in most programming languages? If not then, forgive me MontuZ, but I would take issue with your statement that the reader [of the code] is to blame if he doesn't know this implicitly.

Let me confess that I am ignorant of C and some of the other languages so perhaps this is some standard. I am afraid that I am only familiar with Pascal, BASIC and assembler (old 8 bit machines I am afraid) and therefore may just have missed this as something taken for granted. I don't recall Pascal allowing this type of thing but then again Pascal is a very tight and structured language. I think that's why I like it so well. It forces me to be neat, which if you knew me, you'd know is NOT one of my strong points.
*smiles*

Anyway, it is good to know this exists. I for one shall refrain from taking advantage of it just because, in my opinion, it could be confusing.
Locked