BUG(?) Error at compilation with static functions

Started by Monsieur OUXX, Thu 04/01/2007 20:55:31

Previous topic - Next topic

Monsieur OUXX

I'm not sure it is a bug or not, or if it has already been discorvered by someone else :

I have a code looking like this :

Code: ags


// MODULE 1 HEADER ///////////
struct A {
    import int MyFunction();
    import static int MyStaticFunction();
}

A MyA;

// MODULE 1 BODY///////////
int A::MyFunction() {
    return 0;
}

static int A::MyStaticFunction() {
    return 0;
}


// MODULE 2 HEADER //////////

struct B {
    import int MyOtherFunction();
}

// MODULE 2 BODY ////////////////

int MyArray[10];

int B::MyOtherFunction() {


    return MyArray[MyA.MyFunction()]; // CODE 1 : compiles fine



    int i = A.MyStaticFunction(); // CODE 2
    return MyArray[i];           // CODE 2 : compiles fine



    return MyArray[A.MyStaticFunction()]; // CODE 3 : doesn't compile (unknown symbol/parse error after '[')
}    


What do you think ?

 

monkey0506

Change:

Code: ags
A MyA;


In your "MODULE 1 HEADER" to:

Code: ags
import A MyA;


And in your "MODULE 1 BODY" put:

Code: ags
A MyA;
export MyA;

Monsieur OUXX

Quote from: monkey_05_06 on Thu 04/01/2007 23:51:37
move the declaration of MyA and change the way it is exported

That doesn't explain why Code2 works and Code3 doesn't work!
They are virtually the same.

(Forget about MyA and code1, they were only here as an example of something that works between [] at compilation time)
 

Gilbert

#3
Hmmm, it's odd. Just curious, did it work if the declarations of A is in the same module?

Edit: Nah, I'd tested it even not using modules and it yielded the same error, I think it's quite possibly a compiler bug or limitation, unless there're something that we overlooked.

Monsieur OUXX

Quote from: Gilbot V7000a on Fri 05/01/2007 10:10:56
even not using modules, it yielded the same error, I think it's quite possibly a compiler bug or limitation

I'm not sure, but I might have read something about this in another thread, but I can't find it back : CJ saying something about "it's done on purpose because it is a nonsense calling static functions in non-staic parts of the code" - as I said, I'm not sure at all.

If not, this means that when it builds the code's tree, CJ's grammar analyzer checks integrity of tokens between '[' ']' for non-static tokens only, but not for static tokens. Having programed a compiler, I can understand how easily one can forget cases like this.

 

Khris

I've encountered a similar thing some time ago:

It's not possible to do:
Code: ags
  String name;
  name=Hotspot.GetAtScreenXY(x, y).Name;


It has to be done like this:
Code: ags
  String name;
  Hotspot*h=Hotspot.GetAtScreenXY(x, y);
  name=h.Name;

Pumaman

Sounds similar to the issue KhrisMUC is talking about, I'll look into it.

In the meantime, you've found the workaround yourself :)

SMF spam blocked by CleanTalk