Jump to content

What can you reference in LSL before it is declared / defined?


Love Zhaoying
 Share

You are about to reply to a thread that has been inactive for 85 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

While working on my LSL parser, I finally realized that I need to check for certain items that aren't defined yet. 

In some other languages, you always have to "declare" / "define" an identifier (or function, etc.) before it is used.  For example, in legacy C / C++, "header files" (.h) are used for this.

The reason I need to do this is, I am avoiding making a "multi-pass" parser.  (I could provide a long-winded explanation of how and why, but will only if someone asks.)

The LSL items that I identified so far which you can "reference" before they are defined (declared) are:

- Global variables

- User-defined functions

- States

Below is an example showing this. 

Please let me know of any other examples you know about.

Thanks,

Love

 



Foo1() {

// Reference a variable before it is declared
    llSay(0, "Entered Foo1(), gFoo="+(string)gFoo);
// Reference a function before it is declared
    Foo2();
    
}

integer gFoo = 1;
Foo2() {

    llSay(0, "Entered Foo2()");
    
}


default
{
    state_entry()
    {
        llSay(0, "Entered state default..");        
        llSay(0, "Calling Foo1()");        
// Test referencing a variable and function before they are declared
        Foo1();
// Reference a State before it is declared        
        state StateFoo;
    }

}


state StateFoo
{
    state_entry()
    {
        llSay(0, "Entered state StateFoo..");        
    }

}

 

Output:

Entered state default..
[01:23] JSON Parser Tester: Calling Foo1()
[01:23] JSON Parser Tester: Entered Foo1(), gFoo=1
[01:23] JSON Parser Tester: Entered Foo2()
[01:23] JSON Parser Tester: Entered state StateFoo..

 

 

Link to comment
Share on other sites

2 hours ago, Lucia Nightfire said:

States, UDFs and globals don't rely on scope, so there's no "before it is declared" involved.

Hmmm...technically, I see what you mean.

However, see my example in which I am showing States being referenced by "state switch" from State1, referencing State2 (before State2 is defined).

Same with my example for the UDF's. UDF1 is calling UDF2 before UDF2 is defined.

So in my case, it is "program scope" (global scope).

Link to comment
Share on other sites

Basically, everything defined at the global level -- variables, functions, states -- are always initialized first, no matter where they exist in the source code. The compiler just parse for everything global and create references.

By the time they are accessed within functions and event handlers, they already exist.

Edited by primerib1
  • Thanks 1
Link to comment
Share on other sites

1 minute ago, primerib1 said:

Basically, everything defined at the global level -- variables, functions, states -- are always initialized first, no matter where they exist in the source code. The compiler just parse for everything global and create references.

By the time they are accessed within functions and event handlers, they already exist.

Thanks for confirming. So, that means my approach is in line with how LSL works.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 85 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...