Declaring Var

X3 programming language, variables are used to store and manipulate data. Here's how you can incorporate variables into your programming language:

Variable Declaration:

You can allow users to declare variables using a statement like VAR followed by the variable name and optionally its initial value.

Example:

var x =2
var y = 10

Data Types:

You can support various data types such as integers, floating-point numbers, strings, boolean values, etc. Users can declare variables with specific data types.

Example:

var x : INTEGER
var y : FLOAT
var name : STRING
var flag : BOOLEAN

Variable Assignment:

Users should be able to assign values to variables using the assignment operator =.

Example:

x = 5
y = x + 3
name = "John"
flag = TRUE

Variable Scope:

Variables should have a scope, which defines where they can be accessed. Common scopes include global and local scopes.

Example:

# Global variable
var globalVar = 100

FUN testFunction()
    # Local variable
    var localVar = 50
    # Accessing global variable
    show(globalVar)
    # Accessing local variable
    show(localVar)
end

Variable Interpolation:

You can allow string interpolation to include variables within strings.

Example:

var name = "Alice"
print("Hello, {name}!")

Constants:

You can provide support for constants, which are variables whose values cannot be changed once assigned.

Example:

CONST PI = 3.14159
var radius = 5
var area = PI * radius * radius

By implementing variables and incorporating them into your X3 programming language, you can provide users with the flexibility to store and manipulate data within their programs.

Last updated