A New Language For Programming Microcontrollers

Thread Starter

ajm113

Joined Feb 19, 2011
174
Hello all, I've been working on my little projects of do-dads and what not for a little bit using Great Cow Basic, and I'm getting more and more adjusted to Assembly Language and not being so "scared" of it. The more and more I look at the structure of Assembly language, it's not even close to complicated as C++ syntax. Anyways, I have a idea to write a new language called "Shawarma" for a future project and just wanted input on the ideas and concepts.

The hole idea of this language is to really make it straight forward and easy to remember without having to use a IDE such as Great Cow which is still covered in bugs. How I plan on accomplishing a task like this is to write a Shawarma compiler that takes the source code and converts it to a assembly and then takes that assembly source file into a assembler automatically which would output a hex file.


Here is a example Shawarma source file:
Rich (BB code):
#device PIC16F84
#description "This is a test!"
#author "Ajm113"
#config _RC_OSC + WDT_FF + _PWRTE_ON

//MACRO Test
#define LED PORTA.0

//Variable Test
byte ProgramValue = 0
bool bCanWait = false
int  iNumber = 0
Array array_test = new Array[100]

MainLoop:
Dir LED Output
Dir PORTB.0 Input
Set LED High

If bCanWait
	wait 1000
EndIf

Set LED Low


while true

	If True
		break
	Endif

EndWhile


For iNumber = 0, i < 10, iNumber++


EndFor

	Goto MainLoop


// This function just returns true.
// Doesn't really do anything...
Func bool Test_Func byte x, bool y 

//....
	return true
EndFunc
I wanted the language to feel like a cross with C++, and BASIC, but make it very easy to use for even a 12 year old to use it and could even start writing addons to the language for different microcontrollers out there after a couple of weeks understanding writing a program.

Key Features of Shawarma (Compiler):
Not Case Sensitive.
Auto Variable Types.
(e.g: x = 0 automatically be seen as "int x = 0")
Easy to define microcontroller headers for macros/error handling.
Microcontroller cross platform capabilities for different assemblers.
USB Communication Functions for USB enabled microcontrollers, LCD Functions, and
more.

So how does it sound?

If your wondering why I bother is because I want the community to grow, and make people feel less they have to be richy rich to pay for a compiler that is $400 to $800 dollars to give them verity of languages they can use.

I have 6 years of prior experience writing in C++ from everything from 3D file format loading, OpenGL rendering, Windows and Mac programming, and I can put the hole compiler together on my own around 5 months of building, debugging and testing. I don't mind working on a project like this alone, but a team of more then one person would be nice. People who are depended to their work would be great, even if they can't write code to save their life. ;)
 
Last edited:

Ctenom

Joined Nov 1, 2010
59
Id like the idea of basic with the ability to wright inline C. Like we do now putting Assembly code inline with C. If for eg you had x=0 as int x=0; and the code later needed to divide a odd number by 2 i would want to be able to define x as a double/float. What i would like to see is more code prediction like they are doing with Mplab X. Only more like Visual C++/Basic. The cross platform idea is awesome I'm always trying to recode peoples Atmel/Arduino code for use in my PIC projects.

Just my 2 cents i am very new at coding and someone will probably disagree with everything i just said lol.
 

Thread Starter

ajm113

Joined Feb 19, 2011
174
x=0 as int x=0; and the code later needed to divide a odd number by 2 i would want to be able to define x as a double/float.
I don't think something like that wouldn't be too hard to implement, while the code is compiling, I'll just have it watch for any dividing commands and convert the variable to a float/double on the output asm file, so I can change variable size a head of time.

What i would like to see is more code prediction like they are doing with Mplab X. Only more like Visual C++/Basic
Well I'm not at the point yet to talk about writing a IDE, but personally I dont think writing something like that wouldn't be too hard. I'll keep writing a IDE in my maybe bin too with Shawarma project when the compiler development goes underway. Writing the IDE not to keep tooting my own horn, shouldn't be hard at all since all would be is a advanced notepad, but two people working on two programs would be great and save some time if I can find a team member. One idea I just got for the IDE is to add a convert button. So with one click it automatically converts the pin MACROs to the device your changing it to, to save time converting.

