Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Document most important public API
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Čihař <[email protected]>
  • Loading branch information
nijel committed Oct 12, 2016
1 parent 7ae59f3 commit 3505173
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,35 @@
*/
namespace SimpleMath;


/**
* Core class of the library performing parsing and calculations.
*/
class Math {

protected $variables = array();
protected $stack = null;

/**
* Parses and executes given formula
*
* @param string $string Formula to process
*
* @return int
*/
public function evaluate($string) {
$this->parse($string);
return $this->run();
}

/**
* Parses given formula. The parsed formula is stored in
* the class.
*
* @param string $string Formula to process
*
* @return void
*/
public function parse($string) {
$tokens = $this->tokenize($string);
$output = new Stack();
Expand All @@ -53,10 +72,23 @@ public function parse($string) {
$this->stack = $output;
}

/**
* Registers variable for use withing calculation
*
* @param string $name Name of variable
* @param int $value Value of variable
*
* @return void
*/
public function registerVariable($name, $value) {
$this->variables[$name] = $value;
}

/**
* Executes currently parsed formula with current variables
*
* @return int
*/
public function run() {
$stack = clone $this->stack;
while (($operator = $stack->pop()) && $operator->isOperator()) {
Expand Down

0 comments on commit 3505173

Please sign in to comment.