or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-10/

Formula Evaluator

A utility that processes and evaluates mathematical formulas from user input with variable substitution support.

Capabilities

Parse mathematical expressions

  • Parsing the expression "2 + 3 * 4" returns an object with a type property @test
  • Parsing "x^2 + 2*x + 1" with variables returns an object representing the expression tree @test

Evaluate expressions with variables

  • Evaluating "x + y" with scope {x: 5, y: 3} returns 8 @test
  • Evaluating "sqrt(a^2 + b^2)" with scope {a: 3, b: 4} returns 5 @test

Compile expressions for reuse

  • Compiling "x * 2" and evaluating it with scope {x: 5} returns 10 @test
  • Compiling "a + b + c" and evaluating it twice with different scopes returns correct results for both @test

Create persistent parser state

  • Creating a parser, setting variable "x = 5", then evaluating "x + 3" returns 8 @test
  • Creating a parser, evaluating "y = 10", then evaluating "y * 2" returns 20 @test

Implementation

@generates

API

/**
 * Parses a mathematical expression string into an abstract syntax tree.
 *
 * @param {string} expression - The mathematical expression to parse
 * @returns {object} An object representing the parsed expression tree
 */
function parseExpression(expression) {
  // IMPLEMENTATION HERE
}

/**
 * Evaluates a mathematical expression with the given variable values.
 *
 * @param {string} expression - The mathematical expression to evaluate
 * @param {object} variables - An object containing variable values
 * @returns {number} The result of the evaluation
 */
function evaluateExpression(expression, variables) {
  // IMPLEMENTATION HERE
}

/**
 * Compiles a mathematical expression for efficient repeated evaluation.
 *
 * @param {string} expression - The mathematical expression to compile
 * @returns {object} A compiled expression object with an evaluate method
 */
function compileExpression(expression) {
  // IMPLEMENTATION HERE
}

/**
 * Creates a persistent parser that maintains state between evaluations.
 *
 * @returns {object} A parser object with an evaluate method
 */
function createPersistentParser() {
  // IMPLEMENTATION HERE
}

module.exports = {
  parseExpression,
  evaluateExpression,
  compileExpression,
  createPersistentParser
};

Dependencies { .dependencies }

mathjs { .dependency }

Provides mathematical expression parsing and evaluation capabilities.