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.
The short syntax structure uses the word NEW:
NEW DATA TYPE VARIABLE = VALUEThe long syntax structure uses the word NEW:
NEW DATA TYPE VARIABLE EQUAL TO VALUEVariable names in ProtoLang must follow these rules:
NEW INT x = 5
NEW INT X = 10
PRINTLN x // Displays "5"
PRINTLN X // Displays "10"NEW INT age = 25
NEW STR firstName = "Alice"
NEW FLOAT temperature = 98.6
NEW BOOL isActive = TRUEEvery New command requires three components:
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 IntegerThe unique identifier for the variable.
NEW INT counter = 0 // "counter" is the variable nameThe 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 valueNEW 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"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"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"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"Variables can be initialized with Literals, other Variables, or Expressions.
NEW INT x = 42 // Integer literal
NEW FLOAT pi = 3.14 // Float literal
NEW STR name = "Bob" // String literal
NEW BOOL active = TRUE // Boolean literalNEW INT original = 100
NEW INT copy = original
PRINTLN copy // Displays "100"
NEW STR firstName = "John"
NEW STR username = firstName
PRINTLN username // Displays "John"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"By default, all variables created with New are in global scope, meaning they can be accessed from anywhere in the program after creation.
NEW INT counter = 0
PRINTLN counter // Displays "0"
SET counter = 5
PRINTLN counter // Displays "5"
// counter is accessible throughout the entire programFor information about local scope and how variables behave within functions, see the Functions documentation.
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.
NEW INT age = 25
SET age = 30 // Valid: Integer value
SET age = 35 // Valid: Integer value
PRINTLN age // Displays "35"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 programEach variable name must be unique within its scope. You cannot create two variables with the same name, even if they have different types.
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"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: 4NEW 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: " .. isVerifiedNEW 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"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: " .. playerScoreNEW 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"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"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"ProtoLang provides both short and long forms for data type names. Both forms are equivalent and produce the same token.
NEW BOOL flag = TRUE
NEW INT count = 10
NEW FLOAT price = 9.99
NEW STR myString = "Hello"NEW BOOLEAN flag = TRUE
NEW INTEGER count = 10
NEW FLOAT price = 9.99
NEW STRING myString = "Hello"NEW INT x = 5
NEW INTEGER y = 10
NEW INT result = x + y
PRINTLN result // Displays "15"PRINTLN x // Runtime error! Variable "x" does not exist!
NEW INT x = 5 // This line is not executed because the program crashed!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 variableNEW INT age = 25.5 // Runtime error! Cannot assign Float literal to Integer type
NEW INT age = 25 // Correct: Integer literal matches Integer typeNEW BOOL flag = 1 // Runtime error! Cannot assign Integer to Boolean
NEW BOOL flag = TRUE // Correct: Boolean literal matches Boolean typeNEW STR name = Alice // Syntax error! String literals must be in quotes
NEW STR name = "Alice" // Correct: String literal is enclosed in quotesNEW INT x // Syntax error! Missing initial value
NEW INT x = 0 // Correct: All three components providedNEW 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 IntegersNEW INT myVariable = 5
PRINTLN MyVariable // Runtime error! Variable "MyVariable" does not exist
PRINTLN myVariable // Correct: Matches the exact case used in creationNEW STR message = Hello // Syntax error! Strings must be quoted
NEW STR message = "Hello" // Correct: String is properly quotedNEW FLOAT temperature = 98 // Runtime error! 98 is Integer, not Float
NEW FLOAT temperature = 98.0 // Correct: Float literal with decimal point