-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.lua
54 lines (44 loc) · 1.52 KB
/
example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Dependencies
local MathParser = require("MathParser")
-- Create an instance of MathParser
local myMathParser = MathParser:new()
-- Solving a simple expression
local result = myMathParser:solve("2 + 3")
print(result) -- Outputs: 5
-- Adding variables
myMathParser:addVariable("x", 10)
myMathParser:addVariable("y", 5)
result = myMathParser:solve("x + y")
print(result) -- Outputs: 15
-- Adding functions
myMathParser:addFunction("add", function(a, b)
return a + b
end)
result = myMathParser:solve("add(x, y)")
print(result) -- Outputs: 15
-- Custom operators and precedence levels
local CUSTOM_OPERATOR_PRECEDENCE_LEVELS = {
Unary = { ["-"] = 3 },
Binary = { ["+"] = 1, ["-"] = 1, ["++"] = 1, ["///"] = 2, ["/"] = 2 }
}
local CUSTOM_OPERATORS = { "+", "-", "++", "///", "/" }
local CUSTOM_OPERATOR_FUNCTIONS = {
Unary = { ["-"] = function(a) return -a end },
Binary = {
["+"] = function(a, b) return a + b end,
["-"] = function(a, b) return a - b end,
["/"] = function(a, b) return a / b end,
-- Custom operators
["++"] = function(a, b) return 2 * (a + b) end,
["///"] = function(a, b) return 2 * (a / b) end
}
}
myMathParser:setOperatorPrecedenceLevels(CUSTOM_OPERATOR_PRECEDENCE_LEVELS)
myMathParser:setOperators(CUSTOM_OPERATORS)
myMathParser:setOperatorFunctions(CUSTOM_OPERATOR_FUNCTIONS)
result = myMathParser:solve("10 ++ 10")
print(result) -- Outputs: 40
result = myMathParser:solve("10 /// 10")
print(result) -- Outputs: 2
-- Reset the parser to default settings
myMathParser:resetToInitialState()