Programming and general geekiness.

Posts tagged ‘basic’

BASIC is dead, long live Python?

Many years ago I sat down with a program called Liberty BASIC to learn programming. A few weeks later I progressed to Visual Basic and I stayed with VB for over a year before finally moving on to learn C#. As time went by I gradually rejected BASIC and I don’t think I’ve worked with it in at least two and a half years.

In January this year I had a look at Python for the first time and initially rejected it deeming it as a pointless extension of BASIC whilst assuming that C#, C++ and Java were far better languages. However one Sunday earlier this year I decided it would be sensible to learn Python, so I learned it in a morning on a dying Ubuntu computer. That was when I discovered it was quite a good programming language.

Before stepping into why Python will gradually take over from BASIC I would like to first consider how BASIC came into being. It has existed in one form or another for almost fifty years and stands for Beginner’s All-purpose Symbolic Instruction Code (is it sad that I didn’t have to lo0k that up). When the home computer era began BASIC became incredibly common as many people learned it so that they could make simple games for their new computers – it is these people that currently fuel the global programming industry today.

BASIC had its greatest success in 1991 when Microsoft developed Visual Basic which was designed to make it incredibly fast to design applications and this happened with great success, pushing VB skills to become a common business requirement. The aim was, however, unclear. Was BASIC and introduction to programming or serving as a professional language? Today I would suggest that it provides a platform for learning programming. The problem is that it is no longer best.

With time I realized how excellent a programming language Python is. Not only is it simple (and therefore incredibly easy to learn) but it is also speedy and extendable. According to this chart, we can see that Python is currently two times more popular than Visual Basic. There is also a lot more support on the web for Python and many people actually use it to develop complex software and websites.

Python will definitely become more popular over the next few years. In the UK there is certainly the possibility that it will become common knowledge thanks to projects like Raspberry Pi, but I wouldn’t be surprised if it gets onto the curriculum of other countries as well.

How to build a really simple programming language

This is not a tutorial. It is more a guide of how you could first approach things. It is assumed that you code in some object-orientated language. I think Java would be the best thing to get started with for this. I have based this on some code that I have already written as part of my ThomasScript project, which I intend to release at some point. Maybe.

First of all, I must assume that your programming language (and mine) and simple non-OO languages that have their code written out in text files, or somehow read text into some sort of String object to use as the basis of their ‘code’. Like you have a BASIC interpreter (which you would write) and a .BAS file that someone else would write and run. I’ll let you worry about how you get the content of the .BAS file.

Now that you have roughly figured this out, you need to think about how you are going to structure your language. Here is a rough example of mine:

dim $variable

set $variable = “Hello”

write $avariable

getnum $variable

set $variable + 1

write $variable

if ($variable > 10)

write “Big number”

else

write “Small number”

write “Going to run a function”

run someCode “Hello!”

code someCode $text

write $text

I think it is relatively clear how this program works and indeed how the programming language works – even though it is very simple. The first part of coding the language is to split everything into array, split into individual lines. You then take apart the first line by its spaces: dim and $variable. There is therefore a switch branch in your code that has all of these possible language keywords. The code for dim is found and the argument $variable is given to it. For simplicity purposes all variables start with a dollar symbol for easy lookup. The variable names are stored in a string array with a length of about 10,000 because it is unlikely that you’ll need more, or that you are using a language that allows for arrays of unlimited length. There is then another array with type of object (or equivalent) that stores the variable values. This is relatively simple.

Because it is relatively clear how all the other functions work from this, the next thing to look at is basic (very basic) logic. I have decided that to keep things simple at first I will only use >, <, ==, !=, <= and >=. I will not use any variation on AND or OR. The value in the brackets is now relatively easy to evaluate; you simply get the left hand value and compare using the appropriate operation to the right. After this you get all the code that is indented and run that.

This is a very basic article and doesn’t really discuss the full process, however it certainly offers a simple start. Please note I plan to write a simple version of ThomasScript to the syntax described above before releasing the source code (either C, C++ or Java).

Approaches to coding a BASIC programming language

BASIC taught me programming to begin with. It can (at times) by a surprisingly complex language, but it starts off at a very simple level. There are hundreds of options for beginning BASIC online, and you can gradually progress up to Visual Basic, and then one what I like to think of as ‘proper syntax languages’ – those languages whose syntax uses {curly brackets} and is descended from C.

It is enough a challenge for a beginner to learn BASIC, but for a programmer with three years experience it becomes a lot harder to build a BASIC clone. For instance, how would you handle variables? Control statements? Functions? Parameters? Could it be more advanced, including GUIs and Objects?

My plan is to build a simple programming language first based on BASIC, but then being built-up to be a mix of PHP, Objective-C and VB.  It will have interfaces declared in XML files and code stored in text files that contains code. The procedure has been painless so far and with any luck it will prove to be very easy to use once finished.

Here is how I am approaching challenges:

  • Variables: values are stored in an array of a maximum of 10,000 values. The values are evaluated by a special function that returns their value once Math has been evaluated along with the combining of strings
  • Control statements: These evaluate the variable boolean before executing the appropriate statements
  • Commands/Functions: Currently non-object based, have one argument at the moment, so it is statement then argument, so print might be print “hello world” and this can be split by spaces and easily evaluated.

The language is going to be called ThomasScript, and intend to have the first (Windows) beta version finished by the end of July, ready for a simple version to be integrated into early builds of ThomasOS.

Follow

Get every new post delivered to your Inbox.