From 61e9162243f4ab8f38820cf79c54269806fbd256 Mon Sep 17 00:00:00 2001 From: Draggor Date: Tue, 5 Apr 2011 11:15:03 -0500 Subject: [PATCH] Removing legacy dice.js from repo --- plugins/dice/dice.js | 113 ------------------------------------- plugins/dice/diceParser.js | 40 ++++++------- 2 files changed, 21 insertions(+), 132 deletions(-) delete mode 100644 plugins/dice/dice.js diff --git a/plugins/dice/dice.js b/plugins/dice/dice.js deleted file mode 100644 index 492b376..0000000 --- a/plugins/dice/dice.js +++ /dev/null @@ -1,113 +0,0 @@ -// Parses the this grammar and calculates the result: -// -// expr ::= '[' expr '+' term ']' | '[' term ']' -// term ::= term '*' form | form -// form ::= form 'd' factor | factor -// factor ::= '(' expr ')' | number -// number ::= '-' digit+ | digit+ -// digit ::= '0' | '1' | ... | '9' - -ReParse = require('reparse').ReParse; - -module.exports = read; - -function read(input) { - return (new ReParse(input, true)).start(brackets); -} - -function line() { - return this.match(/\[[\dd\+\*\-\/\.\(\)]*\]/g); -} - -function brackets() { - return this.between(/^\[/, /^\]/, expr); -} - -function expr() { - return this.chainl1(term, addop); -} - -function term() { - return this.chainl1(form, mulop); -} - -function form() { - return this.chainl1(factor, diceop); -} - -function factor() { - return this.choice(group, number); -} - -function group() { - return this.between(/^\(/, /^\)/, expr); -} - -function number() { - return parseFloat(this.match(/^\-?\d*\.?\d+/)); -} - -function diceop() { - return OPS[this.match(/^[dD]/)]; -} - -function mulop() { - return OPS[this.match(/^[\*\/]/)]; -} - -function addop() { - return OPS[this.match(/^[\+\-]/)]; -} - -function dice(a, b) { - var sum = [0, []]; - for(var i = 0; i < a; i++) { - var r = Math.floor(Math.random() * b) + 1; - sum[0] += r; - sum[1].push(r); - } - return sum; -} - -function objOp(op, a, b) { - if(typeof a === 'object' && typeof b === 'number') { - a[0] = op(a[0], b); - a[1].push(b); - return a; - } else if(typeof a === 'object' && typeof b === 'object') { - a[0] = op(a[0], b[0]); - a[1] = a[1].concat(b[1]); - return a; - } else if(typeof a === 'number' && typeof b === 'object') { - b[0] = op(a, b[0]); - b[1].push(a); - return b; - } else if(typeof a === 'number' && typeof b === 'number') { - return [op(a, b), [a, b]]; - } -} - -function add(a, b) { - return a + b; -} - -function sub(a, b) { - return a - b; -} - -function mul(a, b) { - return a * b; -} - -function div(a, b) { - return a / b; -} - -var OPS = { - 'd': dice, - 'D': dice, - '+': function(a, b) { return objOp(add, a, b); }, - '-': function(a, b) { return objOp(sub, a, b); }, - '*': function(a, b) { return objOp(mul, a, b); }, - '/': function(a, b) { return objOp(div, a, b); } -}; diff --git a/plugins/dice/diceParser.js b/plugins/dice/diceParser.js index 2ff36a7..83622a1 100644 --- a/plugins/dice/diceParser.js +++ b/plugins/dice/diceParser.js @@ -66,21 +66,23 @@ function dice(a, b) { } function objOp(op, a, b) { - if(typeof a === 'object' && typeof b === 'number') { - a[0] = op(a[0], b); - a[1].push(b); - return a; - } else if(typeof a === 'object' && typeof b === 'object') { - a[0] = op(a[0], b[0]); - a[1] = a[1].concat(b[1]); - return a; - } else if(typeof a === 'number' && typeof b === 'object') { - b[0] = op(a, b[0]); - b[1].unshift(a); - return b; - } else if(typeof a === 'number' && typeof b === 'number') { - return [op(a, b), [a, b]]; - } + return function(a, b) { + if(typeof a === 'object' && typeof b === 'number') { + a[0] = op(a[0], b); + a[1].push(b); + return a; + } else if(typeof a === 'object' && typeof b === 'object') { + a[0] = op(a[0], b[0]); + a[1] = a[1].concat(b[1]); + return a; + } else if(typeof a === 'number' && typeof b === 'object') { + b[0] = op(a, b[0]); + b[1].unshift(a); + return b; + } else if(typeof a === 'number' && typeof b === 'number') { + return [op(a, b), [a, b]]; + } + }; } function diceOp(a, b) { @@ -122,8 +124,8 @@ function div(a, b) { var OPS = { 'd': diceOp, 'D': diceOp, - '+': function(a, b) { return objOp(add, a, b); }, - '-': function(a, b) { return objOp(sub, a, b); }, - '*': function(a, b) { return objOp(mul, a, b); }, - '/': function(a, b) { return objOp(div, a, b); } + '+': objOp(add, a, b), + '-': objOp(sub, a, b), + '*': objOp(mul, a, b), + '/': objOp(div, a, b) };