The compiler would work with Atmel no problem, but with Arduino is questionable. One, there is a boot loader already on the micro. Second, I'm not sure how it handles programs written, or even if it Arduino's IDE even writes hex files.

Sorry for the typos.
 

RiJoRI

Joined Aug 15, 2007
536
I'd suggest you look for Jack Crenshaw's Let's Build a Compiler! articles. While the original work was in Pascal and for the 68000, I know there was a version in C. While Mr. C. did not complete the articles (he said life got in the way... :( ) the articles are VERY good as far as they go. AND you'll have a working compiler from the start. (OK, a VERY limited compiler, but compile it does!)

--Rich
 

Thread Starter

ajm113

Joined Feb 19, 2011
174
Wow thank you for the very helpful replies! I've been looking at Jack's source code examples and even though I never used that language before, it's quite helpful to help me translate it into C++ so I can very easily cross platform it on Mac, Linux and Windows. I was just thinking too, when I start collage I should bring this up to the computer engineering teacher and ask if it's possible to get recruitment for this open source project. I've heard some collages will support projects like these at times, but I'm not sure if I want to wait all the way till spring to do so... =/

Also would it make the programming language a lot more flexible if I allowed programmers to use assembly language commands in Shawarma? Just like C++ does?

I was thinking of something along the lines of this:
Rich (BB code):
Set PORTA.0 High

// Do a long complicated process.
ASM Begin
movlw 2Ah
;.... More code goes here..
ASM End

//...

END
I've even tested Shawarma language with my own mother to see how well she understands it by looking at it and for the most part a lot of it makes sense. She didn't understand the for loops too well, but as long most of it made sense, it should be very easy for anyone to use this language.
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
Have you seen Eclipse? It's an open source IDE. I know this is probably for a later stage, if at all, but worth looking at.
 

RiJoRI

Joined Aug 15, 2007
536
...
Also would it make the programming language a lot more flexible if I allowed programmers to use assembly language commands in Shawarma? Just like C++ does?
...
As a working professional. I would say "allow assembly!" There are two cases in which I have used assembler blocks:

(1) The compilation of the C source is bloated.
(2) Assembly allows me to use a command that the compiler is unaware of, such as register-indexed, register-offset bit operations. (sbit X, -- set the Xth bit (0..0xFFFF) from the location B (0..0xFFFF) is pointing to.)

The only warning I have is do not use "fancy" ASM/ENDASM markers. The compiler uses /$ and $/ for ASM and ENDASM markers. This means my C code causes many, if not all, C code analyzers to fail.

--Rich
 

Thread Starter

ajm113

Joined Feb 19, 2011
174
Have you seen Eclipse? It's an open source IDE. I know this is probably for a later stage, if at all, but worth looking at.
I've used Eclipse a lot of times using Java so I'm pretty familiar to it's UI and functions when writing code which I think are pretty cool, but I still kinda prefer the make my own from scratch route. If your wondering why I would choose this is because there is less code and configurations I would have to worry about or it would take some time to get my head around it when I could spend more time on the compiler. Plus if I written my own, I can very easily add functionality or features Eclipse doesn't support.

Thank you for the great recommendation though. :)

As a working professional. I would say "allow assembly!"
Thank you for your feedback about assembly support. :) I will indeed add it! It would make it much more flexible to write libraries for people's source code to gain more access to more functions.

The only warning I have is do not use "fancy" ASM/ENDASM markers. The compiler uses /$ and $/ for ASM and ENDASM markers. This means my C code causes many, if not all, C code analyzers to fail.
I like that idea to add "/$" and "$/", it does also make it faster to parse compared to "ASM Begin", which could also cause name space pollution if someone else wanted to call a function named ASM.

Thanks!

Project Update: I went ahead and started on the IDE last night and worked on it till 3AM, so far I got everything from a UI that has editable text with line numbers listed on the left side that also saves and opens source files. Tonight I'm going to recreate the wheel here and recreate the text edit area with my own so I can add color syntax, auto complete, and then get started on the compiler after my warm up. ;)
 

debjit625

