Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Math exercise

Math exercise

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Gore   (207 posts)  Bio
Date Sun 22 Apr 2007 06:57 PM (UTC)
Message
<aliases>
  <alias
   script="evaluate"
   match="^eval (.*?)$"
   enabled="y"
   group="z"
   regexp="y"
   send_to="12"
   ignore_case="y"
   keep_evaluating="y"
   sequence="100"
  >
  <send>Note ('%1'..'='..%1)</send>
  </alias>
</aliases>

function evaluate (n,o,wc)
  Note (tostring(wc[1])..'='..wc[1])
end

when the alias eval 2+2 is used, that script performs the following output:

Quote:
2+2=4 2+2=2+2


The in alias script works correctly, 2+2=4, how do I make the function reference equivelent work correctly, and instead of displaying wc[1] as a string, it performs the evaluation?

Thanks in advance
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 22 Apr 2007 09:29 PM (UTC)
Message
Inside the "send to script" the entire contents is sent to the script engine to be compiled and run.

Thus something like "Note (2+2)" will work.

However when you call the function you are simply displaying the string "2+2".

You can rework the function a bit to use loadstring (which is effectively what the "send to script" does). Here is an example:


function evaluate (n,o,wc)

  -- compile it
  local f, e = loadstring (wc [1])
  
  -- error? show it
  if not f then
    ColourNote ("brown", "", e)
    return
  end -- compile error
  
  -- no errors, execute it
  f ()
  
end


The function loadstring calls the Lua compiler, and returns a function, which is nil if an error occurs, and an error message, if there is an error.

Thus we can test for nil and show the error.

Afterwards, if no error, we can execute the function to cause it to run.

A simpler approach is to use assert, and let the built-in error handling kick in:


function evaluate (n,o,wc)
  
  assert (loadstring ( wc [1])) ()
  
end


However in either case we have a problem - you are supposed to supply an entire Lua chunk, not just an expression. For example:


eval 2+2 --> unexpected symbol near '2'


This is because "2+2" is not valid, any more than it would be inside a "send to script". This works however:


eval print (2+2) --> 4


I presume you are just playing here? What you end up doing depends on what you are really trying to do. For example, if you are always planning to evaluate a calculation, you could make it into an assignent:


function evaluate (n,o,wc)

  local calculation = "i = " .. wc [1]
  
  -- compile it
  local f, e = loadstring (calculation)
  
  -- error? show it
  if not f then
    ColourNote ("brown", "", e)
    return
  end -- compile error
  
  -- no errors, execute it
  f ()
  
  -- print results
  print (i)
  
end


Now I have put the results (2+2) into "i", which is then printed at the end.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,190 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.