Data Types in M7 Script
Overview
M7 Script supports a variety of data types to handle different kinds of values. These types ensure proper data manipulation and enforce structure in scripts.
1. Type Declarations
M7 allows both untyped and explicitly typed variable declarations.
Untyped (var
)
- Declares a variable without specifying its type.
- Type is inferred at runtime.
- Example:
var x = 10; var y = "Hello";
Explicitly Typed Variables
M7 supports explicitly defining a variable's type to enforce consistency.
- Boolean (
bool
) - Storestrue
orfalse
. - Integer (
int
) - Stores whole numbers. - Float (
float
) - Stores decimal numbers. - String (
string
) - Stores text. - Array (
array
) - Stores indexed collections. - Hash (
hash
) - Stores key-value pairs. - Callable (
callable
) - Stores function references.
Example:
bool isActive = true;
int count = 42;
float pi = 3.14;
string message = "Hello";
array numbers = [1, 2, 3];
hash user = { name: "Alice", age: 30 };
callable fn = someFunction;
Constants (const
)
- Declares a value that cannot be reassigned.
- Example:
const MAX_SIZE = 100;
2. Primitive Data Types
Integer (int
)
- Represents whole numbers (both positive and negative).
- Supports decimal, hexadecimal (
0x
), binary (0b
), and octal (0o
) notation. - All integers are stored internally as
long
(int64_t). - Example:
int a = 42; int hexValue = 0x2A; // 42 in hexadecimal int binaryValue = 0b101010; // 42 in binary int octalValue = 0o52; // 42 in octal
Float (float
)
- Represents numbers with decimal points.
- Supports scientific notation (
1.23e4
for 12300). - All floats are stored internally as
double
. - Example:
float pi = 3.14159; float largeNumber = 1.23e4; // 12300
Boolean (bool
)
- Represents truth values
true
orfalse
. - Example:
bool isActive = true; bool isComplete = false;
String (string
)
- Represents text sequences.
- Supports single (
'
), double ("
), and backtick (`
) quoted strings. - Example:
string name = "John Doe"; string message = 'Hello, World!'; string dynamicString = `Hello, ${name}!`;
Null (null
) & Undefined (undefined
)
null
: Represents an explicitly empty or uninitialized value.undefined
: Represents an unassigned variable (default state before initialization).- Example:
var noValue = null; var notAssigned; // Implicitly undefined
3. Composite Data Types
Arrays (array
)
- Indexed collection of values.
- Arrays are always passed by reference.
- Example:
array numbers = [1, 2, 3, 4, 5]; array mixed = [1, "text", true, null];
Hashes (hash
)
- Key-value mapping (associative arrays/dictionaries).
- Supports dot notation (
data.key
) and bracket notation (data["key"]
). - Hashes are always passed by reference.
- Example:
hash user = { name: "Alice", age: 30, active: true }; var value = user.age; // Access via dot notation var valueAlt = user["age"]; // Access via bracket notation
Pass-by-Reference vs. Pass-by-Value
- Scalars (
int
,float
,bool
,string
) are passed by value. - Complex types (
array
,hash
) are passed by reference.
Example:
function modifyArray(array values) {
values[0] = 99; // This modifies the original array
}
var myArray = [1, 2, 3];
modifyArray(myArray);
print(myArray[0]); // Outputs: 99
4. Long & Double Consideration
M7 Script does not differentiate between long
and int
or double
and float
.
- Current Approach: Uses
int
for whole numbers andfloat
for decimal values. - All integers are stored internally as
long (int64_t)
and all floats asdouble
. - Future Considerations:
- The language may be expanded in the future to accommodate explicit
long
anddouble
types if needed.
- The language may be expanded in the future to accommodate explicit
5. Special Literals & Type Handling
- Type Inference: M7 infers types based on assigned values.
- Explicit Type Declarations: Enforced when specified.
- Type Checking: Ensures type consistency during execution.
This document provides an overview of data types in M7 Script and their usage.