Go Down

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

YemSalat

Nov 08, 2015, 03:22 pm Last Edit: Dec 20, 2015, 10:25 am by YemSalat
Latest upate: link to post

Test the language: http://webcloudtools.com/sprk/lang-test/#hello-world

Project on GitHub: https://github.com/YemSalat/spark-lang

Hi everyone, I've been away from the forum for a while, but recently I have been lurking around, reading the forum posts, being amazed at how often some of the beginner questions come up. And I started thinking:

What would a language designed specifically for Arduino beginners look like?

I think many would agree that even though Arduino provides a lot of helpful materials (libraries, examples, etc.) - the language that it is based on, C++ - can still be a pretty big obstacle in getting beginner projects done.


Here is a short list of things that in my opinion could be improved if there was a language designed specifically for Arduino beginners:

Simplify syntax, remove the 'cruft'

C++ has a pretty complex syntax, which is full of semicolons, star symbols, parenthesis, etc.
By removing most of this stuff the language would look much more approachable (think Python/Ruby).
For example start by making semicolons optional and allowing for simpler code structures.
E.g. what a for loop variation could look like:

a = 25
repeat 10 times { a += 5 }



Inferred types instead of explicit declaration

Don't get me wrong - I am all for strictly typed languages, especially when there is almost no other way around it on embedded systems. But having to declare types, while very important in terms of programming techniques - has very little to do with what the beginner is trying to achieve, mainly finishing their project.


Better error messages

For example instead of saying something like ".. expected ')' before '{' token or .. at line at line .."

It could say - "You are missing a ')', make sure all your opening and closing parenthesis match on line .."
And overall all the errors could be more beginner friendly.


Tailored to the environment

How many times did you have to tell people that they should not use C++ Strings when programming for Arduino? Wouldn't it be nice if things like that were handled on the language level.
Apart from that, I think it would be very helpful to have some entities from the Arduino and micro controllers realm be present in the language. For example, it could have interrupts 'built-in' as a language structure, etc.

For example setting up an interrupt like this:
-------------------------

interrupt when pin 12 is RISING {
    // do stuff..
}

-------------------------



Convention over flexibility

A 'predefined path' can be very helpful to beginners. Languages that allow less flexibility and more conventional approaches to solving common problems tend to be easier to learn as they allow to focus on the primary goal without thinking too much about the implementation itself.


Taking care of performance issues and errors

Having a language that follows strict conventions - allows for better optimizing code and also preventing errors.
For example -  the compiler can check if a variable is ever used inside an interrupt - and make it volatile if the user forgot to do so.


Handy stuff

Some things in Arduino are harder to do then they should be. Things like concatenating strings, checking if one string is contained in another one, etc.
Wouldn't it be great if you could just write:

foo = "Hello"
foo + " World" // "Hello World"



Different approaches

Perhaps it would be good to try different approaches to programming. A semi-declarative way of programming could be easier for beginners to grasp.
Set up everything, then assign what happens when (e.g. when sensor reading is higher then X - turn on the pump)

Here is a quick 'sketch' of what it could look like.

A program where an LED light blinks when a button is pressed:


-------------------------

device LedLight {

    write:  pinLed // pin number

    action: blink {
        pinLed = HIGH
        wait 5s // does not prevent other code execution
        pinLed = LOW
    }
}


device Button ( LedLight led ) {

    read: pinButton
   
    // no need for multiple IF statements
    when pinButton is HIGH {
        led -> blink()
    }
}

led1 = create LedLight on pin 9
button1 = create Button on pin 1

-------------------------


What do you think? Do you reckon any of these ideas are worth considering?

What in your opinion should a beginner-oriented Arduino language look like? Is such a language even needed?


I am gonna try and come up with more uses cases where such approach can be useful, and who knows maybe one day I'll even make it happen :)


[ADDED]
Here is how I see it working:

device is the main entity in the language (think Class)

Every device follows this structure:

device DeviceName ( constructor parameters ) {

    write: pin1, pin2, pin3 // pins that the device can write to
    read: pin1, pin2, pin3 // pins that the device can read from

    // Here go predefined blocks that are built into the class
    init { ... } // basically constructor
    actions { ... } // all object 'methods'
    always { ... } // this code is executed all the time
    ....

    // then goes a list of all 'events' - code that executes when a condition is satisfied
    // they all start with keyword 'when'
    when pin1 is HIGH { ... }
    when pin2 is < 512 and pin3 > 128 { ... }
    when pin4 is RISING { ... }
    ...

}

