lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


--- Timon Christl <timonchristl@gmail.com> wrote:

> On 6/11/07, Steve Heller <steve@steveheller.com>
> wrote:
> > I'm trying to do machine translation from an
> extremely
> > "loose" language to Lua. What I mean by "loose" is
> > that this language doesn't even have run-time
> typing.
> > You can compare strings and numbers and it
> converts
> > automatically.
> >
> > I need to find all the occurrences of these type
> > mismatches so my Lua translation will work
> correctly.
> > Order comparisons like '<' and '>=' will throw a
> > runtime error, so I can catch them and fix the
> code.
> > However, '==' and '~=' always produce false and
> true
> > respectively for values that don't have the same
> type,
> > so I won't know that there is a type mismatch.
> >
> > Does anyone have any suggestions as to how to
> catch
> > these errors?
> 
> My first idea would be to try creating a metatable
> with an entry for
> eq and attaching it to a number value. According to
> the manual all
> number values share a single common metatable. The
> same applies to
> strings. I don't know it this really works, but if
> it does it would be
> everything that is needed. Note that the manual says
> the metatable for
> non-table values can only be changed in C code.
> 
> Hope this helps

Thanks. Sorry, I should have mentioned that I knew
about the metatable for numbers. However, I'm using
the lua.exe program to execute my lua code, not
embedding it in another C program, and I don't have
any C functions called by the Lua code. How should I
add the metatable setting code in that circumstance?