forked from B-Lang-org/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssert.bs
64 lines (57 loc) · 2.42 KB
/
Assert.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package Assert(staticAssert, dynamicAssert, continuousAssert) where
assertMessage :: String -> String -> String
assertMessage which mess =
letseq pos = getStringPosition mess -- get position of original message literal
in (which +++" assertion failed: " +++ printPosition pos +++ mess)
--@ \subsubsection{Assert}
--@
--@ \index{Assert@\te{Assert} (package)|textbf}
--@ The \te{Assert} package contains definitions to test assertions
--@ in the code.
--@
empty :: Empty
empty = interface Empty
--@ \index{staticAssert@\te{staticAssert}|textbf}
--@ Compile time assertion. Can be used anywhere a compile-time statement is valid.
--@ \begin{libverbatim}
--@ function Module#(Void) staticAssert(Bool b, String s);
--@ \end{libverbatim}
staticAssert :: (IsModule m c) => Bool -> String -> m (Empty)
staticAssert b s = if not testAssert || b then
return empty
else primError (getStringPosition s) ("Static assertion failed: " +++ s)
--@ \index{dynamicAssert@\te{dynamicAssert}|textbf}
--@ Run time assertion. Can be used anywhere an Action is valid, and is
--@ tested whenever it is executed.
--@ \begin{libverbatim}
--@ function Action dynamicAssert(Bool b, String s);
--@ \end{libverbatim}
dynamicAssert :: Bool -> String -> Action
dynamicAssert = if not testAssert then (\ _ _ -> noAction)
else (\ b s -> if b then noAction else
action
$display (assertMessage "Dynamic" s)
$finish 0
)
nullModule :: (IsModule m c) => m Empty
nullModule =
module
interface
--@ \index{continuousAssert@\te{continuousAssert}|textbf}
--@ Continuous run-time assertion (expected to be True on each clock).
--@ Can be used anywhere a module instantiation is valid.
--@ \begin{libverbatim}
--@ module continuousAssert#(Bool b, String s)(Empty);
--@ \end{libverbatim}
continuousAssert :: (IsModule m c) => Bool -> String -> m Empty
continuousAssert = if not testAssert then (\_ _ -> nullModule)
else (\ b s -> addRules $
rules
{-# ASSERT no implicit conditions #-}
{-# ASSERT fire when enabled #-}
"continuousAssert":
when not b ==>
action
$display (assertMessage "Continuous" s)
$finish 0
)