python like syntax for lua

classic Classic list List threaded Threaded
45 messages Options
123
Reply | Threaded
Open this post in threaded view
|

python like syntax for lua

Serge Semashko
It is interesting what would you think about making some changes to
lua lexer to support indentation for statements grouping like in
Python?

I think that it will make source code more readable (no need for 'end'
keyword - less text to type and easier to read the code) and Python
fans will like it. This approach also has some drawbacks, but Python
is a very popular language and people do not complain much.

Of course, this can't be called 'lua', we can find some other name for
it, maybe something like 'pylua'.


Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Mark Hamburg-4
Eeeew. Whitespace as a syntactically significant construct. Just how wide
are your tabs?

I know. The Python community likes it this way. Is there an expectation that
doing this would lure people away from Python toward Lua?

That being said, I think the license agreement allows you to create pylua if
you want to. I have my own list of syntactic enhancements to Lua I'm
thinking of exploring though I'm trying to keep them essentially compatible
with Lua proper.

Mark

on 6/13/03 11:19 AM, Serge Semashko at [hidden email] wrote:

> It is interesting what would you think about making some changes to
> lua lexer to support indentation for statements grouping like in
> Python?
> 
> I think that it will make source code more readable (no need for 'end'
> keyword - less text to type and easier to read the code) and Python
> fans will like it. This approach also has some drawbacks, but Python
> is a very popular language and people do not complain much.
> 
> Of course, this can't be called 'lua', we can find some other name for
> it, maybe something like 'pylua'.
> 


Eli
Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Eli
In reply to this post by Serge Semashko
This could very easily be done as a pre-processor written in Lua...

I personally like the Python syntax .. Python tends to be very legible. But I
really don't like the way they mix tabs and spaces. It seems to me that the
parser doesn't know until it's read a few lines of the file wether to use
spaces or tabs or both.

In Lua, it also leads to somewhat unattractive things like:

for i in obj:iterator():
	do_stuff(i)

Two colons on the same line? That mean completely different things? Ugh. Maybe
you could make "do" the beginning of a block.

Besides, one of the nice things about Lua is its size, right? With the current
syntax, you could strip all the whitespace out of a lua file and insert some
necessary semi-colons, then gzip the whole thing, resulting in a very small
source ... there's no way to do that if whitespace is significant.

But your idea is just deranged enough that I kinda like it. :)

Serge Semashko said:
> It is interesting what would you think about making some changes to
> lua lexer to support indentation for statements grouping like in
> Python?
> 
> I think that it will make source code more readable (no need for 'end'
> keyword - less text to type and easier to read the code) and Python
> fans will like it. This approach also has some drawbacks, but Python
> is a very popular language and people do not complain much.
> 
> Of course, this can't be called 'lua', we can find some other name for
> it, maybe something like 'pylua'.

Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Gustavo Niemeyer
In reply to this post by Serge Semashko
> It is interesting what would you think about making some changes to
> lua lexer to support indentation for statements grouping like in
> Python?

As a Python fan, I think it'd be a very nice experiment, and I'll
certainly test it. I won't promise you I'll use it though. :-)

-- 
Gustavo Niemeyer

http://niemeyer.net/

Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Asko Kauppi-3

The python indentation was one of the leading arguments _not_ to use Python for me.. So :) as long as you don't cause everyone into that train, it shouldn't matter.

Otherwise, getting rid of 'then' would be pleasant. Most of my lua syntax errors come from that nowadays. (i know this is an old subject - just ignore!)

-ak


Gustavo Niemeyer kirjoittaa perjantaina, 13. kesäkuuta 2003, kello 21:41:

It is interesting what would you think about making some changes to
lua lexer to support indentation for statements grouping like in
Python?

As a Python fan, I think it'd be a very nice experiment, and I'll
certainly test it. I won't promise you I'll use it though. :-)

--
Gustavo Niemeyer

http://niemeyer.net/



Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Gustavo Niemeyer
> The python indentation was one of the leading arguments _not_ to use 
> Python for me..

