Skip to content

Commit

Permalink
add bound checks
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 16, 2022
1 parent e33eaab commit 3c2f99e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion assigns.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.7.0"
version = "0.7.1"
author = "metagn"
description = "syntax sugar for assignments"
license = "MIT"
Expand Down
30 changes: 30 additions & 0 deletions src/assigns/impl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 2 additions & 0 deletions tests/test_default_syntax.nim
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,5 @@ test "checks":
!= 1 := 2
(notin [1, 2, 3]) := 4
(isnot int) := 2.0
(< 5) := 2
(>= 0) := 2

0 comments on commit 3c2f99e

Please sign in to comment.