Skip to content
vczh edited this page Jan 24, 2014 · 5 revisions

Tinymoe has predefined operators for calculating values. All operators are left associative, and ordered by precedence as follows:

To use operators, you should compile your Tinymoe code with the standard library.

Precedence 0

  • Function calls
  • +a: the number a, equivalent to operator POS a
  • -a: the negative a, equivalent to operator NEG a
  • not a: boolean not of a, equivalent to operator NOT a
  • number of a: convert a to a number
  • integer of a: convert a to an integer
  • float of a: convert a to a floating pointer number
  • string of a: convert a to a string

Precedence 1

  • a * b: a multiply b, equivalent to operator a MUL b
  • a / b: a divided by b, equivalent to operator a DIV b
  • a \ b: quotient of a divided by b, equivalent to operator a INTDIV b
  • a % b: remainder of a divided by b, equivalent to operator a MOD b

Precedence 2

  • a + b: a add b, equivalent to operator a ADD b
  • a - b: a substract b, equivalent to operator a SUB b

Precedence 3

  • a & b: a followed by b as a string, equivalent to operator a CONCAT b

Precedence 4

  • a < b: a smaller than b, equivalent to operator a LT b
  • a > b: a greater than b, equivalent to operator a GT b
  • a <= b: a not greater than b, equivalent to operator a LE b
  • a >= b: a not smaller than b, equivalent to operator a GE b
  • a = b: a equals to b, equivalent to operator a EQ b
  • a <> b: a not equals to b, equivalent to operator a NE b

Precedence 5

  • a and b: boolean and of a and b without shortcut, equivalent to operator a AND b

Precedence 6

  • a or b: boolean or of a and b without shortcut, equivalent to operator a OR b

Function call is always at the top precedence and it is left associative. That means, if you type the following code:

module operators
using standard library

phrase sin (x)
	redirect to "Sin"
end

phrase (x) squared
	redirect to "Squared"
end

sentence print (message)
	redirect to "Print"
end

phrase main
	set x to 1
	print sin x + 1
	print sin x squared
	print 1 + sin x squared + 2
	print sin x + 1 squared
	print operator a ADD b * 2
end

You will actually get:

phrase main
	set x to 1
	print (sin x) + 1
	print (sin x) squared
	print (1 + ((sin x) squared)) + 2
	print (sin x) + (1 squared)
	print (operator a ADD b) * 2
end

Operators are allowed to be defined on user defined types using Multiple Dispatching. For example, if you want to define the + on your vector type, you can implement it as follows:

module vector
using standard library

type vector
	x
	y
end

phrase operator (a : vector) ADD (b : vector)
	set the result of new vector of (field x of a + field x of b, field y of a + field y of b)
end

using standard library is important. If you don't use the standard library, you cannot see the operator <expression> ADD <expression> function, then you cannot do multiple dispatching on it.