# Newest Features to be introduced in Echo!

MufiZ v0.10.0 is packing some new exciting features already, firstly we are adding support for `foreach` loops!!!

```plaintext
var a = {1, 2, 3};
foreach(x in a){
    print a;
}
// 1
// 2
// 3
```

The `foreach` loop was something I wanted to bring in Kova but didn’t get the chance to, and between me switching back and forth between `foreach(x in foo)` and `each(x : foo)`, I decided to go with well what I already showed, and I’m happy its now in the language!

Next is more helpful error messages! Our error messaging system was all over the place, and now I have gotten it under control, and better under an Error Manager to handle detecting the type of error, and providing suggestions that may help, these suggestions are quite generic but better than nothing ;)

## Error Categories

The error system categorizes errors into the following types:

### Syntax Errors

* **UNEXPECTED\_TOKEN**: Wrong token found where another was expected
    
* **UNTERMINATED\_STRING**: String literal missing closing quote
    
* **INVALID\_CHARACTER**: Invalid or unexpected character in source code
    
* **MISSING\_SEMICOLON**: Missing semicolon where required
    
* **MISMATCHED\_BRACKETS**: Mismatched \[\], {}, or () brackets
    

### Semantic Errors

* **UNDEFINED\_VARIABLE**: Reference to undeclared variable
    
* **REDEFINED\_VARIABLE**: Variable declared multiple times in same scope
    
* **UNDEFINED\_FUNCTION**: Call to undefined function
    
* **UNDEFINED\_PROPERTY**: Access to undefined object property
    
* **WRONG\_ARGUMENT\_COUNT**: Function called with wrong number of arguments
    

### Type Errors

* **TYPE\_MISMATCH**: Operation between incompatible types
    
* **INVALID\_CAST**: Invalid type conversion
    
* **INCOMPATIBLE\_TYPES**: Types cannot be used together
    

### Runtime Errors

* **STACK\_OVERFLOW**: Too many nested function calls
    
* **INDEX\_OUT\_OF\_BOUNDS**: Array/vector access outside valid range
    
* **NULL\_REFERENCE**: Access to null object
    
* **DIVISION\_BY\_ZERO**: Division or modulo by zero
    

### Memory/Limit Errors

* **TOO\_MANY\_CONSTANTS**: More than 256 constants in one function
    
* **TOO\_MANY\_LOCALS**: More than 256 local variables in function
    
* **TOO\_MANY\_ARGUMENTS**: More than 255 function arguments
    
* **LOOP\_TOO\_LARGE**: Loop body exceeds maximum size (65535 bytes)
    
* **JUMP\_TOO\_LARGE**: Conditional jump distance too large
    

### Class/Object Errors

* **INVALID\_SUPER\_USAGE**: 'super' used outside class or without inheritance
    
* **INVALID\_SELF\_USAGE**: 'self' used outside class context
    
* **CLASS\_INHERITANCE\_ERROR**: Invalid class inheritance (e.g., self-inheritance)
    
* **METHOD\_NOT\_FOUND**: Called method doesn't exist on object
    

## Error Message Format

Each error message includes:

1. **Severity Level**: Error, Warning, Info, or Hint
    
2. **Location**: File, line, and column number
    
3. **Category**: The type of error (Syntax, Runtime, etc.)
    
4. **Message**: Clear description of what went wrong
    
5. **Context**: Relevant source code with error highlighting
    
6. **Suggestions**: Helpful advice on how to fix the error
    
7. **Examples**: Code examples showing correct usage (when applicable)
    

### Example Error Output

```plaintext
Error [script:15:23] (Syntax) Unexpected token 'var', expected ')'
    var result = add(5, var x = 10);
                       ^^^
  Suggestion: Replace 'var' with a valid expression
    Fix: Remove variable declaration from function call
    Example: var result = add(5, x);

  Suggestion: Declare variables outside function calls
    Example: var x = 10; var result = add(5, x);
```

## Common Error Scenarios and Suggestions

### Undefined Variables

**Error**: `Undefined variable 'userName'`

**Suggestions**:

* Declare the variable before using it
    
* Check spelling (did you mean 'username'?)
    
* Ensure variable is in correct scope
    

**Fix Examples**:

```javascript
// Before (error)
print(userName);

// After (fixed)
var userName = "John";
print(userName);
```

### Wrong Argument Count

**Error**: `Function 'add' expects 2 arguments, but 1 were provided`

**Suggestions**:

* Add the missing argument
    
* Check function signature
    
* Verify you're calling the correct function
    

**Fix Examples**:

```javascript
// Before (error)
var result = add(5);

// After (fixed)
var result = add(5, 3);
```

### Stack Overflow

**Error**: `Stack overflow - too many function calls`

**Suggestions**:

* Check for infinite recursion
    
* Add base case to recursive functions
    
* Consider using iteration instead
    
* Limit recursion depth
    

**Fix Examples**:

```javascript
// Before (infinite recursion)
fun factorial(n) {
    return n * factorial(n - 1);
}

// After (with base case)
fun factorial(n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}
```

### Index Out of Bounds

**Error**: `Index 5 is out of bounds for size 3`

**Suggestions**:

* Valid indices are 0 to 2
    
* Check array/vector size before accessing
    
* Use bounds checking
    

**Fix Examples**:

```javascript
// Before (unsafe)
var value = arr[index];

// After (safe)
if (index >= 0 && index < arr.length) {
    var value = arr[index];
} else {
    print("Index out of bounds");
}
```

### Invalid Super Usage

**Error**: `Cannot use 'super' outside of a class`

**Suggestions**:

* Use 'super' only inside class methods
    
* Ensure class has a parent class
    
* Move super call into class definition
    

**Fix Examples**:

```javascript
// Before (error)
super.method();

// After (fixed)
class Child extends Parent {
    method() {
        super.method();
    }
}
```

## Did You Mean Suggestions

The error system includes intelligent "did you mean" suggestions for:

* **Variable names**: Suggests similar variable names based on edit distance
    
* **Function names**: Suggests similar function names when available
    
* **Method names**: Suggests available methods on objects
    
* **Keywords**: Suggests correct keywords for typos
    

### Example

```plaintext
Error: Undefined variable 'usrName'
  Suggestion: Did you mean 'userName'?
```

## Error Recovery

The compiler includes panic mode recovery:

1. **Enter Panic Mode**: When an error is detected
    
2. **Skip Tokens**: Continue parsing by skipping problematic tokens
    
3. **Synchronization Points**: Recover at statement boundaries
    
4. **Exit Panic Mode**: Resume normal parsing
    

This allows the compiler to report multiple errors in a single compilation pass
