shunting-yard.nv
· 2.2 KiB · Text
Raw
Playground
|:eval: * $x $y| :@math: multiply $x $y
|:eval: / $x $y| :@math: divide $x $y
|:eval: + $x $y| :@math: add $x $y
|:eval: - $x $y| :@math: subtract $x $y
|:precedence: *| :precedence: * 2
|:precedence: /| :precedence: / 2
|:precedence: +| :precedence: + 1
|:precedence: -| :precedence: - 1
|:precedence: $x| :not an operator:
|:: ) :ops: (|
|:: $token|
:: classify token :token: $token
:: push to its stack
|:: classify token :token: +?| :operator:
|:: classify token :token: -?| :operator:
|:: classify token :token: /?| :operator:
|:: classify token :token: *?| :operator:
|:: classify token :token: [?| :operator:
|:: classify token :token: ]?| :operator:
|:: classify token :token: $x?| :value:
|:: push to its stack :token: $token? :operator:|
:: evaluate token
|:: push to its stack :token: $token :value:|
:values: $token
|:: evaluate token :token: [|
:operators: [
|:: evaluate token :token: ] :operators: [|
|:: evaluate token :token: $token :operators: [|
:operators: $token
:operators: [
|:: evaluate token :token: ]? :operators: $op :values: ($a $b)|
:eval: $op $b $a
:: push result
:: evaluate token
|:: evaluate token :token: $incoming :operators: $top?|
:: get precedence for $top
:: get precedence for $incoming
:: compare precedence
|:: push result :@math: $result|
:values: $result
|:: evaluate token :token: $token|
:operators: $token
|:: get precedence for $token|
:precedence: $token
|:: compare precedence :precedence: $incoming $x :precedence: $top $y|
:@math: compare $x $y
:: push $incoming operator
|:: push $incoming operator :@math: greater|
:operators: $incoming
|:: push $incoming operator :@math: $not-greater :operators: ($eval $next) :values: ($a $b)|
:: push result :eval: $eval $b $a
:: get precedence for $next
:: get precedence for $incoming
:: compare precedence
:operators: $next
|:: push $incoming operator :@math: $not-greater :operators: $eval :values: ($a $b)|
:: push result :eval: $eval $b $a
:operators: $incoming
|:operators: $eval :values: ($a $b)|
:: push result :eval: $eval $b $a
||
:: (142 + 20 * [ 40 + 5 * 3 ] + 4 * 79 / 4 - 2)
| 1 | |:eval: * $x $y| :@math: multiply $x $y |
| 2 | |:eval: / $x $y| :@math: divide $x $y |
| 3 | |:eval: + $x $y| :@math: add $x $y |
| 4 | |:eval: - $x $y| :@math: subtract $x $y |
| 5 | |
| 6 | |:precedence: *| :precedence: * 2 |
| 7 | |:precedence: /| :precedence: / 2 |
| 8 | |:precedence: +| :precedence: + 1 |
| 9 | |:precedence: -| :precedence: - 1 |
| 10 | |:precedence: $x| :not an operator: |
| 11 | |
| 12 | |:: ) :ops: (| |
| 13 | |
| 14 | |:: $token| |
| 15 | :: classify token :token: $token |
| 16 | :: push to its stack |
| 17 | |
| 18 | |:: classify token :token: +?| :operator: |
| 19 | |:: classify token :token: -?| :operator: |
| 20 | |:: classify token :token: /?| :operator: |
| 21 | |:: classify token :token: *?| :operator: |
| 22 | |:: classify token :token: [?| :operator: |
| 23 | |:: classify token :token: ]?| :operator: |
| 24 | |:: classify token :token: $x?| :value: |
| 25 | |
| 26 | |:: push to its stack :token: $token? :operator:| |
| 27 | :: evaluate token |
| 28 | |
| 29 | |:: push to its stack :token: $token :value:| |
| 30 | :values: $token |
| 31 | |
| 32 | |:: evaluate token :token: [| |
| 33 | :operators: [ |
| 34 | |
| 35 | |:: evaluate token :token: ] :operators: [| |
| 36 | |
| 37 | |:: evaluate token :token: $token :operators: [| |
| 38 | :operators: $token |
| 39 | :operators: [ |
| 40 | |
| 41 | |:: evaluate token :token: ]? :operators: $op :values: ($a $b)| |
| 42 | :eval: $op $b $a |
| 43 | :: push result |
| 44 | :: evaluate token |
| 45 | |
| 46 | |:: evaluate token :token: $incoming :operators: $top?| |
| 47 | :: get precedence for $top |
| 48 | :: get precedence for $incoming |
| 49 | :: compare precedence |
| 50 | |
| 51 | |:: push result :@math: $result| |
| 52 | :values: $result |
| 53 | |
| 54 | |:: evaluate token :token: $token| |
| 55 | :operators: $token |
| 56 | |
| 57 | |:: get precedence for $token| |
| 58 | :precedence: $token |
| 59 | |
| 60 | |:: compare precedence :precedence: $incoming $x :precedence: $top $y| |
| 61 | :@math: compare $x $y |
| 62 | :: push $incoming operator |
| 63 | |
| 64 | |:: push $incoming operator :@math: greater| |
| 65 | :operators: $incoming |
| 66 | |
| 67 | |:: push $incoming operator :@math: $not-greater :operators: ($eval $next) :values: ($a $b)| |
| 68 | :: push result :eval: $eval $b $a |
| 69 | :: get precedence for $next |
| 70 | :: get precedence for $incoming |
| 71 | :: compare precedence |
| 72 | :operators: $next |
| 73 | |
| 74 | |:: push $incoming operator :@math: $not-greater :operators: $eval :values: ($a $b)| |
| 75 | :: push result :eval: $eval $b $a |
| 76 | :operators: $incoming |
| 77 | |
| 78 | |:operators: $eval :values: ($a $b)| |
| 79 | :: push result :eval: $eval $b $a |
| 80 | |
| 81 | || |
| 82 | :: (142 + 20 * [ 40 + 5 * 3 ] + 4 * 79 / 4 - 2) |