code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(78 lines)
1 #!/usr/bin/env nomsu -V7.0.0
2 ###
3 This file contains some definitions of text escape sequences, including ANSI console
4 color codes.
6 use "core/metaprogramming"
7 use "core/operators"
8 use "core/control_flow"
10 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 test:
13 assume "\[1, 2, 3]" == "[1, 2, 3]"
14 assume "foo = \(1 + 2)!" == "foo = 3!"
15 assume ("
16 one
17 two
18 ") == ("
19 one
20 two
21 ")
22 assume "nogap" == "nogap"
23 assume (["x", "y"], joined with ",") == "x,y"
24 assume (["x", "y"], joined) == "xy"
25 assume ("BAR", byte 2) == 65
26 assume ("BAR", bytes 1 to 2) == [66, 65]
27 assume ("asdf", capitalized) == "Asdf"
28 assume ("asdf", uppercase) == "ASDF"
29 assume ("asdf", with "s" -> "X") == "aXdf"
30 assume
31 ("
32 one
33 two
35 "), lines
36 ..== ["one", "two", ""]
37 ($spec とは $body) parses as ($spec means $body)
39 test:
40 $こんにけは = "こんにけは"
41 ($ と言う) とは "\($)δΈ–η•Œ"
42 assume ($こんにけは と言う) == "γ“γ‚“γ«γ‘γ―δΈ–η•Œ"
44 ($expr for $match in $text matching $patt) compiles to:
45 define mangler
46 return Lua ("
47 (function()
48 local \(mangle "comprehension") = a_List{}
49 for \($match as lua expr) in (\($text as lua expr)):gmatch(\($patt as lua expr)) do
50 \(mangle "comprehension")[#\(mangle "comprehension")+1] = \($expr as lua)
51 end
52 return \(mangle "comprehension")
53 end)()
54 ")
56 test:
57 assume "\n" == (newline)
59 test:
60 assume (0xDEADBEEF as hex) == "0xDEADBEEF"
62 external:
63 ($num as hex) means:
64 if ($num < 0):
65 return ("-0x%X", formatted with -$num)
66 ..else:
67 return ("0x%X", formatted with $num)
69 ### Text literals
70 $escapes = {
71 .nl = "\n", .newline = "\n", .tab = "\t", .bell = "\a", .cr = "\r"
72 ."carriage return" = "\r", .backspace = "\b", ."form feed" = "\f"
73 .formfeed = "\f", ."vertical tab" = "\v"
76 for ($name = $str) in $escapes:
77 with [$lua = (Lua (quote $str))]:
78 $(COMPILE RULES).$name = ->$lua