2

I'm trying to write a simple compiler in the Arduino language that I am making up for my lab. The compiler will have to be able to turn a string like this:

"do command_one 3 times"

into a data structure that represents this:

<command_one>, <command_one>, <command_one> 

it should also be able to turn something more complicated like this:

"do do c_one do c_two 2 times 2 times c_thr 2 times"

into a data structure that represents this:

<c_one> <c_two> <c_two> <c_one> <c_two> <c_two> <c_thr> <c_one>
                <c_two> <c_two> <c_one> <c_two> <c_two> <c_thr>

Another function will then read the commands out one by one.

These examples show all of the functionality that I would want from the compiler.

I'm having design issues for the compiler that I want to create. Initially, I just wanted to do some string manipulation and turn the input string into an array of integers representing commands. However, since the compiler will be outputting an arbitrarily large number of commands, and since arrays in Arduino need to be initialized with a length, this could be a problem.

I am not fluent in Arduino so I am hoping that an Arduino whiz can show me a quick solution. I have already come up with the suggested solution on my own in Python, however, my solution in Python has to do with the ability to change the length of lists on the fly.

You will also notice that I posted the same question in the C++ stackexchange. I am trying to write the same code for an Arduino and in C++. I know that Arduino code is based on C++, if the answer is the same in Arduino as it is in C++ that would be very helpful.

3 Answers3

2
  1. The Arduino IDE features nearly full C++ language support (at least for C++03 features).
  2. library support is limited, but any standard component you need, you should be able to get an open source implementation that compiles.
  3. That said, you have 2K of RAM to work with on an Arduino, so the kind of C++ code you would write for a general purpose computer just won’t get you very far.
  4. I could be wrong, but I very much doubt there are full Python implementations for Arduino.

Edit: Thanks to Mr. Penguin for pointing out that we’re dealing with an Arduino Due, not an Uno (I had overlooked the OP’s tag). 96K of memory is actually a decent amount to play with, so grab a library implementation like this one and use the C++ techniques you wish to use.

microtherion
  • 1,512
  • 9
  • 19
2

You question isn't totally clear to me, so this is just a pile of advice:

  • I think that you might be looking for a run() type of command, where you can run any function in code. However, since C++ is a compiled language, this is not possible. You can only do this in scripting languages.
  • Besides that, you want a variable length array? I haven't been able to confirm that this works with Arduino, but I know in standard C++ it is called dynamic memory allocation and you can do this with a pointer.
  • As for the string to store your data, where you're heading is wrong if you care about efficiency. I'd go with an array of integers (unsigned int myData[2] = {1, 290};) since you can store a lot more data in two bytes than you could typing out the function names.

For the "multiple run" function of your language, a simple for loop might work better to save space:

for(int x = 5; x <3; x++) {
  //Code here
}

Another thing you might want to do is store this "code" on a SD card encoded in two characters to yield a number like this:

byte valOne = SD.read();
byte valTwo = SD.read();
int outputVal = (valOne << 8) + valTwo;

This would solve the whole variable array crisis in the first place.

End of unorganized thoughts...

Anonymous Penguin
  • 6,365
  • 10
  • 34
  • 62
1

You may have more luck investigating standard C ways of dealing with this issue, instead of the common C++ methods.

Although it is typically not advised, you can use malloc and free on the Arduino. This allows you to dynamically use memory. I would provide a more detailed code example than below but when using dynamic memory it can be hard to troubleshoot what is going on without the use of a library to simplify the process. I would strongly advise doing a little study and understanding both dynamic memory management, and the repercussions of doing so on the Arduino platform. A solid foundation in this will pay off tenfold in time saved when debugging.

Note that you should check each time that you call malloc that it is successful

These resources might help:

Simple Example:

// Make a pointer to your array
someType *A;

// The size of your array, may be changed programmatically.
int arraySize = 5;

// Reserve memory for your array.
A = malloc(arraySize * sizeof(someType));

// Use Array
A[0] = someData;

// Release A's memory when complete.
free(A);
Élie
  • 222
  • 2
  • 5