Go Down

Topic: Designing a new programming language for Arduino (Read 15185 times) previous topic - next topic

Robin2

I would still like to be able to save user the hassle of having to calculate sizes when the new string must contain both substrings completely.
Regarding the same problems as the original String class - I do want to avoid that kind of stuff, but everywhere I read it seems like snprintf is a really safe option.
I have not studied the code  behind the String class but my understanding is that there are, potentially, two types of problem. One is that it frequently creates new copies of a string while it is doing its work and can therefore use up a lot of memory that the user cannot be aware of. The other is that it may not release all the unused memory in an adequate fashion.

My strong sense is that what you say in the first sentence I have quoted will have a similar effect of consuming memory that the user has not allocated, or made allowance for in his/her program design. It matters little that it might be a tidier user of memory than the String class - that just postpones the crash.

I also think it is psychologically a good thing to force users to be aware of the memory limitations rather than hide it all from them. Hiding the C functions such as strcpy() will be sufficient.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

YemSalat

Quote
I also think it is psychologically a good thing to force users to be aware of the memory limitations rather than hide it all from them. Hiding the C functions such as strcpy() will be sufficient.
I agree with you on this, I'll probably leave this idea for later and will implement simple string concatenations for now (the ones with specified length limits)

I might create a separate thread later in the Programming section to get more info on the subject of strings on Arduino (I think this topic might be too big to cover within this thread)

YemSalat

#107
Dec 17, 2015, 01:28 pm Last Edit: Dec 17, 2015, 04:07 pm by YemSalat
[UPDATE]

A quick update on the compiler progress. Link to test: Test Environment

I am finally done with refactoring the function declarations, so now they also get evaluated by the semantic analyzer:

(full image)

You can have functions with the same name, but different signatures (signatures must be unique).

The analyzer currently runs the following checks on each function declaration:

- Check if function was already declared
- Check if function contains duplicate parameters
- Check if function returns a value (this is skipped for void functions)
- Check if all function return statements have the same type as the function declaration

Please let me know if you find any errors in the implementation.
Thanks in advance!

PS I am also thinking of adding short-hand function declarations, using -> operator
So, for example, instead of writing:
int add (int a, int b) {
  return a + b
}


You could write:
int add (int a, int b) -> a + b

Robin2

PS I am also thinking of adding short-hand function declarations, using -> operator
So, for example, instead of writing:
int add (int a, int b) {
  return a + b
}


You could write:
int add (int a, int b) -> a + b
May I respectfully suggest that it is now time to STOP.

That concept would require users to learn a new idiom that has no parallels in regular Arduino programming.

Ruby-on-Rails was a great idea just before they moved on to Version 2 (dunno where they are now) but then it just got more and more complex because IMHO the developers could find nothing more interesting to occupy them.  :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

YemSalat

#109
Dec 18, 2015, 05:32 am Last Edit: Dec 18, 2015, 05:42 am by YemSalat
May I respectfully suggest that it is now time to STOP.
Yep, fair enough, hehe, I can get carried away sometimes. I agree that its probably best not to introduce any 'brand-new' syntax (at least at this stage) I might save this idea for later.

I am working on evaluating function calls right now and will be uploading more updates later today.


PS
I never really got into RoR (played a bit with one of the earlier versions), I think it could be because I had some bad experiences in the past trying to install a couple apps that were written in it. But your 'analogy' actually made me curious to go and check it out, maybe save some refactoring time in the future :)

Robin2

I had some bad experiences in the past trying to install a couple apps that were written in it.
One of the fascinating things is that they developed a really clever system for simpifying web development but completely neglected the "infrastructure" needed to make it convenient to use it. Eventually some other guy developed "Passenger" aka "modRails" for easy deployment with Apache - as PHP could do all along.

For similar reasons I finally gave up Ruby/JRuby in favour of Python. But I had abandoned RoR before that in favour of Sinatra. The equivalent for Python is Bottle.

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

pYro_65

Yep, fair enough, hehe, I can get carried away sometimes. I agree that its probably best not to introduce any 'brand-new' syntax (at least at this stage) I might save this idea for later.
Well actually, its not as new or unrelated of a concept in C++ as you might think. Your simple example would easily translate to a lambda.


auto func = [](int a, int b){ return a + b; };

//...

int sum = func(4, 5);


They are quite useful with code that requires lots of callbacks which may do very little, they can be used as nested/sub-routines (defined inside another function, not just globally) and can be completely inline.

