Interpreter software

Hi. I was trying a few options for an Arduino interpreter , the goal is to be able to send any command using serial , like inputs,outputs,conditions,loops,and basic math operations. Also, to be able to save these to EEPROM for use on start up .

1.bitlash- which has limitations that I can't solve, and is not being updated anymore. 2.Firmata- which seems huge, but is very unfriendly ( in comparison to bit lash), and I can't really understand if its capable of what we need.

and there are few more small solution which I doubt about them.

[u]Writing my own :[/u]

So, for the basic stuff, its pretty easy to write built in functions , read the serial, and then use some basic protocol to decide which function I am going to call ( inputs and outputs).

The main problem, starts when you need to do things such get a variable, do math operations, basic "if" conditions, etc,

for example, I would like to get something like this from serial and do it :

a=read some input
a=a*b
output(a)

Here I will have to write a lot of code that interpreter this . (not mention "if" statement ).

So, I wonder if there is some transparent interpreter(or a direction to write one) that gets the serial data, and only do the "software" stuff, not the hardware, which means :

Get from serial missions like : -operations on local variables (+,-,*,/,) -conditions and loops(if,while)

that is completely transparent- means I control the serial input and can modify it.

Any direction would be great, thanks!

Very nice idea. You’ll learn a lot!

I’d start with a very limited set of commands with a very strict syntax. Inspired by old BASIC-dialects, something like this should be not too complex to implement:

10 ANALOG-READ 10, $A
20 IF $A < 50 GOTO 10
30 PRINT $A
40 DIGITAL-WRITE 2, HIGH
50 DELAY 100
60 GOTO 10

Parsing this line-number-based instructions should be ‘quite’ easy regarding control structures.

Good luck!

Thanks, but I am not sure how to use your answer, it seems pretty limited . Controlling inputs and outputs is quite easy, problem is to implement "if" statements (or if inside if) , looping, performing math operations on variables, etc.

I think ones need a heavy programming skills here.

My example shows a way how to avoid/workaround nested IF structures to allow a ‘simple’ implementation, so that you can focus on the parsing.

Maybe the arduino is not the right platform to build your first simple interpreter.

I recommend you to download a real C++ IDE for your operating system (VisualStudioCommunity, Eclipse, CodeBlocks, etc.) which help you a lot because you can debug your code step-by-step.

After you finished, you can port your code to the arduino.

To my mind a general purpose interpreter does not make sense for an Arduino. It is not a general purpose computer. It is a microprocessor that can be programmed for a single task (which may or may not be complex).

If you want to re-program it for a different task that only takes a few minutes with the IDE (assuming the other program already exists).

On the other hand I could understand the use for a limited interpreter that could cause an Arduino to call one of several functions depending on the user input. That could be implemented very simply.

...R

I was trying a few options for an Arduino interpreter , the goal is to be able to send any command using serial , like inputs,outputs,conditions,loops,and basic math operations.

A great idea, but could get quite involved. For reference, check out LINX. It's remote I/O connection does what you describe (and more). Supports UNO, Mega2560, Leonardo and Pro Micro (and boards from others).

gil22: Hi. I was trying a few options for an Arduino interpreter , the goal is to be able to send any command using serial , like inputs,outputs,conditions,loops,and basic math operations. Also, to be able to save these to EEPROM for use on start up .

You can already do this. You can type the commands into a text editor, use GCC to translate it into a format that the Arduino can recognize, and use avrdude to send the translated commands to the Arduino where a special portion of it's program intercepts the commands and stores them in non-volatile memory so that they aren't erased when you lose power.

This is more commonly known as "writing and uploading a sketch". What are you trying to achieve that can't be done this way?

Jiggy-Ninja: This is more commonly known as "writing and uploading a sketch". What are you trying to achieve that can't be done this way?

I think the secret lies in his request for ideas on how to write an 'interpreter' - like BASIC, which interactively allows him to push instructions. Not a write-compile-upload cycle.

