Calculator

A scientific calculator, with the ability to store variables and calculate expressions (example: 1+2*5).

Author: Walker@M*U*S*H
Category: Other
Features: regexp commands.
Compatibility: PennMUSH.

Instructions

Copy and paste the below code into a compatible MUSH or MUX.

MUSHCode for Calculator

@create Calculator parent
@link Calculator parent = #4203
@set Calculator parent = LINK_OK
@set Calculator parent = SAFE
@set Calculator parent = VISUAL
&AUTHOR Calculator parent=Walker@M*U*S*H
&CLEANPAREN Calculator parent=[setq(0,sub(words(%0,\(),1))][setq(1,sub(words(%0,\)),1))][if(gt(%q1,%q0),iter(lnum(sub(%q1,%q0)),\(,%b,))]%0[if(gt(%q0,%q1),iter(lnum(sub(%q0,%q1)),\),%b,))]
&CMD.CALC Calculator parent=$^calc ([\^0-9a-zA-Z\-\+\/\*\(\)]+)$:think [set(me,result:[u(fun.calc,%1)])][pemit(%#,Megacalc: [v(result)])]
@set Calculator parent/CMD.CALC=regexp
&CMD.CALC_SHOW Calculator parent=$calc:@pemit %#=[u(shortdesc)]
&CMD.MODE Calculator parent=$^mode (rad|deg)$:think u(fun.mode,%1)[pemit(%#,Mode is now %1)]
@set Calculator parent/CMD.MODE=regexp
&CMD.RCL Calculator parent=$^rcl ([a-z])$:think u(fun.rcl,%1)[pemit(%#,%1: [v(result)])]
@set Calculator parent/CMD.RCL=regexp
&CMD.STORE Calculator parent=$^sto ([a-z])$:think [set(me,var.%1:[v(result)])][pemit(%#,%1: [v(var.%1)])]
@set Calculator parent/CMD.STORE=regexp
&CMD.STORE_X Calculator parent=$^sto ([a-z])=(.*)$:think [set(me,var.%1:%2)][pemit(%#,%1: [v(var.%1)])]
@set Calculator parent/CMD.STORE_X=regexp
&CMD.TRIG Calculator parent=$^(sin|cos|tan)$:think set(me,result:[u(fun.trig,%1,[v(result)])])[pemit(%#,%1: [v(result)])]
@set Calculator parent/CMD.TRIG=regexp
&CONS.@ Calculator parent=v(result)
&CONS.ANS Calculator parent=v(result)
&CONS.E Calculator parent=e()
&CONS.PI Calculator parent=pi()
&CONS.RES Calculator parent=v(result)
&CONS.RESULT Calculator parent=v(result)
@DESCRIBE Calculator parent=A Mega Calculator%r%rThe display reads: [v(result)]%r%rMode is: [v(mode)]%r%rGlowing Neon Buttons:%r%b%b[ansi(h,calc <expression>)] - calculate an expression.%r%b%b[ansi(h,sin)] or [ansi(h,deg)] - change trig mode%r%b%b%b%b[ansi(h,sin)], [ansi(h,cos)], or [ansi(h,tan)] - get sin, cos, or tan of result.%r%b%b[ansi(h,sto <a-z>)] - store result into a variable%r%b%b[ansi(h,sto <a-z>=<function or number>)] - store an expression into a variable.%r%b%b[ansi(h,rcl <a-z>)] - put variable into result buffer%r%b%b[ansi(h,Constants: @ (result), PI, E)]
@set Calculator parent/DESCRIBE=no_command visual
@DROP Calculator parent=You drop the megacalc
@set Calculator parent/DROP=no_command
&FUN.CALC Calculator parent=setq(0,u(parse.negsub,u(parse.twoparens,u(parse.doubleneg,u(parse.vars,u(parse.varmuls,u(parse.result,edit(u(cleanparen,%0),%b,))))))))[iter(parens:\\\\\\\( power:\\\\\\\^ muldiv:/* addsub:~+,setq(0,u(rec.1,%q0,before(##,:),after(##,:))),%b,)]%q0
&FUN.CLEAR Calculator parent=set(me,result:%0)
&FUN.MODE Calculator parent=set(me,mode:%0)
&FUN.RCL Calculator parent=set(me,result:[v(var.%0)])
&FUN.STORE Calculator parent=set(me,var.%0:%1)
&FUN.TRIG Calculator parent=u(trig.[v(mode)].%0,%1)
&MODE Calculator parent=rad
@ODROP Calculator parent=drops the mega calculator
@set Calculator parent/ODROP=no_command
&PARSE.ADDSUB Calculator parent=regedit(%0,\(\(\\\-|\)\[0-9\\\.\]*\)\(\[~+\]\)\(\[\\\-0-9\\\.\]*\),s(switch\($3,~,sub\($1,$4\),+,add\($1,$4\)\)))
&PARSE.ADDSUB.OLD Calculator parent=regedit(%0,\(\(\\\-|\)\[0-9\\\.\]+\)\(\[~+\]\)\(\[\\\-0-9\\\.\]+\),s(switch\($3,~,sub\($1,$4\),+,add\($1,$4\)\)))
&PARSE.DOUBLENEG Calculator parent=edit(%0,--,+)
&PARSE.EXPRESSION Calculator parent=[setq(0,%0)][iter(power:\\\\\\\^ muldiv:/* addsub:~+,setq(0,u(rec.1,%q0,before(##,:),after(##,:))),%b,)]%q0
&PARSE.MULDIV Calculator parent=regedit(%0,\(\(\\\-|\)\[0-9\\\.\]*\)\(\[\\\*\\\/\]\)\(\[\\\-0-9\\\.\]*\),s(switch\($3,/,fdiv\($1,$4\),*,mul\($1,$4\)\)))
&PARSE.MULDIV.OLD Calculator parent=regedit(%0,\(\(\\\-|\)\[0-9\\\.\]*\)\(\[\\\*\\\/\]\)\(\[\\\-0-9\\\.\]*\),s(switch\($3,/,fdiv\($1,$4\),*,mul\($1,$4\)\)))
&PARSE.NEGSUB Calculator parent=edit(regeditall(edit(%0,~,-),\(\^|\[^0-9\]\)\\\-\(\[0-9\\\.\\\(\\\)\]\),$1NEGATIVE$2),-,~,NEGATIVE,-)
&PARSE.PARENS Calculator parent=regedit(%0,\\\(\(\[0-9\\\+\\\~\\\-\\\^\\\*\\\/\\\.\]+\)\\\),u(parse.expression,$1))
&PARSE.POWER Calculator parent=regedit(%0,\(\(\\\-|\)\[0-9\\\.\]*\)\(\\\^\)\(\[0-9\\\.\\\-\]*\),s(power\($1,$4\)))
&PARSE.POWER.OLD Calculator parent=regedit(%0,\(\(\\\-|\)\[0-9\\\.\]+\)\(\\\^\)\(\[0-9\\\.\\\-\]+\),s(power\($1,$4\)))
&PARSE.RESULT Calculator parent=regeditall(%0,\(\[A-Z\@\]+\),\([u(cons.$1)]\))
&PARSE.TRIG Calculator parent=regeditall(%0,\(\(SIN|COS|TAN|ABS\)\\\(
&PARSE.TWOPARENS Calculator parent=edit(%0,\)\(,\)*\(,\(\),\(0\))
&PARSE.VARMULS Calculator parent=regeditall(regeditall(regeditall(regeditall(%0,\\\)\(\[\\\.a-z0-9\]\),\)*$1),\(\[a-z0-9\\\.\]\)\\\(,$1*\(),\(\\\)|\[a-z\]|\[0-9\]\)\(\[a-z\]\),$1*$2),\(\[a-z\]\)\(\\\(|\[a-z\]|\[0-9\]\),$1*$2)
&PARSE.VARS Calculator parent=regeditall(%0,\(\[a-z\]\),s(if\(hasattrval\(me,var.$1\),u\(fun.calc,v\(var.$1\)\),0\)))
&REC.1 Calculator parent=if(regmatch(%0,\[%2\]),u(rec.2,u(parse.%1,%0),%1,%2),%0)
&REC.2 Calculator parent=if(regmatch(%0,\[%2\]),u(rec.2,u(parse.%1,%0),%1,%2),%0)
&RESULT Calculator parent=2
@SHORTDESC Calculator parent=A Mega Calculator%r%rThe display reads: [v(result)]
@set Calculator parent/SHORTDESC=no_command visual
&TRIG.DEG.COS Calculator parent=cos(fdiv(mul(%0,pi()),180))
&TRIG.DEG.SIN Calculator parent=sin(fdiv(mul(%0,pi()),180))
&TRIG.DEG.TAN Calculator parent=tan(fdiv(mul(%0,pi()),180))
&TRIG.RAD.COS Calculator parent=cos(%0)
&TRIG.RAD.SIN Calculator parent=sin(%0)
&TRIG.RAD.TAN Calculator parent=tan(%0)
&VAR.A Calculator parent=2
&VAR.C Calculator parent=1
&VAR.D Calculator parent=4
&VAR.I Calculator parent=1
&VAR.M Calculator parent=1
&VAR.N Calculator parent=3
&VAR.P Calculator parent=3.141593
&VAR.R Calculator parent=RAND
&VAR.X Calculator parent=5
&VAR.Y Calculator parent=5