How to resolve the algorithm Arithmetic evaluation step by step in the E programming language

Published on 12 May 2024 09:40 PM
#E

How to resolve the algorithm Arithmetic evaluation step by step in the E programming language

Table of Contents

Problem Statement

For those who don't remember, mathematical precedence is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic evaluation step by step in the E programming language

Source code in the e programming language

def eParser := 
def LiteralExpr := .asType()
def arithEvaluate(expr :String) {
  def ast := eParser(expr)
  
  def evalAST(ast) {
    return switch (ast) {
      match e`@a + @b` { evalAST(a) + evalAST(b) }
      match e`@a - @b` { evalAST(a) - evalAST(b) }
      match e`@a * @b` { evalAST(a) * evalAST(b) }
      match e`@a / @b` { evalAST(a) / evalAST(b) }
      match e`-@a` { -(evalAST(a)) }
      match l :LiteralExpr { l.getValue() }
    }
  }
  
  return evalAST(ast)
}

? arithEvaluate("1 + 2")
# value: 3

? arithEvaluate("(1 + 2) * 10 / 100")
# value: 0.3

? arithEvaluate("(1 + 2 / 2) * (5 + 5)")
# value: 20.0

  

You may also check:How to resolve the algorithm Hello world/Graphical step by step in the VBA programming language
You may also check:How to resolve the algorithm Mertens function step by step in the 11l programming language
You may also check:How to resolve the algorithm SQL-based authentication step by step in the PHP programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Klong programming language
You may also check:How to resolve the algorithm AVL tree step by step in the AArch64 Assembly programming language