JavaScript-based formula interpreter-11 [function implementation]

Source: Internet
Author: User

FunctionBase function base class

File: FunctionBase. js

VaR $ functionmapper = []; </P> <p> // global function mapper <br/> function $ registerfunction (funcobj) {<br/> if ($ functionmapper [funcobj. get_functionname (). tolowercase ()]! = NULL) <br/> throw new exception (this, "$ registerfunction:" + funcobj. get_functionname () + "has registered"); </P> <p> $ functionmapper [funcobj. get_functionname (). tolowercase ()] = funcobj; <br/>}; </P> <p> function $ getregisterfunction (funcname) <br/>{< br/> if ($ functionmapper [funcname. tolowercase ()]! = NULL) <br/> return $ functionmapper [funcname. tolowercase ()]; <br/> else <br/> return NULL; <br/>}</P> <p> // --------------- functionbase -------------- <br/> function functionbase () {<br/> This. derivefrom (New operatorbase (); <br/> This. set_classname ("functionbase"); <br/> This. _ type = new functiontype ("undefined"); <br/>}; </P> <p> functionbase. prototype = {<br/> get_functionname: function () {return this. get_sign () ;}, <br/> evaluate: function (ARGs) {<br/> throw new exception (this, "not implemented "); <br/>}// evaluate <br/>}; // functionbase. prototype <br/>

 

Note that the implementation here is: there can be no function definition during Formula Parsing, while the global$ FunctionMapperTable for function name search. Corresponding,

The function must pass$ RegisterFunctionTo register.

Because you do not know the prior knowledge of a function, you need a definition to represent the function. This definition is the FunctionUnknown class.

 

FunctionUnknown class

File: FunctionBase. js

// ------------- FunctionUnknown ---------------- <br/> // Used for function parser <br/> function FunctionUnknown (arg) {<br/> this. deriveFrom (new FunctionBase (); <br/> this. set_ClassName ("FunctionUnknown"); </p> <p> if (arg! = Null) <br/> this. set_Sign (arg. toString (); <br/> else <br/> this. set_Sign ("undefined"); <br/>}; </p> <p> FunctionUnknown. prototype. evaluate = function (args) {<br/> var values = []; </p> <p> for (var I = 0; I <args. length; I ++) <br/> values. push (args [I]. get_Value (); </p> <p> $ Debug. writeLine ("Undefined function:" + this. get_Sign () + "(" + values. join (",") + "), assume return 0. "); <br/> return new OperandNumber (0); <br/>}; </p> <p> // Register operation shocould be completed MED after the prototype defined. <br/> $ RegisterFunction (new FunctionUnknown ());

 

Implementation of various functions

All Excel functions are not implemented here. There are only a few in the "frequently-used" directory. The PMT formula is unknown, so it is not implemented either. Leave it for homework:-P.