I've seen these arguments a thousand times, always from people who
have never tried python for more than a few minutes. But then, this
is not a good place to discuss this.

-- 
Gustavo Niemeyer
http://niemeyer.net

Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Gustavo Niemeyer
In reply to this post by Eli
> This could very easily be done as a pre-processor written in Lua...
> 
> I personally like the Python syntax .. Python tends to be very legible. But I
> really don't like the way they mix tabs and spaces. It seems to me that the
> parser doesn't know until it's read a few lines of the file wether to use
> spaces or tabs or both.

The interpreter does its best to do the right thing. You can use
as many spaces as you want, and as many tabs as you want. You can
even mix them, and also exchange them (to exchange you need 8 space
tabs).

> In Lua, it also leads to somewhat unattractive things like:
> 
> for i in obj:iterator():
> 	do_stuff(i)

Yep.. that's awful.

Another hard point will be the 0-indexed stuff, if you're planning
to implement it at all. Lua has a special meaning for 0 in many cases.

[...]
> Besides, one of the nice things about Lua is its size, right? With the
> current syntax, you could strip all the whitespace out of a lua file
> and insert some necessary semi-colons, then gzip the whole thing,
> resulting in a very small source ... there's no way to do that if
> whitespace is significant.

This is not a significant point to me, since I've never used compressed
script files (nor space-stripped ones) in any language. OTOH, you
probably agree that repeated spaces is something that compresses pretty
well. ;-))

-- 
Gustavo Niemeyer
http://niemeyer.net

Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Luiz Henrique de Figueiredo
In reply to this post by Serge Semashko
>Another hard point will be the 0-indexed stuff, if you're planning
>to implement it at all. Lua has a special meaning for 0 in many cases.

To quote myself:

 There is exactly ONE spot in the core of Lua where this is true: the
 parsing of constructors such as {10,20,30} . The convention of starting
 with 1 is only enforced by the libraries, which are not part of the language.
 http://lua-users.org/wiki/CountingFromOne

--lhf

Reply | Threaded
Open this post in threaded view
|

RE: python like syntax for lua

Nick Trout
In reply to this post by Serge Semashko
> The interpreter does its best to do the right thing. You can 
> use as many spaces as you want, and as many tabs as you want. 
> You can even mix them, and also exchange them (to exchange 
> you need 8 space tabs).

If you read the Python style guidelines by Guido you aren't supposed to
mix tabs and spaces for obvious reasons.

http://www.python.org/doc/essays/styleguide.html

If you aren't going to take any other language features, other than
indentation, it seems a little misleading to associate the project with
Python? Isn't the only thing you'll gain that you don't have to type in
do...end ? You are free to indent how you like in Lua.


Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Gustavo Niemeyer
In reply to this post by Luiz Henrique de Figueiredo
> >Another hard point will be the 0-indexed stuff, if you're planning
> >to implement it at all. Lua has a special meaning for 0 in many cases.
> 
> To quote myself:
> 
>  There is exactly ONE spot in the core of Lua where this is true: the
>  parsing of constructors such as {10,20,30} . The convention of starting
>  with 1 is only enforced by the libraries, which are not part of the language.

I remember to have had some kind of problem while building a table
using the C API. That's probably the constructor stuff you mention
above.

>  http://lua-users.org/wiki/CountingFromOne

This is an interesting document. Thanks for mentioning.

-- 
Gustavo Niemeyer
http://niemeyer.net

Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

RLak-2
In reply to this post by Serge Semashko
lhf quotes himself:

 There is exactly ONE spot in the core of Lua where this is true: the
 parsing of constructors such as {10,20,30} . The convention of starting
 with 1 is only enforced by the libraries, which are not part of the 
language.
 http://lua-users.org/wiki/CountingFromOne

Actually, the varargs syntax for function calls creates a 1-indexed table, 
which is sort of the same thing but I think it is technically a different 
"spot".

However, once you have those, the library pretty well has to follow along. 
Otherwise, for example, table.foreachi({x, y, z}, fn) is a no-op. :) 

