Skip to content

An elegant Math Parser written in Lua, featuring support for adding custom operators and functions

License

Notifications You must be signed in to change notification settings

ByteXenon/MathParser.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MathParser.lua logo

MathParser.lua - A Customizable and Fast Lua Math Parser

A powerful and customizable Lua math parser that can solve both simple arithmetic problems and complex mathematical expressions. It supports adding variables, functions, custom operators, and operator precedence levels. It's designed to be safe, fast, and easy to use.

Lua GitHub stars License GitHub last commit GitHub issues Tests Passing 100% Test Coverage

Introduction

MathParser.lua can solve many different types of math problems. It's a tool that can be used for solving simple arithmetic problems, as well as more complex mathematical expressions. It supports adding variables, functions, custom operators, and operator precedence levels (wiki: Order of Operations). It's also designed to be safe, so you can use it without worrying about security issues.

For a deeper understanding of its usage and functionality, refer to the documentation. It provides detailed explanations on how to use MathParser.lua and how it works.

Table of Contents

Quick Links: API | License | Documentation | Example

Features

  1. Easy to Use: MathParser.lua is simple to use. It has a clear API and helpful documentation. If there's a problem, it gives you clear error messages to help you fix it quickly.
  2. Safe: MathParser.lua is designed to be safe. Unlike other math parsers written in Lua, it doesn't use loadstring or load to evaluate expressions. Instead, it uses a custom parser and evaluator that were built from scratch. This makes it safe to use in any environment.
  3. Small and Fast: MathParser.lua is a small project that doesn't need any other dependency to work.
  4. Customizable: You can change MathParser.lua to do exactly what you want. You can add your own functions, variables, and operators. You can even change the order of operations. This makes it a useful tool for many different projects.

Usage

MathParser.lua is a tool that can handle everything from simple arithmetic to complex mathematical expressions. Whether you're integrating it into a large project or using it for quick calculations, here's a comprehensive guide to get you started.

Getting Started

Using MathParser.lua is very easy. Here's a simple example to demonstrate how you can use it to solve basic math problems:

local MathParser = require("MathParser")

-- Create a new parser instance
local myParser = MathParser:new()

-- Add a new variable with the name "x" and the value 5
myParser:addVariable("x", 5)

-- Solve a simple mathematical expression
print(myParser:solve("x + 5")) -- Outputs: 10

-- Solve a more complex expression
print(myParser:solve("(x + 5) * 2")) -- Outputs: 20

It was just a simple example. For more advanced usage, let's move on to the next section.

Advanced Usage

MathParser.lua is not just a simple math parser, it's a powerful tool that can be customized to suit your needs. It supports the addition of custom functions, variables, and operators, allowing you to extend its functionality beyond basic math operations. Let's use all these features to demonstrate how you can use MathParser.lua in more advanced scenarios.

Here's an example code snippet that demonstrates the advanced usage of MathParser.lua:

local MathParser = require("MathParser")
local myParser = MathParser:new()

-- Add a variable and a function
myParser:addVariable("x", 5)
myParser:addFunction("double", function(a) return a * 2 end)

-- Solve expressions using the custom function and variable
print(myParser:solve("-double(x)")) -- Outputs: -10

-- Customize operator precedence levels and functions
local CUSTOM_OPERATOR_PRECEDENCE_LEVELS = {
  Unary = { ["-"] = 3 },
  Binary = { ["^"] = 2, ["+"] = 1, ["-"] = 1 },
  RightAssociativeBinaryOperators = { ["^"] = true }
}

local CUSTOM_OPERATOR_FUNCTIONS = {
  Unary = { ["-"] = function(a) return -a end },
  Binary = {
    ["^"] = function(a, b) return (a + b) % 2 end,
    ["+"] = function(a, b) return a - b end,
    ["-"] = function(a, b) return a + b end
  }
}

myParser:setOperatorPrecedenceLevels(CUSTOM_OPERATOR_PRECEDENCE_LEVELS)
myParser:setOperatorFunctions(CUSTOM_OPERATOR_FUNCTIONS)

-- Solve expressions using the custom operators
print(myParser:solve("5 - 3")) -- Outputs: 8
print(myParser:solve("5 + 3")) -- Outputs: 2
print(myParser:solve("5 ^ 3")) -- Outputs: 0

For more details, refer to the documentation, the API source, or the full example usage file.

Roadmap

MathParser.lua is just getting started. Here are some of the enhancements and features we're planning for future releases:

  • Introduce support for internal functions that can modify the evaluator's state at runtime. This includes setting variables and jumping to different parts of the expression.
  • Package MathParser.lua into a single file for easy distribution and usage. We also plan to make it available on LuaRocks and the project's GitHub releases.
  • Implement full support for Luau to cater to the Roblox developer community.
  • Expand the mathematical function library to include advanced functions not available in the standard Lua math library, such as factorial, permutations, and combinations.
  • Incorporate support for complex numbers to handle more sophisticated mathematical problems.
  • Optimize MathParser.lua further to enhance speed and reduce memory usage.
  • Develop optional support for parsing and solving equations and inequalities.
  • Add optional support for solving differential and integral equations.

We welcome feature requests and suggestions for improvements. Feel free to open an issue or a pull request. Your feedback is highly appreciated!

Contact

Do you have any questions, suggestions, or feedback? Feel free to open an issue on this GitHub repository. Alternatively, you can email the project maintainer, ByteXenon at ddavi142(at)asu(dot)edu

License

MathParser.lua is (re)licensed under the MIT License.