-
Notifications
You must be signed in to change notification settings - Fork 0
Operators
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 tooperator POS a
-
-a
: the negative a, equivalent tooperator NEG a
-
not a
: boolean not of a, equivalent tooperator 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 tooperator a MUL b
-
a / b
: a divided by b, equivalent tooperator a DIV b
-
a \ b
: quotient of a divided by b, equivalent tooperator a INTDIV b
-
a % b
: remainder of a divided by b, equivalent tooperator a MOD b
Precedence 2
-
a + b
: a add b, equivalent tooperator a ADD b
-
a - b
: a substract b, equivalent tooperator a SUB b
Precedence 3
-
a & b
: a followed by b as a string, equivalent tooperator a CONCAT b
Precedence 4
-
a < b
: a smaller than b, equivalent tooperator a LT b
-
a > b
: a greater than b, equivalent tooperator a GT b
-
a <= b
: a not greater than b, equivalent tooperator a LE b
-
a >= b
: a not smaller than b, equivalent tooperator a GE b
-
a = b
: a equals to b, equivalent tooperator a EQ b
-
a <> b
: a not equals to b, equivalent tooperator a NE b
Precedence 5
-
a and b
: boolean and of a and b without shortcut, equivalent tooperator a AND b
Precedence 6
-
a or b
: boolean or of a and b without shortcut, equivalent tooperator 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.