[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Math exercise
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Math exercise

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


Posted by Gore   (207 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] 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.


3,821 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]