microsoft / microsoft/calculator

Develop a parser for arbitrary expressions in the calc engine

Open
#526 13 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement help wanted needs spec Pri: 1
Dominant language
C#
Stars
31.1k
Forks
5.8k
PR merge metrics
No merged PRs in 30d

Description

Problem Statement
Currently, Calculator executes all operations in immediate mode--press an operator, get a new answer. For multiple scenarios, it would be useful for the user to enter an expression like "(5 + 2) * 3" and have Calculator parse and compute the result.

Evidence or User Insights
Several proposed features could leverage this work:

  • Expression paste #344 (includes some prototype code which could be useful for this effort)
  • History editing #163
  • Input via ink or voice #24, #288
  • Editing expressions in scientific calculator #520

Proposal
Add code and tests to the calc engine to parse mathematical expressions as strings.

Goals

  • A developer can use the CalcManager library to convert an expression into a numerical result
  • Expressions support order of operations, e.g. "(5 + 2) * 3" = 21
  • Expressions support evaluation of functions, e.g. "sqrt(4)"
    • Functions are implemented for as many existing calculator operators as possible
    • Each function has a thoughtful name informed by similar systems
  • Expressions are evaluated using the arbitrary-precision engine
  • When invalid expressions are passed to the engine, error information is returned which could be used to construct a helpful error message
  • Parsing and evaluation is deterministic, and ideally not locale-dependent
  • The parser can find expressions embedded in natural language sentences with loose syntax (e.g. "what is the square root of sin(3 - 2)" evaluates to "what is the square root of [expression tree]"), for future use in voice input scenarios

Non-Goals

  • Integrate expression parsing with the Calculator UI (tracked by other issues)
  • Natural language intent recognition, e.g. "what is the square root of 4" evaluating to "sqrt(4)"

Requested Assignment
I'm happy to help with this, but would be great for someone else to implement.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the CalcManager library and its existing calculator operators, then review the prototype referenced in issue #344. Define the parser and error-reporting scope before implementation. Done means expressions such as "(5 + 2) * 3" and "sqrt(4)" evaluate deterministically with arbitrary precision and invalid input returns usable error information.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.