ProtoLang.net

Reference: Group

☰ Hide Sidebar
TOKEN GROUP
ALIAS (

The Group Delimiter is used to control the order of operations in expressions and to enclose function parameters. Groups use parentheses to explicitly define which operations should be evaluated first, overriding the default precedence rules. They are also required syntax when defining and calling functions.

Syntax

The short syntax structure uses the ( ) symbols:

( EXPRESSION )

The long syntax structure uses the words GROUP and END GROUP:

GROUP EXPRESSION END GROUP

Purpose

Groups serve two primary purposes in ProtoLang:

  • Arithmetic Expressions: Control the order of operations by forcing certain calculations to be performed first.
  • Function Syntax: Enclose parameter lists in both function definitions and function calls.

Groups in Arithmetic Expressions

Groups override the default order of operations, ensuring that expressions inside parentheses are evaluated first.

Example: Changing Order of Operations

NEW INT x = 5 + 3 * 2
PRINTLN x // Displays "11" (multiplication happens first)

NEW INT y = ( 5 + 3 ) * 2
PRINTLN y // Displays "16" (addition happens first due to parentheses)

Example: Complex Arithmetic

NEW INT a = 10
NEW INT b = 5
NEW INT c = 2
NEW INT result = ( a + b ) * c
PRINTLN result // Displays "30"

Example: Nested Groups

NEW INT value = ( ( 10 + 5 ) * 2 ) - 3
PRINTLN value // Displays "27"
// Evaluation: (15 * 2) - 3 = 30 - 3 = 27

Example: Temperature Conversion

NEW FLOAT celsius = 25.0
NEW FLOAT fahrenheit = ( celsius * 9.0 / 5.0 ) + 32.0
PRINTLN fahrenheit // Displays "77.0"

Groups in Comparison Expressions

Groups make complex logical conditions clear and explicit.

Example: Grouping Comparisons

NEW INT age = 25
NEW INT minAge = 18
NEW INT maxAge = 65
NEW BOOL inRange = ( age >= minAge ) && ( age <= maxAge )
PRINTLN inRange // Displays "TRUE"

Example: Complex Logical Expression

NEW BOOL a = TRUE
NEW BOOL b = FALSE
NEW BOOL c = TRUE
NEW BOOL result = ( a || b ) && c
PRINTLN result // Displays "TRUE"

Groups in Function Definitions

When defining a Function, groups enclose the parameter list.

Example: Function with No Parameters

FUNCTION greet ( )
    PRINTLN "Hello!"
END FUNCTION

greet ( )

Example: Function with One Parameter

FUNCTION square ( INT n )
    NEW INT result = n * n
    RETURN result
END FUNCTION

NEW INT value = square ( 5 )
PRINTLN value // Displays "25"

Example: Function with Multiple Parameters

FUNCTION add ( INT a , INT b )
    NEW INT sum = a + b
    RETURN sum
END FUNCTION

NEW INT result = add ( 10 , 5 )
PRINTLN result // Displays "15"

Groups in Function Calls

When calling a function, groups enclose the argument list.

Example: Calling Function with Arguments

FUNCTION multiply ( INT x , INT y )
    NEW INT product = x * y
    RETURN product
END FUNCTION

NEW INT answer = multiply ( 6 , 7 )
PRINTLN answer // Displays "42"

Example: Nested Function Calls

FUNCTION double ( INT n )
    RETURN n * 2
END FUNCTION

FUNCTION addTen ( INT n )
    RETURN n + 10
END FUNCTION

NEW INT result = double ( addTen ( 5 ) )
PRINTLN result // Displays "30"
// Evaluation: addTen(5) = 15, then double(15) = 30

Groups in Conditionals

Groups clarify complex conditions in If statements.

Example: Grouped Condition

NEW INT score = 85
NEW INT attendance = 90
IF ( score >= 80 ) && ( attendance >= 85 )
    PRINTLN "Pass with honors"
END IF
// Displays "Pass with honors"

Example: Complex Nested Conditions

NEW BOOL isStudent = TRUE
NEW BOOL isSenior = FALSE
NEW BOOL hasCoupon = TRUE
IF ( isStudent || isSenior ) && hasCoupon
    PRINTLN "Discount applied"
END IF
// Displays "Discount applied"

Practical Applications

Example: Formula Calculation

NEW FLOAT width = 10.0
NEW FLOAT height = 5.0
NEW FLOAT area = width * height
NEW FLOAT perimeter = ( width + height ) * 2.0
PRINTLN "Area: " .. area // Displays "Area: 50.0"
PRINTLN "Perimeter: " .. perimeter // Displays "Perimeter: 30.0"

Example: Discount Calculation

NEW FLOAT price = 100.0
NEW FLOAT discountPercent = 20.0
NEW FLOAT discount = price * ( discountPercent / 100.0 )
NEW FLOAT finalPrice = price - discount
PRINTLN "Final price: $" .. finalPrice // Displays "Final price: $80.0"

Example: Average Calculation

NEW INT a = 10
NEW INT b = 20
NEW INT c = 30
NEW INT average = ( a + b + c ) / 3
PRINTLN "Average: " .. average // Displays "Average: 20"

Important Notes

  • Groups control the order of evaluation in expressions.
  • Groups are required syntax for function definitions and calls.
  • Operations inside groups are always evaluated first.
  • Groups can be nested to create complex expressions.
  • Use groups to make your code's intent clear and explicit.
  • Empty groups ( ) indicate a function with no parameters.

Common Errors

Error: Missing Closing Parenthesis

NEW INT x = ( 5 + 3 * 2
PRINTLN x // Syntax error! Missing closing parenthesis

// Correct:
NEW INT y = ( 5 + 3 ) * 2
PRINTLN y

Error: Missing Opening Parenthesis

NEW INT x = 5 + 3 ) * 2
PRINTLN x // Syntax error! Missing opening parenthesis

// Correct:
NEW INT y = ( 5 + 3 ) * 2
PRINTLN y

Error: Missing Parentheses in Function Call

FUNCTION getValue ( )
    RETURN 42
END FUNCTION

NEW INT x = getValue // Syntax error! Missing parentheses

// Correct:
NEW INT y = getValue ( )

Error: Unbalanced Nested Groups

NEW INT x = ( ( 10 + 5 ) * 2
PRINTLN x // Syntax error! Unbalanced parentheses

// Correct:
NEW INT y = ( ( 10 + 5 ) * 2 )
PRINTLN y