ProtoLang.net

Reference: New

☰ Hide Sidebar
TOKEN NEW
ALIAS None

The New Command is used to create a variable in the current scope's memory (Read about scope in Functions). All variables are global scope by default. The New command is the only way to create variables in ProtoLang, and every variable must be given a unique name, a Data Type, and an initial value when it is created.

Syntax

The short syntax structure uses the word NEW:

NEW DATA TYPE VARIABLE = VALUE

The long syntax structure uses the word NEW:

NEW DATA TYPE VARIABLE EQUAL TO VALUE

Variable Naming Rules

Variable names in ProtoLang must follow these rules:

  • Names can contain letters (both uppercase and lowercase).
  • Names are case-sensitive - x and X are different variables.
  • Names must be unique within their scope.

Example: Case-Sensitive Variable Names

NEW INT x = 5
NEW INT X = 10
PRINTLN x // Displays "5"
PRINTLN X // Displays "10"

Example: Descriptive Variable Names

NEW INT age = 25
NEW STR firstName = "Alice"
NEW FLOAT temperature = 98.6
NEW BOOL isActive = TRUE

Required Components

Every New command requires three components:

1. Data Type

Declares what type of data the variable will hold. Once set, the data type cannot be changed.

NEW INT score = 100 // INT declares this is an Integer

2. Variable Name

The unique identifier for the variable.

NEW INT counter = 0 // "counter" is the variable name

3. Initial Value (with EQUAL TO operator)

The Equal To operator assigns the starting value. The value must match the declared data type.

NEW FLOAT price = 19.99 // = 19.99 assigns the initial value

Creating Variables with Different Data Types

Example: Creating Boolean Variables

NEW BOOL isReady = TRUE
NEW BOOL hasError = FALSE
NEW BOOL isComplete = 5 > 3 // Using comparison expression
PRINTLN isReady // Displays "TRUE"
PRINTLN hasError // Displays "FALSE"
PRINTLN isComplete // Displays "TRUE"

Example: Creating Integer Variables

NEW INT age = 25
NEW INT score = 100
NEW INT count = 0
NEW INT total = score + 50 // Using arithmetic expression
PRINTLN age // Displays "25"
PRINTLN total // Displays "150"

Example: Creating Float Variables

NEW FLOAT temperature = 98.6
NEW FLOAT price = 19.99
NEW FLOAT average = 75.5
NEW FLOAT doubled = average * 2.0 // Using arithmetic expression
PRINTLN temperature // Displays "98.6"
PRINTLN doubled // Displays "151.0"

Example: Creating String Variables

NEW STR name = "Alice"
NEW STR message = "Hello World"
NEW STR empty = ""
NEW STR greeting = "Hello" .. " " .. name // Using concatenate expression
PRINTLN name // Displays "Alice"
PRINTLN greeting // Displays "Hello Alice"

Using Different Value Types

Variables can be initialized with Literals, other Variables, or Expressions.

Example: Initializing with Literals

NEW INT x = 42 // Integer literal
NEW FLOAT pi = 3.14 // Float literal
NEW STR name = "Bob" // String literal
NEW BOOL active = TRUE // Boolean literal

Example: Initializing with Variables

NEW INT original = 100
NEW INT copy = original
PRINTLN copy // Displays "100"

NEW STR firstName = "John"
NEW STR username = firstName
PRINTLN username // Displays "John"

Example: Initializing with Expressions

NEW INT base = 10
NEW INT multiplied = base * 5 // Arithmetic expression
NEW BOOL inRange = TRUE && FALSE // Logical expression
NEW STR label = "Value: " .. base // Concatenate expression
PRINTLN multiplied // Displays "50"
PRINTLN inRange // Displays "FALSE"
PRINTLN label // Displays "Value: 10"

Variable Scope

By default, all variables created with New are in global scope, meaning they can be accessed from anywhere in the program after creation.

Example: Global Scope Access

NEW INT counter = 0
PRINTLN counter // Displays "0"
SET counter = 5
PRINTLN counter // Displays "5"
// counter is accessible throughout the entire program

For information about local scope and how variables behave within functions, see the Functions documentation.

Data Type Enforcement

Once a variable is created with a specific data type, that type cannot be changed. The variable will only accept values that match its declared type.

Example: Type Consistency

NEW INT age = 25
SET age = 30 // Valid: Integer value
SET age = 35 // Valid: Integer value
PRINTLN age // Displays "35"

Example: Multiple Variables with Different Types

NEW INT count = 10
NEW FLOAT average = 85.5
NEW STR name = "Test"
NEW BOOL passed = TRUE
// Each variable maintains its own type throughout the program

Variable Uniqueness

Each variable name must be unique within its scope. You cannot create two variables with the same name, even if they have different types.

Example: Unique Names Required

NEW INT value = 10
NEW INT Value = 20 // Valid: "Value" is different from "value"
NEW INT VALUE = 30 // Valid: "VALUE" is different from both above
PRINTLN value // Displays "10"
PRINTLN Value // Displays "20"
PRINTLN VALUE // Displays "30"

Practical Applications

Example: Counter Initialization

NEW INT counter = 0
WHILE counter < 5
    PRINTLN "Count: " .. counter
    SET counter = counter + 1
END WHILE
// Displays: Count: 0, Count: 1, Count: 2, Count: 3, Count: 4

Example: User Information Storage

