var CounterOp = {
unit : function() {
return 0;
},
mult : function(x, y) {
return x + y;
}
};
var Counter = {
QUEUE_MODE : 1,
DIRECT_MODE : 0,
UPPER_BOUND : 9,
_mode : undefined,
_value : undefined,
_operations : undefined
};
Counter._action_do = function(ops) {
Counter._value = (Counter._value + ops) % (Counter.UPPER_BOUND + 1);
};
Counter.init = function() {
Counter._mode = Counter.QUEUE_MODE;
Counter._value = 0;
Counter._operations = CounterOp.unit();
};
Counter.setMode = function(mode) {
Counter._mode = mode;
};
Counter.operate = function (op) {
var old = Counter._operations;
Counter._operations = CounterOp.mult(old, op);
if (Counter._mode == Counter.DIRECT_MODE) {
Counter.execOperations();
}
};
Counter.execOperations = function() {
var ops = Counter._operations;
Counter._action_do(ops);
Counter._operations = CounterOp.unit();
};
function start() {
Counter.init();
}
function u() {
Counter.operate("u");
}
function d() {
Counter.operate("d");
}
function x() {
Counter.execOperations();
}
function q() {
return Counter._operations;
}
function v() {
return Counter._value;
}
function qmode(f) {
if (f) {
Counter.setMode(Counter.QUEUE_MODE);
} else {
x();
Counter.setMode(Counter.DIRECT_MODE);
}
};