Syntax error: 1+2 without assignment

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

Syntax error: 1+2 without assignment

Pavel
Why such an expression without assignment should result in syntax error?
Even if it is meaningless for numbers, it could be a+b with metametatables
that does something else regardless the return value.




--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

Fernando Jefferson-2
In Lua 5.3 interpreter the expression "1 + 1 <enter>" works perfectly.

I even use it at the beginning of my course for beginners, "Programming
for 21st Century", at PUC-Rio, instead of the usual "print ("Hello
World")

"1 + 1" is the simplest computer program that exists.

"Print (" Hello World "), on the other hand, requires a novice
programming student to understand what a function call is, how to pass a
parameter, and know that a quoted phrase is a string.

If you want to teach Lua to someone who knows "C" or "C ++", great.

Now you try to teach programming to a lawyer, a student of literature or
medicine, as we do ...




Em 2019-10-28 13:22, Pavel escreveu:

> Why such an expression without assignment should result in syntax
> error?
> Even if it is meaningless for numbers, it could be a+b with
> metametatables
> that does something else regardless the return value.
>
>
>
>
> --
> Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

--
Fernando Jefferson
CCE-PUC-Rio - Professor
Projeto UpLua - Coordenador
Cel: +5521-99763-2135

Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

nobody
In reply to this post by Pavel
On 28/10/2019 17.22, Pavel wrote:
> Why such an expression without assignment should result in syntax
> error? Even if it is meaningless for numbers, it could be a+b with
> metametatables that does something else regardless the return value.

An expression isn't a statement.

While the Lua interpreter turns expressions into statements by
implicitly prefixing them with `return ` (which results in the printing
of the result), this doesn't really make sense within a program/script.

You could turn it manually into a statement by saying

   _ = a + b

(and potentially declaring `local _` first somewhere above.)

-----

If you were to change the syntax of Lua to allow `a + b` as a statement
(which should then evaluate the expression and discard the result),
then you'd surely have to also allow `a-b`, `a*b`, … (i.e. all
arithmetic expressions), probably even all unary/binary operators.

And then what's the meaning of

   foo = a
   "b" .. c

?

Is it still `foo = a( "b" ) .. c`?  Is it `foo = a ; "b" .. c`?
Something else?

-- nobody

--
Syntactic sugar causes cancer of the semicolon.  -- Alan Jay Perlis

Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

Coda Highland
In reply to this post by Fernando Jefferson-2


On Mon, Oct 28, 2019 at 12:57 PM Fernando Jefferson <[hidden email]> wrote:
In Lua 5.3 interpreter the expression "1 + 1 <enter>" works perfectly.

