simplification
Download
167 lines 6.1 KiBNomsuModified 2026-11-08 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
#!/usr/bin/env nomsu -V8.0.0
###
    This file contains basic error reporting code
    
use "core/metaprogramming"
use "core/operators"
use "core/control_flow"

### Text is in quotes, so it's clear which values were text
(assumed value $x) means (($x, as lua) if ((type of $x) == "Text") else "\$x")

at compile time:
    ### How a failed assumption describes each kind of comparison
    $comparison_wording = {
        ."1 ==" = "be", ."1 !=" = "not be", ."1 <" = "be <", ."1 >" = "be >"
        ."1 <=" = "be <=", ."1 >=" = "be >=", ."1 is" = "be", ."1 isn ' t" = "not be"
        ."1 is not" = "not be", ."1 deeply equals" = "deeply equal"
    }

public:
    (fail $msg) compiles to ("
        at_1_fail(\(quote (this tree).source),
            \(($msg as lua expr) if $msg else (quote "A failure was triggered here"))
        )
    ")

    ### Where some code is in its file, for pointing at it in error messages
    (source location of $code) compiles to (Lua (quote "\($code.source)"))

    ### Failed assumptions about comparisons say what the values were
    (check assumption at $location $a $b $holds $expectation) means:
        unless ($holds $a $b):
            at $location fail ("
                Assumption failed: This value was \(assumed value $a), but it was expected to
            .... \$expectation \(assumed value $b).
            ")

    ### A failed comparison says what the values were. Each line of (assume: ...) is a
        separate assumption.
    (assume $condition) compiles to:
        if ($condition.type == "Block"):
            $lines = (($ if ($.type == "Comment") else \(assume \$)) for $ in $condition)
            return ("Block" tree from $condition.source with (unpack $lines))
        $wording = (($condition.type == "Action") and $comparison_wording.($condition, get stub))
        unless $wording:
            return
                \(
                    unless \$condition:
                        at (source location of \$condition) fail ("
                            Assumption failed: This assumption did not hold.
                        ")
                )
        $a = $condition.1
        $b = $condition.(#$condition)
        $comparison = ("Action" tree from $condition.source with (unpack $condition))
        $comparison.1 = \$x
        $comparison.(#$comparison) = \$y
        $wording = ("Text" tree from $condition.source with $wording)
        return
            \(check assumption at (source location of \$a) \$a \$b ({$x, $y} -> \$comparison) \$wording)

    test:
        $x = 1
        assume: $x == 1
        assume:
            ### Comments are skipped
            $x == 1
            $x < 2
        try:
            assume:
                $x == 1
                $x == 2
        ..if it fails with $msg:
            assume ($msg, matches "expected to be 2")
    test:
        try: fail
        $worked = (no)
        try:
            fail "xx"
        ..if it fails with $failure:
            $worked = (yes)
        assume ($failure, matches "xx")
        unless $worked:
            fail "'try' failed to recover from failure"
        (return from try) means:
            try:
                return 5
        assume ((return from try) == 5)
        (return from always) means:
            do:
                return 6
            ..then always:
                $finished = (yes)
        assume ((return from always) == 6)
        
        ### Returning several values, some of them nil
        (return with a gap from try) means:
            try: return (nil) 5
        (return with a gap from always) means:
            do: return (nil) 5
            ..then always: do nothing
        {$a, $b} = (return with a gap from try)
        assume ($b == 5)
        {$a, $b} = (return with a gap from always)
        assume ($b == 5)

    ### Try/except
    (try $action if it fails with $msg $fallback) compiles to:
        $fallback_lua = ($fallback as lua)
        if (#"\$fallback_lua" > 0):
            $msg_lua = ($msg as lua expr)
            ### (try ... if it fails ...) passes (nil) as the message, so there's nothing to set
            if ((#"\$msg_lua" > 0) and ($msg.stub != "nil")):
                $fallback_lua, prepend "\n\$msg_lua = _result[2]\n"
                if ($msg_lua, text, is lua id):
                    $fallback_lua, add free vars {($msg_lua, text)}
        
        $fallback_lua, prepend "-- Failure:\n"
        return Lua ("
            do
                local _fell_through = false
                local _result = (function(...) return {n=select("#", ...), ...} end)(xpcall(function()
                    \($action as lua)
                    _fell_through = true
                end, enhance_error))
                if _result[1] then
                    -- Success:
                    if not _fell_through then return unpack(_result, 2, _result.n) end
                elseif IS_EXIT_SIGNAL(_result[2]) then
                    -- (exit) stops the program, even inside (try)
                    error(_result[2], 0)
                else
                    \$fallback_lua
                end
            end
        ")

    (try $action) parses as (try $action if it fails (do nothing))
    (try $action if it fails $fallback) parses as
        try $action if it fails with (nil) $fallback

    test:
        $success = (no)
        $propagated = (no)
        try:
            do: fail
            ..then always:
                $success = (yes)
        ..if it fails:
            $propagated = (yes)
        unless $propagated:
            fail "'try ... then always ...' didn't propagate failure"
        
        unless $success:
            fail "'try ... then always ...' didn't execute the 'always' code"

    (do $action then always $final_action) compiles to ("
        do -- do/then always
            local _fell_through = false
            local _results = (function(...) return {n=select("#", ...), ...} end)(xpcall(function()
                \($action as lua)
                _fell_through = true
            end, enhance_error))
            \($final_action as lua)
            if not _results[1] then error(_results[2], 0) end
            if not _fell_through then return unpack(_results, 2, _results.n) end
        end
    ")