22

I am looking for a scripting language that can be included in an embedded system to allow the user to pre-configure the unit behaviour based on the system events (I/O port changes, time events...). The sort of control required is

if (some_event)
{ 
    do some stuff
    delay N seconds
    do more stuff
    if (some condition)
    {
        do something
    }
    else
    {
        delay until condition
        do something else
    }
}

Each of the "do stuff" parts of the would typically be to change the state of the IO or to allow/disallow the processing of one or more events.

There is no requirement for text processing or file handling unless it is required internally by the scripting language implementation.

The processor that I am using has some 8K of RAM and 20K of program store available after the normal operating code has been built. The firmware is written in C, so any source for the scripting language must also be in C.

uɐɪ
  • 2,540
  • 1
  • 20
  • 23
  • If your target is supported by an open-source compiler, you can bundle the compiler with your configuration software. Generate C from your configuration, compile, then upload. – Kuba hasn't forgotten Monica Oct 17 '13 at 19:55
  • @kuba - Good idea - IF the compiler can fit in 20K ROM and 8Kbytes RAM! – uɐɪ Oct 18 '13 at 07:23
  • Well, the compiler would reside on a PC, right? I presume that there's an embedded part, and a PC-based configuration program? – Kuba hasn't forgotten Monica Oct 18 '13 at 12:35
  • Essentially modifying the code on the embedded processor. Quite apart from the regulatory impossibility of getting such a product licenced, that would be beyond the capability of any of our customers. – uɐɪ Oct 18 '13 at 13:49
  • Well, you're assuming that your scripting environment won't be exploitable, and that's a big if. You're modifying the code no matter whether the code is directly executable by the processor, or if it merely directs a bytecode interpreter. What is the product for? – Kuba hasn't forgotten Monica Oct 18 '13 at 14:40
  • 1
    I second MarkusSchaber's suggestion of PAWN. The bytecode is ROM-able. The compiler can be embedded as well, but there's little point in that. There is no need for garbage collection, all data allocation is static. – Kuba hasn't forgotten Monica Jan 03 '14 at 00:55
  • Languages such as Lua and Squirrel offer IMHO way too much functionality for such a use. That comes at a cost. For what you need, garbage collected memory management is pretty much out of the window. Neither Lua, python nor Squirrel fit that requirement. I've been running rudimentary PAWN scripts (using 16 bit cells) in 512 bytes of RAM - that's only 256 cells! Yet it's still functional. Of course the bytecode is in ROM. – Kuba hasn't forgotten Monica Jan 03 '14 at 00:59
  • 1
    You must understand that a p-code sandbox can still be exploitable, though. So the "modifying the code on the embedded processor" is always there as a possibility for a malicious user. Basically the user must never be given access even to p-code. The p-code must be encrypted by the configuration software, and then decrypted on the target. – Kuba hasn't forgotten Monica Jan 03 '14 at 01:03

9 Answers9

21

We use Squirrel for this job. It is similar to Lua, but reference counted instead of garbage collected, so it tends to work better in very tight memory. On the downside, its community is much smaller.

I have also seen Lisp embedded successfully, particularly a Scheme-like derivative.

See also this other StackOverflow question: What are the available interactive languages that run in tiny memory?

Community
  • 1
  • 1