Joined Apr 17, 2010
790
Array array_test = new Array[100]
Why you are using the C++ syntax, its for dynamic memory allocation its only used if their is some kind of memory management stuff and in case of embedded system like for mcu ,that is useless unless the compiler is meant for writing application for some RTOS which have memory management. I think you are building a generic compiler for microcontrollers or microprocessor so rather deriving ideas from C++ ,derive ideas from C which is a system programming language. C++ is meant for application programming it have a very big difference from C. In embedded world we don't use C++.A simple way for defining array will be good.
int myarray[100]
int myarray[10] = {0,1,2,3,4,5,6,7,8,9}
char myarray[] = {'A','B','C','D'}
char myarray[] = "ABCD"
Auto Variable Types.
(e.g: x = 0 automatically be seen as "int x = 0")
I will say its a bad choice as programmers will have no control over data types. And many other problem like for example casting....

I don't think something like that wouldn't be too hard to implement, while the code is compiling, I'll just have it watch for any dividing commands and convert the variable to a float/double on the output asm file, so I can change variable size a head of time.
That will make problem, sometimes we divide two variables of float kind and use the integer part of the result i.e.. the numbers before decimal points, in Lang C we just make the variable for result as integer type and auto cast happens, in that case your compiler will convert everything to float and we can't cast stuff as you are using auto variables.
USB Communication Functions for USB enabled microcontrollers, LCD Functions, and
more.
In a compiler we don't need these functions it should be part of library as their are 1000s of LCD and you can’t integrate every protocol in compiler and even it goes for USB.

Don't be demoralized by my comments, I am just pointing you some errors that you may face.

Good Luck
 

Thread Starter

ajm113

Joined Feb 19, 2011
174

Why you are using the C++ syntax, its for dynamic memory allocation its only used if their is some kind of memory management stuff and in case of embedded system like for mcu ,that is useless unless the compiler is meant for writing application for some RTOS which have memory management. I think you are building a generic compiler for microcontrollers or microprocessor so rather deriving ideas from C++ ,derive ideas from C which is a system programming language. C++ is meant for application programming it have a very big difference from C. In embedded world we don't use C++.A simple way for defining array will be good.
Yes, your are right and it would make a lot more sense just to use non dynamic array syntax and go with some such as "variable[100]".


I will say its a bad choice as programmers will have no control over data types. And many other problem like for example casting....
Sorry I didn't make that part too clear, auto data types would be optional, you can still use stuff such as e.g:

Rich (BB code):
int x
word y
byte z
string msg
The compiler would just throw a error if the variable is uninitialized if it doesn't have a datatype before it's name, for much more accurate data types from auto data variables.

That will make problem, sometimes we divide two variables of float kind and use the integer part of the result i.e.. the numbers before decimal points, in Lang C we just make the variable for result as integer type and auto cast happens, in that case your compiler will convert everything to float and we can't cast stuff as you are using auto variables.


I think I would be able to work with a problem like that and just have it work for e.g:

Rich (BB code):
//I'm a int!
y = 0

//Keep me as a int.
y = (int)2/5
When it see's that it will automatically know not to make that variable a float and instead if you don't use casting the compiler will think for you on choosing data types.

In a compiler we don't need these functions it should be part of library as their are 1000s of LCD and you can’t integrate every protocol in compiler and even it goes for USB.
Yeah that is very true, plus I could just focus more on the structure and have less executable size on the compiler and just write a library handler to do all the work of loading library files and using them for source files people are writting.

Don't be demoralized by my comments, I am just pointing you some errors that you may face.
Oh by all means, it's perfectly fine, criticism is the best tool for developing good software, thank you very much I appreciate it!. ;)


Project Update: I've attached a preview image of the IDE WIP, so far I have basic syntax highlighting down, better looking left margin for line numbers. I didn't have a lot of time last night to get the compiler going because of technical difficulties with the library I was using to help with the syntax highlighting. :p I'll get started on the compiler tonight.
 

Attachments

Thread Starter

ajm113

Joined Feb 19, 2011
174
Right now at the moment, I'm embedding the compiler and the IDE so the I set up the IDE to read a log after the compiler is done to look at errors or warnings with the script.

I've basically have some pretty basic stuff being written with the compiler such as ignoring comments, Assembly Language Snippets. Then start blue printing class structures for better planning what each class does and being able to get them to work together and make them easy to work with and of course adding some features for fast parsing. I do have a question for the community though.

