Code coverage report for lib/stack.js

Statements: 94.44% (34 / 36)      Branches: 100% (0 / 0)      Functions: 92.31% (12 / 13)      Lines: 94.29% (33 / 35)      Ignored: none     

All files » lib/ » stack.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70          1   1 3 3 20 25     3   1     1 1 1     1 1 1 1 1     1 1 1 1               3 15       3   45   45 45 45       3   57   57 57 57         1  
'use strict';
 
// stack-only words; using only .dpop .dpush
// TODO add manipulations, double, float, return stack words
 
var def = require('./def');
 
function stack (cxt) {
    cxt.DS = [];
    cxt.RS = [];
    cxt.dpop = function () { return this.DS.pop(); },
    cxt.dpush = function (e) { this.DS.push(e); };
 
    // stack primitives
    var core = {
        drop: function () {
            this.dpop();
        },
        dup: function () {
            var t = this.dpop();
            this.dpush(t);
            this.dpush(t);
        },
        over: function () {
            var t = this.dpop();
            var n = this.dpop();
            this.dpush(n);
            this.dpush(t);
            this.dpush(n);
        },
        swap: function () {
            var t = this.dpop();
            var n = this.dpop();
            this.dpush(t);
            this.dpush(n);
        },
        nip: function () {
            'swap'();
            'drop'();
        }
    };
 
    Object.keys(core).forEach(function (key) {
        def(key, core[key], cxt);
    });
 
    // x1 -- x2
    '0<:t<0;0<>:t!==0;0>:t>0;0=:t===0;1+:t+1;1-:t-1;2*:t<<1;2/:t>>1;abs:Math.abs(t);cell+:t+4;cells:t*4;char+:t+1;chars:t;invert:~t;negate:-t'
    .split(';')
    .map(function (e) { return e.split(':'); })
    .forEach(function (e) {
        var fn;
        eval('fn = function () { var t = this.dpop(); this.dpush(' + e[1] + '); };');
        def(e[0], fn, cxt);
    });
 
    // x1 x2 -- x3
    '+:n+t;-:n-t;*:n*t;/:n/t;<>:t!==n;and:n&t;=:n===t;>:n>t;<:n<t;lshift:n<<t;max:Math.max(t,n);min:Math.min(t,n);mod:n%t;or:n|t;rshift:n>>t;u<:n<t;xor:n^t;nip:t;u>:n>t'
    .split(';')
    .map(function (e) { return e.split(':'); })
    .forEach(function (e) {
        var fn;
        eval('fn = function () { var t = this.dpop(); var n = this.dpop(); this.dpush(' + e[1] + '); };');
        def(e[0], fn, cxt);
    });
 
}
 
module.exports = stack;