Skip to content

Parsing

Adam Dernis edited this page May 21, 2020 · 10 revisions

Calc parses, converting strings to Expression Trees.

Parsing is done character by character with a state machine and a progressively built tree. The parser contains a progress cache_, state_, tree_, and parenthesis_depth. The cache_ is a string, used for incomplete function names and multi-digit numbers between character parsing. The state_ gives the context the parser is currently at. For example, a '-' after a number means subtract what comes next. Meanwhile, a '-' after a '*' means the next value should be inversed (multiplied by -1).

Sample: 2+20

When parsing more complex expressions with many operations the parser will work with the expression tree object, using the active_node. The active_node is the most recent operator node to be touched, or was selected when combining nary operator nodes. When a new node is added to the tree, it can go above or below the active_node. The position is determined by the priority. Values have the highest priority and will always be added as a child of the active_node. Operators will walk the active_node up until either they become root, there's a node with override priority, or they have an equal or lower priority than a the new active_node. After being added, the operator node will be become the new active_node.

Sample: x+(3x+5*x)

Each function needs to be parsed differently. As a result, each function has its own parser. These parsers implement the function parser interface. The beginning of a function is indicated by a '\' and are ended with a non-alphabetic character. While parsing a function, the root parser is in the function state.

Sample: 5*\sin{5x+\sin{10}}

Clone this wiki locally