The Colon Programming Language
Colon is an interpreted toy programming language. It is a
product of my learnings from
Thorsten Ball's
Book on the
Monkey Programing Language
, with a syntax that is loosely based on that an older project I
co-authored
Axolotl.
Purpose
I've wanted to implement my own programming language for quite some
time. Thorsten Ball's amazing book, "Writing an interpreter in Go",
was instrumental in the construction of Colon's interpretter. Colon's
syntax may vary from that of the Monkey programming language, but that
book is the source of much of what I have learnt about writing
interpreters. Colon is influenced by functional languages like Haskell
in some ways, and also by imperetive languages like Go itself. Its a
mish-mash of different ideas and is quite interesting to use.
Since it is a toy language though, I haven't designed it with any
specific goals in mind. It is not a production ready language by any
means, and may have bugs that I remain unaware of. If you do find bugs
, feel free to open an issue on GitHub. If you're interested in
building interpreters too, you can even try fixing some bugs yourself
!
Basic Syntax
Datatypes
Colon has 5 basic datatypes - integers, floating point numbers, booleans, strings and lists. Integers, Floats, Booleans and Strings are quite standard, just like they are in other programming languages. The list datatype is similar to lists in Python, elements of a list can be of any datatype. Colon has no usable NULL datatype (even though internally, some expressions can evaluate to the EMPTY datatype).
Variables
In Colon, variables are declared without specifying its datatype and
any variable can hold a value of any datatype. Since there is no
usable NULL datatype, a variable must be initialized during
declaration. Also, variables in Colon are immutable - expressions such
as x = x + 1 give some rather ugly error
messages.
Variable declaration (and simultaneous instantiation) syntax is
Though variables are immutable, you can always reassign a value to a
variable using the declaration syntax. This works because the
expression on the right of an assignment is evaluated before the
expression on the left. Regarding the previous example, if one wants
to assign the value of
x + 1 back to
x one can do something like this :
Operators and Operations
Operations on Integers
Operations on Floating Point Nnumbers
Operations on Booleans
Operations on Strings
Operations on Lists
Builtin Functions
Colon has a very minimal set of builtin functions. Two very useful
functions that Colon provides are the
print and the
input
functions.
The print function can take in any number or arguments. It prints each
argument on a new line and does not support formatting like C or Go's
printf function (if need be, I might add in this feature later
sometime). A simple "hello, world!" print statement may look like
this:
The input function is perhaps the only function in Colon that requires the programmer to specify the datatype of the expected input. The general syntax of an input function call is as follows:
It is important to remember that uninitialized/undeclared variables
cannot be used as arguments to any of these functions. Using variables
that haven't been declared result in errors during runtime.
Strings and Lists have a bunch of builtin functions that can be used
with them. the
len, head and
last functions operate on both strings and
lists.
As expected, the len function returns the length of the string or the
list passed to it as its argument.
This allows us to do something like :
The head and last functions return the first and last elements of the
given list or string respectively. Thus,