How to put equations into the graphing tool im making

So I’ve been off Roblox Studio for a while, and I randomly wanted to make a Roblox graphing calculator. The graphing equations part was very easy, but 20 minutes in, I couldn’t figure out how to translate text into an equation that the code could give an input/X.

The way it currently works is my module would take a function that has the math equation within it and plug in X by its own to get Y.

EX:

local function Square(X)
 return X^2
end

Just gonna randomly throw in a vid of how things currently work. The equation is X^2 btw (Don’t mind the UI lol, Engineers aren’t architects)

1 Like

The right way is to make an operator precedence parser. I have one I can give you but I’d have to look for it.
The cheesy way is you could use loadstring() and attach strings to the front and back to make it a function:
userFunction = loadstring("function f(x) return (" ..thingUserEntered.. ") end")
If you do it that way, be careful that you don’t let the user put anything other than an equation. Otherwise they can run arbitrary code.

DON’T DO THAT
Not only its unsafe but even WORSE its… UNOPTIMIZED!

There is already free open source math interpeters on Luau/Lua so go use them or make your own!

1 Like

I use loadstring all the time I just make sure it doesn’t contain any evils.

1 Like

loadstring causes code to run in --!optimize 0 forcefully canceling all optimization techniques and causing massive breah in safety.
Making math interpeter is faster,safer and more customizable.

1 Like

That first parts probably made up but yes its easy to make arbitrary execution vulnerabilities with it.

That not “made up” https://luau.org/
read all that :smiling_imp:

The language documentation is not documentation for Roblox’s Luau implementation. Link a Roblox source if you want to substantiate the thing about optimization levels.

WARNING: This method disables certain Luau optimizations on the returned function. Extreme caution should be taken when using loadstring(); if your intention is to allow users to run code in your experience, make sure to protect the returned function’s environment by using getfenv() and setfenv().

Basically, don’t use loadstring, it’s a major security risk and also not optimized! Also also, you can’t call it on the client.

use vLua: Loadstring reimplemented in Lua if not using loadstring, I think it the same thing but safer, optimized.

Using any sort of loadstring implementation that allows for execution of actual code and datamodel manipulation is a security risk, and overkill for a graphing tool.

1 Like

if not, SecureLuaVirtualMachine - Controlled Execution Environment

I think this should be what OP is looking for: GitHub - bytexenon/MathParser.lua: An elegant Math Evaluator written in Lua, featuring support for adding custom operators and functions

1 Like

I’ve just read this, and correct me if I’m wrong, but do I have to create a new parser object for every new X value? I mean, I could just like set the variable X, put the user’s equation into the solver, then get an output, but I don’t really want to create new objects for every new X

If there are no better solutions then I’ll just have to use this

I’m not sure, I’ve never used the resource, just found it via a Google search.

All you need for this is a simple math parser implemented with something like a stack and an automata describing its behavior. Basically you need to define your operation symbols(for example +, -, *, /, %, ^, sin, cos, etc), their parsing priority(copy this from math, for example a multiplication needs to run before an addition, when inside the same (), even if it is defined after the addition) and also what is considered a number(for example decimals, hex, pi keyword, etc). Also you can it take a step further and allow the user to define their own variables and constants.

Many people will tell you to use loadstring for this, don’t, it’s an overkill. If you want a quick solution there’re open libraries for this, but they will likely not exactly match your own math logic and symbols, and even if they do you wont be able to make modifications and comprehend the code without exploring the subject.

It’s better if you look up tutorials and try to implement a mathematical parser yourself. They are the easiest form of parser and often the first thing you learn in a parser university class. It will give you useful future knowledge if you attempt to make it yourself.

Modified Version for Roblox: MathParser.rbxm (14.9 KB)

Nope, its very simple.

local MathParser = require(path.to.module)
local myMathParser = MathParser:new() -- create new parser

local result = myMathParser:solve("1 + 2 ^ 3") -- solving the easiest math
print(result) -- 9
result = myMathParser:solve("sqrt(144) + sin(9) + cos(5) + 1235 / 2") -- solving the complex math
print(result)-- ~ 630.195780671

myMathParser:addVariable("x", 10) -- set variable X to 10
myMathParser:addVariable("y", 5) -- set variable Y to 5
result = myMathParser:solve("x + y")
print(result)  -- 15
myMathParser:addVariable("y", 10) -- set the Y to a new value.
result = myMathParser:solve("x + y")
print(result)  -- 20
myMathParser:addVariable("pi", math.pi)) -- set the pi to a new value.
result = myMathParser:solve("pi")
print(result)  -- ~ 3.14159265359

That how you use them.