Page 1 of 1

Code Bug has me Stumped

Posted: 24.03.2006, 05:06
by hharris
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

Posted: 24.03.2006, 09:51
by amaury
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

Posted: 25.03.2006, 08:28
by hharris
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

Re: Code Bug has me Stumped

Posted: 25.03.2006, 15:48
by hank
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

Posted: 25.03.2006, 15:55
by hank
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

Posted: 25.03.2006, 19:10
by hharris
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

Posted: 26.03.2006, 00:45
by hank
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

Posted: 26.03.2006, 01:43
by hharris
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

Posted: 26.03.2006, 02:07
by hank
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

Posted: 26.03.2006, 04:13
by hharris
Good idea! Thanks.

Henry