Walnut/Ordinary Programming/Data Types and Control Flow

From Erights

Revision as of 07:39, 17 December 2008 by (Talk)
Jump to: navigation, search

Simple data types, simple control flow

Here are some of the basics of the language:

 # E sample
 # Comment on this piece of code
 def a := 3
 var b := a + 2
 b += 1
 if (a < b) {
     println("a is less than b")
 } else {
     println("Wow, the arithmetic logic unit in this processor is confused")

Variable declarations in E are made with the var statement. Variables that are only assigned a value once at creation (i.e., constants, or variables declared final) are created with the def statement. In E as in Java, "+=" is shorthand for adding the righthand value to the lefthand variable.

Single-line comments have a "#" at the beginning, and terminate with the end of line. The /**...*/ comment style is used only for writing javadoc-style E comments, discussed later. link here

Assignment uses the ":=" operator. The single equal sign "=" is never legal in E, use ":=" for assignment and "==" for testing equality. The function "println" prints to the console. The "if" statement looks identical to its Java equivalent, but the braces are required.

What is the end-of-statement delineator in E? In Java, you terminate a statement with a semi-colon. In E, the end-of-line is also the end-of-statement unless there is an open operation at the end of the line. In the example, the "if" statement's first line ends with an open brace; E knows the next line must be a continuation line. Some quick examples

 This works
 ? def a := 1 + 2 + 3 + 4
 # value: 10
 And this works
 ? def b := 1 + 2 +
 >   3 + 4
 # value: 10
 But this does not work, because the first line can and does 
 evaluate without a continuation, so the second line is a syntax error:
 ? def c := 1 + 2
 ?   + 3 + 4
 # example syntax error:
 #   + 3 + 4
 #   ^

The end-of-line statement termination of E makes it easy to use E for command lines, as in rune.

If your organization uses a line continuation convention that puts the operator at the beginning of the second line, you can use the explicit continuation character "\" as here:

 This also works
 ? def c := 1 + 2 \
 >   + 3 + 4
 # value: 10


Additional flow of control

We have already seen the if/then/else structure. Other traditional structures include:

  • while (booleanExpression) {...}
  • try{...} catch errorVariable {...} finally{...}
  • throw (ExceptionExpressionThatCanBeAString)
  • break (which jumps out of a while or for loop; if the break keyword is followed by an expression, that expression is returned as the value of the loop)
  • continue (which jumps to the end of a while or for, and starts the next cycle)
  • switch (expression) {match==v1{...} match==v2{...} ... match _{defaultAction}}

One structure that is more powerful than its Java counterpart is the for loop.

 # E sample
 for i in 1..3 {
 for j in ["a", 1, true] {println(j)}

In this simple example, i becomes 1, 2, 3 in succession. In the second, j becomes each of the elements of the list.

The for loop operates not only with number ranges, but also with lists, maps (i.e. hashtables), text files, directories, and other structures discussed later in this book. The expanded version of the for loop that is needed to get both keys and values out of maps is:

 # E syntax
 for key => value in theMap {
     println(`Key: $key Value: $value`)
 # You can get the index and the value from a list at the same time the same way
 for i => each in ["a", "b"] {
     println(`Index: $i Value: $each`)

You can create your own data structures over which the for loop can iterate. An example of such a structure, and a brief explanation of the iterate(function) method you need to implement, can be found in the Library Packages: emakers section later in this chapter, where we build a simple queue object.

The Secret Lives of Flow Control Structures

Flow control structures actually return values. For example, the if-else returns the last value in the executed clause:

 # E sample
 def a := 3
 def b := 4
 def max := if (a > b) {a} else {b}

This behavior is most useful when used with the when-catch construct described in the chapter on Distributed Computing.

The break statement, when used in a for or a while loop, can be followed by an expression, in which case the loop returns the value of that expression.

(Note: the following patch of code is used by updoc.e, the E testing tool, to enable execution of all the upcoming code that depends on Swing)

 ?? in new vat awtVat.e-awt
 ? pragma.syntax("0.9")

Personal tools
more tools