Expressions, Literals, and Lists
In this section, we will explore the basic building blocks of values in pixelWallE: literals, how they combine to form expressions, and how to organize data collections using lists.
Literals
Literals are fixed values that appear directly in your code. They are the simplest form of expression, as they evaluate to themselves.
-
Numbers: Represent integer numerical values.
10 -5 3 0 -
Strings: Represent sequences of characters, usually used for color names, Hex codes, or messages.
"red" "#FF0000" "MyLabel" "Hello PixelWallE" -
Booleans: Represent truth values: true or false.
true false -
Lists: Represent an ordered collection of elements of the same base type.
List< int > [1, 2, 3]
List < string > ["red", "blue", "green"]
List< bool > [] // Empty list -
Function Calls: Although they are operations, the *result* of a function is a value that can be treated as a literal within an expression.
GetActualX() // This returns a number
IsBrushColor("blue") // This returns a booleanSee the Functions section for more details.
Expressions
Expressions are combinations of literals, variables, operators, and function calls that evaluate to produce a value. This value can be a number, a string, a boolean, or a list.
Unary Expressions
Operate on a single operand.
-
Numerical Negation (`-`): Changes the sign of a number.
-10 -(5 + 3) // Evaluates the expression in parentheses first -
Logical Negation (`!`): Inverts a boolean value.
!true // Evaluates to false
!IsBrushColor("black") // If IsBrushColor("black") returns true, this evaluates to false
Binary Expressions
Operate on two operands and an operator.
Arithmetic Operators:
- `+` (Addition)
- `-` (Subtraction)
- `*` (Multiplication)
- `**` (Power)
- `%` (Modulo - remainder of the division)
- `/` (Division)
10 + 5 # Evaluates to 15
GetActualY() * 2 # Multiplies the current Y position by 2
(GetActualX() + 1) / 2 # Adds 1 to the result of GetActualX, then divides by 2
4 ** 2 # Evaluates to 16
10 % 3 # Evaluates to 1
Addition (`+`) can also be used to concatenate strings.
"Pixel" + "WallE" # Evaluates to "PixelWallE"
"Size: " + GetCanvasSize() #Concatenates text and number (automatic conversion)
Logical and Comparison Operators:
- `<` (Less than)
- `<=` (Less than or equal to)
- `>` (Greater than)
- `>=` (Greater than or equal to)
- `==` (Equal to)
- `!=` (Not equal to)
- `&&` (Logical AND)
- `||` (Logical OR)
GetActualX() > 100 # Is the X position greater than 100? (Boolean)
IsColor("#00FF00", 5, 5) == true # Is the pixel at (5,5) green? (Boolean)
true && (GetCanvasSize() > 50) # True AND canvas size is greater than 50? (Boolean)
"red" != "blue" # "red" is not equal to "blue"? (true)
Grouping with Parentheses
Parentheses `()` are used to control the order of evaluation of expressions, ensuring that certain parts are calculated before others. This is fundamental for the clarity and logic of your calculations.
(5 + 3) * 2 # Addition (8) is done before multiplication (8 * 2 = 16)
5 + (3 * 2) # Multiplication (6) is done before addition (5 + 6 = 11)
Lists
Lists are ordered and mutable collections of elements of the same base type (numbers, strings, or booleans). They are useful for storing and manipulating sets of data, such as a sequence of colors or coordinates.
Declaration and Initialization
They are declared using the keyword List, followed by the base type of the elements between <> and an identifier name (the name of your list). They can be initialized at the same time with a list literal.
myNumbers <- List< int >[1, 5, 10, 20]; # Declares and initializes a list of integers
colors <- List< string > ["red", "green", "blue", "#FF0000"]; # Declares and initializes a list of strings
emptyList <- List< bool > []; # Declares and initializes an empty list of booleans
Once declared, you can assign a new list of the same type.
myNumbers <- [100, 200]; # Replaces the content of myNumbers
Accessing Elements
Individual elements are accessed by their index (position) within the list, using square brackets [] after the list name. Indices in pixelWallE, like in many languages, start from 0 for the first element.
firstNumber = myNumbers[0]; # firstNumber now equals 1 (or 100 if reassigned before)
secondColor = colors[1]; # secondColor now equals "green"
lastColor = colors[colors.Length - 1]; # Accesses the last element using Length
Note: Attempting to access an index that does not exist in the list (a negative index or an index greater than or equal to the length of the list) will result in a runtime error.
List Methods
Lists have built-in methods (functions associated with the list) to manipulate their elements. They are called using a dot . after the list name, followed by the method name and parentheses () if the method requires arguments.
-
Add(item): Adds the specifieditemto the end of the list. The type of theitemmust match the base type of the list.myNumbers.Add(25); # myNumbers becomes [..., 25] colors.Add("yellow"); # colors becomes [..., "yellow"] -
RemoveAt(index): Removes the element at the specifiedindex. The index must be a valid number within the list's range.myNumbers.RemoveAt(1); # Removes the second elementNote: Removing an out-of-range index will result in a runtime error.
-
Clear(): Removes all elements from the list, leaving it empty.colors.Clear(); # colors is now an empty list [] -
Length: (It is a property, not a method, so it does not take `()`) Returns the number of elements in the list as an integer.numColors = colors.Length; # numColors will contain the current number of elements in colorsIt is commonly used to iterate over the list or to check if it is empty.