Skip to content

Expressions and Statements

waitxxxx edited this page Feb 27, 2015 · 5 revisions

Expressions and statements are important concepts in a Tinymoe program. Expressions consist of values, unary expressions, binary expressions and function calls. Statements are always function calls. The difference between function calls for expressions and statements is, expressions can only call phrases, statements can only call sentences and blocks.

####Value as an expression

  • 1 is an expression.
  • 2.3 is an expression.
  • null, true and false are expressions.
  • "Text" is an expressions
  • Symbols are expressions.

####Unary/binary expressions

  • -1 is an expression
  • a + b is an expression if a and b are both expressions For details of operators, see Operators.

####Predefined functions for expressions Manipulating Functions:

  • <expression> of <list>
  • continuation <expression> of <list>

Manipulating Arrays:

  • new array of <expression> items
  • item <expression> of array <expression>
  • length of array <expression>

Manipulating Types:

  • new <type> of <list>
  • <expression> is <type>
  • <expression> is not <type>
  • field <argument> of <expression>

####Predefined functions for statements If Else:

  • select <expression>
  • case <expression>
  • case else

Invoking External Functions:

  • redirect to <expression>

Creating or Modifying Variables:

  • set <assignable> to <expression>

Manipulating Arrays:

  • set item <expression> of array <expression> to <expression>

Manipulating Types:

  • set field <argument> of <expression> to <expression>

####Invoking User Defined Functions In previous sections, you have already known how to define a function. For example, the following function sum from a to b:

phrase sum from (first number) to (last number)
	set the result to 0
	repeat with the current number from first number to last number
		add the current number to the result
	end
end

When you want to call it, say sum from 1 to 100, just type sum from 1 to 100!

If the argument name begins with the following token, it is not just an argument for receiving a value:

  • list: This argument requires a list, e.g. (1, 2, 3), which becoms an array inside the function.
  • expression: For sentence and block only. This argument requires an expression, and each time the function read this argument, the provided expression will be evaluated. This means that, reading this argument several times may get different values.
  • assignable: For sentence and block only. This argument requires a variable. If the provided variable does not exist, it will create a new variable, just like the set <assiganble> to <value> statement. This argument can be written inside the function, and the provided variable will be modified.
  • argument: For block only. This argument requires a variable, but the variable is only accessable in side the block body. The block function cannot access this argument.
  • phrase, sentence or block: This argument requires a function.

argument is a special one, but it is very easy to understand. Let us read the following code

repeat with the current number from first number to last number
	add the current number to the result
end

It means that, add the current number to the result will be evaluated several times, with the current number from first number to last number. first number and last number are expressions, but the current number is only accessable by code inside the repeat block. Because the repeat block is declared as:

block (sentence deal with (it)) repeat with (argument item) from (lower bound) to (upper bound)

So inside the block function, item is not an expression, because it declares the argument for the block body add the current number to the result which will be passed in the block body argument sentence deal with (it). When you call deal with 10, add 10 to the result will be evaluated.

For details of function calls, see Phrases, Sentences and Blocks.