lua-users home
lua-l archive

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


From: John Passaniti
> So here's a suggestion that will probably make everyone
> happy. Let's take a page from Forth. In Forth, there is a
> primitive word ("NUMBER") that interprets numbers. Pass
> it a string, and it spits out the numerical value of that
> string. In many Forth implementations, such primitives
> can be revectored to a new routine. That lets the user
> change the conversion of numbers if they want. (Yes, Forth
> already knows how to handle various number bases. Ignore
> that fact...)
>
> So why not do something like this for Lua?

For the runtime case, this is effectively suggesting a "convert to number"
tagmethod. Not so long ago, there was a suggestion of a "convert to boolean"
tagmethod.

I would find both of these very useful for some of the things I have been
trying recently. Is there any likelihood of something like this happening?
(I would imagine that the "convert to boolean" tagmethod would be easier to
implement, as there are relatively few places it would need to be called
(if, while statements and the like). Calling a "convert to number" tagmethod
would be harder to get right. Imagine a + b: do we call the "convert to
number" tagmethod, or the "add" tagmethod? What if a and b have different
tags, and hence different sets of tagmethods?

But I still think the ideas are worthy of some consideration...

> It would likely require some (minor) changes to the
> lexer. I would suggest that a number be defined as a
> numeric digit followed by any length of letters and
> digits. That would allow classic C-style "0xff" and
> "0377" constants, as well as more exotic Smalltalk style
> (number base followed by "r" followed by the number, as in
> 36rSMALLTALK). Even my goofy Roman numerals example would
> be possible by putting a "0" up front: 0mcmlxvii.

What about floating point? 1.23 is a valid number - is 0mcmlxvii.mcm??? What
about 1.23e4? Compare that with 0x1.ABe2 (I use lowercase 'e' to emphasise
where the ambiguity arises....)

> The real point here isn't to support exotic Smalltalk
> numbers and Roman numerals. The point is that one of the
> things that makes Lua great is the extensible semantics.
> By allowing the programmer to extend how numeric strings
> are converted, this suggestion is consistent (I believe)
> with the Lua philosophy.

Changing the compile-time behaviour like this seems to me to be going too
far. How would it work? The lexer does its job before any lua code has been
executed, so we are talking about a C-level API to insert new semantics into
the lexer/parser. Once something like this gets added, I can imagine more
similar requests (inserting code into the variable lookup process to change
the global/local lookup sequence is an obvious example, given the recent
thread).

Run-time flexibility is one thing, but compile-time flexibility is more
dangerous...

Just my few thoughts,
Paul.