Control
There are the usual flow-of-control statements:
if name == "anyone"
print-line: "Anyone"
if x == 0
return 0
else
return x * (factorial: x - 1)
return # No value given, so this returns "nil".
loop
token = next-token
if token type == 'eof'
break
consume-token
if token type == 'comment'
continue
while which-item < num-items
print-line: (items at: which-item)
which-item += 1
for item in items
if item name is-empty
continue
print-line: item name
try
event-loop
else
# "exception" is automatically defined as the caught object.
print-line: exception message
throw MessageException new: "Couldn't connect."
switch justification
'left'
# ...
'right'
# ...
'center', 'centered'
# ...
'justified'
# ...
else
throw --
MessageException --
new: "Bad justification: " + justification string
Expression Statements
An expression on a line by itself also constitutes a statement:
print-line: "Hello world!"
Assignment
Assignment in Trylid is designed to be easy to read (and type), but the rules behind it can be a little complex.
proto MyObject
fields total
grand-total = 0 # A shared variable, with its initial value.
reset
total = 0 # Sets the object's "total" field.
frobnicate: value
massaged-value = value * 7 + 3 # Creates a new local variable.
total = total + massaged-value # Sets the object's "total" field.
grand-total = grand-total + massaged-value # Sets the shared variable.
The basic idea is that an assignment will assign to a variable in the current scope, and if no such variable exists, a new local variable will be created.
Assignment can also be a shorthand for a function call:
(array at: 7) = "hello" # is the same as:
array at: 7 put: "hello"
If the function being set is a unary function, rather than a keyword function, then the setter function's name is the unary function's name with a colon appended:
proto MyProto
fields real-name
name
return real-name
name: new-name
real-name = new-name + " Jr."
obj = MyProto new
obj name = "Joey Jo Jo Shabbadoo"
print-line: obj name # Prints "Joey Jo Jo Shabbadoo Jr."
(Indeed, the compiler treats *all* assignments this way, even those to local variables. So "foo = bar" can always be written as "foo: bar".)
Assignments are expressions, so you can do things like "x = y = 0".