Code Bug has me Stumped

All about writing scripts for Celestia in Lua and the .cel system
Topic author
hharris
Posts: 79
Joined: 23.02.2006
With us: 18 years 9 months
Location: Pasadena, CA 91104

Code Bug has me Stumped

Post #1by hharris » 24.03.2006, 05:06

I am programming under Mac OS X. I've had a few weeks eperience with Lua/Celx now and I've noticed that errors that are not caught at interpretation time can result in strange behavior. Rather than resulting in a crash as most programming languages would have it, the code simply exits the current routine and continues. Mostly I can handle this, but the following has me stumped:

twopi = 2*math.pi
function GetOrbitalPeriod(mu, SMA)
local period = 0
local r

r = SMA*1000 --[[ convert to meters ]]
period = twopi*((r^3)/mu )^(.5)

return period
end

Celestia simply exits the current subroutine when it gets to this function. The value of mu and SMA are correct in my testing.

Usually when this happens I find that either there is an undefined variable or there is an illegal statement, but this has me stumped.

Anybody have an idea what I'm missing?

Henry

amaury
Posts: 32
Joined: 05.03.2006
With us: 18 years 8 months
Location: paris

Post #2by amaury » 24.03.2006, 09:51

did you try the console or celestia:print/flash commands to debug step by step? i would check if the subroutine exit happens before or after the period is returned.

not sure if this is of any help, maybe you did it already

amaury

Topic author
hharris
Posts: 79
Joined: 23.02.2006
With us: 18 years 9 months
Location: Pasadena, CA 91104

Post #3by hharris » 25.03.2006, 08:28

Yes I have done this. The problem occurs exaclty when the program attempts the square root x^(.5.) where x = 650101.6413. There must be something else that is damaging the square root routine but I don't know what that could be.

Henry

hank
Developer
Posts: 645
Joined: 03.02.2002
With us: 22 years 9 months
Location: Seattle, WA USA

Re: Code Bug has me Stumped

Post #4by hank » 25.03.2006, 15:48

hharris wrote:Celestia simply exits the current subroutine when it gets to this function. The value of mu and SMA are correct in my testing.

Usually when this happens I find that either there is an undefined variable or there is an illegal statement, but this has me stumped.

Anybody have an idea what I'm missing?

Henry


It works for me.

- Hank

hank
Developer
Posts: 645
Joined: 03.02.2002
With us: 22 years 9 months
Location: Seattle, WA USA

Post #5by hank » 25.03.2006, 15:55

hharris wrote:Yes I have done this. The problem occurs exaclty when the program attempts the square root x^(.5.) where x = 650101.6413. There must be something else that is damaging the square root routine but I don't know what that could be.

Henry

Is the typo in this message also in your code?

- Hank

Topic author
hharris
Posts: 79
Joined: 23.02.2006
With us: 18 years 9 months
Location: Pasadena, CA 91104

Post #6by hharris » 25.03.2006, 19:10

If only it was.... No, and I've tested this independently in little test scripts and it works fine (without the typo). My theory is that something before this is wrong.

Is there a formal description of Lua/Celx syntax available? I'm not talking about summaries and tutorials which I have read. I'm mainly woking by expermentation here and that's probably the root problem. For example I've discovered that the following is okay:

local x = 1

or

local x, y

But not

local x = 1, y = 2

which is counter intuitive, at least to me.

Henry

hank
Developer
Posts: 645
Joined: 03.02.2002
With us: 22 years 9 months
Location: Seattle, WA USA

Post #7by hank » 26.03.2006, 00:45

hharris wrote:Is there a formal description of Lua/Celx syntax available?

Lua 5.0 is fully described in the Lua 5.0 Reference Manual. There's also an online version.

Lua is a small language. Here's the complete syntax specification:

Code: Select all

Here is the complete syntax of Lua in extended BNF. It does not describe operator priorities or some syntactical restrictions, such as return and break statements can only appear as the last statement of a block.

        chunk ::= {stat [`;??]}

        block ::= chunk

        stat ::=  varlist1 `=?? explist1 |
                     functioncall |
                     do block end |
                     while exp do block end |
                     repeat block until exp |
                     if exp then block {elseif exp then block} [else block] end |
                     return [explist1] |
                     break |
                     for Name `=?? exp `,?? exp [`,?? exp] do block end |
                     for Name {`,?? Name} in explist1 do block end |
                     function funcname funcbody |
                     local function Name funcbody |
                     local namelist [init]

        funcname ::= Name {`.?? Name} [`:?? Name]

        varlist1 ::= var {`,?? var}

        var ::=  Name | prefixexp `[?? exp `]?? | prefixexp `.?? Name

        namelist ::= Name {`,?? Name}

        init ::= `=?? explist1

        explist1 ::= {exp `,??} exp

        exp ::=  nil | false | true | Number | Literal | function | prefixexp | tableconstructor | exp binop exp | unop exp

        prefixexp ::= var | functioncall | `(?? exp `)??

        functioncall ::=  prefixexp args | prefixexp `:?? Name args

        args ::=  `(?? [explist1] `)?? | tableconstructor | Literal

        function ::= function funcbody

        funcbody ::= `(?? [parlist1] `)?? block end

        parlist1 ::=  Name {`,?? Name} [`,?? `...??] | `...??

        tableconstructor ::= `{?? [fieldlist] `}??

        fieldlist ::= field {fieldsep field} [fieldsep]

        field ::= `[?? exp `]?? `=?? exp | name `=?? exp | exp

        fieldsep ::= `,?? | `;??

        binop ::= `+?? | `-?? | `*?? | `/?? | `^?? | `..?? | `<?? | `<=?? | `>?? | `>=?? | `==?? | `~=?? | and | or

        unop ::= `-?? | not

- Hank

Topic author
hharris
Posts: 79
Joined: 23.02.2006
With us: 18 years 9 months
Location: Pasadena, CA 91104

Post #8by hharris » 26.03.2006, 01:43

Thanks. I've just spent the last four hours studying the reference manal as you recommended ?€” very powerful for a small language. I image that non-Celestia implementations also include compile (interpret?) time error reporting.

I suspect that some of my problems are not syntax, but are caused by collisons between commands that are executed at various times. My script is multimode state-based (thrusting, preparing to go to orbit, orbit ops etc) and errors in scheduling can cause inadvertent overlaps causing the program to exit.

Henry

hank
Developer
Posts: 645
Joined: 03.02.2002
With us: 22 years 9 months
Location: Seattle, WA USA

Post #9by hank » 26.03.2006, 02:07

hharris wrote:I suspect that some of my problems are not syntax, but are caused by collisons between commands that are executed at various times. My script is multimode state-based (thrusting, preparing to go to orbit, orbit ops etc) and errors in scheduling can cause inadvertent overlaps causing the program to exit.

You might try using protected function calls to see if you can catch the error that causes your script to exit.

- Hank

Topic author
hharris
Posts: 79
Joined: 23.02.2006
With us: 18 years 9 months
Location: Pasadena, CA 91104

Post #10by hharris » 26.03.2006, 04:13

Good idea! Thanks.

Henry


Return to “Scripting”