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:
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.
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)
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
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.
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.
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.
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.