I even use it at the beginning of my course for beginners, "Programming
for 21st Century", at PUC-Rio, instead of the usual "print ("Hello
World")

"1 + 1" is the simplest computer program that exists.

"Print (" Hello World "), on the other hand, requires a novice
programming student to understand what a function call is, how to pass a
parameter, and know that a quoted phrase is a string.

If you want to teach Lua to someone who knows "C" or "C ++", great.

Now you try to teach programming to a lawyer, a student of literature or
medicine, as we do ...


This is misleading, I'm afraid. It works in the REPL not because it's a valid Lua statement, but because the REPL detects when you've entered an expression instead of a statement and shows the return value. It's as if you wrapped the expression in a print() call.

Now, as far as the original question is concerned... It's been answered THAT expressions aren't statements, but it hasn't been explained WHY. And the answer to why is simply a design decision on Lua's part. Some languages such as C have expression statements, where the return value is discarded. Lua doesn't do this, and the rationale is probably similar to why assignments are statements, not expressions as they are in C: it helps detect and avoid mistakes, and it helps keep coding styles more consistent and readable.

Of course, it's also true that Lua's parser has to figure out where statements end based on what is or isn't a legal continuation of the previous statement. Expressions as statements would complicate this further.

/s/ Adam
Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

Pavel
In reply to this post by nobody
Fernando Jefferson-2 wrote
> In Lua 5.3 interpreter the expression "1 + 1
> <enter>
> " works perfectly.

my question is not about REPL, where each line has a 'return' prepended if
interpretation is failed.

Actually i just realized that allowing such a C-like behavior would break
the present Lua interactive interpreter as it works exactly because of `1+2`
statement causes syntax error.


nobody wrote
> And then what's the meaning of
>    foo = a
>    "b" .. c
> ?
>
> Is it still `foo = a( "b" ) .. c`?  Is it `foo = a ; "b" .. c`?
> Something else?

exactly the same with a parentheses:
a = b+c
(foo())

a=b+c(foo())





--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

v
Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

v
On Tue, 2019-10-29 at 05:07 -0700, Pavel wrote:

> Fernando Jefferson-2 wrote
> > In Lua 5.3 interpreter the expression "1 + 1
> > <enter>
> > " works perfectly.
>
> my question is not about REPL, where each line has a 'return'
> prepended if
> interpretation is failed.
>
> Actually i just realized that allowing such a C-like behavior would
> break
> the present Lua interactive interpreter as it works exactly because
> of `1+2`
> statement causes syntax error.

Try running `string.char(65)` in REPL. This is a valid function call
without return, but result is still printed.

--
v <[hidden email]>


Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

Francisco Olarte
In reply to this post by Fernando Jefferson-2
Hi Fernando:

On Mon, Oct 28, 2019 at 6:57 PM Fernando Jefferson
<[hidden email]> wrote:
> "1 + 1" is the simplest computer program that exists.

what about "1" ? ( and, aamof, I used may times an empty .com file in
CP/M and msdos, it does nothing sometimes and others restart the last
loaded program ).

> Now you try to teach programming to a lawyer, a student of literature or
> medicine, as we do ...

I was taught programming at 12 years ( which was early in spain in 77
), well before school splits in science/letters ( as we call it in
spain ) and certainly well before university level.

That being said, student, ok, but medicine/lawyers, those are smarter,
but it's going to be hard, they tend to think they are smarter than
everyone else ( I have dealt with both ).


Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

Pavel
In reply to this post by v
v wrote
> Try running `string.char(65)` in REPL. This is a valid function call
> without return, but result is still printed.

sure, because REPL just prints what is left on stack and would try add
'return' if line is not loaded properly. (in case of '1+2')

question was why statements like '1+2' are syntax error (opposite to C).

but reimplementing RE[P]L in Lua to fix it is also fine

local str = ""
for s in io.lines("somefile.lua") do
  str = str .. s
  local f, err = load(str)
  if not f then f, err = load("return " .. str) end
  if f then f(); str = "" end
end




--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

Reply | Threaded
Open this post in threaded view
|

Re: Syntax error: 1+2 without assignment

Gabriel Bertilson
On Tue, Oct 29, 2019 at 7:08 AM Pavel <[hidden email]> wrote:
> my question is not about REPL, where each line has a 'return' prepended if
> interpretation is failed.

On Tue, Oct 29, 2019 at 11:24 AM Pavel <[hidden email]> wrote:
> v wrote
> > Try running `string.char(65)` in REPL. This is a valid function call
> > without return, but result is still printed.
>
> sure, because REPL just prints what is left on stack and would try add
> 'return' if line is not loaded properly. (in case of '1+2')

You have it backwards. See "loadline" in lua.c:
https://www.lua.org/source/5.3/lua.c.html#loadline. First "return " is
added to the beginning of the line ("addreturn"), then the line is
compiled on its own ("multiline"). So a function call, which can be
parsed as an expression or a statement, will be treated as an
expression and its return values will be printed.

— Gabriel

Reply | Threaded
Open this post in threaded view
|

Re: Simplest program (1+1=2), CP/M and UpLua Project

Fernando Jefferson-2
In reply to this post by Francisco Olarte
Dear Francisco

Really, "1" is the simplest program possible, if we disregard the null
program ("").

 From now on I will use your definition of simplest program.

Glad you were also a CP/M user/hacker.

At PUC-Rio we did reverse-engineering of CP/M 80, obtained a source
file, first in assembler and then in "C". And even we´ve developed a
CP/M 86, with the providential help of Roberto Ierusalimschy (my
colleague in the master's degree), who developed a genial interpreter
for the 8085 CPU, running on the Intel 8086. Mapping registradors from
one CPU to another!

But it was all in steam computing time.  :-)  :-)  :-)