Code: [Select]
template< typename Func > void longTaskWithCallback( Func func );

void setup() {

  Serial.begin(9600);

  //Call longTask.. and do something when complete.

  longTaskWithCallback([](){
    Serial.print( "done" );
  });

}

void loop(){}

template< typename Func > void longTaskWithCallback( Func func ){

    //Do something long

    //Run callback function
    func();
}


Forum Mod anyone?
https://arduino.land/Moduino/

Robin2

Your simple example would easily translate to a lambda.
I don't think it would help to expose beginners to that sort of complexity - and the purpose of @YemSalat's work is to make life easier for beginners.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

YemSalat

Well actually, its not as new or unrelated of a concept in C++ as you might think. Your simple example would easily translate to a lambda.
I will definitely need to look more into it. I have never really used lambdas in C++, but they do seem very useful (as you said, perfect for short callbacks, etc.). However, it seems like they have only been added in c++11? Do you know if all Arduino IDE versions support it?

As @Robin mentioned, I am currently focusing on making the language beginner-friendly, so it is probably best not to introduce lambdas at this point. If I ever do in the future - I will also need to make sure that the syntax is simple enough and does not conflict with other language constructs.

pYro_65

#114
Dec 20, 2015, 06:25 am Last Edit: Dec 20, 2015, 06:30 am by pYro_65
I will definitely need to look more into it. I have never really used lambdas in C++, but they do seem very useful (as you said, perfect for short callbacks, etc.). However, it seems like they have only been added in c++11? Do you know if all Arduino IDE versions support it?

As @Robin mentioned, I am currently focusing on making the language beginner-friendly, so it is probably best not to introduce lambdas at this point. If I ever do in the future - I will also need to make sure that the syntax is simple enough and does not conflict with other language constructs.
IDE 1.6.6 and above have it enabled by default.

I noticed you are interested in ranged for loops. These exist in C++11 also. For example:

Code: [Select]
float data[10];

for( float &element : data ){

  //use the element
  Serial.print( element, 3 );
  element = 3.14f;
}

//you could simply replace the ':' with the word 'in' and use a reference by default for anything larger than a char
// (and maybe 'for' to 'foreach' << would be easier to do the grammar maybe):

foreach element in data {

}


Although I do not think it is a good idea having the brackets optional, and the curly brackets mandatory. Just enforce both.

All ranged for loops in C++ can be equivalently written as a standard for loop in the older C++98 which the previous IDE's used.

There is also an std template (or I have my own) that will allow you to do numerical ranges, mine will also allow stepping > 1 and reverse iteration. Similar to the boost counting range: http://www.boost.org/doc/libs/1_47_0/libs/range/doc/html/range/reference/ranges/counting_range.html

Forum Mod anyone?
https://arduino.land/Moduino/

YemSalat

#115
Dec 20, 2015, 07:33 am Last Edit: Dec 20, 2015, 10:21 am by YemSalat
IDE 1.6.6 and above have it enabled by default.
Good to know, thanks.
And yes, I would like to add ranged loops, I have also been thinking about having a 'for .. in ..'  syntax to express them.
Its nice that C11 supports them out of the box, but it probably makes sense to generate C++98 as you said.
In terms of writing the grammar - there is really not much difference between having a 'for' or 'foreach' to indicate a ranged loop, but I would really like to get away with just using 'for' (it currently also replaces 'do' and 'while' loops)
PS I'd be interested to see your example as well if you don't mind?

Quote
Although I do not think it is a good idea having the brackets optional, and the curly brackets mandatory. Just enforce both.
To be honest, after a couple weeks of constantly testing the language - I just got really used to omitting the brackets. But you are not the first one to mention this, so I will probably change this in the nearest version.



[ A quick Update ]

I spent some more time on function evaluation. There are still a couple things to tweak, but overall I am quite happy with it.

I also added default function argument values. It uses a well-established syntax (e.g. like Python)
int multiply (int a, int b = 2) {
  return a * b
}


Now if you call:
multiply(7) - with just one parameter,
the compiler will automatically add the default one:
multiply( 7, 2 )

Right now the default values can only be literals (strings, numbers, etc.) but I am planning to allow using constss as well.
All default params - must be declared last, so you can't have this:
int multiply(int a=1, int b) {} // (compiler will throw an error)

What I really like about this feature is that all the default parameters are resolved during compilation, so there is no overhead for using them. It is of course mostly just a developer convenience.

