|
1 | 1 | # MathParser.lua Documentation
|
| 2 | +- [MathParser.lua Documentation](#mathparserlua-documentation) |
| 3 | + - [Introduction](#introduction) |
| 4 | + - [Detailed Breakdown](#detailed-breakdown) |
| 5 | + - [Lexer](#lexer) |
| 6 | + - [Parser](#parser) |
| 7 | + - [Evaluator](#evaluator) |
2 | 8 |
|
3 |
| -## Overview |
4 |
| -`MathParser.lua` is a Lua module that provides functionality for parsing and evaluating mathematical expressions. It uses a Lexer, Parser, and Evaluator to tokenize, parse, and evaluate expressions respectively. |
| 9 | +## Introduction |
5 | 10 |
|
6 |
| -## Dependencies |
7 |
| -- `Evaluator`: Evaluates the Abstract Syntax Tree (AST) of a mathematical expression. |
8 |
| -- `Lexer`: Tokenizes a mathematical expression into a list of tokens. |
9 |
| -- `Parser`: Parses a list of tokens into an AST. |
| 11 | +MathParser.lua is a tool that can understand and solve math problems given as text. It's built around three main parts: "Lexer", "Parser", and "Evaluator". Each part plays a key role in understanding and solving the math problems that are given to it. |
10 | 12 |
|
11 |
| -## Methods |
| 13 | +## Detailed Breakdown |
12 | 14 |
|
13 |
| -### `MathParserMethods:tokenize(expression)` |
14 |
| -Tokenizes the given expression into a list of tokens. Each token represents a mathematical symbol or number. |
| 15 | +### Lexer |
15 | 16 |
|
16 |
| -Example: |
17 |
| -```lua |
18 |
| -local tokens = MathParser:tokenize("2 + 2") |
19 |
| --- tokens: { {TYPE = "Number", Value = "2"}, {TYPE = "Operator", Value = "+"}, {TYPE = "Number", Value = "2"} } |
20 |
| -``` |
| 17 | +The Lexer is the first step. It takes a text input, which is a math problem, and breaks it down into a list of words and symbols. This list is called tokens. The Lexer is good at recognizing different things like numbers, variables, math symbols, and more. This process lets us understand the problem in smaller pieces, getting it ready for the next step. |
21 | 18 |
|
22 |
| -### `MathParserMethods:parse(tokens)` |
23 |
| -Parses the given tokens into an Abstract Syntax Tree (AST). The AST represents the hierarchical structure of the mathematical expression. |
| 19 | +### Parser |
24 | 20 |
|
25 |
| -Example: |
26 |
| -```lua |
27 |
| -local AST = MathParser:parse({ {TYPE = "Number", Value = "2"}, {TYPE = "Operator", Value = "+"}, {TYPE = "Number", Value = "2"} }) |
28 |
| --- AST: { TYPE = "Operator", Value = "+", Left = { TYPE = "Number", Value = "2" }, Right = { TYPE = "Number", Value = "2" } } |
29 |
| -``` |
| 21 | +The Parser takes over from the Lexer. It takes the list of tokens and turns it into a tree structure, called an Abstract Syntax Tree (AST). The AST is a way of showing the math problem where each branch is a math operation, and the leaves are the numbers or variables. This tree structure lets us understand the problem in terms of its math relationships, which is important for solving it correctly. |
30 | 22 |
|
31 |
| -### `MathParserMethods:evaluate(AST)` |
32 |
| -Evaluates the given AST and returns the result of the mathematical expression. |
| 23 | +Example of an AST: |
| 24 | + |
33 | 25 |
|
34 |
| -Example: |
35 |
| -```lua |
36 |
| -local result = MathParser:evaluate({ TYPE = "Operator", Value = "+", Left = { TYPE = "Number", Value = "2" }, Right = { TYPE = "Number", Value = "2" } }) |
37 |
| --- result: 4 |
38 |
| -``` |
| 26 | +### Evaluator |
39 | 27 |
|
40 |
| -### `MathParserMethods:solve(expression)` |
41 |
| -Solves the given expression by tokenizing, parsing, and evaluating it. This is a convenience method that combines the tokenize, parse, and evaluate methods. |
42 |
| - |
43 |
| -Example: |
44 |
| -```lua |
45 |
| -local result = MathParser:solve("2 + 2") |
46 |
| --- result: 4 |
47 |
| -``` |
48 |
| - |
49 |
| -### `MathParserMethods:addVariable(variableName, variableValue)` |
50 |
| -Adds a variable and sets its value. |
51 |
| - |
52 |
| -Example: |
53 |
| -```lua |
54 |
| -MathParser:addVariable("x", 5) |
55 |
| -local result = MathParser:solve("x + 2") |
56 |
| --- result: 7 |
57 |
| -``` |
58 |
| - |
59 |
| -### `MathParserMethods:addVariables(variables)` |
60 |
| -Adds multiple variables and sets their values. |
61 |
| - |
62 |
| -Example: |
63 |
| -```lua |
64 |
| -MathParser:addVariables({x = 5, y = 2}) |
65 |
| -local result = MathParser:solve("x + y") |
66 |
| --- result: 7 |
67 |
| -``` |
68 |
| - |
69 |
| -### `MathParserMethods:addFunction(functionName, functionValue)` |
70 |
| -Adds a function to the parser, allowing it to be used in mathematical expressions. |
71 |
| - |
72 |
| -Example: |
73 |
| -```lua |
74 |
| -MathParser:addFunction("double", function(a) return a * 2 end) |
75 |
| -local result = MathParser:solve("double(5)") |
76 |
| --- result: 10 |
77 |
| -``` |
78 |
| - |
79 |
| -### `MathParserMethods:addFunctions(functions)` |
80 |
| -Adds multiple functions to the parser, allowing them to be used in mathematical expressions. |
81 |
| - |
82 |
| -Example: |
83 |
| -```lua |
84 |
| -MathParser:addFunctions({double = function(a) return a * 2 end, triple = function(a) return a * 3 end}) |
85 |
| -local result = MathParser:solve("double(5) + triple(5)") |
86 |
| --- result: 25 |
87 |
| -``` |
88 |
| - |
89 |
| -### `MathParserMethods:setOperatorPrecedenceLevels(operatorPrecedenceLevels)` |
90 |
| -Sets the operator precedence levels that the parser will use. |
91 |
| - |
92 |
| -Example: |
93 |
| -```lua |
94 |
| -MathParser:setOperatorPrecedenceLevels({ |
95 |
| - Unary = { ["-"] = 2 }, |
96 |
| - Binary = { ["+"] = 1, ["-"] = 1, ["++"] = 1 } |
97 |
| -}) |
98 |
| -``` |
99 |
| - |
100 |
| -### `MathParserMethods:setVariables(variables)` |
101 |
| -Sets the variables that the evaluator will use. |
102 |
| - |
103 |
| -Example: |
104 |
| -```lua |
105 |
| -MathParser:setVariables({x = 5, y = 2}) |
106 |
| -``` |
107 |
| - |
108 |
| -### `MathParserMethods:setOperatorFunctions(operatorFunctions)` |
109 |
| -Sets the operator functions that the evaluator will use. |
110 |
| - |
111 |
| -Example: |
112 |
| -```lua |
113 |
| -MathParser:setOperatorFunctions({ |
114 |
| - Unary = { ["-"] = function(a) return -a end }, |
115 |
| - Binary = { |
116 |
| - ["+"] = function(a, b) return a + b end, |
117 |
| - ["-"] = function(a, b) return a - b end, |
118 |
| - ["++"] = function(a, b) return 2 * (a + b) end |
119 |
| - } |
120 |
| -}) |
121 |
| -``` |
122 |
| - |
123 |
| -### `MathParserMethods:setOperators(operators)` |
124 |
| -Sets the operators that the lexer will use. |
125 |
| - |
126 |
| -Example: |
127 |
| -```lua |
128 |
| -MathParser:setOperators({"+", "-", "++"}) |
129 |
| -``` |
130 |
| - |
131 |
| -### `MathParserMethods:setFunctions(functions)` |
132 |
| -Sets the functions that the evaluator will use. |
133 |
| - |
134 |
| -Example: |
135 |
| -```lua |
136 |
| -MathParser:setFunctions({ |
137 |
| - double = function(a) return a * 2 end, |
138 |
| - triple = function(a) return a * 3 end |
139 |
| -}) |
140 |
| -``` |
141 |
| - |
142 |
| -### `MathParserMethods:resetToInitialState(operatorPrecedenceLevels, variables, operatorFunctions, operators, functions)` |
143 |
| -Resets the `MathParser` to its initial state with the provided operator precedence levels, variables, operator functions, operators, and functions. If any of these parameters are not provided, the corresponding property will be reset to its default value. |
144 |
| - |
145 |
| -## Class |
146 |
| - |
147 |
| -### `MathParser:new(operatorPrecedenceLevels, variables, operatorFunctions, operators, functions)` |
148 |
| -Creates a new MathParser. You can specify operator precedence levels, variables, operator functions, operators, and functions. If you don't specify any of these, the default values will be used. |
149 |
| - |
150 |
| -Example: |
151 |
| -```lua |
152 |
| ---* Dependencies *-- |
153 |
| -local MathParser = require("MathParser") |
154 |
| - |
155 |
| ---* Constants *-- |
156 |
| -local OPERATOR_PRECEDENCE_LEVELS = { |
157 |
| - Unary = { |
158 |
| - -- Unary minus precedence |
159 |
| - ["-"] = 2 |
160 |
| - }, |
161 |
| - Binary = { |
162 |
| - ["+"] = 1, |
163 |
| - ["-"] = 1 |
164 |
| - }, |
165 |
| -} |
166 |
| - |
167 |
| -local VARIABLES = { |
168 |
| - x = 5 |
169 |
| -} |
170 |
| - |
171 |
| -local OPERATOR_FUNCTIONS = { |
172 |
| - Unary = { |
173 |
| - ["-"] = function(a) return -a end, |
174 |
| - }, |
175 |
| - Binary = { |
176 |
| - ["+"] = function(a, b) return a + b end, |
177 |
| - ["-"] = function(a, b) return a - b end |
178 |
| - } |
179 |
| -} |
180 |
| - |
181 |
| -local OPERATORS = { "-", "+" } |
182 |
| - |
183 |
| -local FUNCTIONS = { |
184 |
| - double = function(a) return a * 2 end |
185 |
| -} |
186 |
| - |
187 |
| --- Create an instance of MathParser with custom operator precedence levels and functions |
188 |
| -local myMathParser = MathParser:new(OPERATOR_PRECEDENCE_LEVELS, VARIABLES, OPERATOR_FUNCTIONS, OPERATORS, FUNCTIONS) |
189 |
| -local result = myMathParser:solve("2 - -x + double(2)") |
190 |
| - |
191 |
| -print(result) -- Outputs: 11 |
192 |
| -``` |
| 28 | +The Evaluator is the last step. It takes the AST from the Parser and solves it to give the answer. The Evaluator goes through the AST, doing the math operations as it goes along. The result of this is the final answer to the math problem. |
0 commit comments