Arduino code emulation[in progress]

hi, im making a program to take Arduino code stored in a String and run it. So far I've managed to emulate a lot, but I'm stuck on running string functions. Here's the issue put simply:

String code = " examplestring.indexOf(param1,param2); ";

lets say the emulator needs to find param1. we would just do

code.substring(code.indexOf("("),code.indexOf(",",code.indexOf("(") ) );

however, the issue is, what if paramater 1 contains a string function? looking like this:

String code = " examplestring.indexOf(param1.substring(0,3) , param2);

so, how could i find param1 in this scenario.

hey again! this has gotten alot of discussion, making me want to do it even more.
ill be posting updates on the emulation code, if anyone can comprehend the mess.

C++ is compiled... so you can't interpret at run time C++ expressions unless you include a full C++ parser and interpreter inside your code....

your parser needs to be smarter and use recursion etc... follow the C++ grammar

yep, thats what im doing, although im not interpreting all of the arduino langauge, just variables,
String class, if statements, math operators and some other stuff.

I will look into this.
it would still be nice to know a way of extracting param1 from indexOf( , ) in String code

Define a grammar for your language. I have no idea what you mean with your example code. If you can explain the string content to me, you can write the parser for your language.

there is no such thing really as the Arduino language. the language is C++
if you want to write an interpreter for the Arduino String class API, then yes that's specific

you need to start defining the exact grammar you want to support and read about parsing theories to build the code intent and then execute the right code.

(see Comp 11 C++ Grammar for example - not sure what it's worth but found it through a quick google search) or Hyperlinked C++ BNF Grammar

1 Like

Are you doing it just because or you have a particular goal in mind?

the language being interpreted is C++ (some of it) , therefore the grammer follows that of c++.

this interpreter will be run on an esp32 which is a fairly powerful microcontroller. the interpreter will get code from a webserver, then run it. the emulator is going to be able to read tftEspi functions( tftEspi is a library for displaying on tft displays) and display them on a tft display. This means that you could download code and run it on possibly a little wearable or handheld device.

well - that little wearable or handheld device would need to have the interpreter embedded, access the internet to fetch the code and then interpret it... seems heavy... C++ might not be the best language either... may be Basic or something like that would be more appropriate.

You could also check other attempts, like Espruino on ESP32 (https://www.espruino.com)

Have a look at Lua.

1 Like

won't arduino code run on the esp32? i use the Arduino IDE for my esp32/12 projects

this is not the issue. the esp32 contains a sketch that can take c++ code inside a string and run it. yes its a bit complicated

are you using lex/yacc?

It really can't, in the way that C++ is compiled. You can make a virtual machine, and with that machine you can interpret line by line.

This is different though as it will not utilize the hardware anywhere near as efficiently as C++ and will lack all optimizations as well as direct manipulation of a dedicated stack and heap. The stack and the heap that you will have access to is that of the virtual machines. Though I suppose that you could map a chunk of the heap for a dedicated stack/heap/freespace block, but you have limited resources even on the ESP32.

I know, I've written my own interpreted language in Arduino C++.

I found it to be much easier to ditch the C++ syntax and go with a modified form of BASIC.

What is the purpose of this? I ask because if it's to examine the effect of the code then it will not yield true results.

Read about Harvard Architecture vs Van Neumann Architecture.

It is fun writing interpretation code, most all the old-timers here have done it. "bitlash" is a rather complete BASIC for Arduino.

But, outside of a learning experience, and specifically on the ESP32 microcontroller, my choice would be microPython.

If you are desiring a more Arduino flavor, Adafruit's CircuitPython
is your better choice.

IMO, this is misleading, as
https://www.arduino.cc/reference/en/
clearly states:

Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure.

What is relevant in the above is that all of the Arduino 'language' is based on C/C++ with a few lines of assembly code. Arduino also utilizes the C++ preprocessor for implementing templates.

well that is the infinite debate about what is a programming language and should it be confused with provided frameworks and IDE…

in my opinion, no.. the C++ language is very clearly defined, that’s what our compiler is about. The arduino stuff is a sugar layer on top of this to make it sweet to interact with the hardware or supposedly easier for beginners though IDE’s black magic. But in the end C++ is generated and compiled.

The arduino team clearly sees that differently but they are not aligned with Wikipedia’s definition for example (and tons of other academic references).

The fact is Some people say an API is a language… they are entitled to their views :wink:

You want an interpreter, you might have to write what you want.

C++ String objects and dynamic allocation are Not Suited to small RAM environments.

I read serial or char array null-terminated text 1 char at a time using state machines instead of pre-buffering and using string.h commands, I started doing that in the 80's.

As in programming language for Arduino, not Arduino++ or something. Using Assembly is well within C++ specification. I agree with @J-M-L It is C++, and you know why that is? Arduino uses C++ compiler, not an Arduino compiler.

Yes, but the definition of "small RAM" and association with microcontrollers and subsequently with "Arduino" has become fuzzy over the past 7 years. With regard to the ESP32, String functions are the essence of web-applications; the ESP32 seems to withstand and survive. I'm certain that Strings impart some inefficiency but the ease of programming clearly offsets those inefficiencies.

I was around back in the dark ages when using Strings with ArduinoIDE was a dance with the devil and application stability could not be guaranteed (or even expected.) But nearly a decade has transpired and Arduino Strings no longer exhibit the weirdness and caveats of the past; at least with the cores I routinely use: ESP8266, ESP32, RP2040, Teensy 3.2. On the AVR, I have used strings with the Arduino Mega 2560, but judiciously.

To be a bit argumentative, C/C++ is not a language, eventually everything gets ground to assembly code; thus, the only true language is native assembly.

As a member of the Arduino Forum, I choose to use the corporate view that Arduino is a language; many straightforward sketches can be written without knowing that gcc is at work in the background. Continuing to hound newbies that they must learn C++ is a real turn-off to someone simply blinking a LED and that they must never use delay() or they will damn their soul for all eternity. Such BS.

There is the Arduino Language Reference and while there may be better code implementations; this is where many beginners start. As newbies evolve, then they (may) become concerned about more mature programming and writing their own functions and libraries.

Anyway, I really do not care what members call the Arduino Language - we all have opinions. But it appears that I am not going to be swayed as long as Arduino Corporate continue to list it clearly as a language in their documentation.

They call a bunch of global methods they designed to make Arduino programming easier a language, according to your own link to Arduino language reference. Well some people have opinion that the Earth is flat. Always stick to your opinion it can never be wrong!