RiscLua 7
Steffen Huber (91) 1949 posts |
You mean Lua stores its data without a reference to a particular address in memory? Now that would be a most impressive technology :-) If I were Rick, I would dream up a datatype that must match a particular memory layout because it accesses a hardware API that needs it in that particular way.
No need to use pointer arithmetic in C for even the most complex data types. Anyway, pointer arithmetic is overrated as the “root of all evil” for many common programming errors. Try dangling pointers, off-by-one, invalid cast, failed malloc and “no or wrong free” for a start. |
Rick Murray (539) 13806 posts |
NetScape?
No idea what a Lua table is.
Pretty much every variable beyond the most basic data types is associated with an address in some manner. You might just not realise it. DIM x% 11 x%!0 = 7 x%!4 = 44 x%!8 = 9 What’s ‘x%’? Oh, that’s right. It’s a variable pointing to a bit of memory, with the ! syntax meaning “write a word at address plus offset”. C is a bit like that too, although moreso (as strings are actually arrays of bytes). I would imagine that Lua is also similar, otherwise it would fail the moment that you need to pass a pointer to something (which happens a lot in the RISC OS API).
Absolutely. I won’t argue that. However, if you take an address (you can’t get around them – parameters to a SWI? return from memory claims?) and assign it to a struct, you can then access it as elements of a structure. All the pointer magic happens behind the scenes. Don’t try to do it yourself. You’ll fail. Pointer maths is hard and extremely liable to blowing up (given what you’re dealing with). So |
GavinWraith (26) 1563 posts |
Sorry, NetSurf. Senior moment.
No. Of course it does, internally. But the Lua programmer has no way of referencing it. There are two aspects to Lua: the Lua-programmer’s and the C-programmer’s. From the C perspective Lua is just a C library to be linked with sources of some host application, so that the application is extended with an interpreter for Lua scripts. From the Lua perspective, the Lua language gives no way of referencing the hardware platform. No peeking or poking or references to anything OS- or machine-dependent.
Lua is not similar.
That is where your problem lies. A table is a pointer Because Lua is designed to be embedded it cannot presume the existence of error-handlers or this or that resource, apart from the IO that ANSI C presumes. It cannot know about directories, for example, because ANSI C does not. Every now and then there are clamours for Posix compliance, but to give in to them would narrow Lua’s universality. To be sure, Lua distributions for Unix usually offer Posix operations, just as RiscLua offers SYS calls, but these are not part of the Lua core but extras provided by separate libraries. This emphasis on security and machine-independence in Lua made it hard to marry with the many insecurities and compromises that are found everywhere in RISC OS. The earliest versions of RiscLua tried to mitigate these: for example, there were expensive bound-checks on peeking and poking buffers in memory. That policy failed because RISC OS has insufficient typing; the PRMs do not always tell you what type of value is in a particular register when an SWI returns: a Boolean, a number, a memory address, an offset, … ? In the end I swallowed my pride and made the same kludge that BASIC makes – confuse numbers with addresses. The only mutable datatype in Lua is the table. How tables are represented in memory is very interesting, quite cunning, and again not something the programmer should be concerned with. Tables themselves can be values, or keys, in other tables, and in themselves. How about In RiscLua, courtesy of the riscos library, but not in pure Lua, you can have
just as in BASIC. Then x is indeed a variable pointing at an address of a 12-byte buffer. Unlike BASIC, if you set the value block[x] to nil then that buffer becomes garbage-collectible. Knowing the numerical value of x helps not at all.
|
GavinWraith (26) 1563 posts |
More on tables here. |
Rick Murray (539) 13806 posts |
If you want to pass it to a system API, you’ll need some way of exposing the value. Having said this, why are we talking about tables and whether or not Lua can do better than C with hidden pointer magic? If it’s because of my dislike of dynamic typing, you might be a little mistaken. var = "three" print car var = 3 print var I just made that up, don’t bother to syntax check it. The point is, it’s a variable that’s a string, a real, and number, a date, or whatever else the just language supports. Which is which? Depends upon how you use it… But it can lead to weird bugs, like: ; param1 is the value "3" from the input parameters ; param2 is the value "4" likewise var = param1 + param2 ; what's var now? How that behaves is language defined. Some languages (I think PHP is one) will give that expected answer ‘7’ because it will demote to a number of it makes sense in context. Other languages (VB, I think?) will blindly add the two strings to give a string result – “34”. 1 Actually you can’t cast a string number into an integer in C, if you tried the best you’d get is the ASCII code of the first number. You need something like atoi() to explicitly change the string (array) to integer. |
Matthew Phillips (473) 719 posts |
The other way of avoiding problems about whether "4"+"7" should be “47” or 11 is to have different operators. After all, they are different operations! Perl takes this approach, with + for addition and . for concatenation. Similarly < > <= >= != and == for numeric tests, and lt gt le ge ne eq for string tests. It is much clearer than PHP and less likely to lead you into difficulties. |
Matthew Phillips (473) 719 posts |
Lua is really nice, and I agree with Gavin that it would be much better if new RISC OS programmers started learning Wimp application development using Lua rather than BASIC, as it is a much more powerful language (if you overlook the BASIC assembler, of course). There are libraries to write Toolbox and plain Wimp applications in Lua, but not enough tutorial material yet — the main advantage of BASIC is the sheer number of articles and example applications you can study. Lua is also very good as an embedded scripting language. Hilary uses it in House of Cards for the game definitions. |
GavinWraith (26) 1563 posts |
Using + for string concatenation is horrible. So is coercion between strings and numbers. Lua uses x..y by default for the concatenation of strings x and y. It should be pointed out that by using metamethods a great deal of Lua’s notation can be altered or extended by the user. This goes for all operation symbols. If you wanted to use + for concatenation of strings all you have to do in RiscLua is
The RiscLua notation ?[x] that makes ? look like a table indexed by memory addresses with values the byte at that address, is an example of this ability to extend the language’s syntax using metamethods.I prefer to use the functions tonumber and tostring explicitly. I believe there is a patch to remove automatic string~number coercion.
You can decorate Lua code with type-assertions like this: yields
|
Willard Goosey (5119) 257 posts |
I am glad to see RiscLua is being maintained and improved. I haven’t really done anything in Lua yet, but it is on my to-learn list. :-) |
nemo (145) 2529 posts |
Steffen said
• Assumption of success (absence of failure handling) |
nemo (145) 2529 posts |
Rick complained of dynamic typing
And yet: DEFFNjustLikeBasicThen(T%) CASET%OF WHEN1:=TRUE WHEN2:=2 WHEN3:=PI WHEN4:="For?" ENDCASE |
nemo (145) 2529 posts |
Gavin wrote
Unfortunately you’ve broken that link, Gavin. However, on your actual page you claim
A straw man argument. May not exist in some other programming languages, but in others they definitely do. Tables are associate arrays and are available under various names in many languages, such as Python, Javascript, PostScript, Ruby, and even Perl. Lua is not special here. There’s a much stronger argument that Javascript objects are unlike other associative arrays because of their prototypical inheritance, but that can be implemented using a magic key in other languages too. |
nemo (145) 2529 posts |
Rick parted with
You’re guaranteed to never get that. Since “a string” in C is a |
GavinWraith (26) 1563 posts |
Sorry. I think the Textile help is wrong.
I think Lua’s tables are special in that keys and values can be any non-nil value. Also, I do not think other languages have the concept of metatables . I have just updated the Utilities in the RiscLua 7 distribution with !Linker and !Absolua. These let you create standalone Absolute versions your Lua programs, which will run on computers that do not have the RiscLua interpreter. |
GavinWraith (26) 1563 posts |
Some minor changes in RiscLua 7:
Libraries do not have to live in and then use require "mylib" .
|
Fred Graute (114) 645 posts |
There does seem to be some difference though as AppLua works fine on RiscLua 5 and 6 but it fails on version 7 (segmentation fault; stack frame out of bounds). I’ll do some more digging later on. The cause of the error was in AppLua (using MessageTrans before file descriptor was set up). However the same code doesn’t crash on RiscLua 6, the call to MessageTrans returns an error. This suggests that RiscLua 7 doesn’t handle errors returned by SWIs correctly.
It’s coming along albeit in bits and pieces, and there’s still a long way to go. Anyway, I’ve taken what I’ve got so far and put it on my website (I’ll do a proper announcement later on). |
GavinWraith (26) 1563 posts |
Many thanks. I have been trying it with RiscLua 7. Perhaps I should apologize for its most recent innovation, declaration of globals. has to be changed to
for the first use of I feel a bit guilty about making RiscLua a moving target, but in this case less so. RiscLua 6 used dynamic linking for C libraries, but since then GCC has moved to a newer version and I cannot get dynamic linking to work. I suspect it has something to do with the translation between Unix and RISC OS filenames. Anyway, I have reverted for the time being to static compilation for RiscLua 7. The actual code for the I much admire the work you have put in to AppLua. Following Joe’s lead with AppBasic is an interesting exercise. But, as I may have already expressed, making a translation (calque?) from BASIC to Lua makes one aware of the differences, particularly when it comes to scoping of variables. In BASIC global variables are the norm, in Lua local variables are the norm. Still, there is much to be said for getting things to work first, and then tackling changes of style later in a controlled way. |
Tristan M. (2946) 1039 posts |
I just wanted to say that I use it. Most people only speak up when things go wrong. Admittedly I’m using it like a vanilla Lua because I have no idea how to use the additions for RISC OS and don’t really know where to start. |
GavinWraith (26) 1563 posts |
I am very happy to hear this. Have you tried Lua scripts in StrongED? You write a Lua program that can read in the text in a StrongED window as if it were from a file named by arg [1] by (SHIFT-/CTRL-) dragging the file containing the program onto the green down-pointing arrow (the apply icon) on the smarticon-bar at the top of a StrongED window. The program’s print output replaces the contents of the window. This makes a sweet environment for the programming of short scripts in vanilla Lua. |
Tristan M. (2946) 1039 posts |
Gavin, i haven’t tried that yet. I kind of miss the Iconbar icon for RiscLua. I think I cobbled it back in with a previous version but haven’t done it for 7. The Lua editing in StrongEd is very good. As for many things, it’s one of the better editors out there. I wanted to ask, would it be possible to have some simple examples of library usage? Either in the example code, in the included docs, or even on your site. I’ll say outright, I’m not good at Lua. I find it a useful tool, but my knowledge is very limited. |
GavinWraith (26) 1563 posts |
Over the years I wrote masses of tutorial stuff for RiscLua. However, fairly recently, when I decided to clean up my website and make it conform better to validator.w3.org, I drastically reduced all that. Currently there is a link RiscLua at the top of the list on the bottom of the RiscLua webpage to some sporadic notes. I add to these now and then, so keep your eyes peeled, and do complain if I do not explain things clearly. I am very happy to do special orders . |
Tristan M. (2946) 1039 posts |
I saw this in your notes.
Thank you! The notes section is very useful. Thankyou. I’m reading through it now. |
Steffen Huber (91) 1949 posts |
Exceptions ARE part of the program flow. But of the “error” flow and not of the “normal” flow. At the end of the day, exceptions are “just” a special kind of goto. I extensively used Java with its checked exception philosophy and Ada with its unchecked-and-for-serious-errors-only exceptions as well as various languages without exceptions. All in all, I prefer Java’s approach, because it makes the “error flow” pretty obvious for the reader of the code.
In certain situations, Exception-for-debugging is indeed a powerful tool – “new Exception().printStackTrace()” is spectacularly useful :-) Java has a fine distinction between “Exception”, “Error” and “RuntimeException”. It is key to a well-structured software to use the correct thing for the “exceptional situation” to be handled. And it is hard. Coding the “happy path” is always easy – it is handling exceptional situations and arbitrary inputs (especially user input) that makes things horribly complex. |
GavinWraith (26) 1563 posts |
I have updated RiscLua7 on my website. The new application !rlua7 puts an icon on the iconbar. Dragging a file containing a Lua program to it runs it in a taskwindow. There is a new directory altlib_ex in the Examples directly showing how you can require different libraries from the default. In fact I am in the middle of revising the wimp libraries, but have only updated the those for tasks, menus and the iconbar so far. The fact that global variables must now be declared with a non-nil initial value in RiscLua7 is a help in debugging and perhaps a reminder to keep all variables as local as possible.
|
Rick Murray (539) 13806 posts |
I’m not really aware of how Java/Lua handle (error) exceptions; suffice to say that I tend to prefer a BASIC style “ON ERROR” method rather than C’s every-function-returns-a-status. Why? Because generally, in a function, a set of specific types of error will occur and it would be useful to try to handle them in one place (I quite like VB’s ability to resume after handling an error) rather than checking for the same set of status after every single flippin’ function call. There’s a lot of code (including some I’ve written) where it doesn’t bother checking for errors. Bad form, perhaps. But without the use of That’s my gripe with C. No “ON ERROR LOCAL”. I suppose this might have been what C++’s throw/catch was intended to resolve? |