need example code

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

need example code

Jay Glascoe
hello Lua List,

I've read the Ref. Manual and perused the goodies in the "test" 
directory. So, I feel fairly comfortable with Lua's language/syntax,
but I'm still left wondering what a good Lua/C extension looks like. 
Is there any code out there implementing, say, trees or matrices in C
as Lua "userdata" types? 

<blah blah blah>
Also, at one point I thought I knew what "extension language" meant: I
use Python all the time and implement compute-intensive tasks as C
extensions (dynamically loaded, it's real cool).  So, I thought an
extension language was something where you could extend the
(presumably high level) interpreter with (low level) compiled code.

But it seems that Lua is more designed for extending the application
at hand.  That's sort of the opposite of what I do: Lua is for
extending a low level language with a high level one...  ???

I'm confused.  Can you recommend any good books detailing the
philosophy behind extension/embedding?

Okay, wait a second, here: I still haven't read this "Lua-an
Extensible Extension Language".
</blah blah blah>  ;)

	tia,
	Jay

-- 
        "Don't be too proud of this technological
         terror you've constructed."

        -- Darth Vader


Reply | Threaded
Open this post in threaded view
|

Re: need example code

David Jeske-2
On Wed, Jan 20, 1999 at 11:36:38PM -0200, Jay Glascoe wrote:
> I've read the Ref. Manual and perused the goodies in the "test" 
> directory. So, I feel fairly comfortable with Lua's language/syntax,
> but I'm still left wondering what a good Lua/C extension looks like. 
> Is there any code out there implementing, say, trees or matrices in C
> as Lua "userdata" types? 

Hmm.. I wrote some code to the mailing list a while back which
explained exporting C data-types as user-data types which act like lua
structures. I have a bunch of code which does different types of
extensions in my stuff, but it's pretty nasty and probably more
confusing than it is helpful.

