suve

awful: tutorial: chaining expressions

One thing a wary reader might have noticed is that, so far, we have only used single-level expressions - we didn't nest function calls in any way. Sure, we used many different functions and passed arguments to them, but never used nested calls. I guess it's about time we learn how it's done.

Think you can enchain me?

:writeln s'Today is ' :dt-str s"Y-M-D H:I:S"
:writeln :mul s"=" i40

The above example demonstrates how to chain expressions in awful. As can be seen, this is extremely simple. We write the main function as did normally, and then, when passing parameters, simply make write another colon-prefixed function call. The remaining arguments will be treated as parameters for the second function. We can, of course, nest further - by no means are we limited to two levels.

The initial wave of joy fades soon, as we realise that we can only place nested function calls at the end of an expression. Or do we?

I ain't gonna be the last!

:set &a &b &c
:writeln s'Input three numbers: '
:read &a &b &c i0

:writeln $a s'x*x + ' $b s'x + ' $c s' = 0'
:writeln s'delta = ' :sub (:pow $b i2 | :mul i4 $a $c )

:writeln s'Interpreter running time: ' (:ticks) s' milliseconds.'

Whoa. The example above may seem confusing at first sight, but it's not that complicated as it looks. Let's take a look at the last expression first. We can see a :ticks call nested inside a :writeln call. The nested call is surrounded by parentheses, which mark both its beginning and its end. As can be guessed, the nested call does not take any arguments. The main call, thus, takes three arguments: string literal, nested call, string literal. On output, we should see that the string literal, result of nested call, and the second string literal were printed.

Now, let's analyse the delta-calculating expression. Obviously, at the top is a :writeln call. It takes a string literal and a nested :sub call - which, in turn, takes two arguments. We perform subtraction on the result of :pow call and :mul call. As can be seen, instead of putting both bottom-level calls into separate parentheses, they are thrown into one pair of parens and separated with a pipe (|) character. The pipe in awful is simply a shorthand way to write )(. You can use either the pipe, or just close and reopen the parentheses - they are semantically equivalent, so just use whichever is more comfortable for you.


Next: ⇒ Conditional statements ⇒
Back: ⇐ Arithmetic ⇐

wikipage modified on 2014/0601/2317