How to resolve the algorithm Arithmetic evaluation step by step in the TXR programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic evaluation step by step in the TXR 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 TXR programming language
Source code in the txr programming language
@(next :args)
@(define space)@/ */@(end)
@(define mulop (nod))@\
@(local op)@\
@(space)@\
@(cases)@\
@{op /[*]/}@(bind nod @(intern op *user-package*))@\
@(or)@\
@{op /\//}@(bind (nod) @(list 'trunc))@\
@(end)@\
@(space)@\
@(end)
@(define addop (nod))@\
@(local op)@(space)@{op /[+\-]/}@(space)@\
@(bind nod @(intern op *user-package*))@\
@(end)
@(define number (nod))@\
@(local n)@(space)@{n /[0-9]+/}@(space)@\
@(bind nod @(int-str n 10))@\
@(end)
@(define factor (nod))@(cases)(@(expr nod))@(or)@(number nod)@(end)@(end)
@(define term (nod))@\
@(local op nod1 nod2)@\
@(cases)@\
@(factor nod1)@\
@(cases)@(mulop op)@(term nod2)@(bind nod (op nod1 nod2))@\
@(or)@(bind nod nod1)@\
@(end)@\
@(or)@\
@(addop op)@(factor nod1)@\
@(bind nod (op nod1))@\
@(end)@\
@(end)
@(define expr (nod))@\
@(local op nod1 nod2)@\
@(term nod1)@\
@(cases)@(addop op)@(expr nod2)@(bind nod (op nod1 nod2))@\
@(or)@(bind nod nod1)@\
@(end)@\
@(end)
@(cases)
@ {source (expr e)}
@ (output)
source: @source
AST: @(format nil "~s" e)
value: @(eval e nil)
@ (end)
@(or)
@ (maybe)@(expr e)@(end)@bad
@ (output)
erroneous suffix "@bad"
@ (end)
@(end)
You may also check:How to resolve the algorithm Sum of squares step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Stirling numbers of the second kind step by step in the C programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the J programming language
You may also check:How to resolve the algorithm Mouse position step by step in the xTalk programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the BASIC programming language