RustCC - Rust Expression Calculator
RustCC is a simple expression calculator implemented in Rust, supporting basic mathematical operations (addition, subtraction, multiplication, division). This project demonstrates how to implement lexical analysis, syntax analysis, Abstract Syntax Tree (AST), and an interpreter using Rust.
Features
- Supports basic arithmetic operations: addition(+), subtraction(-), multiplication(*), division(/)
- Follows operator precedence rules
- Scanner converts input text into tokens
- Parser constructs Abstract Syntax Tree (AST)
- Interpreter traverses AST and computes results
- Comprehensive error handling
Installation and Build
-
Ensure you have the Rust development environment installed. If not, visit rust-lang.org to install it.
-
Clone or download the project code to your local machine
-
Navigate to the project directory
- Build the project
Usage
After building, you can run the calculator in the following ways:
# Using a test input file
# Passing an expression directly as an argument
Example
Input expression: 10 + 20 * 3 - 40 / 5
Output result: 62
Project Structure
rustcc_dev/
├── .gitignore
├── Cargo.lock
├── Cargo.toml
├── README.md
├── README_EN.md
├── src/
│ ├── ast.rs # Abstract Syntax Tree definitions
│ ├── interpreter.rs # Interpreter implementation
│ ├── main.rs # Program entry point
│ ├── parser.rs # Syntax parser
│ └── scanner.rs # Lexical scanner
└── test_input.txt # Test input file
Contribution Guidelines
Contributions to the project are welcome! If you have any improvement suggestions or find bugs, please submit an issue or pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.