Not that I'm complaining. I have no trouble with 1-indexing. I actually 
like it. It means that, for example, the size of a collection and the 
index of the last element of the collection are the same number (eg. 
strings, vectors created with table.insert). (But this is not a troll -- I 
know lots of folk grudgingly accept it and I don't want to get in an 
argument with anyone.)

In any event, the original post had to do with Python's *syntax*, not its 
*semantics*. Here I am prepared to take a stronger stand: I don't like 
indentation as a syntactic element. It makes it too difficult to rearrange 
code in a text editor.

By the way, speaking of indentation, I think I tried to survey the list on 
this quite a while ago, but I don't recall getting much feedback. The 
question is: how do you prefer to indent "if" statements:


--Alternative 1:
if a == 0 then
  print "a is zero"
  sign = 0
elseif a < 0 then
  print "a is negative"
  sign = -1
else
  print "a is positive"
  sign = 1
end

--Alternative 2:
if a == 0 then
    print "a is zero"
    sign = 0
  elseif a < 0 then
    print "a is negative"
    sign = -1
  else
    print "a is positive"
    sign = 1
end

--Alternative 3:
if a == 0 then
  print "a is zero"
  sign = 0
 elseif a < 0 then
  print "a is negative"
  sign = -1
 else
  print "a is positive"
  sign = 1
end

--Other alternatives ?

I'm trying to work my way through vim's indenting rules, and I was 
wondering what people thought. I used to use alternative 2 because it is 
clearer where the end and the if match. But it uses more horizontal space, 
but it seems wierd when there are no elseif/else clauses:

if a > b then
    a, b = b, a
end
while a < b do
  a = (a + b) / 2
  print(a)
end

I'm kind of drawn to alternative 3, but it means getting vim to 
co-operate. (I don't mean to start a flame-war about text editors, either. 
I happen to like vim, but it's a very personal thing.)



Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Asko Kauppi-3
In reply to this post by Nick Trout

*to throw more heat on the rocks - i'm a Finn after all..* ;)

I've lately experimented with the idea of compiling Python extension modules (that there's lots of) as Lua extensions. This is still in very infancy - it's called 'hypton' within the gluax releases - but it could just as well work.

The benefits:
- proven code from the Py community
- possibly 1-to-1 API with the Py community

To make this clear, you wouldn't need Py at all for anything. Just rip the modules you like, recompile (for lua) and off you go..

-ak



Nick Trout kirjoittaa perjantaina, 13. kesäkuuta 2003, kello 22:44:


The interpreter does its best to do the right thing. You can
use as many spaces as you want, and as many tabs as you want.
You can even mix them, and also exchange them (to exchange
you need 8 space tabs).

If you read the Python style guidelines by Guido you aren't supposed to
mix tabs and spaces for obvious reasons.

http://www.python.org/doc/essays/styleguide.html

If you aren't going to take any other language features, other than
indentation, it seems a little misleading to associate the project with
Python? Isn't the only thing you'll gain that you don't have to type in
do...end ? You are free to indent how you like in Lua.



Reply | Threaded
Open this post in threaded view
|

Re[2]: python like syntax for lua

Serge Semashko
In reply to this post by RLak-2
Friday, June 13, 2003, 11:23:41 PM, RLake wrote:

Roop> In any event, the original post had to do with Python's *syntax*, not its
Roop> *semantics*. Here I am prepared to take a stronger stand: I don't like 
Roop> indentation as a syntactic element. It makes it too difficult to rearrange 
Roop> code in a text editor.

Roop> By the way, speaking of indentation, I think I tried to survey the list on 
Roop> this quite a while ago, but I don't recall getting much feedback. The 
Roop> question is: how do you prefer to indent "if" statements:


Roop> --Alternative 1:
...

Roop> --Alternative 2:
...

Roop> --Alternative 3:
...

Roop> --Other alternatives ?

