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+2

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.

Clone this wiki locally