|
| 1 | +import macros, strutils |
| 2 | + |
| 3 | +const moduleName = "assert/contracts" |
| 4 | + |
| 5 | + |
| 6 | +proc contractViolation(msg: string) {.noreturn.} = |
| 7 | + stderr.writeLine("CONTRACT VIOLATION:\n\t" & msg) |
| 8 | + |
| 9 | + when compileOption("stacktrace"): |
| 10 | + let trace = getStackTrace() |
| 11 | + for line in trace.splitLines(): |
| 12 | + if not line.contains(moduleName) and line.len > 0: |
| 13 | + stderr.writeLine(line) |
| 14 | + |
| 15 | + quit(QuitFailure) |
| 16 | + |
| 17 | + |
| 18 | +proc extractContracts(docComment: string): tuple[requires, ensures, |
| 19 | + invariants: seq[string]] = |
| 20 | + result.requires = @[] |
| 21 | + result.ensures = @[] |
| 22 | + |
| 23 | + let lines = docComment.splitLines() |
| 24 | + var currentSection = "" |
| 25 | + |
| 26 | + for line in lines: |
| 27 | + let trimmedLine = line.strip() |
| 28 | + if trimmedLine.startsWith("Requires:"): |
| 29 | + currentSection = "requires" |
| 30 | + continue |
| 31 | + elif trimmedLine.startsWith("Ensures:"): |
| 32 | + currentSection = "ensures" |
| 33 | + continue |
| 34 | + |
| 35 | + if currentSection == "requires" and trimmedLine.len > 0: |
| 36 | + result.requires.add(trimmedLine) |
| 37 | + elif currentSection == "ensures" and trimmedLine.len > 0: |
| 38 | + result.ensures.add(trimmedLine) |
| 39 | + |
| 40 | + |
| 41 | +macro contract*(procDef: untyped): untyped = |
| 42 | + ## Applies Design by Contract principles to a procedure based on its documentation. |
| 43 | + result = procDef |
| 44 | + # For production builds, don't add contract checks |
| 45 | + if defined(noContracts): |
| 46 | + return |
| 47 | + |
| 48 | + var docComment = "" |
| 49 | + if procDef.kind == nnkProcDef: |
| 50 | + |
| 51 | + # Extract doc comment if it exists |
| 52 | + if procDef[6].kind == nnkStmtList and procDef[6].len > 0: |
| 53 | + if procDef[6][0].kind == nnkCommentStmt: |
| 54 | + docComment = procDef[6][0].strVal |
| 55 | + |
| 56 | + if docComment == "": |
| 57 | + return # No documentation, so no contracts to apply |
| 58 | + |
| 59 | + # Extract contracts from the documentation |
| 60 | + let contracts = extractContracts(docComment) |
| 61 | + |
| 62 | + # Create the contract checking code |
| 63 | + var preconditions = newStmtList() |
| 64 | + for req in contracts.requires: |
| 65 | + let condExpr = parseExpr(req) |
| 66 | + preconditions.add(quote do: |
| 67 | + if not(`condExpr`): |
| 68 | + let info = instantiationInfo() |
| 69 | + contractViolation(info.filename & ":" & $info.line & |
| 70 | + "\n\t\tPrecondition failed: " & `req`) |
| 71 | + ) |
| 72 | + |
| 73 | + var postconditions = newStmtList() |
| 74 | + for ens in contracts.ensures: |
| 75 | + let condExpr = parseExpr(ens) |
| 76 | + postconditions.add(quote do: |
| 77 | + if not(`condExpr`): |
| 78 | + let info = instantiationInfo() |
| 79 | + contractViolation(info.filename & ":" & $info.line & |
| 80 | + "\n\t\tPostcondition failed: " & `ens`) |
| 81 | + ) |
| 82 | + |
| 83 | + # Insert the contract checking code into the procedure |
| 84 | + let procBody = procDef[6] |
| 85 | + var newBody = newStmtList() |
| 86 | + |
| 87 | + # Copy the comment if it exists |
| 88 | + if procBody.len > 0 and procBody[0].kind == nnkCommentStmt: |
| 89 | + newBody.add(procBody[0]) |
| 90 | + |
| 91 | + # Add precondition checks |
| 92 | + for precheck in preconditions: |
| 93 | + newBody.add(precheck) |
| 94 | + |
| 95 | + # Copy the original body except the first statement if it's a comment |
| 96 | + for i in 0..<procBody.len: |
| 97 | + if i > 0 or procBody[0].kind != nnkCommentStmt: |
| 98 | + newBody.add(procBody[i]) |
| 99 | + |
| 100 | + # Add postcondition checks |
| 101 | + for postcheck in postconditions: |
| 102 | + newBody.add(postcheck) |
| 103 | + |
| 104 | + # Replace the procedure body with the new one |
| 105 | + procDef[6] = newBody |
0 commit comments