Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
|||
def ops: ['+','-','*','/'];
|
||||
|
||||
data binaryExpression <{left: <node>, op: <?($ops <[<=$::raw>]>)>, right: <node>}>
|
||||
data node <binaryExpression|"1">
|
||||
|
||||
composer parseArithmetic
|
||||
(<WS>?) <addition|multiplication|term> (<WS>?)
|
||||
rule addition: {left:<addition|multiplication|term> (<WS>?) op:<'[+-]'> (<WS>?) right:<multiplication|term>}
|
||||
rule multiplication: {left:<multiplication|term> (<WS>?) op:<'[*/]'> (<WS>?) right:<term>}
|
||||
rule term: <INT"1"|parentheses>
|
||||
rule parentheses: (<'\('> <WS>?) <addition|multiplication|term> (<WS>? <'\)'>)
|
||||
end parseArithmetic
|
||||
|
||||
templates evaluateArithmetic
|
||||
<´node´ {op: <='+'>}> ($.left -> evaluateArithmetic) + ($.right -> evaluateArithmetic) !
|
||||
<´node´ {op: <='-'>}> ($.left -> evaluateArithmetic) - ($.right -> evaluateArithmetic) !
|
||||
<´node´ {op: <='*'>}> ($.left -> evaluateArithmetic) * ($.right -> evaluateArithmetic) !
|
||||
<´node´ {op: <='/'>}> ($.left -> evaluateArithmetic) ~/ ($.right -> evaluateArithmetic) !
|
||||
otherwise $ !
|
||||
end evaluateArithmetic
|
||||
|
||||
def ast: '(100 - 5 * (2+3*4) + 2) / 2' -> parseArithmetic;
|
||||
'$ast;
|
||||
' -> !OUT::write
|
||||
'$ast -> evaluateArithmetic;
|
||||
' -> !OUT::write
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
composer calculator
|
||||
(<WS>?) <addition|multiplication|term> (<WS>?)
|
||||
rule addition: [<addition|multiplication|term> (<WS>?) <'[+-]'> (<WS>?) <multiplication|term>] ->
|
||||
\(when <?($(2) <='+'>)> do $(1) + $(3) !
|
||||
otherwise $(1) - $(3) !
|
||||
\)
|
||||
rule multiplication: [<multiplication|term> (<WS>?) <'[*/]'> (<WS>?) <term>] ->
|
||||
\(when <?($(2) <='*'>)> do $(1) * $(3) !
|
||||
otherwise $(1) ~/ $(3) !
|
||||
\)
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue