Eliminate some unnecessary redundancy
Even though I explained the workings of the language in my new book, it's been over a year since I last actually modified the source code for my Zilk programming language. Since I built that language pretty much on a whim as I went along since it was just a learning experiment, and the whole thing was originally supposed to be at most a simple toy language roughly equivalent to the interpreted Basic of the old Commodore 64 computers, I didn't really design it properly. I would do many things differently both in the language itself, the compiler and the runtime environment if I started a project from scratch. But even so, I am happy how it eventually turned out, especially the integration to Java so that Java classes can be used inside Zilk as if they were ordinary Zilk classes.
Today I finally made one small change that had annoyed me since ever, but hadn't found the energy to do it. After this change the language is insensitive to punctuation and it is not needed, although it is allowed for extra clarity. The compiler knows where each expression ends from the whitespace, so why should the programmer indicate this with syntactic salt? The statements don't need to be separated with semicolons or anything like that, but also expressions in lists and parameter lists need no commas between them. For example,
def sum |a b c|
a+b+c # implicitly return the value of last expression
enddef
println sum(1 2 3) # outputs 6
would
work as you'd expect. I liked the syntax of vertical bars that Ruby
uses for block parameters so much that I decided to use them also with
ordinary function parameter lists.
The change that I just made
corrected the silly choice of using an asterisk to explode lists into
elements. Doing things this way suddenly made commas important, because
in the code
a is [2 3] # is for assignment, eq for comparison
println sum(1, *a)
println sum(1 *a)
the first function call works just fine, calling sum(1 2 3)
, whereas the second tries to multiply integer 1 with list a
,
resulting in a runtime error. D'oh. Since the ampersand character
wasn't used for anything in Zilk yet, I chose the ampersand to be the
list explosion operator, removing this syntactic ambiguity.
The Zilk language was originally inspired by the two great languages Python and Ruby,
although giving much more weight to the latter. For example, that each
statement is by itself a legal expression and that the names of fields
start with @
are taken straight from Ruby. The syntax of
Python, of course, heavily depends on whitespace in form of indentation
that defines the program structure, which looks neat but gives the
Pythonistas pain now that they would like to add real lambdas to the
language. Since whitespace separates expressions in Zilk, unlike in
Ruby it makes a huge difference whether you write
sum(1 2 3) # function call
sum (1 2 3) # two separate expressions
since the second line is not actually a function call, but it consists of two separate expressions sum
and (1 2 3)
,
which are evaluated in sequence. The first one silently evaluates to a
function object, and the second one, which is actually three
expressions grouped in a code block, silently evaluates to the value of
its last expression (the same way as in Scheme), which is 3
. For a more complex example that also illustrates that all statements are expressions,
println (1 println 2 if false 3 else "Hello" endif)
would output
2
Hello
Since I also like the Prolog
language, one idea I have been tinkering with would be to create a
language that essentially works like Prolog but whose syntax would be
more traditional and thus more to the liking of programmers who are
familiar with the imperative languages. I am sure that such a language
would be an interesting exercise, but I don't really know where to even
begin. Prolog is, of course, already by itself so elegant, complete and
self-containing that it's hard to see how it could be any way
different. Other than the trivial modifications such as losing the
silly convention that anything that starts with a capital letter is a
variable and replacing this by the use of some special character such
as $
or @
.
Comments