From 3c2f99e562282cd8fee96727d2802ab5ca8ec8d1 Mon Sep 17 00:00:00 2001 From: hlaaftana <10591326+metagn@users.noreply.github.com> Date: Fri, 16 Sep 2022 12:31:53 +0300 Subject: [PATCH] add bound checks --- assigns.nimble | 2 +- src/assigns/impl.nim | 30 ++++++++++++++++++++++++++++++ tests/test_default_syntax.nim | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/assigns.nimble b/assigns.nimble index 4a302d6..38cc867 100644 --- a/assigns.nimble +++ b/assigns.nimble @@ -1,6 +1,6 @@ # Package -version = "0.7.0" +version = "0.7.1" author = "metagn" description = "syntax sugar for assignments" license = "MIT" diff --git a/src/assigns/impl.nim b/src/assigns/impl.nim index 888fd6e..406dcd8 100644 --- a/src/assigns/impl.nim +++ b/src/assigns/impl.nim @@ -17,6 +17,8 @@ type ## error for failed equality checks in assignments AssignContainsError* = object of AssignError ## error for failed contains checks in assignments + AssignBoundError* = object of AssignError + ## error for failed bound checks in assignments template assignCheckEqual*(a, b): untyped = ## template for equality checks in assignments @@ -48,6 +50,26 @@ template assignCheckNotContains*(a, b): untyped = if a in b: raise newException(AssignContainsError, "did not expect " & astToStr(a) & " to be in " & astToStr(b)) +template assignCheckLess*(a, b): untyped = + ## template for non-equality checks in assignments + if not (a < b): + raise newException(AssignBoundError, "expected " & astToStr(a) & " to be less than " & astToStr(b)) + +template assignCheckLessEqual*(a, b): untyped = + ## template for non-equality checks in assignments + if not (a <= b): + raise newException(AssignBoundError, "expected " & astToStr(a) & " to be less than or equal to " & astToStr(b)) + +template assignCheckGreater*(a, b): untyped = + ## template for non-equality checks in assignments + if not (a > b): + raise newException(AssignBoundError, "expected " & astToStr(a) & " to be greater than " & astToStr(b)) + +template assignCheckGreaterEqual*(a, b): untyped = + ## template for non-equality checks in assignments + if not (a >= b): + raise newException(AssignBoundError, "expected " & astToStr(a) & " to be greater than or equal to " & astToStr(b)) + template openAssign*(lhs, rhs: NimNode, ak: AssignKind = akLet): NimNode = ## Creates a node that calls an open symbol `assign` with `lhs` and `rhs`. if rhs.kind == nnkVarTy: @@ -125,6 +147,14 @@ proc defaultAssign*(lhs, rhs: NimNode, kind = akLet): NimNode = result = newCall(bindSym"assignCheckEqual", rhs, lhs[1]) of "!=": result = newCall(bindSym"assignCheckNotEqual", rhs, lhs[1]) + of "<": + result = newCall(bindSym"assignCheckLess", rhs, lhs[1]) + of "<=": + result = newCall(bindSym"assignCheckLessEqual", rhs, lhs[1]) + of ">": + result = newCall(bindSym"assignCheckGreater", rhs, lhs[1]) + of ">=": + result = newCall(bindSym"assignCheckGreaterEqual", rhs, lhs[1]) of "is": result = newCall(bindSym"assignCheckType", rhs, lhs[1]) of "isnot": diff --git a/tests/test_default_syntax.nim b/tests/test_default_syntax.nim index b8f2252..0e1b08b 100644 --- a/tests/test_default_syntax.nim +++ b/tests/test_default_syntax.nim @@ -210,3 +210,5 @@ test "checks": != 1 := 2 (notin [1, 2, 3]) := 4 (isnot int) := 2.0 + (< 5) := 2 + (>= 0) := 2