-
What do you want to achieve?
I want to be pointed in the right direction regarding how I would make a math interpreter or any simple code interpreter. I am not asking for, and do not want scripts, but rather a word of advice, tips, and a nudge toward the right concepts. Here is the end goal of what I am trying to make: String Calculator - Resources / Community Resources - DevForum | Roblox -
What is the issue?
I have got to the point of the 4 basic arithmetic operators, but I am stuck and confused as to how to continue. -
What solutions have you tried so far?
I have looked on the dev forum and other places and will continue to look, but the tutorials (few as they are) are either complicated algorithms or solutions that I do not understand. Here is my code, it allows you to perform the order of operations with ±*/, but I would like help expanding it.
local functions = {}
functions["+"] = function(num1, num2)
return tonumber(num1) + tonumber(num2)
end
functions["-"] = function(num1, num2)
return tonumber(num1) - tonumber(num2)
end
functions["*"] = function(num1, num2)
return tonumber(num1) * tonumber(num2)
end
functions["/"] = function(num1, num2)
return tonumber(num1) / tonumber(num2)
end
local function eval(exp)
exp = exp:gsub("%s", "")
local Lexer = {}
local LastMath = ""
local SplitTable = exp:split("")
local i = 1
while 0 < #SplitTable do
if tonumber(SplitTable[i]) then
LastMath = LastMath .. SplitTable[i]
table.remove(SplitTable,i)
else
Lexer[#Lexer+1] = tonumber(LastMath)
LastMath = ""
if ("+-/*"):find(SplitTable[i]) then
Lexer[#Lexer+1] = SplitTable[i]
table.remove(SplitTable,i)
end
end
end
Lexer[#Lexer+1] = LastMath
while #Lexer > 1 do
local operation = table.find(Lexer, "*") or table.find(Lexer, "/")
if operation then
local res = functions[Lexer[operation]](Lexer[operation - 1],Lexer[operation + 1])
Lexer[operation-1] = res
table.remove(Lexer, operation)
table.remove(Lexer, operation)
elseif tonumber(Lexer[1]) and functions[tostring(Lexer[2])] and tonumber(Lexer[3]) then
local res = functions[Lexer[2]](Lexer[1],Lexer[3])
table.remove(Lexer,1)
table.remove(Lexer,1)
Lexer[1] = res
end
end
return Lexer[1]
end
Just do print(eval(“your math expression”)) in order to try it. As I said, I am stuck at this point and do not know how to continue. Thanks for any help you give!