Latest upate: link to post
Test the language: http://webcloudtools.com/sprk/lang-test/#hello-world
Project on GitHub: GitHub - YemSalat/spark-lang: Sparklang, aka Sparc(k) compiler; New experimantal programming language for Arduino and microcontrollers.
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
[color=#5f3e31]repeat[/color] 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:
[color=gray]-------------------------[/color]
[color=#5f3e31]interrupt[/color] [b]when[/b] pin 12 [b]is[/b] RISING {
// do stuff..
}
[color=gray]-------------------------[/color]
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 = [i]"Hello"[/i]
foo + [i]" World"[/i] [color=gray]// "Hello World"[/color]
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:
[color=gray]-------------------------[/color]
[i]device[/i] [color=green][b]LedLight[/b][/color] {
[color=#5f3e31]write:[/color] pinLed [color=gray]// pin number[/color]
[color=#5f3e31]action:[/color] [color=blue]blink[/color] {
pinLed = [b]HIGH[/b]
wait [b]5s[/b][color=gray] // does not prevent other code execution[/color]
pinLed = [b]LOW[/b]
}
}
[i]device[/i] [color=green][b]Button[/b][/color] ( [i]LedLight led[/i] ) {
[color=#5f3e31]read:[/color] pinButton
[color=gray]// no need for multiple IF statements[/color]
[b]when[/b] pinButton [b]is[/b] HIGH {
led ->[color=blue] blink()[/color]
}
}
[i]led1[/i] = [b]create[/b] LedLight on pin 9
[i]button1[/i] = [b]create[/b] Button on pin 1
[color=gray]-------------------------[/color]
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:
[i]device[/i] [b][color=green]DeviceName[/color][/b] ( [i]constructor parameters[/i] ) {
[color=#5f3e31]write:[/color] pin1, pin2, pin3 [color=gray]// pins that the device can write to[/color]
[color=#5f3e31]read:[/color] pin1, pin2, pin3 [color=gray]// pins that the device can read from[/color]
[color=gray]// Here go predefined blocks that are built into the class[/color]
[color=#5f3e31]init[/color] { ... } [color=gray]// basically constructor[/color]
[color=#5f3e31]actions[/color] { ... } [color=gray]// all object 'methods'[/color]
[color=#5f3e31]always[/color] { ... } [color=gray]// this code is executed all the time[/color]
....
[color=gray]// then goes a list of all 'events' - code that executes when a condition is satisfied[/color]
[color=gray]// they all start with keyword 'when'[/color]
[b]when[/b] pin1 [b]is[/b] HIGH { ... }
[b]when[/b] pin2 [b]is <[/b] 512 [b]and[/b] pin3 > 128 { ... }
[b]when[/b] pin4 [b]is[/b] 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.