Compiler Error Messages - Improvements Summary
Overview
The FluxSharp compiler now provides clear, helpful error messages to guide developers in fixing their code. All error messages are in English and include specific hints about what went wrong and how to fix it.
Error Detection Categories
1. Syntax Errors
Missing Semicolon Detection
- Error Type:
โ POSSIBLE SYNTAX ERROR - Detection: Lines containing assignments, function calls, or array operations that don't end with a semicolon
- Example:
int x = 10 // Missing semicolonError Message:
โ POSSIBLE SYNTAX ERROR at line 3:
int x = 10
Hint: Statement appears to be missing a semicolon (;) at the end
Expected format: int x = 10;Float Literal Format Error
- Error Type:
โ ๏ธ FLOAT LITERAL ERROR - Detection: Float literals without the required 'f' or 'F' suffix
- Example:
float f = 3.14; // Should be 3.14fError Message:
โ ๏ธ FLOAT LITERAL ERROR at line 3:
float f = 3.14;
Hint: Float literals must end with 'f' or 'F'
Correct format: 3.14f or 3.14FUnmatched Parenthesis
- Error Type:
โ UNMATCHED PARENTHESIS - Detection: Mismatched opening and closing parentheses
- Example:
int x = max(10, 20; // Missing closing parenthesisError Message:
โ UNMATCHED PARENTHESIS at line 3:
int x = max(10, 20;
Hint: Found 1 opening '(' but only 0 closing ')'Unmatched Bracket
- Error Type:
โ UNMATCHED BRACKET - Detection: Mismatched array brackets
- Example:
int arr[10; // Missing closing bracketError Message:
โ UNMATCHED BRACKET at line 3:
int arr[10;
Hint: Found 1 opening '[' but only 0 closing ']'2. Type Errors
Undefined Variable
- Error Type:
โ UNDEFINED VARIABLE - Details: Variable used before declaration
- Message Format:
โ Undefined variable: 'variableName'
Make sure this variable is declared before use with: type variableName = value;Type Mismatch in Operations
- Error Type:
โ ๏ธ TYPE ERROR - Details: Invalid arithmetic operations on incompatible types
- Examples:
- Cannot perform arithmetic on strings
- Cannot mix string and numeric types without concatenation
- Invalid operator for specific type
Invalid Float/Double Operations
- Message: Detailed hints about required numeric types and proper syntax
3. Function Errors
Undefined Function
- Error Type:
โ UNDEFINED FUNCTION - Message Format:
โ Undefined function: 'functionName'
Available math functions: abs, max, min, pow, floor, ceil, round, sqrtFunction Argument Count Mismatch
- Error Type:
โ FUNCTION ERROR - Examples:
max()requires exactly 2 argumentssqrt()requires exactly 1 argument- Function called with wrong number of arguments
Error Message Format:
โ Function Error: max() requires exactly 2 arguments, but got 1
Usage: max(value1, value2)Invalid Function Arguments
- Error Type:
โ ๏ธ WARNING - Details: Complex expressions not fully supported in certain contexts
4. Math Function Specific Errors
The compiler provides specific error messages for each built-in math function:
- abs(): Requires numeric argument (int, float, or double)
- max()/min(): Both arguments must be the same numeric type
- pow(): Both arguments must be numeric types
- floor()/ceil()/round(): Requires numeric argument
- sqrt(): Requires numeric argument
Example Error:
โ ๏ธ Type Error: max() requires both arguments to be the same numeric type
Use: max(intA, intB), max(floatA, floatB), or max(doubleA, doubleB)Color-Coded Error Indicators
All error messages use visual indicators:
โ- Syntax errors that prevent compilationโ ๏ธ- Type errors and warnings that may prevent runtime execution๐ด- Critical compilation failures
Error Prevention Tips
- Always include semicolons at the end of statements
- Float literals must end with 'f' or 'F' (e.g.,
3.14f) - Match parentheses and brackets - each opening character needs a closing one
- Declare variables before using them
- Use the correct type for operations - don't mix incompatible types
- Check function arguments - ensure correct number and type of arguments
Testing the Error Messages
Several test files are included to demonstrate error detection:
test_missing_semi.fsh- Detects missing semicolonstest_float_error.fsh- Detects float literal format errorstest_unmatched_paren.fsh- Detects unmatched parenthesestest_undefined_var.fsh- Tests handling of undefined variables
Implementation Details
The error detection system is implemented in:
- error_handler.rs: Defines error types and formatting
- main.rs: Contains the
detect_common_errors()function for early error detection
All parsing errors are caught and enhanced with line numbers and helpful suggestions.