macro librarylibs/Check/src/Check.xtlm
Check: its macro library -- c_ases<, table-driven checks named by their own source text. Imported with the functions, under one alias: "k:" u_se< "Check". A macro is a function from the source text written left and right of its call to the source that replaces the call, run before the program is compiled (X_eTaL MC10). Names with m: are macros; those under h: are private to this file.
Rows
ʰe̲scaped : Char -> Char
A text as the inside of an X_eTaL string literal: \ and " escaped.
ʰe̲scaped ← { s → ((e̲nclose dq) c̲at e̲nclose bs c̲at dq) ᵗr̲eplace ((e̲nclose bs) c̲at e̲nclose bs c̲at bs) ᵗr̲eplace s }
ʰr̲ow? : Truthy a => Char -> a
Whether a row is "input -> expected".
ʰr̲ow? ← { r → 1 = t̲ally "->" ᵗf̲ind r }
ʰc̲heck : Char -> Char -> Char
One check: f applied to the row's input, compared (m_atch) with its expected value, both evaluated where the call is (hygiene: the lambda's w and g only receive them); a line like Check's own.
ʰc̲heck ← { f r → at ← f̲irst "->" ᵗf̲ind r input ← ᵗt̲rim (at − 1) t̲ake r want ← ᵗt̲rim (at + 1) d̲rop r label ← ʰe̲scaped f c̲at " " c̲at input "(" c̲at want c̲at ") { w g -> w m_atch g ? \"ok: " c̲at label c̲at "\"; \"FAIL: " c̲at label c̲at ": expected \" c_at (f_ormat w) c_at \", got \" c_at f_ormat g } (" c̲at f c̲at " (" c̲at input c̲at "))" }
The macro
ᵐc̲ases< : Char -> Char -> Char
"u:s_quare" k:c_ases< "2 -> 4; 3 -> 9; -1 -> 1": a table of checks of one function, a row per case, input -> expected. When the program is compiled it writes one check per row, named by its own source text ("ok: u:s_quare 2", or "FAIL: u:s_quare 3: expected 9, got 10"), so a failing case says which, without writing the expression twice. Expected and actual values must share a type: a mismatch is a type error at compile time. It stands as a statement of its own. A row without one -> stops the compiler: error[bad-cases].
ᵐc̲ases< ← { f body → rs ← '{ r → ᵗt̲rim d̲isclose r } m̲ap ";" ᵗs̲plit body rs ← (0 < '{ t̲ally d̲isclose ⍵ } e̲ach rs) r̲eplicate rs 0 = t̲ally rs ? "bad-cases right" ⎕R̲EJECT "no cases: write input -> expected; ..." bad ← (n̲ot '{ ʰr̲ow? d̲isclose ⍵ } e̲ach rs) r̲eplicate rs 0 < t̲ally bad ? "bad-cases right" ⎕R̲EJECT (d̲isclose f̲irst bad) c̲at " is not a case: write input -> expected" "\n" ᵗj̲oin '{ r → f ʰc̲heck d̲isclose r } m̲ap rs }