Hone logo
Hone
Problems

Simple Expression Generator in Rust

This challenge focuses on implementing a basic code generator in Rust. The goal is to take a simple mathematical expression represented as a string and generate Rust code that evaluates that expression. This is a foundational exercise in understanding code generation and abstract syntax trees, useful for building compilers, interpreters, and domain-specific languages.

Problem Description

You are tasked with creating a Rust program that takes a string representing a simple mathematical expression and generates equivalent Rust code to evaluate that expression. The expressions will consist of integers, addition (+), subtraction (-), multiplication (*), and division (/). The generated Rust code should be a single println! statement that evaluates the expression and prints the result to the console.

Key Requirements:

  • Input: A string representing a valid mathematical expression.
  • Output: A string containing a valid Rust println! statement that evaluates the input expression and prints the result.
  • Error Handling: If the input expression is invalid (e.g., contains invalid characters, unbalanced parentheses, or division by zero), the program should return an error message string.
  • Operator Precedence: Multiplication and division should be performed before addition and subtraction.
  • Integer Only: The expressions will only contain integers.

Expected Behavior:

The program should parse the input expression, construct an internal representation (you can choose how to do this - a simple string manipulation approach is acceptable for this challenge), and then generate the corresponding Rust code. The generated code should be executable and produce the correct result when compiled and run.

Edge Cases to Consider:

  • Empty input string.
  • Expressions with only a single number.
  • Expressions with multiple operators.
  • Division by zero.
  • Invalid characters in the expression.
  • Whitespace in the expression (should be handled gracefully).

Examples

Example 1:

Input: "2 + 3 * 4"
Output: "println!(\"{}\", 2 + 3 * 4);"
Explanation: The expression is evaluated according to operator precedence (multiplication before addition).

Example 2:

Input: "10 - 5 / 2"
Output: "println!(\"{}\", 10 - 5 / 2);"
Explanation: Division is performed before subtraction.

Example 3:

Input: "7"
Output: "println!(\"{}\", 7);"
Explanation: A single number is a valid expression.

Example 4:

Input: "1 + 2 * (3 - 1)"
Output: "println!(\"{}\", 1 + 2 * (3 - 1));"
Explanation: Parentheses are respected.

Example 5:

Input: "10 / 0"
Output: "Error: Division by zero."
Explanation: Division by zero is an invalid operation.

Example 6:

Input: "1 + a"
Output: "Error: Invalid character 'a'."
Explanation: Invalid characters are not allowed.

Constraints

  • The input string will have a maximum length of 256 characters.
  • The input string will only contain digits (0-9), the operators '+', '-', '*', '/', and whitespace characters. Parentheses '(' and ')' are allowed.
  • The generated Rust code must be a valid println! statement.
  • The program should handle division by zero gracefully and return an error message.
  • The program should return an error message if the input contains invalid characters.
  • Performance is not a primary concern for this challenge; readability and correctness are more important.

Notes

  • You can use string manipulation techniques to parse and generate the Rust code. A more sophisticated approach using an Abstract Syntax Tree (AST) would be beneficial for more complex code generation scenarios, but is not required for this challenge.
  • Consider using a simple error handling mechanism (e.g., returning an Result<String, String>) to indicate success or failure.
  • Focus on generating correct Rust code that evaluates the expression. The generated code does not need to be optimized or particularly elegant.
  • Parentheses should be handled correctly to enforce operator precedence.
  • Whitespace in the input expression should be ignored.
  • The generated code should be a single line.
  • The output should be a string, not printed directly to the console.
Loading editor...
rust