Nintenduh Report post Posted January 31, 2005 I think a calc command would kick @$$ Usually I run in window mode but when I'm in fullscreen this would be rather usefull. Examples #calc 100+25 The gods returned the number 125 #calc 100*25 The gods returned the number 2500 #calc 100/25 The gods returned the number 4 Share this post Link to post Share on other sites
EclonisDureva87 Report post Posted January 31, 2005 linux has a calc funtion u can use... just pm him with #calc [math equation] and that's it Share this post Link to post Share on other sites
Vegar Report post Posted January 31, 2005 (edited) I've already made a patch for this. I'll post it here later, when I get home Edited January 31, 2005 by Vegar Share this post Link to post Share on other sites
Placid Report post Posted January 31, 2005 linux has a calc funtion u can use... just pm him with #calc [math equation]and that's it The syntax is: /Linux calc equation. e.g. /Linux calc 100/75 I'd wait for Vegar's patch link, its worth it Share this post Link to post Share on other sites
Leeloo Report post Posted January 31, 2005 Does it still do this? [PM to linux: calc 2+2+2][PM from linux: 4] Try Minai's calc funtion if it's more complex, if she's online (not that often unfortunately). Share this post Link to post Share on other sites
Grum Report post Posted January 31, 2005 Does it still do this? [PM to linux: calc 2+2+2][PM from linux: 4] ROFLMAO! Share this post Link to post Share on other sites
Platyna Report post Posted January 31, 2005 (edited) Linux calculates "simple expressions" it means she can do only one operation on max two arguments at once, therefore all arguments after first two value and first operations will be ignored. Seems quite sufficient for EL needs and no one was complaining, she gets daily like 400+ calc requests... Regards. Edited January 31, 2005 by Platyna Share this post Link to post Share on other sites
Grum Report post Posted January 31, 2005 O, come on plat. res = arg pop () while (plus_follows) pop () res += arg pop () return res Share this post Link to post Share on other sites
Platyna Report post Posted January 31, 2005 What? ;-) I am a noob. I am having problems with parse errors sometimes. :-D Anyway thank you for code, I will see what I can do with that. Regards. Share this post Link to post Share on other sites
Grum Report post Posted January 31, 2005 What? ;-) I am a noob. I am having problems with parse errors sometimes. :-D Doesn't everybody? Anyway thank you for code, I will see what I can do with that. It is of course a gross oversimplification (for instance you need compute multiplications and divisions first before you do the addition). But I think that with a simple loop in the current code, you can solve this problem. Share this post Link to post Share on other sites
Platyna Report post Posted January 31, 2005 Well, I will take care of it when I will finish new items db for her. Regards. Share this post Link to post Share on other sites
Leeloo Report post Posted January 31, 2005 It is of course a gross oversimplification (for instance you need compute multiplications and divisions first before you do the addition). But I think that with a simple loop in the current code, you can solve this problem. Either you make it sound way simpler than it is, or I completely missed somthing when writing Minai's calc funtion, because it is pretty complicated. Ok, the worst is the "in" operator, because it takes three arguments (two before the word "in" and one after) Share this post Link to post Share on other sites
Vegar Report post Posted January 31, 2005 Here it is http://sunscream.net/dev/calc.diff There might be some bugs, let me know if you find any. Usage: #calc expression #calc 2+2+2 will return '2+2+2 = 6' #calc v100 will return 'v100 = 10' (square root) If you want this on Windows you have to add calc.o to OBJS in Makefile.win Share this post Link to post Share on other sites
Placid Report post Posted January 31, 2005 (edited) Could you not use python/perl to access bash and execute expr <equation from pm>? Ok, I dont know python, but I know bash. (Obviously providing some formatting before execution, i.e. escaping asterisks etc) Edited January 31, 2005 by Placid Share this post Link to post Share on other sites
Vegar Report post Posted January 31, 2005 Could you not use python/perl to access bash and execute expr <equation from pm>? Ok, I dont know python, but I know bash. (Obviously providing some formatting before execution, i.e. escaping asterisks etc) That could be risky. Let's say we get an expression like 2+2;rm -rf / If you don't think about evil people when coding it you may end up with some missing files. Share this post Link to post Share on other sites
Grum Report post Posted January 31, 2005 (edited) Either you make it sound way simpler than it is, or I completely missed somthing when writing Minai's calc funtion, because it is pretty complicated. Is that a challenge? I don't know what 'in' is supposed to do, but, for simple expressions (+,-,*./,^, and parentheses) the following will suffice: OP_ADD  = '+' OP_SUB  = '-' OP_MUL  = '*' OP_DIV  = '/' OP_POW  = '^' OP_OPEN  = '(' OP_CLOSE = ')' def calc_prim (stack):   if stack[0] == OP_OPEN:     arg, stack = calc_expr (stack[1:])     assert stack[0] == OP_CLOSE   else:     arg = stack[0]   return arg, stack[1:] def calc_factor (stack):   arg1, stack = calc_prim (stack)   if stack and stack[0] == OP_POW:     arg2, stack = calc_factor (stack[1:])     arg1 **= arg2   return arg1, stack def calc_term (stack):   arg1, stack = calc_factor (stack)   while stack and (stack[0] in [OP_MUL, OP_DIV]):     op = stack[0]     arg2, stack = calc_factor (stack[1:])     if op == OP_MUL:       arg1 *= arg2     else:       arg1 /= arg2   return arg1, stack def calc_expr (stack):   arg1, stack = calc_term (stack)   while stack and (stack[0] in [OP_ADD, OP_SUB]):     op = stack[0]     arg2, stack = calc_term (stack[1:])     if op == OP_ADD:       arg1 += arg2     else:       arg1 -= arg2   return arg1, stack   stack = [2, '+', 15, '/', '(', 5, '*', 3, ')']; print calc_expr (stack) Yes, it's python because I just cobbled it together, and I have completely ignored building the stack, but it works, and it's simple. EDIT: board messing up indentation EDIT2: added quote so that we know what we're talking about Edited January 31, 2005 by Grum Share this post Link to post Share on other sites
Placid Report post Posted January 31, 2005 Let's say we get an expression like 2+2;rm -rf / Seriously good point. I obviously didnt think of that. But thats not to hard to code against. Pseudo code: method(String pm_expression){ if( expression.indexOf( ";" ) >= 0 ){ //DEPENDING ON IMPLEMENTATION/CHOICE: //EITHER: pm_expression = pm_expression.substring( 0,pm_expression.indexOf( ";" ) ); //OR: return_pm_to_sender.( "Im not going to execute THAT!" ); } } Obviously providing similar method to get rid of any other possible hacking. Not so bad...? Share this post Link to post Share on other sites
Grum Report post Posted January 31, 2005 There are so many ways to execute code if you feed it a raw bash script, that it'd be impossible to code against it without reimplementing the whole shell. Perhaps a python implementation which only has access to the local namespace would be doable, bu I'm no expert. Share this post Link to post Share on other sites
Leeloo Report post Posted January 31, 2005 if( expression.indexOf( ";" ) >= 0 ){ 2+2 | rm -rf / 2+$(rm -rf /) 2+`rm -rf /` 2+2 <(rm -rf /) 2+2 >(rm -rf /) Just the bunch I could think of. Share this post Link to post Share on other sites
Placid Report post Posted January 31, 2005 if( expression.indexOf( ";" ) >= 0 ){ 2+2 | rm -rf / 2+$(rm -rf /) 2+`rm -rf /` 2+2 <(rm -rf /) 2+2 >(rm -rf /) Just the bunch I could think of. Good points; I used semi-colon as an example. Which is why I said Obviously providing similar method to get rid of any other possible hacking You've rightly pointed out that my suggested method would be too much work, when you could probably use local python methods/functions (dont flame me, I said im not a python master ). I was simply suggesting an alternative method to fixing the 2+2+2=4 problem. Share this post Link to post Share on other sites
Vegar Report post Posted January 31, 2005 What about my patch? Is it working correctly? Share this post Link to post Share on other sites
Grum Report post Posted January 31, 2005 Seems to work for me. Share this post Link to post Share on other sites
crusadingknight Report post Posted January 31, 2005 There are so many ways to execute code if you feed it a raw bash script, that it'd be impossible to code against it without reimplementing the whole shell. Perhaps a python implementation which only has access to the local namespace would be doable, bu I'm no expert. Frak has made a python implementation, built into ELC.. could probably be extended quite easily. Share this post Link to post Share on other sites
ttlanhil Report post Posted May 27, 2005 First off, yes, I know, this is serious thread necromancy. I wanted to add something like #calc, and found this thread, and the patch on berlios that was classed 'rejected'. I thought I remembered using #calc before, and was wondering where it went. If a #calc is not wanted in the game, let me know, and I'll can the idea; otherwise, if it's an issue of implementation, I'd like to give this a go. I would do this in C, no calling out to external programs (that may or may not be available) for portability, if that was an issue Share this post Link to post Share on other sites