Phillip Trelford's Array

POKE 36879,255

Pimping BASIC with lower casing & JSON literals

Back on April Fool’s day Anthony Green published a funny article “How ‘Roslyn# Finally Unshackled Visual Basic From The Tyranny of the Pretty-Lister”, showing VB.Net with lower case keywords, and how it made it look cooler. And more recently Mads Torgersen gave an “Early View of C# 7” demonstrating pattern matching, a feature that didn’t make it into C# 6, and syntax for tuples.

Taking some inspiration from this I thought I’d add some new extensions to my own language project, Fun Basic (try it free on the Windows Store). But Fun Basic already supports lower case keywords and tuples with pattern matching (which I implemented about 2 years ago), so instead over the last week I’ve added some different language features including cleaner aesthetics for BASIC and JSON literal support.

Hipster BASIC

People often complain about the verbosity of VB.Net, I see two parts to this:

  • End statements – End If, End While, End Select, etc.
  • Casing – Dim is the same length as var, but var just looks smaller

Fun Basic now lets you write `end` and it will infer the type of end for you:

 while i < 10
    i = i - 1
 end

With the lower case keywords & simple end statement you could easily mistake this syntax for Ruby code.

JSON literals

VB.Net has XML literal support, which was a cool feature at the time, but these days XML is more associated with big enterprise, and all the hipsters are using JSON. With that in mind I’ve added JSON literal support to Fun BASIC:

name = "Phil"
age = 27
phil = {"name":name, "age":age}

This allows you to build up strings to send to web based services.

The syntax is also quite close to record syntax in ML, OCaml, F#, TypeScript etc.

Pattern matching

JSON literals are cool, but most of the time you’re using it the other way around, and consuming JSON from an API. For this I’ve added pattern matching over JSON literals.

For example say we want to get the temperature and humidity in London, we can use the Open Weather API, which spits back:

{"coord":{"lon":-0.13,"lat":51.51},
 "weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],
 "base":"cmc stations",
 "main":{"temp":282.515,"pressure":1026.96,"humidity":97,"temp_min":282.515,"temp_max":282.515,
         "sea_level":1037.24,"grnd_level":1026.96},
 "wind":{"speed":5.75,"deg":228.012},"rain":{"3h":0.6475},"clouds":{"all":92},"dt":1447504501,
         "sys":{"message":0.003,"country":"GB","sunrise":1447485423,"sunset":1447517536},
 "id":2643743,
 "name":"London",
 "cod":200}

With the new Fun Basic pattern matching syntax we can easily extract the values of temp and humidity:

london = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0"
weather = Web.Get(london)
{"main":{"temp":temp, "humidity":humidity}} = weather

We can also use the pattern matching in a select case statement:

function ageGroup(person)
  select case person
    case { "age": Is >= 18 }
       return "Adult"
    case else
       return "Child"
  end       
end

sean = {"name":"sean", "age":9}
TextWindow.WriteLine(ageGroup(sean))

Summary

Both features were easy to implement (JSON literals took me the morning), and feel quite natural in the setting of BASIC, you can try them out now in Fun Basic, who knows one day we might see them in mainstream enterprise languages Smile

Fun Basic Preview

Fun Basic is an extended clone of Microsoft’s Small Basic programming language that runs as an app in the Windows Store. The app provides a range of sample programs from simple turtle graphics through to video games and physics simulations. The samples have been selected from programs in the public domain created by the Small Basic community. Each sample is presented with it’s corresponding source code allowing you to edit the code and run the program.

FunBasic Entry Screenshot

The concept is that you can learn elements of programming by reading the provided programs and editing and enhancing them. The inspiration comes from the type-in programs circulated in computer magazines throughout the 80s, and through which I had some of my earliest forays into programming.

The editor on the other hand affords more modern conveniences including syntax colouring, code completion and tooltips over API calls.

FunBasic 1942 Screenshot

Why not head over to the Windows Store and try the preview (new features will be appearing weekly).

It’s free and there’s games to play! http://tinyurl.com/funbasic

Language resources

Fun Basic provides all the language features of Small Basic and it extends the language with parameters on subroutines. You can learn more about language features in the Small Basic Tutorial.

The app is currently a preview, in future releases extended support for function return values, tuples and pattern matching (using Select Case) will be enabled.

Technical bit

Fun Basic is an offshoot from an extended Small Basic Compiler project I wrote for fun in early 2014 while learning FParsec, a parser combinator libraryfor F#. You can learn more about that effort in my presentation at NDC London “Write Your Own Compiler in 24 Hours” and an interview on .Net Rocks on “Writing Compilers”.

My kids (8 & 12) have been enjoying programming and downloading apps from the Windows Store so last month I set out to cover both of these interests with a Store app version.

Interpreter

The Windows Store sandbox doesn’t support compilation within apps so the compiler was out. Fortunately I’d also knocked together an interpreter for Small Basic during early prototyping so I used this as a basis for the runtime. The interpreter simply pattern matches over the Abstract Syntax Tree (AST) generated by the parser, executing instructions and evaluating expressions.

Library

The next challenge was supporting Small Basic’s runtime library which provides simple graphics and text based operations. This had to be written from scratch as it needed to work against the Windows Store API and run in process with the app. All API calls are made asynchronously and I’ve employed separate canvases for the drawing, shapes and text layers. There’s also support for the Flickr API which unfortunately at the time of writing is broken in Small Basic.

image

Editor

The editor employs the ActiPro SyntaxEditor control to provide syntax colouring, code completion and tool tips. ActiPro’s Language Designer wizard meant I had syntax colouring set up in minutes, and it was relatively easy to set up the code completion and tooltips using my existing parser and AST. I’m planning to enable more of the SyntaxEditor’s features in future versions.

App

To build the app I used the Windows Store Grid App project template that’s built-in to Visual Studio. All that was needed was to customize the grid items with pictures and descriptions for the master view and add the editor and program panels to the detail view.

Logo

Special thanks to Sergey Tihon,, author of the excellent F# Weekly, for putting together a nice logo for the app!

Source Code

The source is open and available on BitBucket, see http://bitbucket.org/ptrelford/funbasic

If you delve in you’ll find a mixture of F#, C# and VB.Net. F# is used for the parser and interpreter, C# for interfaces and small imperative functions and VB.Net for gluing the app together.

Releases

I’m currently releasing new features weekly, let me know if there’s a feature you “must have” via Twitter @ptrelford :)

Small Basic on Mac & Linux

Microsoft’s Small Basic is a simple programming language and environment aimed at beginners.

It ships with an IDE for Windows, a commands line compiler and a small .Net library. Small Basic programs can also be run in the browser on Windows & Mac via SIlverlight.

The shipped .Net library for Small Basic targets WPF for graphics which is unfortunately not supported on Mono, which means Small Basic apps will not run directly on Mac or Linux.

To get Small Basic apps running from the command prompt on Mac and Linux all that is needed is a new library without the WPF dependency.

Recently I knocked up such a library providing support for command line input and output, graphics support is a work-in-progress.

But this does mean I can now write and run FizzBuzz, or even work through the majority of the Small Basic Tutorial, on Linux or Mac via Mono:

Small Basic on Mac

Combine this with my open source Small Basic compiler project (written in F#) and there’s now a cross platform version of Small Basic :)

If you fancy having a play with an early version of the source download it here: http://trelford.com/SmallBasicLibrary2012.zip

Future work

I’m currently evaluating GtkSharp, OpenTK and WinForms as options for a cross platform version of the graphics library.

As well as the compiler, I’ve also written an interpreter for Small Basic which means it should be possible to edit and run programs on iOS and Android, but that’s another story…