Channels ▼

Christopher Diggins

Dr. Dobb's Bloggers

Version 0.7 of the Heron Programming Language

November 26, 2009

I've just released version 0.7 of the Heron interpreter.  

Version 0.7 of Heron is now available at http://code.google.com/p/heron-language/. This version  implements over 90% of the language features I plan for version 1.0, and has a reasonable set of tests.

Today I implemented the Sieve of Eratosthenes as a test program, which I thought would be a good way to talk a bit about some of the features of Heron, and its current status. The sieve is an ancient algorithm for computing primes. Here is the Heron implementation:  

<table border="0" style="border-collapse: collapse; padding: 0px; margin: 0px"><tbody><tr id="sl_svn89_1" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">script Sieve;<br /></td></tr><tr id="sl_svn89_2" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px"> </td></tr><tr id="sl_svn89_3" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">var max = 10;<br /></td></tr><tr id="sl_svn89_4" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px"> </td></tr><tr id="sl_svn89_5" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">Main() <br /></td></tr><tr id="sl_svn89_6" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">{<br /></td></tr><tr id="sl_svn89_7" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">	var primes = 0..(max * max);<br /></td></tr><tr id="sl_svn89_8" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">	foreach (i in 2..max)<br /></td></tr><tr id="sl_svn89_9" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">		primes = select (j from primes) j % i != 0;		<br /></td></tr><tr id="sl_svn89_10" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">	foreach (i in primes)<br /></td></tr><tr id="sl_svn89_11" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">		Console.WriteLine(i);	<br /></td></tr><tr id="sl_svn89_12" style="padding: 0px; margin: 0px"><td class="source" style="font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 4px; white-space: pre; vertical-align: top; margin: 0px">}<span style="border-collapse: separate; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 14px; white-space: normal; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px" class="Apple-style-span"> </span></td></tr></tbody></table>

Heron programs are either modules containing a class named "Main" or a script. When executing a module Heron instantiates an instance of the class named Main. When executing a script, the function named "Main()" is executed first (after the variables are initialized).

Variable declarations in Heron are identified via the keyword "var". The type annotation is optional, and is written in the Pascal style with the name following a colon (':'). The same syntax and optional typing is used for function definitions.

The '..' operator is called the range operator and is currently only supported on integers.  It creates a sequence (also known as a generator), which is a lazily evaluated collection.  Each value in the collection is retrieved as needed.  

The 'select' keyword is a list operator. It takes a sequence or list, and a boolean expression which represents a predicate condition. The result is  a new list that only contains the elements of the list that satisfy the condition. The 'select' keyword is not guaranteed to be executed, if the expression semantics are preserved. The impact of this is that side-effects should not be relied upon in the predicate expression. This was done so that optimizers can perform list-based optimizations.

The call to 'Console.WriteLine()' executes a .NET library function which was exposed to the interpreter.  .NET libraries are currently exposed to the library on an ad-hoc basis. I haven't yet formalized how the foreign function interface (FFI) is going to work.

I'd love to hear your thoughts and questions about Heron. I am hoping that it turns out to be relatively easy for the majority of programmers to understand intuitively.  If you are interested please download the source  and look at the test files, and the unit tests (HeronTests.cs). 

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video