MathEvaluator | Evaluate mathematical expressions at runtime

MathEvaluator

This is a heavily updated module to my old one that can be found here. It allows you to input a string such as 2 + 2 and have it evaluate following presedence and associativity.

Examples

local result = MathEvaluator('20.5 + 0.5')
print(result) --> 21
local result = MathEvaluator('2^2^3')
print(result) --> 256
local result = MathEvaluator('sqrt[sqrt[81]]')
print(result) --> 3

Built-In Functions

N can be any expression not just a number.

  • sqrt[N]: Returns the square root of N
  • sin/cos/tan[N]: Returns the sine, cosine, or tangent of N
  • asin/acos/atan[N]: Returns the arcsine, arccosine, or arctangent of N
  • abs[N]: Returns the absolute value of N

Changes

I made so many changes to the previous module I figured I would just repost it as it’s basically brand new.

  • Added error messages with tracebacks:
    image
  • Made it so functions can accept full expressions rather than just numbers.
  • Created a new parser using the shunting yard algorithm but left the ability to use the old one.
  • Added TestEZ tests for major components of the module.
  • Fixed exponentation on the recursive descent parser having left associativity.
  • Optimized many things (thanks @Halalaluyafail3)

Recursive Descent vs Shunting Yard

While both of these parsers were a lot of fun to make shunting yard is definitely better suited for this project. I ran some performance tests and it appeared to be slightly (like by a hundred thousandth of a second) faster than the old recursive descent parser. That said I left the RD parser in and will continue to update both alongside each other. Using the old one is fairly simple:

local MathEvaluator = require(script.MathEvaluator)
-- Optional second argument is the parser which should be
-- either the SYParser or RDParser
local result = MathEvaluator('2 + 2', MathEvaluator.RDParser)

Download

https://www.roblox.com/library/7323648466/

13 Likes

This might prove to be useful when dealing with large scale calculations, much appreciated for making it open source.

1 Like

You basically just wrote a whole language interpreter, I’m impressed.

Why did you do this though, and why did you have to make it so complex?

1 Like

Mainly for the challenge to be honest. It took a lot of research but understanding how everything works is really cool. And in terms of this being complex that’s really just how it is :man_shrugging:

I probably could’ve simplified a good portion of things for example by removing the symbol and token modules but it helps me stay organized. There’s really nothing I can do about the parsers though. They’re about as standard as they get even if they are hard to understand. I might make a tutorial explaining how to make your own very basic lexer, parser, and interpreter.

4 Likes

I’d love that. I wish to make my own language one day, and a basic idea would really help.

1 Like

:new: Update

  • Added abs[N] to get the absolute value of N
    • MathEvaluator('abs[-3 * 2]')6
  • Added sin[N], cos[N], tan[N]
  • Added asin[N], acos[N], atan[N]
  • Fixed unary minus operator not properly being translated in the SY parser. Should now work with parentheses and brackets as expected
    • MathEvaluator('(-4 * -2)')8

Download

I actually started writing the tutorial yesterday and it’s quite long but I think it’ll be a good resource. I’ll probably have it up by tomorrow.

1 Like

You were following a tutorial by CodePulse on making custom implementation of the Basic language right? I see a good number of similarities in your source code.

1 Like

Yep his series on it is really nice but unfortunately he only scrapes the surface of the topic. I would say in total I used around 15-20 different resources to make this and learn how everything functions something I think he unfortunately poorly explained. But yes the RD parser uses very similar concepts as his videos.

1 Like

Where is the tutorial? You said you will make the tutorial 27 days ago. But we cannot see the tutorial?

Are you fooling us?

I wrote part of it but realized it would end up being the longest tutorial on DevForum. I think watching a video on the topic or reading an article (and there are a lot of good ones) that already exists is a better choice.

Try making the tutorial shorter. There are no tutorials about the lexer in the dev forum. Tutorials are important for your learning. It will be helpful to many developers. I do not recommend you to write a long tutorial. Tutorials are always good. The tutorial is about transferring knowledge. I am requesting you to do the tutorial. But I hate long tutorials, they take a lot of time to go through. Why can’t you make the tutorial shorter? There are no resources in the dev forum to learn about the lexer. Longer tutorials help me learn a lot. Your tutorial is going to help a lot of people because there are no tutorials about the lexical analyzer in the Dev forum. Please do not disappoint me with your reply. I am expecting a good reply from you. It will be a good resource if you release the tutorial.

why do i have a feeling someone will take this and remake matlab from this at some point in the future lmao