Arithmetic evaluation: Difference between revisions

→‎{{header|Tailspin}}: Simplify to use left recursion
m (→‎{{header|Tailspin}}: simplify typing)
(→‎{{header|Tailspin}}: Simplify to use left recursion)
Line 6,114:
data binaryExpression <{left: <node>, op: <?($ops <[<=$>]>)>, right: <node>}>
data node <binaryExpression|"1">
 
templates leftAssociate
when <[](1)> do $(1) !
otherwise [ { left: $(1), op: $(2), right: $(3)}, $(4..last)...] -> #
end leftAssociate
 
composer parseArithmetic
(<WS>?) <addition|multiplication|term> (<WS>?)
rule addition: [{left:<addition|multiplication|term> (<addedTermWS>+?) op:<'[+-]'> -(<WS>?) leftAssociateright:<multiplication|term>}
rule addedTermmultiplication: {left:<multiplication|term> (<WS>?) op:<'[+-*/]'> (<WS>?) right:<multiplication|term>}
rule multiplication: [<term> <multipliedTerm>+] -> leftAssociate
rule multipliedTerm: (<WS>?) <'[*/]'> (<WS>?) <term>
rule term: <INT|parentheses>
rule parentheses: (<'\('> <WS>?) <addition|multiplication|term> (<WS>? <'\)'>)
Line 6,156 ⟶ 6,149:
composer calculator
(<WS>?) <addition|multiplication|term> (<WS>?)
rule addition: [<addition|multiplication|term> (<addedTermWS>?) <'[+-]'> (<WS>?) <multiplication|term>] ->
\(when <[]?(1$(2) <='+'>)> do $(1) + $(3) !
when <?($(2) <='+'>)> do [otherwise $(1) +- $(3), $(4..last)...] -> #!
otherwise [ $(1) - $(3), $(4..last)...] -> #
\)
rule addedTermmultiplication: [<multiplication|term> (<WS>?) <'[+-*/]'> (<WS>?) <multiplication|term>] ->
\(when <[]?(1$(2) <='*'>)> do $(1) * $(3) !
rule multiplication: [<term> <multipliedTerm>+] ->
\(when <[] otherwise $(1)> ~/ $(13) !
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>? <'\)'>)
Anonymous user