Can a new operator be created in JavaScript? [duplicate]

2

I need to use this logical operator: ⇒ which will be inserted in a string like this:

var mystring = "(true ⇒ false) ⇒ true";

That way I should be able to evaluate that expression with eval(mystring) and return the truth value to me.

But JavaScript does not support it, is there any way to create it? It has certain very basic rules, it can even be simulated with this function that receives two Boolean variables:

function Condicional(v1,v2){
    return (!v1)||v2;               //formula de una condicional
}

but that function can not be applied to any other type of operation in which you use ⇒, or at least it has not occurred to me how.

Maybe there is a place where I can define that logical operator for JavaScript to recognize me, I do not know, maybe in the same place where others are defined as the "!", "& & amp; "," || "," === "...

Is it possible?

edit

I need to know if there is a way to create an operator like this: ⇒ but I will also need to create two more operators, so what I'm looking for is how to create any operator other than what javascript already has.

    
asked by Roberto Sepúlveda Bravo 03.06.2016 в 02:56
source

3 answers

0

I do not know what you want to do, but what is sure is that javascript is not the best language to do it. What you seem to look for is something that allows you to create a DSL (Domain Specific Language) and, for your Logical statements, I would say that what fits you best is a functional language.

Among the possible options, there are several functional languages for JVM that allow a transpilation from java to javascript. The one I have more experience with is scala.js that would allow you to create things like these:

package proposionallogic

import scala.scalajs.js.JSApp

object ProposionalLogic {    
    implicit class BooleanRich(a:Boolean) {
        def :=>(b:Boolean):Boolean = !a && b
    }
}

object MyApp extends JSApp {

    import ProposionalLogic._

    def main() = {
        println((true :=> false) :=> true)
    }
}

I do not know if it can be worth it. I have left the entire project in github .

    
answered by 03.06.2016 / 11:08
source
3

No, JavaScript does not support creating new operators or modifying existing ones natively. You can only create functions.

    
answered by 03.06.2016 в 04:00
0

You can not create an operator, but if it is possible to make a parser, it will allow you to evaluate that type of expressions.

An example in javascript that does something like this is js-sequence-diagrams that uses jison to generate the parser and be able to evaluate the expressions.

    
answered by 03.06.2016 в 06:13