From dea8d09b80cce9cc65b5ba2da66f8ed433be3012 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Tue, 7 Apr 2015 10:10:34 -0700 Subject: [PATCH] Define Logic.isNameTerm, etc. type checks Starting to break dependency on "check" --- packages/logic-solver/logic.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/logic-solver/logic.js b/packages/logic-solver/logic.js index 061165ac11..d367d6a5f8 100644 --- a/packages/logic-solver/logic.js +++ b/packages/logic-solver/logic.js @@ -32,24 +32,33 @@ Logic._MiniSat = MiniSat; // Expose for testing and poking around // "-" will be parsed in the string form, but we try to // keep it to either one or zero of them. -Logic.NumTerm = Match.Where(function (x) { - return Match.test(x, Match.Integer) && x !== 0; -}); +Logic.isNumTerm = function (x) { + // 32-bit integer, but not 0 + return (x === (x | 0)) && x !== 0; +}; -Logic.NameTerm = Match.Where(function (x) { +Logic.isNameTerm = function (x) { // NameTerm must not be empty, or just `-` characters, or // look like a number. Specifically, it can't be zero or // more `-` followed by zero or more digits. return (typeof x === 'string') && ! /^-*[0-9]*$/.test(x); -}); +}; -Logic.Term = Match.OneOf(Logic.NameTerm, Logic.NumTerm); +Logic.isTerm = function (x) { + return Logic.isNumTerm(x) || Logic.isNameTerm(x); +}; + +Logic.NumTerm = Match.Where(Logic.isNumTerm); +Logic.NameTerm = Match.Where(Logic.isNameTerm); +Logic.Term = Match.Where(Logic.isTerm); // WholeNumber: a non-negative integer (0 is allowed) -Logic.WholeNumber = Match.Where(function (x) { - return Match.test(x, Match.Integer) && x >= 0; -}); +Logic.isWholeNumber = function (x) { + return (x === (x | 0)) && x >= 0; +}; + +Logic.WholeNumber = Match.Where(Logic.isWholeNumber); // Abstract base class. Subclasses are created using _defineFormula. Logic.Formula = function () {};