You can check the latest version here http://webcloudtools.com/sprk/lang-test/#functions
(I updated the functions example)

Next step is gonna be adding arrays and working on string concatenation.

pYro_65

Good to know, thanks.
And yes, I would like to add ranged loops, I have also been thinking about having a 'for .. in ..'  syntax to express them.
Its nice that C11 supports them out of the box, but it probably makes sense to generate C++98 as you said.
In terms of writing the grammar - there is really not much difference between having a 'for' or 'foreach' to indicate a ranged loop, but I would really like to get away with just using 'for' (it currently also replaces 'do' and 'while' loops)
PS I'd be interested to see your example as well if you don't mind?
I just mentioned that it was translatable to C++98, however I do not recommend it, no need to go backwards. The newer compiler has far more optimisations (and the changes in the standard allow for more) which are going to help, especially with generated code.

In the newer IDE you can make your own toolchain (just copy and modify the AVR one). Then you can run your own compilation recipe and run your process and pass the finished C++ source directly to the compiler. No need for a new IDE, just add in your own features that people can download from the board manager.

I can upload my lib to GitHub for you later. For now there is a few versions of forward iterators here (only forward & step of 1):

http://stackoverflow.com/questions/7185437/is-there-a-range-class-in-c11-for-use-with-range-based-for-loops

I used these in the past before I went further and made my own with more functionality.


Forum Mod anyone?
https://arduino.land/Moduino/

YemSalat

#117
Dec 20, 2015, 09:12 am Last Edit: Dec 20, 2015, 09:41 am by YemSalat
The newer compiler has far more optimisations (and the changes in the standard allow for more) which are going to help, especially with generated code.
I think you are right, I'll start looking more in that direction.

In the newer IDE you can make your own toolchain (just copy and modify the AVR one). Then you can run your own compilation recipe and run your process and pass the finished C++ source directly to the compiler. No need for a new IDE, just add in your own features that people can download from the board manager.
That's a good idea actually ( I did not know about the official IDE update when I started ), plus it will be quicker to do than finishing my own IDE since I already have the compiler binary. I'll look into it when I have time.

I would still like to release my own IDE as well though, because it allows me to add more 'UX' features that aid development.
Like real-time error checking, jumping to definitions, etc. It is just quicker to add these to my own IDE, rather then developing separate plugins for the official one; I might do that at a later stage.
The last feature that I was working on for the IDE is actually quite useful - its a side panel that, whenever you place the cursor anywhere in the code - shows you the complete scope ( all the variables that are 'visible' at that location ). It also sorts them by scope 'depth' and allows jumping to the locations where each variable was declared which can be really helpful.

By the way, please don't be mislead by the link I posted above (http://webcloudtools.com/sprk/lang-test/) - this is not the actual IDE that I am working on, its just a quick way to review some of the language features.

I will be releasing the IDE once the language is more complete (most likely after new year)


PS thanks for the link, would be good if you could upload yours later, but I think I got the general idea.

Robin2

@YemSalat, please don't force people to use the latest Arduino IDE. I mostly use 1.5.6 but I have some stuff that I need to use 1.0.6 for - I have not yet figured why.

Also, be aware that the Arduino development folks don't seem to bother at all with backwards compatibility.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pYro_65

#119
Dec 20, 2015, 11:30 am Last Edit: Dec 20, 2015, 11:31 am by pYro_65
@YemSalat, please don't force people to use the latest Arduino IDE. I mostly use 1.5.6 but I have some stuff that I need to use 1.0.6 for - I have not yet figured why.

Also, be aware that the Arduino development folks don't seem to bother at all with backwards compatibility.

...R
Unfortunately every new board will require it and there is little reason to stay in the dark ages. That compiler is over 10 years old.

What is the problem? It can't be made compatible with the older IDE's. You will have to be willing to copy the generated code into the IDE and compile it... with every change... yeah right!

You'll still need another IDE to be remotely useful, so it might as well as be one that (eventually) most will have.

On a side note: If you have a problem with the new IDE, start a thread and we can help you knuckle out a solution...

@YemSalatOn the contrary to robins argument if you force people to use a separate IDE, you are going to significantly reduce the number of people that can/will use your language. Think schools/universities that are quite selective in what they install on their machines.

Hanging onto the past is why internet technologies are so bloated like writing CSS. Adobe flash is still around because people refuse to stop using it, while HTML5 makes it completely unnecessary.

Forum Mod anyone?
https://arduino.land/Moduino/

Go Up