Today I am managing the UpLua Project, which aims to boost the use of
Lua Language in the (but not only) areas of IoT and Artificial
Intelligence.

One of the activities of this project is to teach Lua to people outside
the computer and technical area. Our audience: doctors, biologists,
psychologists, lawyers, audio visual producers, film and theater
directors and actors, literature, human resources, etc. And also high
school students.

The focus of the course is different from the traditional one when
teaching a programming language, where it is assumed that the student
already knows another programming language, or at least a good
background in math and logic.

Our course starts with "1 + 1 = 2", which is the only prerequisite (in
addition to basic English).

Please click in this link to have more information about the course and
the testimony of some students.
https://www.slideshare.net/FernandoJefferson/ts21-up-luacoursea

I will send more information to this list about Up-Lua Project and find
out if someone could help us in this challenging project.

Regards,

Fernando Jefferson
CCE-PUC-Rio - Professor
Project UpLua - Coordinator
Cel: +5521-99763-2135

Em 2019-10-31 05:25, Francisco Olarte escreveu:

> Dear Fernando:
>
> On Thu, Oct 31, 2019 at 3:39 AM Fernando Jefferson
> <[hidden email]> wrote:
>> Really, "1" is the simplest program possible, if we disregard the null
>> program ("").
>>  From now on I will use your definition of simplest program.
>
> Ah, mathematical background showing ;-> .
>
> In fact the problem is probably defining program, and it will highly
> depend on the environment, you could always develop a repl which pops
> up space invaders, or a wordprocessor, when fed the empty string and
> interpret it as 4k basic otherwise.
>
>> Glad you were also a CP/M user/hacker.
> It was a great learning tool, one OS you could fully know.
>
>> At PUC-Rio we did reverse-engineering of CP/M 80, obtained a source
>> file, first in assembler and then in "C". And even we´ve developed a
>> CP/M 86, with the providential help of Roberto Ierusalimschy (my
>> colleague in the master's degree), who developed a genial interpreter
>> for the 8085 CPU, running on the Intel 8086. Mapping registradors from
>> one CPU to another!
>
> Folklore says MSDOS .COM support was dessigned in a way where you
> could take a CPM assembler program for the 8080, clean it up a bit,
> apply a (simple) translator, and get a working program. Not sure if
> anyone did that, in those times there was no www so it was all floppy
> passing around and keeping up with these kind of things was really
> hard.
>
> The C one was synthetic, I assume. I played mainly with cpm 3, and a
> bit of 2.2, on the amstrad CPC ( it was a really good combo, the
> things the cpc managed with a z80 where amazing, it did even have
> (floppy) disk cache. I had my own version, with patched floppy support
> for some extra k and printer support for a hardware hack I did. And
> IIRC, it was assemblear mainly. I played a bit with C there, but the
> implementations where for 8080 and not too good, so I normally used
> either turbopascal or just coded in masm ( with a huge set of macros /
> libs I accumulated, and in Z80 mode, which allowed me to use the extra
> regs as frame pointers for cleaner subroutines ).
>
>> But it was all in steam computing time.  :-)  :-)  :-)
>
> Yep. I remember then fondly, simpler times and being younger helps,
> and the brain tends to forget the bad things, but not necessarily
> better.
>
>> Today I am managing the UpLua Project, which aims to boost the use of
>> Lua Language in the (but not only) areas of IoT and Artificial
>> Intelligence.
>
> Those are two broad areas, and I see Lua can fit well in lots of
> places.
>
>> One of the activities of this project is to teach Lua to people
>> outside
>> the computer and technical area. Our audience: doctors, biologists,
>> psychologists, lawyers, audio visual producers, film and theater
>> directors and actors, literature, human resources, etc. And also high
>> school students.
>
> It's simple enough to be grasped by any motivated student with those
> cualifications.
>
>> The focus of the course is different from the traditional one when
>> teaching a programming language, where it is assumed that the student
>> already knows another programming language, or at least a good
>> background in math and logic.
>
> The major problem I've stumbled upon when teaching someone with zero
> programming has always been the concepts of variables, the a=1, b=2,
> a=3. I've found one of the things for the (Pascal? getting old ! )
> a:=1 notation was it is, in my limitted experience, much easier to
> explain. Also I've found people with some math/logic background are
> more amenable to think on decompossing problems in a way which lead to
> algorithm/program development. As I say to anyone who wants to listen,
> try to make your kids learn some of it, it "shapes" the mind in an
> useful problem solving way.
>
>
>
>> Our course starts with "1 + 1 = 2", which is the only prerequisite (in
>> addition to basic English). See attached the poster (unfortunately in
>> Portuguese) and the testimony of some students.
>
> Well, portuguese shouldn't be a problem, I'm spanish, languages are
> similar and I've previously managed to read and follow instructions
> when in Portugal.
>
> ... After reading it it seems either you found an english version to
> post or somehow google autotranslated to english the image!.
>
>> I will send more information to this list about Up-Lua Project and
>> find
>> out if someone could help us in this challenging project.
>
> We'll be waiting!
>
>
> Francisco Olarte.

