Expressions and operators

P* have mostly adapted operators and their precedence from the C-language. The basics of the expression syntax is also the same as in C. Expressions are run right to left. Most operators are mapped directly to their C-equalient, and behave the same way.

The associativity for an operator decides the return value of the expression. In the example below, we see that the return value of the expression is different depending on which variable we have on the left side of the + operator.

SCENE main {
	int a = 10;
	float b = "1.5";

	/* The expression a + b will return an integer */
	echo "Integer result: " . (a + b) . "\n";

	/* The expression b + a will return a float */
	echo "Float result: " . (b + a) . "\n";
}

This program will output

Integer result: 11
Float result: 11.500000

Table 3.4. Operators in P*

PrecedenceOperatorDescriptionAssociativityExample
1()Function callLeft or nonecall(2,3)
[]Array subscription (hashes and arrays)Leftmy_array[2]
->Element selection (structs and classes)my_struct->variable
*Indirection - Return the value of a pointerRight*my_pointer
*First element selector - Returns first element of an array, same as [0]*my_array
&Pointer To - Returns a pointer to a value&my_value
     
2++Postfix incrementLefti++
--Postfix decrementi--
@Element counter (arrays and hashes)Right@my_array
isemptyEmpty check logic (strings)isempty my_string
definedCheck if array, hash or ENV element is defineddefined my_array[2]
keysReturn an array of all keys in a hashmy_array<string> keys_of_hash = keys my_hash
3++Prefix incrementRight++i
--Prefix decrement--i
~Bitwise not~invert_this
!Logical not!var
     
4=~Pattern matchLefta =~ /regular expression/
!~Pattern negative matcha !~ /regular expression/
     
5*MultiplicationLefta*b
/Divisiondividend / divisor
%Modulusdividend % divisor
     
6+Sumnumber_a + number_b 
-Subtractionnumber_a - number_b 
.String concatenationstring_a . string_b 
      
7<<Left bitshiftLefta << 1 
>>Right bitshifta >> 1 
      
8&Bitwise ANDLefta & b 
9^Bitwise XORa ^ b 
10|Bitwise ORa | b 
      
11<=Logical less than or equalLefta <= b 
>=Logical more than or equala >= b 
<Logical less thana < b 
>Logical more thana > b 
     
12==Logical equala == b 
!=Logical not equala != b 
     
13&&Logical ANDLefta && b 
14||Logical ORa || b 
15=>Push discard chain to arrayRight10, 20, 30 => my_array 
==>Same as =>, but clears array first10, 20, 30 ==> my_array 
      
16=AssignRighta = b 
+=Assign by suma += b 
-=Assign by differencea -= b 
*=Assign by producta *= b 
/=Assign by quotienta /= b 
%=Assign by remaindera %= b 
&=Assign by bitwise ANDa &= b 
^=Assign by bitwise XORa ^= b 
|=Assign by bitwise ORa |= b 
<<=Assign by bitwise left shifta <<= b 
>>=Assign by bitwise right shifta >>= b 
.=Append to stringa .= b 
      
17..Range operator inclusiveLeftforeach (i; 0..10) { /* Loops 0 through 10 */ } 
...Range operator exclusiveforeach (i; 0..10) { /* Loops 1 through 9 */ } 
      
18,Discard operator (comma)Lefta, b 
19echoWrite expression to standard outputRightecho "Hello world!" 
errchoWrite expression to standard errorerrcho "This is an error message" 
      
20breakBreak out from nearest loopNonebreak 
returnReturn from nearest functionRight or nonereturn 1