A New Language For Programming Microcontrollers

Discussion in 'The Projects Forum' started by ajm113, Oct 9, 2011.

  1. ajm113

    Thread Starter Member

    Feb 19, 2011
    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:
    Code ( (Unknown Language)):
    2. #device PIC16F84
    3. #description "This is a test!"
    4. #author "Ajm113"
    5. #config _RC_OSC + WDT_FF + _PWRTE_ON
    7. //MACRO Test
    8. #define LED PORTA.0
    10. //Variable Test
    11. byte ProgramValue = 0
    12. bool bCanWait = false
    13. int  iNumber = 0
    14. Array array_test = new Array[100]
    16. MainLoop:
    17. Dir LED Output
    18. Dir PORTB.0 Input
    19. Set LED High
    21. If bCanWait
    22.     wait 1000
    23. EndIf
    25. Set LED Low
    28. while true
    30.     If True
    31.         break
    32.     Endif
    34. EndWhile
    37. For iNumber = 0, i < 10, iNumber++
    40. EndFor
    42.     Goto MainLoop
    45. // This function just returns true.
    46. // Doesn't really do anything...
    47. Func bool Test_Func byte x, bool y
    49. //....
    50.     return true
    51. 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

    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: Oct 10, 2011
  2. Ctenom


    Nov 1, 2010
    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.
  3. ajm113

    Thread Starter Member

    Feb 19, 2011
    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.

    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.
  4. RiJoRI

    Well-Known Member

    Aug 15, 2007
    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!)

  5. DickCappels


    Aug 21, 2008
    The best approach, in my opinion is to write it for yourself and then trow it out there and see whether others take to it.
  6. MrChips


    Oct 2, 2009
    It can be done. I wrote my own assemblers for 6800, 6805, 6809, HC11, AVR and I'm still using them today.
  7. ajm113

    Thread Starter Member

    Feb 19, 2011
    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:
    Code ( (Unknown Language)):
    3. Set PORTA.0 High
    5. // Do a long complicated process.
    6. ASM Begin
    7. movlw 2Ah
    8. ;.... More code goes here..
    9. ASM End
    11. //...
    13. 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: Oct 10, 2011
  8. Markd77

    Senior Member

    Sep 7, 2009
    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.
  9. RiJoRI

    Well-Known Member

    Aug 15, 2007
    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.

  10. ajm113

    Thread Starter Member

    Feb 19, 2011
    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. :)

    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.

    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.


    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. ;)
  11. debjit625

    Well-Known Member

    Apr 17, 2010

    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.
    I will say its a bad choice as programmers will have no control over data types. And many other problem like for example casting....

    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.
    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
  12. ajm113

    Thread Starter Member

    Feb 19, 2011
    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]".

    Sorry I didn't make that part too clear, auto data types would be optional, you can still use stuff such as e.g:

    Code ( (Unknown Language)):
    2. int x
    3. word y
    4. byte z
    5. 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.

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

    Code ( (Unknown Language)):
    2. //I'm a int!
    3. y = 0
    5. //Keep me as a int.
    6. 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.

    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.

    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.
  13. ajm113

    Thread Starter Member

    Feb 19, 2011
    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:

    Code ( (Unknown Language)):
    2. //swl = Shawarma Library File
    4. //E.g #1:
    5. #importLib "Somelibrary.swl"
    7. //E.g #2:
    8. #use "Somelibrary.swl"
    10. //E.g #3:
    11. #require "Somelibrary.swl"
    Let me know if if you like any of these or have one your own. :)

    Thanks, Andrew.
  14. pilko

    Active Member

    Dec 8, 2008
    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)?


  15. ajm113

    Thread Starter Member

    Feb 19, 2011
    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:
    Code ( (Unknown Language)):
    2. symbol LED  = B.4 ; rename output4 ‘LED’
    3. symbol buzzer = B.2 ; rename output2 ‘buzzer’
    5. main: ; make a label called ‘main’
    6.      high LED ; LED on
    7.      low buzzer ; buzzer off
    8.      pause 1000 ; wait 1 second (1000 ms)
    9.      low LED ; LED off
    10.      high buzzer ; buzzer on
    11.      wait 1 ; wait 1 second
    12. goto main ; jump back to the start
    Shawarma Language:

    Code ( (Unknown Language)):
    2. #define LED  B.4 // rename output4 ‘LED’
    3. #define buzzer B.2 // rename output2 ‘buzzer’
    5. //Set the port directions to output:
    6.      Dir LED Output
    7.      Dir BUZZER Output
    9. main: // make a label called ‘main’
    11.      SET LED HIGH // LED on
    12.      SET buzzer LOW // buzzer off
    13.      WAIT 1000 ms // wait 1 second (1000 ms)
    14.      SET LED LOW // LED off
    15.      SET BUZZER HIGH // buzzer on
    16.      WAIT 1 s // wait 1 second
    18. 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: Oct 14, 2011
  16. MrChips


    Oct 2, 2009
    The GOTO statement was deem as toxic and abandoned in the 1960s and replaced with DO-WHILE and REPEAT-UNTIL.
  17. ajm113

    Thread Starter Member

    Feb 19, 2011
    May I ask why it was? 0.o I never had any issues with goto on any language.
  18. kubeek

    AAC Fanatic!

    Sep 20, 2005
    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.
  19. MrChips


    Oct 2, 2009
    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.
    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: Oct 14, 2011
  20. ajm113

    Thread Starter Member

    Feb 19, 2011
    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: Oct 14, 2011