středa 30. listopadu 2016

Forth syntax colorizer in VS Code

FlashForth is a wonderful Forth implementation for selected MCU types, namely PIC18, PIC24, PIC30 and dsPIC33, and also AVR ATMega.

I have quit playing with PIC processors many years ago, at the time when the most exciting PIC was the good old PIC16F84, but AVR processors drew my attention thanks to various Arduino variants but especially because of the omnipresent Arduino Nano clones from China, cheap and easily available in quantities.

I have never been very much excited by the Arduino IDE, and programming in C or C++ doesn't make me happy either.

That is why Forth implementations for AVR/Arduino have always attracted me.

The only problem seems to be lack of reasonable code editor or IDE supporting Forth (because it is a weird language for most people in the world). On the one hand Forth is a language in which you are supposed to develop your program interactively on the target system, at the same time doing the debugging, and incrementally build the final solution. On the other hand, the usual bunch of tools available for embedded Forth fans – serial port monitors and terminal programs – do not provide many syntax highlighting features or other amenities.

So where to go for some more comfortable source editing?

First of all I tried my long-time favorite editor, Notepad++.  Unfortunately, I had to give up. FlashForth supports three different numerical notations (#decimal, $hex and %binary), one too many for NPP's User defined language template. Additionally, NPP was unable to highlight any of the two features properly - neither numerical constants, nor comments.

Then I realized that in fact the most comfortable and powerful code editor I have been using recently was ... Visual Studio Code! Microsoft bashing aside, VS Code is a really pleasant tool and, compared with any bloated IDE I had been using before, it is as light as air. And it is apparently less quirky and more flexible than Chrome developer tools.

It took me several hours to learn how to write language grammar file (the JSON version, the XML option is just too horrible...) but in the end I do have a language grammar file that works.  For sake of readability and thanks to the easy configuration of the language grammar file I added a few (key)words that are distinct among the forth dictionary entries - words allowed in compile mode only, words modifying word's behavior (immediate and inlined) and defining words create, constant, variable.

I think the highlighted code looks quite good:

I will try to post the (Flash)Forth language extension to VS market as soon as I learn how to do that.  In the meantime, if you are interested, you can ask me for the language file by e-mail. (this will be later replaced by a link to github or to VS market.)  Any takers wishing to test and post the Forth extension on my behalf will be more than welcome...

1 komentář:

  1. I would like a copy of your Forth language extension file. Have a created a github page? Thanks.

    OdpovědětVymazat