rustcc 0.1.1

An little C Complier( now it's just WIP :) )
rustcc-0.1.1 is not a library.

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

Prerequisites

  • Rust Development Environment: Ensure you have the Rust development environment installed. If not, visit rust-lang.org to install it.
  • Compiler Toolchain: For Windows users, you'll need to have the Build Tools for Visual Studio installed (for link.exe). For Linux/macOS, ensure you have GCC or Clang installed.
  • x86-64 Architecture: This project generates x86-64 assembly code and requires an x86-64 compatible system.

Multi-system Support

This project supports the following operating systems:

  • Windows: Windows 10 or later
  • Linux: Ubuntu 20.04 or later, Fedora 32 or later
  • macOS: macOS 11 (Big Sur) or later

On Linux and macOS, you may need to install additional dependencies such as build-essential on Ubuntu or Xcode Command Line Tools on macOS.

Build Steps

  1. Clone or download the project code to your local machine

  2. Navigate to the project directory

cd rustcc_dev
  1. Build the project
cargo build --release

Usage

After building, you can run the calculator in the following ways:

# Using a test input file (default: precedence parser)
cargo run test_input.txt

# Using a specific parser
cargo run -- --parser=precedence test_input.txt
cargo run -- --parser=recursive test_input.txt

# Passing an expression directly as an argument
cargo run -- "10 + 20 * 3 - 40 / 5"

# Generate assembly code and executable
cargo run test_input.txt
./out.exe

The calculator now uses the precedence parser by default. You can specify the parser type using the --parser option.

Example

Input expression: 10 + 20 * 3 - 40 / 5

Output result: 62

Project Structure

rustcc_dev/
├── .gitignore
├── Cargo.lock
├── Cargo.toml
├── README.md
├── README_CN.md
├── src/
│   ├── ast.rs       # Abstract Syntax Tree definitions
│   ├── codegen.rs   # Code generator for x86-64 assembly
│   ├── interpreter.rs  # Interpreter implementation
│   ├── main.rs      # Program entry point
│   ├── parser.rs    # Recursive descent parser
│   ├── precedence_parser.rs  # Operator precedence table 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.