Cola (programming language)

From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search

Cola is a programming language similar to Java and C# with some higher level features of Perl and Ruby including first-class regular expressions. It can be compiled for the Microsoft .NET CLR or the Parrot virtual machine.

Cola is a compiled language with static type checking, but the designer mixed in some of the features more common to powerful scripting languages like Perl and Ruby. According to the author, after years of using Java and C++ for large projects, and Perl for small to medium ones, there seemed to be no language to fill the gap. Cola is less of a new language "design" than a mix of several very powerful languages with the following key principles in mind:

Curly braces and semi-colons at the heart - Java and Perl programmers should both be comfortable, whitespace has no effect on the semantics.

Convention over Code - Things that are conventional don't need to be explicit (Java/C# public keyword on methods)

High signal-to-noise ratio - Use Convention over Code to reduce keyword clutter and improve readability. Don't clutter the expression of an idea with implementation of the idea. Java and C# exhibit this in what many consider poor choice of default scope access of fields and methods. In practice, the use of the word public dominates Java and C# syntax.

Strings and text processing are top priority - Most programming involves strings and text manipulation. Supporting regular expression dramatically increases the power of a language, but BUILTIN syntax for regular expressions improves the readability. When combined, they increase the productivity of the programmer.

Scriptlets should be possible - No default class with a static main required just to do a small job.

A Hello world program written in Cola three ways:

// C/Perl-style without classes
// Cola package/global methods are static by default, void by default

string mesg = "Hello world\n";
main() {
   print(mesg);
}
// Cola OO-style
// classes, methods and constructors public by default, fields private by default
class Hello {
   string mesg;
   new() { mesg = "Hello world\n"; }
   static void main() {
       print(mesg);
   }
}
// Cola (C#/Java "explicit wordy" style)
// Note scope resolution begins a typical declaration. This style is
// available but discouraged in Cola
public class Hello {
   private string mesg;
   public new() { mesg = "Hello world\n"; }
   public static void main() {
       print(mesg);
   }
}

In the examples above, it is nearly possible to write Java in Cola, but not Cola in Java.

An example of Cola's support for regular expression and matching is given below. First a conventional example that echoes Perl's approach.

// implicit match backreference
if(mesg =~ /(yes)(madam)/) {
   print("In string: " + $0);
   print("we matched: " + $1 + ", " + $2);
}

Cola includes a more powerful, expressive notation for matching called match aliasing. Simply put, any match expression can declare an object of type match as part of the expression (similar to the spirit of declaring a named iterator with C++ or Java in a for loop: for(int i = 0; i < 100; i++)

// explicit named match example
if("foobar" =~ /foo(bar)/ as match1) {
   // default is matchA
   print(match1.$1);
}

For the scope of the if statement, match1 is in scope and can be used for clarity. The advantage to match aliasing is more evident when we have multiple, nested matches.

if("foobar" =~ /foo(bar)/ as match1) {
   // We can use a backreference in a new match expression
   // $1 refers to match1 until a new match expression, after which
   // it refers to the new match in immediate scope. To refer back
   // we use match1 alias (match1.$1)
   if($1 =~ /b(ar)/ as match2) {
      print(match1.$1);    // match1 still in scope
      print(match2.$1);    // match2 now in scope, and the default
      print($1);           // same as match2.$1
   }
} 

Cola also includes foreach() iteration, and currently works for the builtin .NET non-generic types in System.Collections. Cola foreach() is implemented similar to the C# compiler, generating code to use the IEnumerable interface, or to check for the proper enumeration signature on the underlying container.

  // Uses .NET System.Collections
  System.Collections.ArrayList list = new;   // note the inferred type for new
  list.Add("LITTLEDOG");
  list.Add("BIGDOG");
  list.Add("LITTLECAT");
  list.Add("BIGCAT");
  foreach(string s in list) {
     // Nested named matching
     if(s =~ /(LITTLE|BIG)/ as size) {
        if(s =~ /(DOG|CAT)/ as animal)
           print("Animal is a " + size.$1 + " " + animal.$1);
     }
  }

A current alpha version of the Cola compiler for .NET is available via email from the author (Melvin Smith). [1]

References

  1. http://www.itechdata.com/cola.html

External links

See also

Personal tools

Served in 0.082 secs.