> Also, at one point I thought I knew what "extension language" meant: I
> use Python all the time and implement compute-intensive tasks as C
> extensions (dynamically loaded, it's real cool).  So, I thought an
> extension language was something where you could extend the
> (presumably high level) interpreter with (low level) compiled code.

It could be... 

> But it seems that Lua is more designed for extending the application
> at hand.  That's sort of the opposite of what I do: Lua is for
> extending a low level language with a high level one...  ???

You can use Lua for either task. The point is, lua is a small library
of code which you can easily link into your application. This means
you don't have to ask a user to install python, and you don't have to
support some monstrous set of python modules which you are used to
programming with (Lua only has basic stuff). 

The distinction I usually grab onto is "where is your main loop". 

I have one piece of software where the main loop is in C/C++, because
it's a game, and it needs to be fast. It calls into Lua for tasks like
'asking objects in the world to do things'. That way, I can write the
object code in Lua, and all the hard-coded render loop stuff is in
C/C++ where it's fast. In this case, I have a C program with Lua
scripting.

I have another piece of software where the C code merely does some
basic program initialization, exports a bunch of functions to lua, and
then runs "init.lua". The Lua code 'runs' the program by calling the
extension functions. In this case, I have a Lua program with C
extensions.

However, the nice thing about Lua is that it so easily and nicely fits
both roles. Lua dosn't have every feature you might want, but it's
simple enough that you can get it taylored to a task very easily.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

       "Whoever thinks a faultless piece to see,
    Thinks what ne'er was, nor is, nor e'er shall be." - Alexander Pope

Reply | Threaded
Open this post in threaded view
|

What is an extension language (was Re: need example code)

Bennett Todd-3
In reply to this post by Jay Glascoe
[ warning: this is a wandering bunch of musings on programming language style
  and ways of using languages, not a whole lot of meat here ]

1999-01-21-01:36:38 Jay Glascoe:
> Also, at one point I thought I knew what "extension language" meant: I
> use Python all the time and implement compute-intensive tasks as C
> extensions (dynamically loaded, it's real cool).  So, I thought an
> extension language was something where you could extend the
> (presumably high level) interpreter with (low level) compiled code.

I think that use is what most folks are gonna be calling an extensible
language. 

Which Lua is.

When I think "extension language", I think of things like Emacs's Lisp, in
which extensions to the basic app can be written. TCL was originally designed
to be an extension language. Performance wasn't a design goal, and as it turns
out "everything's a string" isn't an attractive fundamental abstraction for
programming-in-the-large, though it works fine for tiny stuff, and was
fabulous for the two tools (or libraries) Tk and Expect. But TCL is slow and
huge, that makes most folks loathe to tack it onto any but the slowest and
hugest of apps. So for "extension language" I think something kinda like
"embedded language", in the sense that the language gets embedded in some
other app. But that usage runs afoul of "embedded code", which most folks use
to mean "code that runs in toasters and like boxes, on raw iron without an
OS", so I shouldn't use that phrase:-).

For lots of kinds of programming, Perl is my favourite language; I find I can
express myself with less strain and feeling of constraint in that language;
the many different idioms make for fluid expression --- for me. And in the
last few years it has successfully borrowed the magnificent feature that
Python in turn borrowed from the Lispers: dynamically loadable extensions
elegantly coupled with O-O namespace management.

C remains my favourite "portable assembler"; I think it's wildly the best
choice of language to e.g. implement another programming language in. Or write
small fast simple system utilities. Or write extensions for a nicer,
higher-level language that do specific performance-demanding subtasks
efficiently.

Lua is attractive to me for the case where _most_ of the work can and should
be done in C, and all you need is a lightweight programming language atop that
to provide limberness and flexibility. I'm hoping to write a procmail
replacement, for instance, by gluing Maildir delivery (already written) and
message parsing (work in progress) underneath Lua; let the equivalent of
dotprocmailrc be written in Lua, a way sexier language --- and Lua is still so
small and fast that the end result can hope to be as tiny and as
breathtakingly fast as procmail. I hope.

Another project I dream of is a super-lightweight database peruser. I just
adore the user interface of the builtin database app in the HP 100/200LX
palmtop. So much that I'm constantly running into limits, namely in the size
of database it can manipulate. I'd love to be able to prepare much bigger
databases --- multi-megabyte anyway --- on a workstation, and download 'em to
my palmtop, and have a super-fast lightweight browser with the look-n-feel of
the builtin DB. As long as I'm at it, why not use a really good efficient
compact format, say huffman coding the data with fixed trees; I've got all the
CPU in the world to prepare the data, I just need a tiny efficient browser.
Doing the entirity of such a thing in C would be a drag and a hassle; once you
get used to nice garbage-collected languages with O-O features and useable
strings and so on, it really sucks to go back. But doing the
performance-critical bits as C libraries (mostly already done by other folks,
hooray!) and the top-level app design and implementation in Lua would rock.

So I guess I see Lua as partly an extension language and partly an extensible
language. It's also a terrific candidate for an embeddable language, in the
conventional sense of a language for writting embedded apps. Small _is_
beautiful.

Until I met Lua I was leaning towards Scheme for this sort o' role, but
always ran into a couple of barriers: first, while Scheme is so elegant in
theory it's scary, actually looking at code makes my eyeballs ache. A bowl of
fingernail parings. And second, while it _seems_ like you oughta be able to
enjoy a full-featured high-performance Scheme interpreter in a couple dozen
KB of executable, somehow the actual ones end up being a couple hundred KB
--- fine for many apps --- but procmail is a 50-odd KB executable on a lot of
platforms. Quadruple that and you will significantly cut its delivery speed at
the extremes, and the extremes are where speed counts.

-Bennett

Reply | Threaded
Open this post in threaded view
|

Re: What is an extension language (was Re: need example code)

Jay Glascoe
On Thu, 21 Jan 1999, Bennett Todd wrote:

> [ warning: this is a wandering bunch of musings on programming language style
>   and ways of using languages, not a whole lot of meat here ]
> 

no problem, I read it all.

> For lots of kinds of programming, Perl is my favourite language; I find I can
> express myself with less strain and feeling of constraint in that language;
> the many different idioms make for fluid expression --- for me. And in the

yeah, I know what you mean.  I can see myself using both Perl and
Python for a long time to come.  Text munging, shortish scripts (okay
if they grow ~1k lines) and such for Perl, compute intensive stuff for
Python/C.  I just gave up on Perl as an extension language because IMO
the Perl/C API is too bizarre.

> Doing the entirity of such a thing in C would be a drag and a hassle; once you
> get used to nice garbage-collected languages with O-O features and useable
> strings and so on, it really sucks to go back. But doing the

right.  If you can believe it, I learned C for the sole purpose of
writing C extensions.  I mean, I had Mark Lutz's book and couldn't
make sense out of the extending/embedding stuff.  So I learnt C.

> Until I met Lua I was leaning towards Scheme for this sort o' role, but
> always ran into a couple of barriers: first, while Scheme is so elegant in
> theory it's scary, actually looking at code makes my eyeballs ache. A bowl of
> fingernail parings.

have you taken a look at Guile yet?  It's still a few years away from
being a serious contender in the extension/embedding language field,
but I really like it.  The Guile/C API is very clean.

> -Bennett
> 

	Jay Glascoe
	[hidden email]
	www.giss.nasa.gov/cgi-bin/csci/change.pl

-- 
echo '
Goldfish will grow to fit the size of their aquaria.
small aquarium => small goldfish
large aquarium => large goldfish
' | perl -ne '
	s/goldfish/personal expenditures/gi;
	s/aquari(a|um)/income/g;
	print;
'


Reply | Threaded
Open this post in threaded view
|

Re: need example code

Jay Glascoe
In reply to this post by David Jeske-2
On Thu, 21 Jan 1999, David Jeske wrote:

> The distinction I usually grab onto is "where is your main loop". 
> 

totally depends on what I'm going to use Lua for...  I've been
thinking a lot about this.  What I've come up with is to use it as a
pre-processor for these mean plotting and graphing utilities we use
over here.  The data files for the utilities are basically columns of
numbers ("x" over on the left, "ys" on the right).  But to specify
things like size, color, etc. we have stuff like

 &CHOICE
  NIMAGE=1,
  INFO=1,

<snip>

  HEADS(3)='Model-Observations',
  heads=3*' ',
  HEADS_COL=1.,2.,3.,
  HEADS_SIZE=.011,
  SHIFT_HEADS=0.,

<snip>

  JM=46,46,46, 46,46,46, 46,46,46, 46,46,46, 3*46, 3*46,
  LM=1,1,1, 1,1,1, 1,1,1, 1,1,1, 3*1, 3*1,
  FACTOR=1.,1.,1., 1.,1.,1., 1.,1.,1., 1.,1.,1., 3*1., 3*1.,
  OFFSET=0.,0.,0., 0.,0.,0., 0.,0.,0., 0.,0.,0., 3*0., 3*0.,
  UNDEFINED=-1.E30,-1.E30,-1.E30, -1.E30,-1.E30,-1.E30, 12*-1.E30,
  UNDEFINED_COLOR=.62,.62,.62,
  WMAP=T,T,T, T,T,T, T,T,T, T,T,T, 6*T,     
    ICOLOR_map=0,0,0, 0,0,0, 0,0,0, 0,0,0, 6*0, 
    IDOT_map  =0,0,0, 0,0,0, 0,0,0, 0,0,0, 6*0, 
    LMASK     =0,0,0, 0,0,0, 0,0,0, 0,0,0, 6*0, 
    PROJECT   ='CE','CE','CE', 'CE','CE','CE', 12*'CE',
    POLE      ='N','N','N', 'N','N','N', 12*'N',
  EDGE=T,T,T, T,T,T, T,T,T, T,T,T, 6*T,     

<BLAH, BLAH, etc.>

So, I think I could use Lua to my benefit here.  Is Lua well suited to
this type of task?  It may be advantageous to peruse the actual data
files (with C) to determine max's min's etc.  So, C will probably be
involved in some way (extend/embed... I dunno).  BTW, the actual
graphing apps are, essentially, legacy code in FORTRAN.  It would be
nice to put a Lua interpreter inside these apps, but I don't think
that's feasible.


> C/C++ where it's fast. In this case, I have a C program with Lua
> scripting.

okay.

> 
> I have another piece of software where the C code merely does some
> basic program initialization, exports a bunch of functions to lua, and
> then runs "init.lua". The Lua code 'runs' the program by calling the
> extension functions. In this case, I have a Lua program with C
> extensions.

yes, this I can identify with.  This is good.

> However, the nice thing about Lua is that it so easily and nicely fits
> both roles. Lua dosn't have every feature you might want, but it's
> simple enough that you can get it taylored to a task very easily.

Okay, now I know that I'll *have* to do something with Lua.

> 
> -- 
> David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]
> 
>        "Whoever thinks a faultless piece to see,
>     Thinks what ne'er was, nor is, nor e'er shall be." - Alexander Pope
> 
> 

	Jay Glascoe
	[hidden email]
--
boing.

Reply | Threaded
Open this post in threaded view
|

Re: What is an extension language (was Re: need example code)

Bennett Todd-3
In reply to this post by Jay Glascoe
1999-01-23-00:57:22 Jay Glascoe:
> I just gave up on Perl as an extension language because IMO the Perl/C API
> is too bizarre.

As an extension language --- a language to use for the config file and for
user customization in an app written in some other language (e.g. C) my big
gripe with perl is that it's so _huge_. Likewise Python, and TCL. And even the
Scheme implementations I've seen, smaller than the above three sometimes, but
still 4-6 times bigger than Lua, or worse.

> have you taken a look at Guile yet?  It's still a few years away from
> being a serious contender in the extension/embedding language field,
> but I really like it.  The Guile/C API is very clean.

I peeked at it a couple of years back.

Back then it was based on a nice, quite popular (for a Scheme:-) interpreter,
SCM, which already wasn't tiny; I kinda assumed that by the time it was ready
to be called GNU Software the core interpreter would be well over a meg of
binary. Some times I think the Free Software Foundation has Microsoft envy.

Didn't even occur to me to give it a look; I was looking for something small,
I don't look to the FSF for that.

-Bennett

Reply | Threaded
Open this post in threaded view
|

Re: need example code

Tomas-14
In reply to this post by Jay Glascoe
> So, I think I could use Lua to my benefit here.  Is Lua well suited to
> this type of task?
        Yes!  We used Lua (version 2.3 at that time) to develop a bind
to IUP (an interface toolkit) where we have many definitions of interface
elements.  Something like this (I'm not at the develop team anymore, so I'm
probably using wrong function names, but for the example this is enough):

ok_button = IupButton { title = "Ok", bgcolor = WHITE }
cancel_button = IupButton { title = "Cancel", bgcolor = WHITE }
dialog = IupDialog { ok_button, cancel_button; title = "Confirmation", }
dialog:display()

	I had also used Lua to make an interface for a (FORTRAN) program
which have ugly configuration files.  The system offers some dialog boxes
with the options of the configuration file.  After selecting the options
the user could start the Fortran program that will process the configuration
file and the input data files.  This activation could be done by just
pressing a button: at that moment, the interface will generate the
configuration file and execute the program.
	Despite the advantage of using a visual interface instead of a
big text file with no explicit default values, the interface system had
the ability to convert from Celsius to Fahrenheit or to Kelvin and many
other conversions of Chemical scales, also it could verify ranges of
values so the Fortran program would never be started with a wrong
configuration file.

	Hope that this will help you!
		Tomas