Category talk:Wren-check: Difference between revisions

→‎Source code: Fixed bug in Check.char method.
m (→‎Source code: Fixed syntax highlighting.)
(→‎Source code: Fixed bug in Check.char method.)
 
(6 intermediate revisions by the same user not shown)
Line 1:
===Source code===
 
<syntaxhighlight lang=ecmascript"wren">/* Module "check.wren" */
 
/*
Line 15:
if (name == "" || name == null) name = "Value"
Fiber.abort("'%(name)' must be %(desc), have '%(value)'.")
}
 
static isSafeInt_(value) {
return (value is Num) && value.isInteger && value.abs <= Num.maxSafeInteger
}
 
Line 102 ⟶ 106:
}
 
static posInt(name, value) { int(name, value, 1) }
static nonNegInt(name, value) { int(name, value, 0) }
 
static safeInt(name, value) { int(name, value, Num.minSafeInteger, Num.maxSafeInteger) }
static posSafeIntsafeInt(name, value) { int(name, value, 1, Num.maxSafeInteger) }
static nonNegSafeInt(name, if (!isSafeInt_(value)) { intabort_(name, value, 0,"a Num.maxSafeInteger)'safe' }integer")
}
 
static posSafeInt(name, value) {
if (!(isSafeInt_(value) && value > 0)) {
abort_(name, value, "a positive 'safe' integer")
}
}
 
static nonNegSafeInt(name, value) {
if (!(isSafeInt_(value) && value >= 0)) {
abort_(name, value, "a non-negative 'safe' integer")
}
}
 
static nonZeroSafeInt(name, value) {
if (!(safeIntisSafeInt_(value) && value != 0)) abort_(name, value, "a non-zero 'safe' integer"){
abort_(name, value, "a non-zero 'safe' integer")
}
}
 
Line 153 ⟶ 172:
int("max", max, min)
char(name, value)
if (value.codePoints[0] < min.codePoints[0] || value.codePoints[0] > max.codePoints[0]) {
abort_(name, value, "a character between '%(min)' and '%(max)'")
}
Line 297 ⟶ 316:
 
static ok(condition) { ok(condition, "Condition is not met.") }
}
 
/*
SafeInt is a class containing self-explanatory static methods to check
integer arithmetic is 'safe' i.e. within the ± 2^53 limit.
*/
class SafeInt {
static add(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i + j
Check.safeInt("Result", res)
return res
}
 
static sub(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i - j
Check.safeInt("Result", res)
return res
}
 
static mul(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i * j
Check.safeInt("Result", res)
return res
}
 
static pow(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i.pow(j)
Check.safeInt("Result", res)
return res
}
}
 
/* SafeInts checks the safety of arithmetic operations for a list of integers. */
class SafeInts {
static sum(a) { a.reduce(0) { |acc, x| SafeInt.add(acc, x) } }
static prod(a) { a.reduce(1) { |acc, x| SafeInt.mul(acc, x) } }
}
 
9,476

edits