--
Fernando Jefferson
CCE-PUC-Rio - Professor
Projeto UpLua - Coordenador
Cel: +5521-99763-2135

Reply | Threaded
Open this post in threaded view
|

Re: Simplest program (1+1=2), CP/M and UpLua Project

Tim Hill


On Oct 31, 2019, at 6:58 PM, Fernando Jefferson <[hidden email]> wrote:

At PUC-Rio we did reverse-engineering of CP/M 80, obtained a source file, first in assembler and then in "C". And even we´ve developed a CP/M 86, with the providential help of Roberto Ierusalimschy (my colleague in the master's degree), who developed a genial interpreter for the 8085 CPU, running on the Intel 8086. Mapping registradors from one CPU to another!

Ah good memories. I did a similar thing for a commercial computer system way back, which allowed us to move to x86 systems but still run all the 8-bit 8080 software(quite a lot at the time) on the newer machines. It really wasn’t that difficult as the x86 used a similar model for flags, so it was really a matter of decoding the 8080 instruction and then mapping it to the equivalent 8086 instruction(s). As I recall, the emulator run at about the same speed as the real hardware. The OS mapping was equally simple, as CP/M and CP/M 86 mapped 1:1 for OS calls.

Happy days .. when 256K was a LOT of RAM :)

—Tim

Reply | Threaded
Open this post in threaded view
|

Re: Simplest program (1+1=2), CP/M and UpLua Project

Fernando Jefferson-2
Really, happy days ...

Did you said 256K? Lucky you ... We started with 32K and then 64K.

My master's degree project was to develop part of a node for REDPUC, an
experimental pre-Ethernet packet network.

We've developed it all from scratch: hardware, BIOS, multiprogramming
core and OSI protocol up to level 2.

With a huge team of one person (Me) ...


Em 2019-11-02 19:16, Tim Hill escreveu:

>> On Oct 31, 2019, at 6:58 PM, Fernando Jefferson
>> <[hidden email]> wrote:
>> At PUC-Rio we did reverse-engineering of CP/M 80, obtained a source
>> file, first in assembler and then in "C". And even we´ve developed
>> a CP/M 86, with the providential help of Roberto Ierusalimschy (my
>> colleague in the master's degree), who developed a genial
>> interpreter for the 8085 CPU, running on the Intel 8086. Mapping
>> registradors from one CPU to another!
>
> Ah good memories. I did a similar thing for a commercial computer
> system way back, which allowed us to move to x86 systems but still run
> all the 8-bit 8080 software(quite a lot at the time) on the newer
> machines. It really wasn’t that difficult as the x86 used a similar
> model for flags, so it was really a matter of decoding the 8080
> instruction and then mapping it to the equivalent 8086 instruction(s).
> As I recall, the emulator run at about the same speed as the real
> hardware. The OS mapping was equally simple, as CP/M and CP/M 86
> mapped 1:1 for OS calls.
>
> Happy days .. when 256K was a LOT of RAM :)
>
> —Tim

--
Fernando Jefferson
CCE-PUC-Rio - Professor
Projeto UpLua - Coordenador
Cel: +5521-99763-2135