NEW STR username = "alice123"
NEW INT userAge = 28
NEW FLOAT accountBalance = 1250.75
NEW BOOL isVerified = TRUE
PRINTLN "User: " .. username
PRINTLN "Age: " .. userAge
PRINTLN "Balance: $" .. accountBalance
PRINTLN "Verified: " .. isVerified

Example: Calculation Setup

NEW FLOAT width = 10.5
NEW FLOAT height = 7.5
NEW FLOAT area = width * height
NEW FLOAT perimeter = ( width + height ) * 2.0
PRINTLN "Area: " .. area // Displays "Area: 78.75"
PRINTLN "Perimeter: " .. perimeter // Displays "Perimeter: 36.0"

Example: Game State Initialization

NEW INT playerHealth = 100
NEW INT playerScore = 0
NEW STR playerName = "Hero"
NEW BOOL gameOver = FALSE
NEW INT level = 1
PRINTLN playerName .. " - Level " .. level
PRINTLN "Health: " .. playerHealth
PRINTLN "Score: " .. playerScore

Complex Initializations

Example: Creating Variables from Calculations

NEW FLOAT basePrice = 100.0
NEW FLOAT taxRate = 0.08
NEW FLOAT tax = basePrice * taxRate
NEW FLOAT totalPrice = basePrice + tax
PRINTLN "Base Price: $" .. basePrice // Displays "Base Price: $100.0"
PRINTLN "Tax: $" .. tax // Displays "Tax: $8.0"
PRINTLN "Total: $" .. totalPrice // Displays "Total: $108.0"

Example: Creating Variables from Comparisons

NEW INT minimumAge = 18
NEW INT userAge = 21
NEW BOOL isAdult = userAge >= minimumAge
NEW BOOL canVote = isAdult
IF canVote == TRUE
    PRINTLN "Eligible to vote"
END IF
// Displays "Eligible to vote"

Example: Creating Variables with Combined Logic

NEW FLOAT temperature = 72.5
NEW FLOAT humidity = 45.0
NEW BOOL isTempGood = ( temperature >= 68.0 ) && ( temperature <= 78.0 )
NEW BOOL isHumidityGood = humidity <= 60.0
NEW BOOL isComfortable = isTempGood && isHumidityGood
IF isComfortable == TRUE
    PRINTLN "Weather is comfortable"
ELSE
    PRINTLN "Weather is uncomfortable"
END IF
// Displays "Weather is comfortable"

Using Data Type Aliases

ProtoLang provides both short and long forms for data type names. Both forms are equivalent and produce the same token.

Example: Short Form Data Types

NEW BOOL flag = TRUE
NEW INT count = 10
NEW FLOAT price = 9.99
NEW STR myString = "Hello"

Example: Long Form Data Types

NEW BOOLEAN flag = TRUE
NEW INTEGER count = 10
NEW FLOAT price = 9.99
NEW STRING myString = "Hello"

Example: Mixing Short and Long Forms

NEW INT x = 5
NEW INTEGER y = 10
NEW INT result = x + y
PRINTLN result // Displays "15"

Important Notes

  • Every variable must be created with New before it can be used.
  • Variable names are case-sensitive but tokens are not.
  • Each variable requires a unique name within its scope.
  • Data types cannot be changed after creation.
  • The initial value must match the declared data type.
  • All three components (name, type, value) are mandatory.
  • Variables are global scope by default unless created inside a function.

Common Errors

Error: Using Variable Before Creating It

PRINTLN x // Runtime error! Variable "x" does not exist!
NEW INT x = 5 // This line is not executed because the program crashed!

Error: Creating Duplicate Variable

NEW INT x = 5
NEW INT x = 10 // Runtime error! Variable "x" already exists!
// Correct: Use SET to change existing variable
NEW INT x = 5
SET x = 10 // Valid: Updates existing variable

Error: Mismatched Data Type and Value

NEW INT age = 25.5 // Runtime error! Cannot assign Float literal to Integer type
NEW INT age = 25 // Correct: Integer literal matches Integer type

Error: Wrong Type for Boolean

NEW BOOL flag = 1 // Runtime error! Cannot assign Integer to Boolean
NEW BOOL flag = TRUE // Correct: Boolean literal matches Boolean type

Error: Wrong Type for String

NEW STR name = Alice // Syntax error! String literals must be in quotes
NEW STR name = "Alice" // Correct: String literal is enclosed in quotes

Error: Missing Required Component

NEW INT x // Syntax error! Missing initial value
NEW INT x = 0 // Correct: All three components provided

Error: Mixing Integer and Float Types

NEW INT x = 5
NEW FLOAT y = 2.5
NEW INT result = x + y // Type error! Cannot mix Integer and Float in expression
// Correct: Keep types consistent
NEW INT a = 5
NEW INT b = 2
NEW INT result = a + b // Valid: Both are Integers

Error: Case Sensitivity Confusion

NEW INT myVariable = 5
PRINTLN MyVariable // Runtime error! Variable "MyVariable" does not exist
PRINTLN myVariable // Correct: Matches the exact case used in creation

Error: Using String Without Quotes

NEW STR message = Hello // Syntax error! Strings must be quoted
NEW STR message = "Hello" // Correct: String is properly quoted

Error: Integer Instead of Float

NEW FLOAT temperature = 98 // Runtime error! 98 is Integer, not Float
NEW FLOAT temperature = 98.0 // Correct: Float literal with decimal point