Expressions and statements
c0de separates statements and expressions strictly: a line is a statement, an expression is only a part of it and ends with ;. Assignment is a statement of its own, functions are called by a statement of their own, and some operators look familiar but mean something else. This lesson compares the operators with C, Java and Python and shows where the precedence differs.
What you need
A creative world with cheats on (see Tutorials), a trigger block, a wrench, a button and eleven uninitialized variable items. For integer and float arithmetic and the exercises: two INTEGER variable items named a and b.
Statements and expressions
- Every line is a statement, and its first block is a keyword:
ASSIGN,CALL,IF… There are no expression statements:x + 1 ;alone in a line isn't valid. - An expression ends with
;, an OPERATOR block. This is how c0de knows where it ends, so several expressions can share one line:FOR i INTEGER 1 ; n ; 2 ;has three. - An expression may go on in the next line, one block further east than the first line of the statement.
- ASSIGN is the only assignment.
ASSIGN total total + 1 ;– there is no=assignment, no+=, no++, and an assignment has no value of its own. - Functions are called with CALL, never inside an expression. A CALL lists its arguments as PARAMETER lines and the variables that get its results as RESULT lines; you use the results in the next statement.
The target of an ASSIGN may have an index: ASSIGN list[3] 10 ;, ASSIGN stock["gold"] 5 ;, ASSIGN spot.x 1 ;. Assigning to the index right after the end of an array appends, assigning to a new key adds it to a hash.
The operators
| C / Java | Python | c0de | |
|---|---|---|---|
| equal, not equal | == != | == != | = ≠ |
| less or equal, greater or equal | <= >= | <= >= | ≤ ≥ |
| division | / (integer for two ints) | / | / (always FLOAT) |
| integer division | / | // | c0de/math/DIV (a function) |
| remainder | % | % | \ |
| power | Math.pow, pow | ** | ^ |
| join text | + | + | ⊕ |
| logic | && || ! | and or not | AND OR NOT, and XOR |
| bits | & | ^ ~ | & | ^ ~ | & | ⊻ ¬ |
| shift | << >> | << >> | ← → |
| assignment | = | = | the statement ASSIGN |
The precedence, from highest to lowest:
( )¬NOT^(from right to left:2 ^ 3 ^ 2is2 ^ 9)- unary
- */\+-←→⊕=≠<>≤≥&⊻|ANDXOROR
Most of it is as in C. Four places are not: \ binds weaker than *, ⊕ weaker than +, NOT stronger than the comparisons, and the bit operators weaker than the comparisons (as in C, but not as in Python).
The program
A function whose results show one rule each:
PROGRAM operators
NOOP
NOOP
FUNCTION operators
RESULT remainder INTEGER
RESULT power INTEGER
RESULT half FLOAT
RESULT order INTEGER
RESULT joined STRING
RESULT same BOOLEAN
RESULT before BOOLEAN
RESULT flags INTEGER
RESULT inverted INTEGER
RESULT safe BOOLEAN
RESULT total INTEGER
BEGIN
VARIABLE zero INTEGER 0
ASSIGN remainder -7 \ 2 ;
ASSIGN power 2 ^ 3 ^ 2 ;
ASSIGN half 2 ^ -1 ;
ASSIGN order 7 \ 4 * 2 ;
ASSIGN joined "Total: " ⊕ 2 + 3 ;
ASSIGN same "a" ⊕ 1 = "a1" ;
ASSIGN before "Z" < "a" ;
ASSIGN flags 1 ← 4 | 12 & 10 ;
ASSIGN inverted ¬ 0 ;
ASSIGN safe zero ≠ 0 AND 10 / zero > 1 ;
ASSIGN total 1 + 2 + 3
+ 4 + 5 ;
END FUNCTIONRunning it
Set the trigger block to the program operators and the function operators, put eleven uninitialized variable items into its input inventory and press the button. Right-click the result items to read them:
| Result | Expression | Value | Why |
|---|---|---|---|
| remainder | -7 \ 2 | -1 | the remainder has the sign of the dividend, as in C and Java (Python: 1) |
| power | 2 ^ 3 ^ 2 | 512 | ^ is evaluated from right to left |
| half | 2 ^ -1 | 0.5 | a negative exponent gives a FLOAT |
| order | 7 \ 4 * 2 | 7 | * before \: 7 \ 8 (C and Python: 6) |
| joined | "Total: " ⊕ 2 + 3 | "Total: 5" | + before ⊕ (Python, JavaScript: "Total: 23" or an error) |
| same | "a" ⊕ 1 = "a1" | true | ⊕ before = |
| before | "Z" < "a" | true | text is compared by Unicode code points |
| flags | 1 ← 4 | 12 & 10 | 24 | ← first, then &, then |: 16 | 8 |
| inverted | ¬ 0 | -1 | bitwise negation of a 64 bit integer |
| safe | zero ≠ 0 AND 10 / zero > 1 | false | AND stops after a false left side, so there is no division by zero |
| total | 1 + 2 + 3 … + 4 + 5 | 15 | the expression goes on in the next line |
Integer and float arithmetic
The type of a result follows from the types of the operands: two INTEGERs give an INTEGER, a FLOAT on either side gives a FLOAT. Two operators break this rule: / always gives a FLOAT, and ^ gives a FLOAT for a negative exponent. \ only takes INTEGERs.
PROGRAM numbers
NOOP
INCLUDE c0de/math
NOOP
FUNCTION divide
PARAMETER a INTEGER 7
PARAMETER b INTEGER 2
RESULT quotient FLOAT
RESULT whole INTEGER
RESULT floor INTEGER
RESULT rest INTEGER
RESULT check BOOLEAN
RESULT exact BOOLEAN
BEGIN
ASSIGN quotient a / b ;
CALL c0de/math/DIV
PARAMETER dividend a ;
PARAMETER divisor b ;
RESULT result whole
CALL c0de/math/INT
PARAMETER value quotient ;
RESULT result floor
ASSIGN rest a \ b ;
ASSIGN check whole * b + rest = a ;
ASSIGN exact 0.1 + 0.2 = 0.3 ;
END FUNCTIONSet the trigger block to the program numbers and the function divide. Put in the INTEGER variable items a and b and four uninitialized items: divide has six results, and a and b take the place of two of them. Press the button and right-click the result items. Then change a to -7, and after that a to 7 and b to 0. With b = 0, the process crashes, the trigger block shows the error, and the items come out unchanged:
a, b | quotient | whole (DIV) | floor (INT) | rest | check | exact |
|---|---|---|---|---|---|---|
| 7, 2 | 3.5 | 3 | 3 | 1 | true | false |
| -7, 2 | -3.5 | -3 | -4 | -1 | true | false |
| 7, 0 | DIVISION_BY_ZERO in line 14 |
- DIV and
\belong together like/and%in C and Java: they round towards zero, andwhole * b + rest = ais always true. Python's//and%round down instead; INT of the quotient does that. - DIV is a function of
c0de/math, so it needs a CALL and a variable for its result. The same goes for INT: an expression can only combine values that are already there. 0.1 + 0.2 = 0.3is false, as everywhere with binary floating point numbers.- With
b= 0, the first line already raises DIVISION_BY_ZERO:/doesn't returnInfinity, not even for FLOATs.
Differences you'll trip over
=compares.ASSIGN a b = c ;stores whetherbequalscina.^is the power, not the bitwise XOR of C, Java and Python. That one is⊻.7 \ 4 * 2is 7, not 6:\binds weaker than*and/.NOT a = bis( NOT a ) = b, a TYPE_MISMATCH for numbers. Writea ≠ borNOT ( a = b ).flags & 4 = 4isflags & ( 4 = 4 ), a TYPE_MISMATCH. Put brackets around bit operations:( flags & 4 ) = 4./of two INTEGERs gives a FLOAT, and a FLOAT can't be assigned to an INTEGER. Use DIV, or INT, FIX or ROUND.- There is no truthiness: an IF needs a BOOLEAN,
IF count ;is a TYPE_MISMATCH. INTEGER and FLOAT can be compared with each other, other different types can't. - An index is a literal or a variable, not an expression: there is no
list[i + 1]. Calculate the index into a variable first. - Shifting uses only the lowest 6 bits of the count, as in Java:
1 ← 64is 1.
Try it yourself
- Write a function
modulothat returns a remainder that is never negative for a positiveb, like Python's%:-7and2give 1.
Solution
PROGRAM modulo
NOOP
NOOP
FUNCTION modulo
PARAMETER a INTEGER
PARAMETER b INTEGER
RESULT rest INTEGER
BEGIN
ASSIGN rest ( a \ b + b ) \ b ;
END FUNCTION-7 \ 2 is -1, plus 2 is 1, and 1 \ 2 is 1. For -9 and 4, the result is 3; for positive numbers, nothing changes.
- What is
NOT 1 = 2 ;, and how do you write what was meant?
Solution
NOT binds stronger than =, so c0de tries NOT 1 first and raises TYPE_MISMATCH. Write 1 ≠ 2 ; or NOT ( 1 = 2 ) ;, which are both true.
Read more
- The language: Expressions, Operators and ASSIGN
- Statements: ASSIGN and OPERATOR: the blocks and how to set their symbol
- DIV, INT, FIX, ROUND: whole numbers from divisions