lastchancename: I think the secret lies in his request for ideas on how to write an 'interpreter' - like BASIC, which interactively allows him to push instructions. Not a write-compile-upload cycle.

It is perfectly reasonable to ask why someone is slogging through the difficult task of trying to make an interpreter instead of using the normal way of changing the functionality of a microcontroller.

That doesn't mean there has to be any functional reason given. I think "Because I want to" is a perfectly valid reason when trying to experiment and play around.

Jiggy-Ninja:
I think “Because I want to” is a perfectly valid reason when trying to experiment and play around.

Absolutely agree. I wrote a Pilot interpreter decades ago and it was not efficient at all, but it didn’t have to be since it was only used for an interactive training manual. Still, if I had the talent, my wish list for the Arduino would be a full-blown symbolic debugger. I think the primary reason we used Visual Studio in the intro programming course at Purdue was because of its debugger, plus you could “teach” enough of the IDE in one class period to be useful. Eclipse, Netbeans, etc, are all very powerful IDE’s, but took far too long to teach the IDE.

Perhaps Arduino BASIC would be a good starting point: https://github.com/robinhedwards/ArduinoBASIC

You need to create tokens, or "opcodes".

You would parse each input line and reduce it to a series of opcodes that you store in your program area. For example: (01) would be if (02) would be this is the end of the statements to execute if (01) is true (03) would be else - execute these statements if (01) is false until you get to (02) (04) could be the end of a loop, go back up to the for or while token and check if we should exit the loop

and so on.

Jiggy-Ninja: I think "Because I want to" is a perfectly valid reason when trying to experiment and play around.

I agree with that.

I wrote Reply #4 in case the OP is confused about how to describe what he actually needs.

...R

Thanks Robin…
I suspect the other posters didn’t grasp what I wrote in #7

An interpreter doesn’t have to be a programming language, it simply has to understand commands on the fly to perform new functions… indeed, an interpreter is rarely written in its own language - more likely in another compiled high-level language (or assembly)…

For example, I write in C/++, but frequently write interactive code that is saved to perform various functions based on what the user needs, not on what I shipped him… macros f you will - are ‘interpreted’.

Let me also mention my humble attempt to write arduino BASIC interpreter. https://sourceforge.net/projects/terminal-basic/.

It is under heavy development, full of bugs and lacks functionality, but works through the serial port, and supports reals and arrays.

It would be nice if the Arduino C/C++ had an "evaluate()" statement then all the OP's problems would be solved :) The most practical direction, and most of the elements of it already appear in this thread, is to take an existing simple language (where all the syntax, rules, grammar etc.) are already defined, then define the syntax etc. of the Arduino-specific language extensions (and also define what can be stripped out). But that is all no trivial task. Then the next stage is to implement it all. It'll be quite a project.

Hi all, I made my own interpreter and intepreted language (starting from uBasic), it has some interesting features and can run programs from sd card (so you don't have to load the entire program in SRAM).

It can be used as a standalone interpreter or you can set up a minimal shell you can use to remotely manage i/o from a bash script!

Hope you'll have a look!

http://avil.altervista.org/

alejho: It can be used as a standalone interpreter or you can set up a minimal shell you can use to remotely manage i/o from a bash script!

Do you know how well it performs? For example could it be used for a time critical program using interrupts to detect and control the speed of a motor? In the case I am working on the interrupts will be a min of about 3000 microsecs apart.

I presume it would be possible to send it text to store on the SD Card and then start interpreting that text as the program?

...R

Of course if you set it up as a "command shell" you can remotely upload a program and then execute it (you'll find an example on the website) It's not exactly an high performance language since it works loading one line of program at a time in sram from the sd. But when high performances are required for a particular task you can implement it as new language's function (there's a tutorial about it on the website).

My initial purpose was exactly this : to create a simple generic system customizable with basic functions you can "compose" with a simple language (without the needs to recompile)

Thanks. I will think some more about it.

...R