So there isn't much code outside the devices. (There can be other entities though, like timers for example, with their own set of "actions" and "events")
PS I do appreciate any criticism though, as it is entirely possible that I am missing some huge flaws in this.

pYro_65

If you can write the compiler, go for it. Once started, you'll see the actual effort required to do this task.
What are you going to write it in?... C++??
Forum Mod anyone?
https://arduino.land/Moduino/

Robin2

I think this is probably more appropriate for the Bar Sport section until you come up with a first working draft of your compiler.

(By the way, I agree with a lot of what you say.)

Making the compiler might not be monumentally difficult if you create a program to translate your code into C/C++ that could be processed by the Arduino IDE.

You may be interested in this Mirah project. Look for the Blogs by Charles Nutter who created it. He is very clever and claims that the underlying system can be used for other cross-converting projects.

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

Delta_G

Syntax is the easy part.  Anyone can learn the syntax.  The hard part about programming is the logic and that's the same in any language. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

YemSalat

Of course I am not planning to write my own compiler. Just write a grammar in some appropriate notation and use a compiler-compiler to generate the parser.

I wrote a few toy languages back at uni, with modern tools it is really not that hard (relative to writing your own compiler by hand)

The 'hardest' and most interesting part would be coming up with appropriate language structure. If we can come up with something useful - I will generate a compiler for it.

YemSalat

Making the compiler might not be monumentally difficult if you create a program to translate your code into C/C++ that could be processed by the Arduino IDE.
That is definitely one way of doing it, plus the Arduino IDE would also take care of all the other parts of the toolchain.
Since it accepts pure C/C++ - the compiler will just need to generate the code which fits with the setup / loop routines.
This way would also be easiest for people who don't already have the Arduino IDE installed

[EDIT] Sorry, I think there might be a confusion here, when I say 'compile' I actually mean transpile the language to C/C++ and then just use either Arduino IDE or avr-gcc directly to compile it to machine code. Writing my own compiler for avr would be crazy, I agree :)

Syntax is the easy part.  Anyone can learn the syntax.  The hard part about programming is the logic and that's the same in any language. 
I disagree, syntax has major effect on how easy it is to understand the code, plus syntax is not everything, the way language is structured is very important as well. Compare C to x86 Assembler for example.

UKHeliBob

We surely don't need yet another language.  Whilst it may at first appear that using C++ as the language for Arduino is a hurdle I see it as a positive advantage.  There is tons of help already available in this forum and elsewhere on line if people are willing to look for it or ask questions.

If a new language is to be used then there will initially be no experts or experience in its use and no reference material to go to.  When users want advanced features they will either need to switch to another language, probably C++, or the new language will need to have them built in, adding more complication again.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

YemSalat

Just thinking of how some potential tasks can be solved with the proposed approach.
For example, rotating a stepper motor based on potentiometer reading (basic)

device Stepper {

    write: pinMotor // pin numb er

    // initialize
    init {
        degrees = 0
        self -> rotate( degrees )
        wait 500ms
    }

    // all object 'methods'
    actions {
        rotate ( int deg ) {
            pinMotor = deg
        }
    }

}

device Potentiometer ( Stepper st ) {

    read: pinReading

    // repeat actions within this block continiously
    always {
        st -> rotate( pinReading )
    }

}

stepper1 = create Stepper on pin 9
potentiometer1 = create Potentiometer on pin 1



[EDIT] @UKHeliBob, sorry, did not notice your reply at first.
We surely don't need yet another language.  Whilst it may at first appear that using C++ as the language for Arduino is a hurdle I see it as a positive advantage.  There is tons of help already available in this forum and elsewhere on line if people are willing to look for it or ask questions.
I do not disagree with you, Arduino does have a great (and huge!) community, yet I think it can be useful to think of other ways of solving project-related programming tasks

Quote
If a new language is to be used then there will initially be no experts or experience in its use and no reference material to go to.  When users want advanced features they will either need to switch to another language, probably C++, or the new language will need to have them built in, adding more complication again.
Well, there can be basic documentation and examples :) I think the question of "advanced language features" is not relevant for many Arduino users (which is fine) I think the community might be loosing a lot of people because the technology could be more beginner-friendly. Surely if people want more advanced features - they can use more appropriate tools for that.

Delta_G

Code: [Select]

