ProtoLang.net

Reference: Expression

☰ Hide Sidebar
TOKEN None
ALIAS None

An Expression is a combination of Literals, Variables, and operators that evaluates to a single value. Expressions allow you to perform calculations, comparisons, and logical operations to produce new values.

Types of Expressions

Arithmetic Expressions

Arithmetic expressions use Arithmetic Operators to perform mathematical calculations on numeric values. The expression evaluates to a single numeric result.

NEW INT x = 5 + 3 // Expression: 5 + 3, evaluates to 8
NEW INT y = 10 * 2 // Expression: 10 * 2, evaluates to 20
NEW INT z = 100 / 4 // Expression: 100 / 4, evaluates to 25
PRINTLN x // Displays "8"
PRINTLN y // Displays "20"
PRINTLN z // Displays "25"

Comparison Expressions

Comparison expressions use Comparison Operators to compare two values. The expression always evaluates to a Boolean result (True or False).

NEW BOOL x = 5 > 3 // Expression: 5 > 3, evaluates to TRUE
NEW BOOL y = 10 == 10 // Expression: 10 == 10, evaluates to TRUE
NEW BOOL z = 7 < 2 // Expression: 7 < 2, evaluates to FALSE
PRINTLN x // Displays "TRUE"
PRINTLN y // Displays "TRUE"
PRINTLN z // Displays "FALSE"

Logical Expressions

Logical expressions use Logical Operators to combine Boolean values. The expression evaluates to a single Boolean result.

NEW BOOL x = TRUE && FALSE // Expression evaluates to FALSE
NEW BOOL y = TRUE || FALSE // Expression evaluates to TRUE
NEW BOOL z = ( 5 > 3 ) && ( 10 < 20 ) // Expression evaluates to TRUE
PRINTLN x // Displays "FALSE"
PRINTLN y // Displays "TRUE"
PRINTLN z // Displays "TRUE"

String Expressions

String expressions use the Concatenate operator to combine multiple values into a single string. Any data type can be concatenated with strings.

NEW STR greeting = "Hello" .. " " .. "World" // Expression evaluates to "Hello World"
NEW STR message = "Value: " .. 42 // Expression evaluates to "Value: 42"
NEW STR status = "Active: " .. TRUE // Expression evaluates to "Active: TRUE"
PRINTLN greeting // Displays "Hello World"
PRINTLN message // Displays "Value: 42"
PRINTLN status // Displays "Active: TRUE"

Complex Expressions

Expressions can combine multiple operators and values. Use Groups to control the order of evaluation.

Example: Multi-Step Arithmetic

NEW INT x = ( 5 + 3 ) * 2 // Expression: ( 5 + 3 ) * 2, evaluates to 16
NEW INT y = 10 + 5 * 2 // Expression: 10 + 5 * 2, evaluates to 20
PRINTLN x // Displays "16"
PRINTLN y // Displays "20"

Example: Complex Logical Conditions

NEW INT age = 25
NEW BOOL hasLicense = TRUE
NEW BOOL canDrive = ( age >= 18 ) && hasLicense
PRINTLN canDrive // Displays "TRUE"

Example: Mixed Expression Types

NEW INT a = 10
NEW INT b = 5
NEW STR result = "Sum: " .. ( a + b ) .. ", Product: " .. ( a * b )
PRINTLN result // Displays "Sum: 15, Product: 50"

Expressions in Different Contexts

Example: Expressions in Variable Assignment

NEW INT x = 5
NEW INT y = x + 10 // Expression: x + 10
NEW INT z = y * 2 // Expression: y * 2
PRINTLN z // Displays "30"

Example: Expressions in Print Statements

NEW INT x = 5
NEW INT y = 3
PRINTLN x + y // Expression: x + y, displays "8"
PRINTLN x > y // Expression: x > y, displays "TRUE"

Example: Expressions in Conditionals

NEW INT score = 85
IF score >= 90 // Expression: score >= 90
    PRINTLN "Grade: A"
ELIF score >= 80 // Expression: score >= 80
    PRINTLN "Grade: B"
END IF

Example: Expressions in Loops

NEW INT i = 1
WHILE i <= 5 // Expression: i <= 5
    PRINTLN i
    SET i = i + 1 // Expression: i + 1
END WHILE

Important Notes

  • Expressions must follow data type rules - you cannot mix incompatible types in arithmetic operations.
  • Comparison expressions always produce Boolean results.
  • Use groups to make the order of operations explicit and clear.
  • The Concatenate operator works with any data type when creating strings.
  • Expressions are evaluated before being assigned to variables or used in commands.

Common Errors

Error: Mixing Integer and Float in Arithmetic Expression

NEW INT x = 5
NEW FLOAT y = 2.5
NEW INT z = x + y // Type error! Cannot mix Integer and Float in expression

Error: Using Arithmetic Operator on Strings

NEW STR x = "Hello"
NEW STR y = "World"
PRINTLN x + y // Type error! Use the Concatenate operator (..) for strings
PRINTLN x .. y // Correct! Displays "HelloWorld"

Error: Comparing Incompatible Types

NEW INT x = 5
NEW STR y = "5"
NEW BOOL result = x == y // Type error! Cannot compare Integer and String

Error: Using Boolean in Arithmetic Expression

NEW BOOL x = TRUE
NEW INT y = 5
NEW INT z = x + y // Type error! Cannot use Boolean in arithmetic expression