The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
For Loops
by Christopher Diggins
September 11, 2005
Summary
For loop constructs in programming languages are usually syntactic sugar, but what should they map to?

Advertisement

In C/C++ a for statement maps from:

for (initialization_statement; boolean_invariant_condition; variant_statement) 
  loop_body
to more-or-less the following:
{
  initialization_statement;
  while (boolean_invariant_condition) 
  {
    {
      loop_body;
    }
    variant_statement;
  }
}
I wanted do something new with for loops in Heron which leverages generics. Here is the general form:
for iterator_type iterator_name in iterable_expression {
  loop_body
}
Some examples:
for int i in range(0, 100) { 
  write_int(i); write_char('\n'); 
}

for char c in "hello" {
  write_char(c);
}

for object o in (42, 3.14, 'q') {
  write_object(o);
}

I am happy with this so far, but I want to go further and generalize the notion so that programmers can define new constructs like for-loops. For instance I would like to be able to iterate over the collection in an non-deterministic order. This is important for optimization on concurrent machines.

The best way to generalize this I can think of is to use closures. This way for-loops and programmer defined constructs can map to anonymous functions. The problem right now is trying to figure out an elegant and efficient way to implement closures in HeronFront. In order to implement closures in C++, It seems I have to generate a context data type and pass it to the anonymous function.

Any thoughts?

Talk Back!

Have an opinion? Readers have already posted 10 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use