Falls Creek User Guide

“Falls Creek” is the code name for the first pre-ocean interpreter.  All pre-ocean interpreters will be creeks while full-ocean interpreters will be rivers.  Compilers will be named for seas and oceans. Being the first, the language is extremely limited.  The two particular areas of experimentation are two-dimensional structure and looping and conditional statements.

Input/Output

The only available source of input is the command line arguments of which a fixed number must be given.  The only destination for output is stdout with no complex formatting. A trivial demonstration of IO is:

program a b:
    print "In reverse order:", b, a

There must be 2 command line arguments passed to this program, and they will be printed out in reverse order. Note that ‘oceani’, the interpreter, treats the file passed to it as a “markdown” formatted file and extracts code fragments (which can reference other fragments).  To create a file that just contains code and no documentation (suitable only for quick tests), start the file with “~~~~~~” e.g.

~~~~~~
program a b:
    print b, a, b

Statement Blocks

This example demonstrates how statement blocks are formatted.  A statement which can have a sub-block (such as “program”) introduces the block with a colon (“:”)  then the block follows with all statements indented.  As an alternative, braces (“{}”) can be used like in C.

program a b {
    print "In reverse order:", b, a
}

The indenting here is not required, but is included for readability.

Variables

Variables are not declared, they are just used.  A variable can be a string, a number, or a Boolean.  The type of a particular variable is determined by usage:  if assigned a number or added to a value, it must be a number.  If used as the condition of an “if”, it must be a boolean. If nothing determines the type of the a variable, and  it is included in the list for the “program” statement, the variable is assumed to be a string.  Otherwise the variable is treated as a number and assigned an arbitrary unique value.  So

program:
    print foo

Will print an arbitrary number. Numbers are implemented as arbitrary precision rational numbers using the ‘gmp’ library.  So:

print (1 + 1 / 1e100) - 1

Will not suffer a rounding error.

Simple Statements

Simple statements may be separated by semicolons (“;”) or by being placed on consecutive lines.  If a simple statement needs to be longer than one line, the portion after the first line must be indented with respect to the start of the first line.  This will be treated as a continuation, not as a new line. The available simple statements are:

  • “pass” – does nothing.  There is no “empty” statement like “;” or “{}” in C, so “pass”, when needed, must be spelt out.
  • “print expression list” – prints out the value of each expression with spaces between expressions and a newline at the end.  The expressions are separated by commas.  If there is a trailing comma, then the trailing newline is suppressed.
  • “variable = expression” – evaluates the expression and assigns the value to the variable.
  • “use expression” – This is explained later.

Some examples showing how statements are separated and continued:

a = 1 + 2; print "the value is", a,
b = 1 + 2 + 3 + 4 + 5
      + 6 + 7 + 8
print b
pass ; pass ; c =
  1 + x; use b

The indent in the last line isn’t easy to read, but it is correct.

Structured statements

The only structured statement (apart from “program” which may only appear as the whole program) is a combined iteration/alternation statement.  It combines a conditional “if”, a “switch” for selection and a “for/while” looping statement. The simplest loop has the form

while Expression Block

Where Block can be introduced with a ‘:’ or a ‘{‘.  The Expression must have a Boolean value and the Block is repeated as long as that value is True. The Expression can be replaced by a Block, in which case the keyword “do” is needed to introduce the body block.  Inside the first block, the “use” simple statement can provide a value which is used in place of the expression.  So

while:
   print a,
   use a < 10
do:
   print ", ", ; a = a + 1
print

will print the value in ‘a’ and every subsequent value up to 10, with commas separating the values. A “while” can be prefixed with a “for” clause with simple statements or a block and an optional “then” clause again with simple statements or a block.

for i = 0
then:
    i = i + 1
while i < 100:
    print i

The “for” part (simple statements here) is executed once and the “then” part (as a block here) is executed after the body of the loop.  This provides a similar construct to the 3-part “for” loop in C, though the parts are in a different order. An “if/then/else” is structured similarly to the “while” with an expression or a block following the “if”, and “then” only needed if there was a block

if a == b:
     print "equal"
if:
   a = a / 2
   use a == b
then:
     print "double"
else:
     print "something else"

The “else” clause holds few surprises. It can be followed by a block (shown here) or another condition statement, thus allowing cascaded “else if” clauses. A “switch” statement can be  given an expression or a block which provides a “use” value.  It then has a number  of “case Expression Block” clauses with optionally a final “else”.  There  is no clear marker of the end of a “switch” any more than there is for a series of “else if” statements. The “switch” statement can be combined with the “while” statement by providing “case” clauses and an optional “else”  clause after  the body.

while:
    if a < 3:
        use a
do:
    a = a - 3
case 0 {print "divisible by three" }
case 1 {print "remainder of one" }
case 2 {print "two is remainder" }
else:
    print "Value was negative"

In this case the loop repeats as long as the “while” block does not “use” value.  Once it does the appropriate ‘case’ will be executed and the loop terminates.

A particularly interesting variant is possible if otherwise unused variables are used in the “use” statements and the “case” expressions.  As such variables are assigned arbitrary unique values they act like labels:

switch:
   if a < b:
       use Less
   else if a > b:
       use More
   else:
       use Same
case Less: print "Less"
case More: print "More"
case Same: print "Same"

Expressions

Expressions are formed by combining constants and  variables with various operators. String constants can be given using single or double quotes, or as a multi-line constant beginning and ending with a triple single quote or triple double quote. Boolean constants are “True” and “False”. Numeric constants are decimal with optional decimal point and ‘e’ exponent, or binary/octal/hex with a prefix of “0b”, “0o”, or “0x” and an optional binary point and ‘p’ power (always a power of 2).

print "pi", "π", 3.14159, 314159e-5, 0x3.243F6, 0o0.311037p3
print 'The Truth is:', True, '''
  With several
  lines of
  exceptions and justifications.
  '''

Operators that work on numbers are:

  • prefix “+” – absolute value
  • prefix “-” – negative
  • infix “+”, “-“, “*”, “/'” – addition, subtraction, multiplication and division.
  • infix “<“, “>”, “<=”, “>=”, “!=”, “==” – comparison

Operators that work on strings are:

  • infix “++” – concatenation
  • infix “<“, “>”, “<=”, “>=”, “!=”, “==” – comparison

Operators that work on Boolean are:

  • prefix “not” – logical inverse
  • infix “and”, “or” – conjunction and disjunction
  • infix “<“, “>”, “<=”, “>=”, “!=”, “==” – comparison

Precedence is “or”, then “and”, then “not”, then comparison, then infix “+” and “-“, then “*”, “/”, “++”, then prefix “+” and “-“.  Parentheses can be used as normal to over-ride precedence.

Leave a Reply

Your email address will not be published. Required fields are marked *


× 2 = four


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>