/// Function template <br/>/* <br/> function functionxxx () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionxxx"); <br/> This. set_sign ("XXX"); <br/>}; </P> <p> functionxxx. prototype. evaluate = function (ARGs) {</P> <p> // shocould return a value <br/> }; </P> <p> $ registerfunction (New functionxxx ()); <br/> */</P> <p> /////////////////////////// //// // <br/> // sum <br/> ////// /// // <Br/> function functionaverage () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionaverage"); <br/> This. set_sign ("average"); <br/>}; </P> <p> functionaverage. prototype = {<br/> evaluate: function (ARGs) {<br/> var res = 0; </P> <p> for (VAR I = 0; I <args. length; I ++) {<br/> If (! (ARGs [I]. isnumber () | ARGs [I]. isboolean () <br/> throw new exception (this, "evaluate", "unsupported argument:" + $ T (ARGs [I]); </P> <p> res + = ARGs [I]. get_value (); <br/>}</P> <p> return New operandnumber (RES/args. length); <br/>}< br/>}; </P> <p> $ registerfunction (New functionaverage ()); </P> <p> ///////////////////////////////// /// // <br/> // average <br/> /////////////////// /////////////////////// /<Br/> function functionsum () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionsum"); <br/> This. set_sign ("sum"); <br/>}; </P> <p> functionsum. prototype. evaluate = function (ARGs) {<br/> var res = 0; </P> <p> for (VAR I = 0; I <args. length; I ++) {<br/> If (! (ARGs [I]. isnumber () | ARGs [I]. isboolean () <br/> throw new exception (this, "evaluate", "unsupported argument:" + $ T (ARGs )); </P> <p> res + = ARGs [I]. get_value (); <br/>}</P> <p> return New operandnumber (RES); <br/> }; </P> <p> $ registerfunction (New functionsum ()); </P> <p> ///////////////////////////////// /// // <br/> // If <br/> /////////////////// /// // <br/> function functionif () {<br /> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionif"); <br/> This. _ type = new functiontype ("if", 3); <br/>}; </P> <p> functionif. prototype. evaluate = function (ARGs) {<br/> If (! ARGs [0]. isboolean () <br/> throw new exception (this, "evaluate", "first argument shocould be boolean"); </P> <p> return ARGs [0]. get_value ()? ARGs [1]: ARGs [2]; <br/>}; </P> <p> $ registerfunction (New functionif ()); </P> <p> ///////////////////////////////// /// // <br/> // count <br/> /////////////////// /// // </P> <p> ///////// /// // <br/> // max <br/> /////////////////////////////////// /// // <br/> function functionmax () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionmax"); <br/> This. set_sign ("Max"); <br/>}; </P> <p> functionmax. prototype. evaluate = function (ARGs) {<br/> var res = 0; </P> <p> for (VAR I = 1; I <args. length; I ++) <br/> If (ARGs [I]. isnumber () <br/> If (RES <ARGs [I]. get_value () <br/> res = ARGs [I]. get_value (); </P> <p> return New operandnumber (RES); <br/>}; </P> <p> $ registerfunction (New functionmax ()); </P> <p> ///////////////////////////////// /// // <br/> // min <br/> /////////////////// /// // </P> <p> ///////// /// // <br/> // sin <br/> /////////////////////////////////// /// // <br/> function functionsin () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionsin"); <br/> This. _ type = new functiontype ("sin", 1); <br/>}; </P> <p> functionsin. prototype. evaluate = function (ARGs) {<br/> return New operandnumber (math. sin (ARGs [0]. get_value (); <br/>}; </P> <p> $ registerfunction (New functionsin ()); </P> <p> ///////////////////////////////// /// // <br/> // sumif <br/> /////////////////// /// // <br/> function functionsumif () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionsumif"); <br/> This. set_sign ("sumif"); <br/>}; </P> <p> functionsumif. prototype. evaluate = function (ARGs) {</P> <p> // depend on cell <br/> throw new exception (this, "evaluate", "not implemented "); <br/>}; </P> <p> $ registerfunction (New functionsumif ()); </P> <p> ///////////////////////////////// /// // <br/> // PMT <br/> /////////////////// /// // <br/> function functionpmt () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionpmt"); <br/> This. set_sign ("PMT"); <br/>}; </P> <p> functionpmt. prototype. evaluate = function (ARGs) {</P> <p> throw new exception (this, "evaluate", "not implemented"); <br/> }; </P> <p> $ registerfunction (New functionpmt ()); <br/> //////////////////////////////////// //// // <br/> // STDev <br/> ////////////////////// /// // <br/> function functionstdev () {<br/> This. derivefrom (New functionbase (); <br/> This. set_classname ("functionstdev"); <br/> This. set_sign ("STDev"); <br/>}; </P> <p> functionstdev. prototype. evaluate = function (ARGs) {<br/> var squaresum = 0, <br/> sum = 0; </P> <p> for (VAR I = 0; I <ARGs; I ++) {<br/> squaresum + = ARGs [I]. get_value () * ARGs [I]. get_value (); <br/> sum + = ARGs [I]. get_value (); <br/>}</P> <p> var res = math. SQRT (ARGs. length * squaresum-sum * sum)/(ARGs. length * (ARGs. length-1); </P> <p> return New operandnumber (RES); <br/> }; </P> <p> $ registerfunction (New functionstdev ());

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.