1

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.

  • Arduino has full c++ language support, but does not have the standard library, so if your c++ answer is "use std::vector<type>" you will need to find or write a separate vector implementation. Outside of implementation, the answers should be the same. – BrettAM Oct 10 '14 at 16:50
  • That's the only answer I've gotten so far. If that continues to be the only answer then I'll have to find something else for the Arduino – Paul Terwilliger Oct 10 '14 at 17:03
  • 3
    Do you want to run your compiler on the Arduino ? That seems a bit odd. – microtherion Oct 10 '14 at 17:29
  • Yes I do. I want to be able to insert an SD card with the code and I want the arduino to compile it. – Paul Terwilliger Oct 10 '14 at 17:49
  • Posting the same question on multiple sites is not permitted. Arduino is C++, but beware you have fairly extreme memory limitations. Ordinarily one would want to do any compiling on the device where the text is being provided, and put only the distilled result into the limited resources of the traditional Arduino board. – Chris Stratton Oct 10 '14 at 18:55
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);
| improve this answer | |
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...

| improve this answer | |
  • 1
    For a while, "New" wasn't implemented, but it is now. "malloc" and "free" and the like have always been available on arduino for dynamic memory management. I don't use it often because with such a small amount of RAM I'm always worried about memory fragmentation, but I have used it. – BrettAM Oct 11 '14 at 3:40
  • @Brett nice to know! The OP is using a Due, so RAM should be sufficient for most cases. – Anonymous Penguin Oct 11 '14 at 3:49
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.

| improve this answer | |
  • You have 96 KB of RAM on the Due if I remember right, 2K is on the Uno. That still doesn't make it better... – Anonymous Penguin Oct 11 '14 at 3:08
  • You probably also have hardware capability to execute-from-ram. It really changes the whole story. – Chris Stratton Oct 12 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.