That reminds me old discussion of pascal or basic users whether to
capitalize keywords or not. Having no case sensitivity for keywords
gives more freedom to users. And for example, if you make some changes
to the language and restrict users to lowercase keywords only, many
will complain.

The same situation is here. If the language is indentation sensitive, it
restricts you to only one indentation style whether you like it or
not.

This is not so bad as it seems first, just different sources for this
language will look more alike and there will be less coding style
flamewars.


Reply | Threaded
Open this post in threaded view
|

Speaking of tables [Was: Re: python like syntax for lua]

Tuomo Valkonen-2
In reply to this post by Luiz Henrique de Figueiredo
While this may have been discussed many times in the past, speaking
of positive integer table indices, I find the 'n' field annoying. 
While it is debatable whether Lua itself should automatically keep
track 'n' (AFAIK only the table library uses it), why not at least
change the table library implementation to e.g. use a weak table to
store the information? 

local ns=setmetatable({}, {__mode='k'})

function table.getn(tab)
    if ns[tab]==nil then
        -- Calculate n
	ns[tab]=n
    end
    return ns[tab]
end

etc.

-- 
Tuomo

Reply | Threaded
Open this post in threaded view
|

Re: Speaking of tables [Was: Re: python like syntax for lua]

RLak-2
> While this may have been discussed many times in the past

Yep.

> While it is debatable whether Lua itself should automatically keep
track 'n'

It doesn't; only the table library uses it. However, there is a C
interface for people who want to interact with the table library.

> why not at least change the table library implementation
> to e.g. use a weak table to store the information?

It has been changed in version 5, exactly as you describe.
However, the old behaviour is still supported as well.

Reply | Threaded
Open this post in threaded view
|

Re[2]: python like syntax for lua

Serge Semashko
In reply to this post by Eli
Friday, June 13, 2003, 9:39:43 PM, Eli wrote:

E> This could very easily be done as a pre-processor written in Lua...
You are right, though it has to handle string literals, comments and
may have other problems.

Here is a pre-alpha pre-processor that converts lua-scripts to
python-like syntax (just removes extra 'end' keywords for now) and
back. It is just a concept test and works only for very simple scripts
(at least it works for its own source).

-------------------------------------------------------------------
-- gets indentation level
-- does not allow mixed tabs and spaces in the same source file
local ws = nil
function get_indent(l)
   local _, len, prefix = string.find(l, "^(%s*)")
   if not ws and len > 0 then
      ws = string.sub(l, 1, 1)
   end
   assert(not ws or prefix == string.rep(ws, len))
   return len
end

local indent_tbl = {}
function get_prev_indent()
   if table.getn(indent_tbl) > 0 then
      return indent_tbl[table.getn(indent_tbl)]
   else
      return 0
   end
end

-- returns the change in indentation level
function get_indent_change(l)
   local indent = get_indent(l)
   local prev_indent = get_prev_indent(l)
   if indent > prev_indent then
      table.insert(indent_tbl, indent)
      return 1
   elseif indent == prev_indent then
      return 0;
   else
      local res = 0
      while get_prev_indent() > indent do
         res = res - 1
         table.remove(indent_tbl)
      end
      return res
   end
end

function has_end_of_block_keyword(l)
   local _, _, keyword = string.find(l, "^%s*(%w+)")
   return keyword == "end" or keyword == "else" or keyword == "elseif"
end

function has_end_keyword(l)
   local _, _, keyword = string.find(l, "^%s*(%w+)")
   return keyword == "end"
end

-- removes the first keyword in the string (returns nil if there is nothing
-- left in this string)
function remove_first_keyword(l)
   local res = string.gsub(l, "^(%s*)(%w+)", "%1")
   if string.find(res, "%S") then
      return res
   else
      return nil
   end
end

if string.find(arg[1], "%.lua$") then
-- process lua sources to produce python-like syntax
   for l in io.lines(arg[1]) do
      local indent_change = get_indent_change(l)
      if indent_change < 0 then
         assert(has_end_of_block_keyword(l))
         if has_end_keyword(l) then
            assert(indent_change == -1)
            l = remove_first_keyword(l)
         end
      end
      if l then
         print(l)
      end
   end
