Lolcode - Operators


Advertisements

Operators play an important role to perform various operations on variables. This chapter brings you various operators in LOLCODE and their usage.

Operators

Mathematical operators depend on a prefix notation i.e. the notation that comes before the operand. When all the operators have known number of arguments or operands, then no grouping markers are necessary. In cases where operators don’t have fixed arguments or operands, the operation is closed with MKAY.

An MKAY may not be used if it coincides with the end of the statement. In such cases, the EOL keyword should be used. To use unary mathematical operators , use the following syntax −

<operator> <expression>

The AN keyword can optionally be used to separate arguments, and apply a single operation on more than one operand, so a binary operator expression has the following syntax −

<operator> <expression1> AN <expression2>

Any expression containing an operator with infinite number of arguments can be expressed with the following syntax −

<operator> <expression1> [[AN <expression2>] AN <expression3> ...] MKAY

Math

Following are the basic mathematical operations in LOLCODE −

SUM OF <a> AN <b>      BTW This is a plus + operator
DIFF OF <a> AN <n>     BTW This is a minus - operator
PRODUKT OF <a> AN <n>  BTW This is a multiply operator *
QUOSHUNT OF <a> AN <n> BTW This is a divide operator
MOD OF <a> AN <n>      BTW This is a modulo operator
BIGGR OF <a> AN <n>    BTW This is a max operator
SMALLR OF <a> AN <n>   BTW This is a min operator

<a> and <b> can each be unique expressions in the above, so mathematical operators can be nested and grouped indefinitely.

Math is performed considering arguments as integer math in the presence of two NUMBRs, but if either of the expressions is NUMBAR, then operations are considered as floating point operations.

Example

HAI 1.2
   I HAS A m ITZ 4
   I HAS A n ITZ 2
VISIBLE SUM OF m AN n      BTW +
VISIBLE DIFF OF m AN n     BTW -
VISIBLE PRODUKT OF m AN n  BTW *
VISIBLE QUOSHUNT OF m AN n BTW /
VISIBLE MOD OF m AN n      BTW modulo
VISIBLE BIGGR OF m AN n    BTW max
VISIBLE SMALLR OF m AN n   BTW min
KTHXBYE

The above code will produce the following output when you run it −

sh-
4.3$ lci main.lo
6
2
8
2
0
4
2

Important Points −

Consider the following important points related to working with mathematical operators in LOLCODE−

  • If one or both arguments in an expression are YARN, they are treated as NUMBARs.

  • If any of the arguments cannot be safely casted internally to a numerical type, then it fails with an error

Boolean

Boolean operators are applied on those values that may be true or false. Boolean operators working on TROOFs are as following −

BOTH OF <m> AN <n>             BTW its and operation: WIN if m = WIN and n = WIN
EITHER OF <m> AN <n>           BTW its or operation: FAIL iff m = FAIL, n = FAIL
WON OF <m> AN <n>              BTW its xor operation: FAIL if m = n
NOT <m>                        BTW its an unary negation: WIN if m = FAIL
ALL OF <m> AN <n> ... MKAY     BTW it will take infinite arguments and apply AND
ANY OF <m> AN <n> ... MKAY     BTW it will take infinite arguments and apply OR.

Please note that <m> and <n> in the expression syntax above are automatically cast as TROOF values if they are not already TROOF Values.

Comparison

When you want to compare two or more operands in LOLCODE, you can do so in any of the following methods −

Method 1

You can compare two binary operands using equality operators. The syntax is shown below −

BOTH SAEM <m> AN <n>   BTW this will return WIN if m is equal to n
DIFFRINT <m> AN <n>    BTW this will return WIN if m is not equal to n

Method 2

You can compare if both the values are of NUMBRs type. Remember that if either of the values are NUMBARs, then they are compared as floating point values.

Method 3

You can also perform comparison using the minimum and maximum operators. The syntax is shown below −

BOTH SAEM <m>   AN BIGGR OF <m> AN <n>

BOTH SAEM <m>  AN SMALLR OF <m> AN <n>
DIFFRINT <m>  AN SMALLR OF <m> AN <n>
DIFFRINT <m> AN BIGGR OF <m> AN <n>

Example

HAI 1.2
I HAS A VAR11 ITZ 7
BOTH SAEM VAR11 SMALLR OF VAR11 AN 8, O RLY?
YA RLY
VISIBLE "TRUE"
NO WAI
VISIBLE "FALSE"
OIC
KTHXBYE

You can see the following output when you execute the given code −

sh-
4.3$ lci main.lo

TRUE

Concatenation of Values

LOLCODE allows you to explicitly concatenate infinite number of YARNs using the SMOOSH…MKAY operator. For concatenation, multiple arguments can be separated with the AN operator.

Example

HAI 1.2
I HAS A VAR1 ITZ A YARN
VAR1 R "TRUE"

I HAS A VAR2 ITZ A YARN
VAR2 R "ANOTHER TRUE"

I HAS A VAR3 ITZ A YARN
VAR3 R "ONE MORE TRUE"
VISIBLE SMOOSH VAR1 " " VAR3 " " VAR2 MKAY
KTHXBYE

The above given code will produce the following result upon execution −

sh-
4.3$ lci main.lo

TRUE ONE MORE TRUE ANOTHER TRUE

Type Casting

Operators that work on specific types implicitly cast or convert the values of one type to other type safely. If the value cannot be safely converted to other type, then it results in an error.

An expression's value may be explicitly casted or converted to some other type with the binary MAEK operator. The syntax of MAEK Operator is −

MAEK <expression> A <type>

where, <type> can be one of TROOF, YARN, NUMBR, NUMBAR, or NOOB.

To explicitly cast a variable to some other type, a normal assignment statement with the MAEK operator can be used, or a casting assignment statement may be used as follows −

<Any_variable> IS NOW A <type>  BTW this code will be equal to
<Any_variable> R MAEK <variable> A <type>

Example

HAI 1.2
I HAS A food ITZ "111.00033"
VISIBLE food

BTW this is how we do type casting
MAEK food A NUMBAR
VISIBLE food
KTHXBYE

The above code will produce the following output−

sh-4.3$ lci main.lo
111.00033
111.00033
Advertisements