Arithmetic evaluation: Difference between revisions

→‎{{header|Tailspin}}: add directly calculated solution
m (→‎{{header|Tailspin}}: readability fix)
(→‎{{header|Tailspin}}: add directly calculated solution)
Line 6,016:
16
</pre>
 
If we don't need to get the AST, we could just evaluate right away:
<lang tailspin>
composer calculator
(<WS>?) <addition|multiplication|term> (<WS>?)
rule addition: [<multiplication|term> <addedTerm>+ ] ->
\(when <[](1)> $(1) !
when <?($(2) <='+'>)> do [ $(1) + $(3), $(4..last)...] -> #
otherwise [ $(1) - $(3), $(4..last)...] -> #
\)
rule addedTerm: (<WS>?) <'[+-]'> (<WS>?) <multiplication|term>
rule multiplication: [<term> <multipliedTerm>+] ->
\(when <[](1)> $(1) !
when <?($(2) <='*'>)> do [ $(1) * $(3), $(4..last)...] -> #
otherwise [ $(1) ~/ $(3), $(4..last)...] -> #
\)
rule multipliedTerm: (<WS>?) <'[*/]'> (<WS>?) <term>
rule term: <INT|parentheses>
rule parentheses: (<'\('> <WS>?) <addition|multiplication|term> (<WS>? <'\)'>)
end calculator
 
'(100 - 5 * (2+3*4) + 2) / 2' -> calculator -> !OUT::write
'
' -> !OUT::write
</lang>
{{out}}
<pre>16</pre>
 
=={{header|Tcl}}==
Anonymous user