Sunday, April 21, 2013

Syntax of Unobtainabol

Unobtainabol doesn't have syntax (I'll explain how that is in a later post) but it's hard to discuss langauge features without giving examples, so I'm going to present here a sort of default syntax for discussing the language.

In many languages, there is some specific syntax for declaring a program (Pascal), or a program has to be declared in functions (C, C++) or a program consists entirely of classes (Java) or of packages (Eiffel). These kinds of restrictions don't really seem to add anything of value, so Unobtainabol opts for the simplicity and flexibility of the Lisp model: a program is just a programming language expression.

Of course this means that things like function, class, or module declarations are expressions. In Unobtainabol, everything is an expression. There are no special syntactic categories for declarations, statements, or other phrases that are not expressions.

The entire purpose of this syntax is to explain the language, so I'm going to use a syntax that is easy to to read and figure out for someone who doesn't know the rules of the language --one inspired by SQL.

 There are two basic forms of expression, clauses and terms. Syntactically clauses are used for things that would be statements in other languages and terms are used for things that would be expressions. But I'm not calling them statements and expressions because everything in Unobtainabol is an expression.

Terms look like expressions in common programming languages:
x
x+y
f(x,y)
x := y
 but they can also use keywords as in SQL
x between y and z
x in y
A function call with the traditional parenthesis always evaluates and dereferences all of its arguments and passes them by value. So if the argument is a variable, the value of the variable cannot be changed by the call. Of course if the variable refers to a changeable object, the object may be changed. There are also procedure calls that are syntactically different, using curly brackets:
p{x,y}
This kind of call can use any kind of parameter-passing method so the user can't make any assumptions about it without looking at the signature. Unfortunately, this is a novel syntax that breaks the rule about programs being easy to understand for people who don't know the language, but at least the odd syntax will warn them that there is something unusual going on.

A term can be made into a clause by putting a semi-colon after it:
f(x,y);
p{x};
x := y;
This becomes important because there are constructs that allow a list of clauses and in such a list, you need a syntactic way to tell when a term ends. The other clauses all look like statements do in languages that fully brackets with keywords

if x < y then
  y := f(x);
  x := x+1;
elif y < x then
  x := f(y);
  y := x+1;
else
  g{x,y}
fi
do while x < y;
  x := 2*x;
od
do for var i := 1 to 10;
  p{x,i};
  y := y+i;
od
Declarations are usually terms that begin with a keyword. Since they are terms and they usually appear in a list of clauses, they are almost always followed by a semi-colon:
do while x < y;
  var i:integer;
  i := 2*x;
  x := i;od
The type name is not a keyword. Types are first-class objects in Unobtainabol and a type name is  just a name that happens to be bound to a type.

Because Unotbainabol has high dynamicness, variables do not have to be statically typed. Type declarations are optional, but the declaration of the name is required. Every name has to have a declaration that shows what its scope and lifetime is. Also, declarations can have initialization, so the above example could be rewritten as
do while x < y;
  var i;
  i  := 2*x;
  x := i;
od
or
do while x < y;
  var i := 2*x;
  x := i;
od
The loop is a highly flexible structure in Unobtainabol. Other examples are
do
  var z := f(x,y);
  for var i := 1 to z;
  p{x,i};
od
do
  var i := x+y;
  x := y-1;
  while x > y;
od

No comments:

Post a Comment