device Stepper {

    write: pinMotor // pin numb er

    // initialize
    init {
        degrees = 0
        self -> rotate( degrees )
        wait 500ms
    }

    // all object 'methods'
    actions {
        rotate ( int deg ) {
            pinMotor = deg
        }
    }

}

device Potentiometer ( Stepper st ) {

    read: pinReading

    // repeat actions within this block continiously
    always {
        st -> rotate( pinReading )
    }

}

stepper1 = create Stepper on pin 9
potentiometer1 = create Potentiometer on pin 1


I don't see how this is any easier to understand or follow than the equivalent C++ code.  All you've really saved anyone from is a missing semicolon error, and I think that should be the least of the worries.  Most people can get the semicolon there.  That's not so hard.  With this code there is just as much structure to figure out, it's just different. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

Code: [Select]
potentiometer1 = create Potentiometer on pin 1

Take this line for instance.  How is this any easier than creating a potentiometer class in C++ and putting it in a library for the noobs so they can write.

Code: [Select]
Potentiometer myPot(1);  // creates a pot on pin 1

and later

Code: [Select]
int val = myPot.read();

You've just added 3 new keywords to learn and get right, create, on and pin.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

YemSalat

potentiometer1 = create Potentiometer on pin 1

Take this line for instance.  How is this any easier than creating a potentiometer class in C++ and putting it in a library for the noobs so they can write.

Potentiometer myPot(1);  // creates a pot on pin 1

and later

int val = myPot.read();
The point of this is not just a different syntax, but different approach to the whole program structure.
Everything follows a strict convention and a lot of the stuff comes 'out of the box'
Timing is handled for you, so you don't need to worry about delay() halting your program flow.

You would loose some flexibility, but I think many things can be taken out if the language is aimed at beginners (pointer arithmetic for example)

Here is how I see it working:

device is the main entity in the language (think Class)

Every device follows this structure:

device DeviceName ( constructor parameters ) {

    write: pin1, pin2, pin3 // pins that the device can write to
    read: pin1, pin2, pin3 // pins that the device can read from

    // Here go predefined blocks that are built into the class
    init { ... } // basically constructor
    actions { ... } // all object 'methods'
    always { ... } // this code is executed all the time
    ....

    // then goes a list of all 'events' - code that executes when a condition is satisfied
    // they all start with keyword 'when'
    when pin1 is HIGH { ... }
    when pin2 is < 512 and pin3 > 128 { ... }
    when pin4 is RISING { ... }
    ...

}

So there isn't much code apart from within the devices. (There can be other entities though, like timers for example, with their own set of "actions" and "events")

There would be a small runtime that would take care of timing and executing events.
PS I do appreciate any criticism though, as it is entirely possible that I am missing some huge flaws in this.

Delta_G

PS I do appreciate any criticism though, as it is entirely possible that I am missing some huge flaws in this.

Absolutely.  It's nothing personal.  Every idea needs skeptics.  That's how we weed out the shite.  We don't always see the flaws in ideas we're too close to. 

I'm with you that it might be fun to create a new set of syntax and for some projects it might be useful.  I don't see how procedural code would work here once the plan got much bigger than read the pot, scale it, and write PWM to the led.  I'm afraid this would run out of usefulness once the code got any bit of complicated and then the noob would have to go back to square 1 learning C++. 

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

YemSalat

I don't see how procedural code would work here once the plan got much bigger than read the pot, scale it, and write PWM to the led.  I'm afraid this would run out of usefulness once the code got any bit of complicated and then the noob would have to go back to square 1 learning C++.
That's a fair point, that's why I am trying to come up with various use cases where it can be applied.
Overall I am planning to get away with 3 main entities for now: device, timer and state machine. I just haven't settled on all the concepts yet. If someone is planning on writing a web server or something like that - this would most likely not be the right tool for the job. I think it would be more well-suited for home automation projects and  things like that.

Also, its worth noting that a lot of those projects never get "much bigger than read the pot, scale it, and write PWM to the led"

Robin2

@YemSalat, what you are proposing will not really benefit from discussion.

If you can start a Thread in the Other Software Development section with a working version of your "transpiler" so that people can try it out you may get converts.

I would like a Python-like syntax simply so that I don't get confused switching between Python and Arduino code.

I was almost tempted to say I would like a Ruby-like syntax, except that I abandoned Ruby in favour of Python simply because Python has a better infrastructure and is more widely used (at least in the Arduino context).

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

MarkT

Have you looked at https://micropython.org/ ?  I keep meaning
to and see if its a good fit for this kind of use (presumably its meant to be!)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up