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
Code Bug has me Stumped
Re: Code Bug has me Stumped
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
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 authorhharris
- Posts: 79
- Joined: 23.02.2006
- With us: 18 years 9 months
- Location: Pasadena, CA 91104
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
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
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 authorhharris
- Posts: 79
- Joined: 23.02.2006
- With us: 18 years 9 months
- Location: Pasadena, CA 91104
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
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
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