capitalex revised this gist 6 months ago. Go to revision
1 file changed, 82 insertions
shunting-yard.nv(file created)
| @@ -0,0 +1,82 @@ | |||
| 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) | |
Newer
Older