simplification
Download
73 lines 2 KiBNomsuModified 2026-11-08 raw
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
#!/usr/bin/env nomsu -V8.0.0
###
    This file contains some definitions of text escape sequences, including ANSI console
    color codes.
    
use "core/metaprogramming"
use "core/operators"
use "core/control_flow"

test:
    assume ("\{1, 2, 3}" == "{1, 2, 3}")
    assume ("foo = \(1 + 2)!" == "foo = 3!")
    assume
        ("
            one
            two
        ") == ("
            one
            two
        ")
    assume ("nogap" == "nogap")
    assume (({"x", "y"} joined with ",") == "x,y")
    assume (({"x", "y"} joined) == "xy")
    assume (("BAR", byte 2) == 65)
    assume (("BAR", bytes 1 to 2) deeply equals {66, 65})
    assume (("asdf", capitalized) == "Asdf")
    assume (("asdf", uppercase) == "ASDF")
    assume (("asdf", with "s" -> "X") == "aXdf")
    assume
        (
            ("
                one
                two
                
            "), lines
        ) deeply equals {"one", "two", ""}

test:
    $こんにちは = "こんにちは"
    ($ と言う) means "\($)世界"
    assume (($こんにちは と言う) == "こんにちは世界")

public:
    ### Each part of $text that matches $patt (or its first capture), in order
    (all matches of $patt in $text) means:
        $matches = {}
        ($text, with $patt -> ($ -> ($table.insert $matches $)))
        return $matches

    ($expr for $match in $text matching $patt) parses as
        $expr for $match in (all matches of $patt in $text)

    test:
        assume ("\n" == (newline))

    test:
        assume ((0xDEADBEEF as hex) == "0xDEADBEEF")

    ($num as hex) means:
        if ($num < 0):
            return ("-0x%X", formatted with -$num)
        ..else:
            return ("0x%X", formatted with $num)

### Text literals
$escapes = {
    .nl = "\n", .newline = "\n", .tab = "\t", .bell = "\a", .cr = "\r"
    ."carriage return" = "\r", .backspace = "\b", ."form feed" = "\f"
    .formfeed = "\f", ."vertical tab" = "\v"
}

public:
    for ($name = $str) in $escapes:
        $(COMPILE RULES).$name = ->(Lua (quote $str))