Crashworks
  • 40,496
  • 12
  • 101
  • 170
  • Thank you for this suggestion. I have been looking at Lua and have been worried about fitting it into the very tight processor environment that I have. I like the idea of being able to cross compile to byte code and load this as an "executable" into my target. Further investigation required but this may be the way to go. – uɐɪ Jul 22 '11 at 06:51
  • @Ian The general term for that technique is "p-code" ( http://en.wikipedia.org/wiki/P-code_machine ) and it's been successfully used in many places. Personally, I think that if you are precompiling source code, you may as well precompile it to native code and then you might as well just use C, but I don't know what your particular constraints are. – Crashworks Jul 22 '11 at 06:53
  • I remember coming across ucsd p-code in the pre-IBMPC daysof the late 1970's/ early 1980's. Compiling to native code is not really an option. What I am trying to get is a control script, much like a batch file, that is loaded into my device to control its operation and allow it to be deployed remotely. I have a small, 16 bit processor, 16K Ram, 100K code and a bucket-load of configurable options for its operation. – uɐɪ Jul 22 '11 at 07:03
  • 1
    @Ian, yep, sounds like a job for pcode! On this site it's hard to tell whether someone is a longtime veteran or a fresh engineering student, so I'm never sure how much background to assume an OP has before answering. The first pcode I studied closely was the Z-Machine that Infocom used to write their old text adventures ( http://www.csd.uwo.ca/Infocom/Articles/small.html ) -- which was a marvel of fitting lots of content into a tiny space. – Crashworks Jul 22 '11 at 07:11
10

Lua is my first choice as an embedded language. It's written in C, easy to expose your own functions to Lua, and by the looks of it some work has been done to get it working on embedded systems.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
7

For embedded systems, try eLua.

lhf
  • 70,581
  • 9
  • 108
  • 149
7

A traditional choice is Forth. Small embedded systems such as microcontrollers are Forth's platform of choice. There are Forth many implementations with a variety of features, target platforms and licensing. The Forth Interest Group has a list of commercial implementation and non-commercial implementations.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
4

I would give Lua a shot as it is a very small scripting language. It can be fully embedded

Lua is a fast language engine with small footprint that you can embed easily into your application. Lua has a simple and well documented API that allows strong integration with code written in other languages. It is easy to extend Lua with libraries written in other languages. It is also easy to extend programs written in other languages with Lua. Lua has been used to extend programs written not only in C and C++, but also in Java, C#, Smalltalk, Fortran, Ada, Erlang, and even in other scripting languages, such as Perl and Ruby.

Lua homepage

Matten
  • 17,365
  • 2
  • 42
  • 64
2

I had good experience using pawn (http://www.compuphase.com/pawn/pawn.htm) in a former project. A derivate of it is also used in the "E17" desktop environment, they call it "embryo". (https://trac.enlightenment.org/e/wiki/Embryo).

MarkusSchaber
  • 757
  • 1
  • 8
  • 16
1

There are four fairly common languages for embedding (in no particular order):

Of these, Lisp and Forth are arguably the least end-user friendly. Lua currently seems to be the most popular (it's very popular in the gaming world), Tcl is arguably the simplest to learn (only 12 rules for the whole language) and is the defacto language in the EDA world.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

Hope these helps you embedding perl script in c applications

http://www.ibm.com/developerworks/linux/tutorials/l-perlscript/index.html

Just google it for more informataion.I believe perl helps you alot in pattern (strings) searching. To learn perl go through this tutorial

http://mj12net.org/courses/perl/Teach%20Yourself%20Perl%20in%2021%20Days.pdf

Its a good tutorial,You can explore more than u think. Thank you

niko
  • 9,285
  • 27
  • 84
  • 131
  • As I said, the one thing that I definitely do not need is any form of string processing, but thanks for the suggestion. – uɐɪ Jul 22 '11 at 06:53
0

You could also try JavaScript, the V8 engine from Google is very easy to embed. Also the language itself is really small, so you can pretty much shape it into something that's suitable for your domain.

harald
  • 5,976
  • 1
  • 24
  • 41
  • Only if his embedded system is ARM, though. (V8 only compiles to x86, x64 and ARM.) – Isvara Sep 03 '13 at 05:51
  • 2
    I'd like to add that there are two new contenders for embedding Javascript in a C environment: [Duktape](http://duktape.org/) and [MuJS](http://mujs.com/) – Vortico Aug 15 '15 at 07:18
  • The target is not ARM and the stated memory sizes (max 8K bytes of RAM) probably rule this out. – uɐɪ Mar 27 '18 at 09:24
  • Please drop a link to embed v8 engine to a 8K RAM and 20K Flash memory or delete this post – freewill Sep 10 '22 at 22:09