Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Changelog

All notable changes to the Doglang project will be documented in this file.

## [Unreleased] - 2025-10-01

### Added
- **Operator Precedence Parser**: Implemented a custom Pratt parser for expression evaluation
- Replaces Python's `eval()` for improved security and control
- Supports all arithmetic, comparison, and logical operators
- Proper operator precedence (multiplication before addition, etc.)
- Support for parentheses to override precedence
- Support for unary operators (`-` for negation, `!` for logical NOT)
- **ExpressionParser Module** (`doglang/ExpressionParser.py`)
- `ExpressionParser` class implementing precedence climbing algorithm
- `parse_and_evaluate()` convenience function
- Comprehensive error handling with clear error messages
- **Unit Tests** (`tests/test_expression_parser.py`)
- 34 test cases covering all operators and precedence rules
- Tests for arithmetic, comparison, logical operators
- Tests for parentheses and operator precedence
- Tests for unary operators
- Tests for variable lookup
- Tests for error conditions (division by zero, undefined variables, etc.)
- **Integration Tests** (`tests/test_integration.py`)
- 14 integration tests for complete Doglang programs
- Tests for conditionals, loops, and complex expressions
- End-to-end testing of the interpreter with the new parser
- **Documentation**
- `docs/OPERATOR_PRECEDENCE.md`: Comprehensive documentation of the parser
- Updated `Readme.md` with information about the new parser
- Detailed operator precedence table
- Usage examples and best practices

### Changed
- **main.py**: Updated `expression_stmt()` method to use ExpressionParser instead of `eval()`
- **SemanticAnalyser.py**: Updated `check()` method to use ExpressionParser instead of `eval()`
- **README.md**:
- Added section about expression parsing and security improvements
- Updated operator documentation with precedence information
- Added testing section
- Updated project structure

### Improved
- **Security**: Eliminated use of `eval()`, preventing arbitrary code execution
- **Error Handling**: More informative error messages for expression evaluation
- **Performance**: Direct evaluation without string conversion overhead
- **Maintainability**: Clear, well-documented code with comprehensive tests

### Technical Details

#### Operator Precedence Levels
1. Parentheses: `()`
2. Unary operators: `!`, `-` (unary)
3. Multiplicative: `*`, `/`, `%`
4. Additive: `+`, `-`
5. Comparison: `<`, `>`, `<=`, `>=`
6. Equality: `==`, `!=`
7. Logical AND: `&&`
8. Logical OR: `||`

#### Example Expression Evaluations
- `2 + 3 * 4` → `14` (multiplication first)
- `(2 + 3) * 4` → `20` (parentheses override)
- `10 % 2 == 0` → `true` (modulo then equality)
- `5 > 3 && 10 < 20` → `true` (comparisons then AND)

### Testing
- All 34 unit tests passing
- All 14 integration tests passing
- All example programs (prog.doggy, conditions.doggy, prog1.doggy) working correctly

### Breaking Changes
None. This change is fully backward compatible with existing Doglang programs.

### Migration Guide
No migration needed. All existing Doglang programs will continue to work as before.

### Future Enhancements
- Support for floating-point numbers
- String concatenation with `+` operator
- Bitwise operators
- Ternary conditional operator
- Function calls in expressions
- Type checking for expressions
- Constant folding optimization
67 changes: 63 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,25 @@ DogLang is a programming language with the following components:
## Language Features

- Variable assignments
- Arithmetic operations
- Arithmetic operations with proper operator precedence
- Conditional statements (sniff)
- Loop constructs (wagtail)
- Print statements (bark)
- Input (fetch)
- Comparison operators
- Logical operators (&&, ||, !)
- **Custom operator precedence parser** (replaces eval for improved security)

### Expression Parsing

DogLang now features a custom-built **operator precedence parser** that evaluates expressions safely and efficiently. This eliminates the use of Python's `eval()` function, providing:

- **Enhanced Security**: No arbitrary code execution
- **Better Error Handling**: Clear, informative error messages
- **Full Control**: Custom operator precedence and behavior
- **Improved Performance**: Direct evaluation without string conversion

For detailed information about operator precedence and expression parsing, see [OPERATOR_PRECEDENCE.md](docs/OPERATOR_PRECEDENCE.md).

## Syntax Guide

Expand Down Expand Up @@ -155,9 +168,40 @@ DogLang programs use the `.doggy` file extension.
- `else`: Alternative branch for conditionals

### Operators
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Comparison: `==`, `!=`, `>`, `<`, `>=`, `<=`
- Assignment: `=`

#### Arithmetic Operators
- `+`: Addition
- `-`: Subtraction
- `*`: Multiplication
- `/`: Division
- `%`: Modulo

#### Comparison Operators
- `==`: Equal to
- `!=`: Not equal to
- `>`: Greater than
- `<`: Less than
- `>=`: Greater than or equal to
- `<=`: Less than or equal to

#### Logical Operators
- `&&`: Logical AND
- `||`: Logical OR
- `!`: Logical NOT

#### Assignment Operator
- `=`: Assignment

#### Operator Precedence
Operators follow standard precedence rules (see [documentation](docs/OPERATOR_PRECEDENCE.md)):
1. Parentheses `()`
2. Unary operators `!`, `-`
3. Multiplicative `*`, `/`, `%`
4. Additive `+`, `-`
5. Comparison `<`, `>`, `<=`, `>=`
6. Equality `==`, `!=`
7. Logical AND `&&`
8. Logical OR `||`

## Tips for Writing DogLang Programs
1. Each statement should end with a semicolon (optional in some contexts)
Expand All @@ -173,5 +217,20 @@ DogLang programs use the `.doggy` file extension.
- `Tokenizer.py`: Lexical analyzer
- `SyntaxAnalyser.py`: Parser
- `SemanticAnalyser.py`: Semantic analyzer
- `ExpressionParser.py`: Operator precedence parser for expressions
- `main.py`: Interpreter implementation
- `SymbolTable.py`: Symbol table for variable management
- `tests/`: Unit and integration tests

## Testing

Run the test suite to verify everything works correctly:

```bash
# Run all tests
python tests/__init__.py

# Run specific test files
python -m unittest tests.test_expression_parser -v
python -m unittest tests.test_integration -v
```
Loading