Slag:Operators
From Plasmaworks
Contents |
Built-In Operators
| Symbol | Name | Types | Behavior |
| + | Plus | numerical | 3 + 4 → 7 |
| - | Minus | numerical | 5 - 4 → 1 |
| * | Times | numerical | 3 * 4 → 12 |
| / | Divided By | numerical | 99.0 / 100.0 → 0.99 99 / 100 → 0 (integer division only keeps the whole part of the result). |
| % | Mod (modulo) | numerical | |
| ^ | Power | numerical | 2 ^ 8 → 256 |
| ++i i++ |
Increment | numerical | i++ or ++i → i = i + 1 Increment must be a stand-alone statement in Slag. |
| --i i-- |
Decrement | numerical | i-- or --i → i = i - 1 Decrement must be a stand-alone statement in Slag. |
| & | Bitwise AND | integer, logical | 0b0011 & 0b0101 → 0b0001 |
| | | Bitwise OR | integer, logical | 0b0011 | 0b0101 → 0b0111 |
| ~ | Bitwise XOR | integer, logical | 0b0011 ~ 0b0101 → 0b0110 |
| ! | Bitwise Complement | integer, logical | !0b00110101 → 0b11001010 |
| left_shifted() | Left Shift | integer | a.left_shifted(b) (0b10100011).left_shifted(2) → 0b1010001100 |
| right_shifted() | Right Shift | integer | (0b10100011).right_shifted(2) → 0b101000 Zeros are shifted into the left-side sign bit. |
| right_xshifted() | Right Shift with Sign Extend | integer | This is like right_shifted() except that the sign bit is replicated with each shift. |
| left_rotated() | Left Rotate | integer | With each rotation the bits are left-shifted and the leftmost bit (of 32 or 64 bits) is placed into the rightmost bit. |
| right_rotated() | Right Rotate | integer | With each rotation the bits are right-shifted and the rightmost bit is placed into the leftmost bit (of 32 or 64 bits). |
| == | Is Equal | primitives |
Results in a Logical true or false result. |
| != | Is Not Equal | primitives | 3 != 4 → true |
|
< |
Less Than LT or Eq To |
numerical | 3 < 4 → true 3 <= 4 → true |
| <> | Compare | numerical | a <> b → gt, eq, or lt Underneath: a <> b → Logical(b - a) |
| is | Is | reference | a is b → true only if a is referencing the same object as b. |
| isNot | Is Not | reference | a isNot b → true only if a and b reference different objects. |
| instanceOf | Instance Of | reference | a instanceOf b → true only a and b are the same type or if b's type is an ancestor of a's type. |
| notInstanceOf | Not Instance Of | reference | a notInstanceOf b → not (a instanceOf b) |
| as | As | reference | a as SomeType → SomeType reference if (a instanceOf SomeType) or null if (a notInstanceOf SomeType). |
| and | Logical AND | logical |
false and false → false |
| or | Logical OR | logical | false or false → false false or true → true |
| xor | Logical XOR | logical | false xor false → false false xor true → true |
| not | Logical NOT | logical | not true → false not false → true |
| ? | Logicalize | all types | The Postfix Logicalize operator results in true if its operand is non-zero, non-null, or true depending on what type the operand is. integer_value? → (integer_value != 0) |
| upTo .. |
Up To | numerical |
a upTo b |
| upToLessThan ..< |
Up To Less Than | numerical | a upToLessThan b [step s] a..<b [step s] |
| downTo | Down To | numerical | a downTo b [step s] Returns a range iterator that counts down from a to b. The step size is assumed to be (-1) unless otherwise specified. |
| downToGreaterThan ..> |
Down To Greater Than | numerical | a downToGreaterThan b [step s] a..>b [step s] |
| . | Member | reference | obj.m → Accesses member m of object obj. |
| v.(CastToType) PrimitiveType(v) |
Cast | all | Primitives: changes value v to be the given type. References: forces reference v to be treated as type CastToType. |
| v coerceAs DataType | Coerce As | all | Forces the compiler to treat the left-hand value as being of the right-hand DataType, bypassing the conversions or type checks resulting from normal casts. This is a dangerous command recommended for advanced programmers only, as misuse will crash or corrupt the program. |
| duplicate(v) | Duplicate | primitive | duplicate(a) → a No effect with by-value primitives; implemented as a primitive operator to be compatible with the duplicate() operator method. |
Operator methods
These may be defined in your classes to allow the given convenience behavior.
| Usage | Method Signature | Behavior |
| == | op==( DataType other ).Logical | obj1 == obj2 → obj1.equals(obj2) |
| != | obj != obj2 → not obj1.equals(obj2) | |
| > | op<>( DataType other ).Logical | obj1 > obj2 → obj1.op<>(obj2) == gt |
| < | obj1 < obj2 → obj1.op<>(obj2) == lt | |
| >= | obj1 >= obj2 → obj1.op<>(obj2) != lt | |
| <= | obj1 <= obj2 → obj1.op<>(obj2) != gt | |
| <> | obj1 <> obj2 → obj1.op<>(obj2) | |
| + | op+( DataType other ).ResultType | obj1 + obj2 → obj1.op+(obj2) |
| - | op-( DataType other ).ResultType | obj1 - obj2 → obj1.op-(obj2) |
| * | op*( DataType other ).ResultType | obj1 * obj2 → obj1.op*(obj2) |
| / | op/( DataType other ).ResultType | obj1 / obj2 → obj1.op/(obj2) |
| % | op%( DataType other ).ResultType | obj1 % obj2 → obj1.op%(obj2) |
| ^ | op^( DataType other ).ResultType | obj1 ^ obj2 → obj1.op^(obj2) |
| & | op&( DataType other ).ResultType | obj1 & obj2 → obj1.op&(obj2) |
| | | op|( DataType other ).ResultType | obj1 | obj2 → obj1.op|(obj2) |
| ~ | op~( DataType other ).ResultType | obj1 ~ obj2 → obj1.op~(obj2) |
| ! | op!().ResultType | !obj → obj.op!() |
| - (unary) | op-().ResultType | -obj → obj.op-() |
| duplicate | duplicate(existing_object) | duplicate(obj) → Object.duplicate(obj).(DataType) When the duplicate(obj) command is used, the singleton method Object.duplicate() is called, which either returns "null" if obj is null or "obj.create_duplicate()" otherwise. To have "duplicate" work correctly on objects of type Mouse, define "method create_duplicate().Object:" in class Mouse and return a copy of the object itself. |
| PrimType(obj) | to_PrimType.PrimType | Int32(obj) → obj.to_Int32() Called when an object is implicitly or explicitly cast to a primitive type. |
| p.(DataType) | DataType.create_from( [p.type] n ) .DataType | (5.3).(String) → String.create_from( 5.3 ) 5 + "slag" → String.create_from(5).op+("slag") |
| obj[index] | get( [index.type] index ).ResultType | obj[index] → obj.get(index) Works with any number of "index" parameters, e.g. obj[3,5]. |
| obj[index] = value | set([index.type] index,[value.type] value) | obj[index] = value → obj.set(index,value) |