else
-- restore lua source from preprocessed file
   for l in io.lines(arg[1]) do
      local indent_change = get_indent_change(l)
      if has_end_of_block_keyword(l) then
         indent_change = indent_change + 1
      end
      while indent_change < 0 do
         print("end")
         indent_change = indent_change + 1
      end
      print(l)
   end
   local indent_change = get_indent_change("")
   while indent_change < 0 do
      print("end")
      indent_change = indent_change + 1
   end
end
-------------------------------------------------------------------

E> I personally like the Python syntax .. Python tends to be very legible. But I
E> really don't like the way they mix tabs and spaces. It seems to me that the
E> parser doesn't know until it's read a few lines of the file wether to use
E> spaces or tabs or both.
IMHO the source must use either tabs or spaces, the mixture of tabs
and spaces must be detected as a syntax error.

E> In Lua, it also leads to somewhat unattractive things like:

E> for i in obj:iterator():
E>         do_stuff(i)

E> Two colons on the same line? That mean completely different things? Ugh. Maybe
E> you could make "do" the beginning of a block.
This is not so easy question. Just replacing 'then' and 'do' with ':'
adds ambiguety (as you have pointed). In addition, probably this will
require some changes in the parser as we replace two different tokens
with colon.
For now, I want to try to make 'end' keyword optional and see what
happens. Maybe this idea is not good at all.


Reply | Threaded
Open this post in threaded view
|

Re: Speaking of tables [Was: Re: python like syntax for lua]

Tuomo Valkonen-2
In reply to this post by RLak-2
On Fri, Jun 13, 2003 at 04:42:17PM -0500, [hidden email] wrote:
> It has been changed in version 5, exactly as you describe.
> However, the old behaviour is still supported as well.

Hmm.. I'm sure I got an extra 'n' field at some point when
iterating a table (should've been using ipairs, but wasn't),
but I can't reproduce that now. Maybe there was some other
error then.

-- 
Tuomo

Reply | Threaded
Open this post in threaded view
|

Re: Speaking of tables [Was: Re: python like syntax for lua]

Peter Shook-5
In reply to this post by RLak-2

[hidden email] wrote:
It has been changed in version 5, exactly as you describe.
However, the old behaviour is still supported as well.

Not quite exactly, but very close. Note that 'n' takes precedence over setn.

$ lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> t = { 11,22,33 }
> table.setn(t, 3)
> = table.getn(t)
3
> t.n = 2
> = table.getn(t)
2
>


Reply | Threaded
Open this post in threaded view
|

Re: Speaking of tables [Was: Re: python like syntax for lua]

RLak-2
In reply to this post by Tuomo Valkonen-2
> Not quite exactly, but very close.  Note that 'n' takes precedence over
> setn.

True.

setn will never create an "n" field which doesn't exist. getn and setn
both use an "n" field if it exists; otherwise they use the weak table.

However, varargs still set the "n" field, which is how you can end
up with one.

$ lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
 > t = { 11,22,33 }
 > table.setn(t, 3)
 > = table.getn(t)
3
 > t.n = 2
 > = table.getn(t)
2
 >


Reply | Threaded
Open this post in threaded view
|

Re: python like syntax for lua

Luiz Henrique de Figueiredo
In reply to this post by Serge Semashko
>I've lately experimented with the idea of compiling Python extension 
>modules (that there's lots of) as Lua extensions. This is still in very 
>infancy - it's called 'hypton' within the gluax releases - but it could 
>just as well work.
>
>The benefits:
>- proven code from the Py community
>- possibly 1-to-1 API with the Py community
>
>To make this clear, you wouldn't need Py at all for anything. Just rip 
>the modules you like, recompile (for lua) and off you go..

That would be very nice! You'd still need the Python core, wouldn't you?
Or do you plan to reimplement this on top of Lua?
--lhf

123