What should the library "import" command should look like? I want it to be original looking and not too obvious, but easy to remember. I was thinking on the lines of something like this:

Rich (BB code):
//swl = Shawarma Library File

//E.g #1:
#importLib "Somelibrary.swl"

//E.g #2:
#use "Somelibrary.swl"

//E.g #3:
#require "Somelibrary.swl"
Let me know if if you like any of these or have one your own. :)

Thanks, Andrew.
 

pilko

Joined Dec 8, 2008
213
Will the language be similar or at least as simple as "Picaxe Basic".
I am very interested as I have found this very easy to learn (even in my senior years)?

Regards

pilko
 

Thread Starter

ajm113

Joined Feb 19, 2011
174
Hello pilko!

Yes, if you read the example I posted it's not as much different.

Here I'll show you them side by side:

PICAxe Basic Code:
Rich (BB code):
symbol LED  = B.4 ; rename output4 ‘LED’
symbol buzzer = B.2 ; rename output2 ‘buzzer’

main: ; make a label called ‘main’
     high LED ; LED on
     low buzzer ; buzzer off
     pause 1000 ; wait 1 second (1000 ms)
     low LED ; LED off
     high buzzer ; buzzer on
     wait 1 ; wait 1 second
goto main ; jump back to the start
Shawarma Language:

Rich (BB code):
#define LED  B.4 // rename output4 ‘LED’
#define buzzer B.2 // rename output2 ‘buzzer’

//Set the port directions to output:
     Dir LED Output
     Dir BUZZER Output

main: // make a label called ‘main’

     SET LED HIGH // LED on
     SET buzzer LOW // buzzer off
     WAIT 1000 ms // wait 1 second (1000 ms)
     SET LED LOW // LED off
     SET BUZZER HIGH // buzzer on
     WAIT 1 s // wait 1 second

goto main // jump back to the start
It's really not too much different, except for setting the pins to input or output and replacing "symbols" to "#define". I know C++ is a hard language to learn at the beginning, but I want to introduce some syntax thats close to it. So your only getting a tea spoon of it here or there then a full drink.

Now I haven't coded on a PIC AXE before, but if I can write assembly to a assembler for it, I just might be able to work with it or unless I think of a way to allow the compiler to work with foreign languages to make it more much more cross-platformable so it could maybe even work with Arduino, but thats kinda thinking a little bit too much of a head from this point. I can set it as a goal one day though when I get my language working with PICs and atmel's microcontrollers.
 
Last edited:

kubeek

Joined Sep 20, 2005
5,794
The GOTO statement was deem as toxic and abandoned in the 1960s and replaced with DO-WHILE and REPEAT-UNTIL.
Stupid use of goto is toxic indeed, but there are many situations where it is useful. For example see here. Also don´t forget that assembly is just a huge heap of conditional an unconditional gotos.
 

MrChips

Joined Oct 2, 2009
30,706
I have been teaching Structured Programming for the past 40 years. I have seen firsthand how code can become a bowlful of spaghetti if GOTO is used improperly.

Here are four structures you should adhere to:

1. Simple sequence. One entry point. One exit point.
2. IF-THEN-ELSE
3. DO-WHILE or REPEAT-UNTIL
4. CASE

Asm allows you to use GOTO and that is ok. Any high level language should avoid the use of GOTO and stick with the above four structures. In my 40 years as a professional programmer I have never found the need for a GOTO.

There are times when it is desirable to reinvent the wheel. With past experience we can learn from our mistakes and can do a better job the next time around.

(In terms of language implementation, it would be a simple thing to include and leave it up to the educated user not to use it. It's a bit like giving someone sufficient rope to hang himself.)
 
Last edited:

Thread Starter

ajm113

Joined Feb 19, 2011
174
I can see how it can turn code into something such as a pain to debug if used in properly, I'll be sure to include to add loops with things such as break or continue then and use more examples that use them with Shawarma language for the new guys.

The only time I would use goto is when it comes to faster array search and destroy methodes to also avoid crashes when my "for" loop needs to restart it's self. Though it could be rewritten as the for loop being placed in a while loop and when the for loop cant find anything it can break the while loop with a simple if statement.

Update: I decided to use " #importLib "somelib.swl" " as a library loading call.
 
Last edited:
Top