simplification
Download
705 lines 29.1 KiBNomsuModified 2026-11-09 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
#!/usr/bin/env nomsu -V8.0.0
###
    This file defines upgrades from Nomsu <8 to 8. In Nomsu 8, all collections are
    plain Lua tables written with {...}, so [...] list literals were removed, block
    comprehensions were replaced with ($expr for $var in $iterable), and list methods
    were replaced with actions like (add $x to $list). Files are no longer split into
    sections by "~~~" lines, which are removed. Lambdas are only written with (->), and
    take {$x, $y} for several arguments, so (for $x: ...) became ($x ->: ...) and
    (($x $y) -> ...) became ({$x, $y} -> ...). A block written on one line has only
    one statement, and a method call calls only one method, so (: a; b) is written
    as an indented block and ($x, (a; b)) became separate calls in a (result of) block.
    Functions are called with no arguments by (call $fn) instead of ($fn()). In
    multi-line text, the only escapes are "\\" and "\" followed by a value, and a long
    line is continued by starting the next line with "...." in place of its
    indentation, instead of ending it with "\" and starting the next with "..".

    Bitwise operators, classes, time, and UUID actions moved into the "bitwise",
    "things", "time", and "id" libraries, and (use) statements for them are added to
    files that use them. (smoothed by) and (mixed by) were removed, and their
    definitions are added to files that use them. (tau), (golden ratio),
    (wrap $var around $n), and (とは) were removed, (test that) is now just (assume),
    (assume $a == $b) and the other comparisons are (assume ($a == $b)), and
    (command line args) is (the command line arguments). The (if it succeeds) forms
    of (try) were removed, and that code is moved to the end of the (try) block.

    Assigning a variable now changes the variable with that name from an enclosing
    scope, if there is one, so functions that reuse an outer variable's name get a
    (local) declaration, and (with external) blocks are removed. (external) was renamed
    to (public), and compile rules at the top level of a file are put in (public:)
    blocks, since compile rules are now private to their file otherwise. (external)
    inside of an action is removed, so a variable it assigned is now local to the
    action unless the variable exists outside of it.

    Some code can't be upgraded automatically, because it depends on what kind of
    value is used at runtime. In particular, (==) now compares tables by identity
    (use (deeply equals) to compare contents), (#) on a dictionary no longer counts
    its keys (use (number of keys in $)), and the methods (, add), (, has),
    (, reversed), and (, from) also exist on text or code objects, so they are left
    alone. Looping over a coroutine stored in a variable needs
    (for $ in (values yielded by $coroutine)), and (1 to 10) outside of a loop's
    header is now a plain list instead of a Range object. Numbers, booleans, actions,
    coroutines, and nil no longer have metatables, so text can't be added with (+)
    (use interpolation), text can't be indexed by number (use ($text, character $i)),
    coroutines can't be called (use (resume $coroutine)), and indexing nil is an error.
    Since sections no longer run separately, a compile rule that uses a variable or
    (means) action from its own file needs that defined in an (at compile time:) block
    (this is reported as a compile error).

use "compatibility/compatibility"

### Unwrap a single-statement block into the statement
(only statement in $) means
    $.1 if (($.type == "Block") and (#$ == 1)) else $

### [: for $x in $list: add ($x * 2)] -> (($x * 2) for $x in $list)
    {: for $x in $list: if ($x > 0): add $x = 1} -> ($x = 1 for $x in $list if ($x > 0))
(comprehension from $tree) means:
    unless ((#$tree == 1) and ($tree.1.type == "Block") and (#$tree.1 == 1)):
        return (nil)
    $loop = $tree.1.1
    unless (($loop.type == "Action") and ($loop.stub == "for 1 in")):
        return (nil)

    $var = $loop.2
    $iterable = $loop.4
    $body = (only statement in $loop.5)
    $condition = (nil)
    if (($body.type == "Action") and ($body.stub == "if") and (#$body == 3)):
        $condition = $body.2
        $body = (only statement in $body.3)

    unless ($body.type == "Action"):
        return (nil)

    if (($body.stub == "add 1 =") and (#$body == 4)):
        $entry = {$body.2, "=", $body.4}
    ..else:
        unless (($body.stub == "add") and (#$body == 2)):
            return (nil)
        if ($tree.type == "Dict"):
            ### In {...} comprehensions, (add $x) used to mean (add $x = (yes))
            $entry = {$body.2, "=", "Action" tree from $body.source with "yes"}
        ..else:
            $entry = {$body.2}

    $bits = ($entry concatenated with {"for", $var, "in", $iterable})
    if $condition:
        $bits = ($bits concatenated with {"if", $condition})
    return ("Action" tree from $tree.source with (unpack $bits))

### ($list, joined with ", ") -> ($list joined with ", "), etc.
(action for method $tree) means:
    unless (#$tree == 2):
        return (nil)
    $target = $tree.1
    $method = $tree.2
    $source = $tree.source

    ### These methods were spelled several ways, and now there's only one:
    $renamed = {
        ."as a lua identifier" = {"as", "lua", "id"}
        ."as a lua id" = {"as", "lua", "id"}, ."is a lua identifier" = {"is", "lua", "id"}
        ."is a lua id" = {"is", "lua", "id"}
    }

    if $renamed.($method.stub):
        $new_method =
            "Action" tree from $method.source with (unpack $renamed.($method.stub))
        return ("MethodCall" tree from $source with $target $new_method)

    if ($method.stub == "rounded"):
        return ("Action" tree from $source with $target "rounded")

    if ($method.stub == "rounded down"):
        return ("Action" tree from $source with $target "rounded" "down")

    if ($method.stub == "rounded up"):
        return ("Action" tree from $source with $target "rounded" "up")

    if ($method.stub == "to the nearest"):
        return ("Action" tree from $source with $target "to" "the" "nearest" $method.4)

    if ($method.stub == "backwards"):
        return ("Action" tree from $source with $target "reversed")

    if ($method.stub == "joined"):
        return ("Action" tree from $source with $target "joined")

    if ($method.stub == "joined with"):
        return ("Action" tree from $source with $target "joined" "with" $method.3)

    if ($method.stub == "pop"):
        return ("Action" tree from $source with "pop" "from" $target)

    if ($method.stub == "first"):
        return ("Action" tree from $source with "first" "of" $target)

    if ($method.stub == "last"):
        return ("Action" tree from $source with "last" "of" $target)

    if ($method.stub == "remove index"):
        return
            "Action" tree from $source with "remove" "index" $method.3 "from" $target

    if ($method.stub == "index of"):
        return ("Action" tree from $source with "index" "of" $method.3 "in" $target)

    for $suffix in {"st", "nd", "rd", "th"}:
        if ($method.stub == "1 \$suffix to last"):
            return
                "Action" tree from $source with $method.1 $suffix "to" "last" "of" $target

    return (nil)

### ($x, (add 1; add 2)) -> (result of: $x, add 1; return ($x, add 2)), since a method
    call only calls one method now
(method calls in $tree) means:
    $source = $tree.source
    $target = $tree.1
    $statements = {}
    unless ($target.type == "Var"):
        $var = ("Var" tree from $source with "method_target")
        add ("Action" tree from $source with $var "=" $target) to $statements
        $target = $var

    for $i in (2 to #$tree):
        $call = ("MethodCall" tree from $source with $target $tree.$i)
        $call = ((action for method $call) or $call)
        if ($i == #$tree):
            $call = ("Action" tree from $source with "return" $call)
        add $call to $statements

    return
        "Action" tree from $source with "result" "of"
            "Block" tree from $source with (unpack $statements)

### "\(: for $x in $list: add $x)" -> "\(($x for $x in $list) joined)"
(text with comprehensions from $tree) means:
    $changed = (no)
    $bits = {}
    for $bit in $tree:
        if (($bit is syntax tree) and ($bit.type == "Block")):
            $comprehension = (comprehension from ("List" tree from $bit.source with $bit))
            if $comprehension:
                $bit = ("Action" tree from $bit.source with $comprehension "joined")
                $changed = (yes)

        add $bit to $bits

    if $changed:
        return ("Text" tree from $tree.source with (unpack $bits))

### Some actions moved from the core library into their own libraries
$library_for_stub = {
    ."1 |" = "bitwise", ."1 &" = "bitwise", ."1 ~" = "bitwise", ."~" = "bitwise"
    ."1 <<" = "bitwise", ."1 >>" = "bitwise", ."1 is 2 with" = "things"
    ."1 class named" = "things", ."a thing" = "things", .now = "time"
    ."a Time" = "time", ."sleep for 1 seconds" = "time"
    ."sleep for 1 second" = "time", .uuid = "id", ."id of" = "id"
}

(libraries used by $tree) means:
    $libraries = {}
    for $t in recursive $tree:
        if ($t.type == "Action"):
            $library = $library_for_stub.($t.stub)
            if ($library and (not ($libraries has $library))):
                add $library to $libraries
        for $child in $t:
            if ($child is syntax tree):
                recurse $t on $child

    sort $libraries
    return $libraries

### (smoothed by) and (mixed by) were removed, so files that use them get a copy of
    their definitions. ($nums mixed by $) uses ($lo to $hi mixed by $).
$removed_definitions = {
    {
        .defines = "1 smoothed by", .stubs = {"1 smoothed by"}
        .code = \(
            ($ smoothed by $smoothness) means:
                $ = ($ clamped between 0 and 1)
                if ($smoothness == 0): return $
                $k = (2 ^ $smoothness)
                if ($ < 0.5):
                    return (0.5 * (2 * $) ^ $k)
                ..else:
                    return (1 - 0.5 * (2 - 2 * $) ^ $k)
        )
    }

    {
        .defines = "1 to 2 mixed by", .stubs = {"1 to 2 mixed by", "1 mixed by"}
        .code = \(
            ($lo to $hi mixed by $amount) means:
                $ = ($amount clamped between 0 and 1)
                return ((1 - $) * $lo + $ * $hi)
        )
    }

    {
        .defines = "1 mixed by", .stubs = {"1 mixed by"}
        .code = \(
            ($nums mixed by $amount) means:
                $ = ($amount clamped between 0 and 1)
                $i = (1 + ($ * (#$nums - 1)))
                if ((floor $i) == #$nums):
                    return $nums.(floor $i)
                {$lo, $hi} = {$nums.(floor $i), $nums.(floor ($i + 1))}
                return ($lo to $hi mixed by ($i mod 1))
        )
    }
}

### The removed definitions that a file uses, unless it defines them itself
(removed definitions used by $tree) means:
    $used = {}
    $defined = {}
    for $t in recursive $tree:
        if ($t.type == "Action"):
            $used.($t.stub) = (yes)
            if (($t.stub == "1 means") and ($t.1 is syntax tree)):
                $defined.($t.1.stub) = (yes)

        for $child in $t:
            if ($child is syntax tree):
                recurse $t on $child

    $definitions = {}
    for $removed in $removed_definitions:
        unless $defined.($removed.defines):
            for $stub in $removed.stubs:
                if ($used.$stub and (not ($definitions has $removed.code))):
                    add $removed.code to $definitions

    return $definitions

### Add statements after a file's leading comments and (use) statements
(file $file with $additions added) means:
    $new = (SyntaxTree ($k = $v for ($k = $v) in $file))
    $block = $new.1
    $statements = ($ for $ in $block)
    $i = 1
    repeat while (($i <= #$statements) and (($statements.$i.type == "Comment") or ($statements.$i.stub == "use"))):
        $i += 1

    for $addition in $additions:
        for $s in (($ for $ in $addition) if ($addition.type == "Block") else {$addition}):
            add $s to $statements at index $i
            $i += 1

    $new.1 = ("Block" tree from $block.source with (unpack $statements))
    return $new

### In Nomsu 8, assigning a variable changes the variable with that name from an
    enclosing scope, if there is one. In Nomsu 7, every function made new local variables
    for everything it assigned (except in (external) and (with external) blocks), so
    functions that assign a variable that exists outside of them get a (local)
    declaration. Tests ran as separate files, so they're treated like functions.
    (do) blocks are scopes now, so variables that a (do) block assigns are declared
    before it, and (with $vars $body) became a (do) block that starts with (local: $vars).
$compile_rule_stubs =
    {."1 compiles to", ."1 all compile to", ."1 parses as", ."1 all parse as"}

$assignment_stubs =
    {."1 =", ."1 +=", ."1 -=", ."1 *=", ."1 /=", ."1 ^=", ."1 and=", ."1 or="}

($var's lua id) means ($var, as var, as lua id)

### (with $vars $body) and (do $body) from Nomsu 7, but not methods with those names
(old block $t) means:
    if (($t.stub == "with") and (#$t == 3) and ($t.2.type == "Table")): return (yes)
    return (($t.stub == "do") and (#$t == 2) and ($t.2.type == "Block"))

(local declaration of $vars from $source) means:
    $declared = $vars.1
    if (#$vars > 1):
        $declared = ("Table" tree from $source with (unpack $vars))
    return ("Action" tree from $source with "local" $declared)

### Which child of a function definition is its body, and what its arguments are
(function parts of $t) means:
    unless ($t.type == "Action"): return (nil)
    when $t.stub is:
        "1 ->":
            return {.args = $t.1, .body = 3}

        "->" "coroutine" "test":
            return {.body = 2}

        "result of":
            return {.body = 3}

        "1 means" "1 compiles to" "1 parses as":
            return {.args = $t.1, .body = #$t}

        "1 all mean" "1 all compile to" "1 all parse as":
            return {.args = $t.1.1, .body = #$t}

    return (nil)

(argument names in $args) means:
    $vars = {}
    if ($args is syntax tree):
        when $args.type is:
            "Var":
                $vars = {$args}

            "Action" "MethodCall":
                $vars = ($args, get args)

            "Table":
                $vars = ($ for $ in $args)

    return
        ($'s lua id) = (yes) for $ in $vars if (($ is syntax tree) and ($.type == "Var"))

### The variables assigned in some code (not counting functions inside it), and which
    of them are in (external) or (with external) blocks
(assignments in $code) means:
    $assigned = {}
    $seen = {}
    $external = {}
    (visit $t) means:
        unless ($t is syntax tree): return
        if (function parts of $t): return
        if ($t.type == "Action"):
            if (old block $t): return
            when $t.stub is:
                "with external":
                    for $v in $t.3:
                        $external.($v's lua id) = (yes)
                    visit $t.4
                    return

                "external":
                    $before = #$assigned
                    visit $t.2
                    for $i in (($before + 1) to #$assigned):
                        $external.($assigned.$i's lua id) = (yes)
                    return

            if $assignment_stubs.($t.stub):
                for $v in ($t.1 if ($t.1.type == "Table") else {$t.1}):
                    if (($v.type == "Var") and (not $seen.($v's lua id))):
                        $seen.($v's lua id) = (yes)
                        add $v to $assigned

        for $child in $t:
            visit $child

    visit $code
    return {.assigned = $assigned, .seen = $seen, .external = $external}

### Upgrade a tree, given the names that are visible from enclosing scopes.
    (with external) blocks are unwrapped, and (external) becomes (public), or is
    unwrapped inside of functions, where (public) isn't allowed.
(upgrade scopes in $t with $visible in function $in_function) means:
    unless ($t is syntax tree): return $t
    $new = (SyntaxTree ($k = $v for ($k = $v) in $t))
    $parts = (function parts of $t)
    if $parts:
        $args = (argument names in $parts.args)
        $info = (assignments in $t.($parts.body))
        $inner = ($k = (yes) for ($k = $) in $visible)
        $needs_local = {}
        for $v in $info.assigned:
            $id = ($v's lua id)
            if ($visible.$id and (not $args.$id) and (not $info.external.$id)):
                add $v to $needs_local
            $inner.$id = "assigned"

        for ($a = $) in $args:
            $inner.$a = "argument"

        $body = (upgrade scopes in $t.($parts.body) with $inner in function (yes))
        if ((#$needs_local > 0) and ($body.type == "Block")):
            $declaration = (local declaration of $needs_local from $body.source)
            $body = ("Block" tree from $body.source with $declaration (unpack $body))
        $new.($parts.body) = $body
        return $new

    if ($t.type == "Action"):
        if (old block $t):
            ### Variables that the block assigns, other than the ones (with) lists, belong
                to the enclosing function, so they're declared there if they don't exist yet
            $body = $t.(#$t)
            $items = (($ for $ in $t.2) if ($t.stub == "with") else {})
            $listed = {}
            for $item in $items:
                $listed.(($item if ($item.type == "Var") else $item.1)'s lua id) = (yes)
            $info = (assignments in $body)
            $inner = ($k = $v for ($k = $v) in $visible)
            $outer = {}
            for $v in $info.assigned:
                $id = ($v's lua id)
                $exists = (($visible.$id == "assigned") or ($visible.$id == "argument"))
                unless ($listed.$id or $info.external.$id or $exists):
                    add $v to $outer
                $inner.$id = "assigned"

            for ($id = $) in $listed:
                $inner.$id = "assigned"

            $do_body = ("Block" tree from $body.source)
            if (#$items > 0):
                $vars = ("Block" tree from $t.2.source with (unpack $items))
                if ((#$vars == 1) and ($vars.1.type == "Var")): $vars = $vars.1
                $vars = (upgrade scopes in $vars with $visible in function $in_function)
                $do_body, add ("Action" tree from $t.2.source with "local" $vars)
            $body = (upgrade scopes in $body with $inner in function $in_function)
            for $s in (($ for $ in $body) if ($body.type == "Block") else {$body}):
                $do_body, add $s
            $do = ("Action" tree from $t.source with "do" $do_body)
            if (#$outer == 0):
                return $do

            return
                "Block" tree from $t.source with (local declaration of $outer from $t.source) $do

        if ($t.stub == "with external"):
            return (upgrade scopes in $t.4 with $visible in function $in_function)

        if ($t.stub == "external"):
            $body = (upgrade scopes in $t.2 with $visible in function $in_function)
            if $in_function:
                return $body
            return ("Action" tree from $t.source with "public" $body)

    for $i in (1 to #$t):
        $new.$i = (upgrade scopes in $t.$i with $visible in function $in_function)

    if ($t.type == "Block"):
        ### Unwrapped blocks become part of this block
        $statements = {}
        for $statement in $new:
            if (($statement is syntax tree) and ($statement.type == "Block")):
                for $s in $statement:
                    add $s to $statements
            ..else:
                add $statement to $statements

        return ("Block" tree from $t.source with (unpack $statements))

    return $new

### Compile rules are now private to their file unless they're in a (public:) block
(statements $statements with public compile rules) means:
    $result = {}
    $public = (nil)
    for $s in $statements:
        if (($s.type == "Action") and $compile_rule_stubs.($s.stub)):
            unless $public:
                $public = ("Block" tree from $s.source)
                add ("Action" tree from $s.source with "public" $public) to $result
            $public, add $s
        ..else:
            $public = (nil)
            add $s to $result

    return $result

(file $file with new scopes) means:
    $visible = {}
    for $chunk in $file:
        for ($id = $) in (assignments in $chunk).seen: $visible.$id = "assigned"
    $new = (SyntaxTree ($k = $v for ($k = $v) in $file))
    for $i in (1 to #$file):
        $chunk = (upgrade scopes in $file.$i with $visible in function (no))
        $statements = (($ for $ in $chunk) if ($chunk.type == "Block") else {$chunk})
        $statements = (statements $statements with public compile rules)
        $new.$i = ("Block" tree from $chunk.source with (unpack $statements))
    return $new

upgrade $tree to "8" as:
    if ($tree.type == "FileChunks"):
        $tree = (file $tree with new scopes)
        $additions = {}
        for $library in (libraries used by $tree):
            add
                "Action" tree from $tree.source with "use"
                    "Text" tree from $tree.source with $library
            ..to $additions

        for $definition in (removed definitions used by $tree):
            add $definition to $additions

        if (#$additions > 0):
            $tree = (file $tree with $additions added)

        ### The chunks that "~~~" lines separated become one list of statements
        $statements = {}
        for $chunk in $tree:
            for $s in $chunk:
                add $s to $statements

        $file = ("File" tree from $tree.source with (unpack $statements))
        $file.shebang = $tree.shebang
        return $file

    if ($tree.type == "Text"):
        return (text with comprehensions from $tree)

    if (($tree.type == "List") or ($tree.type == "Dict")):
        $comprehension = (comprehension from $tree)
        if $comprehension:
            return $comprehension

        ### Entries like (.x = 1) are table entries now, not (=) actions
        $items = {}
        for $item in $tree:
            if
                ($item.type == "Action") and ($item.stub == "1 =") and ($item.1.type == "Index")
            ..:
                $item = ("TableEntry" tree from $item.source with $item.1 $item.3)
            add $item to $items
        return ("Table" tree from $tree.source with (unpack $items))

    if ($tree.type == "MethodCall"):
        if (#$tree > 2):
            return (method calls in $tree)
        return (action for method $tree)

    ### for $ in (coroutine: ...) -> for $ in (values yielded by (coroutine: ...))
    if (($tree.type == "Action") and ($tree.stub == "for 1 in")):
        $iterable = $tree.4
        if ($iterable.type == "Action"):
            if (($iterable.stub == "coroutine") or ($iterable.stub == "coroutine from")):
                $new = ("Action" tree from $tree.source with (unpack $tree))
                $new.4 =
                    "Action" tree from $iterable.source with "values" "yielded" "by" $iterable
                return $new

    $is_type_check =
        ($tree.stub == "1 is") or ($tree.stub == "1 isn ' t") or
            $tree.stub == "1 is not"

    if (($tree.type == "Action") and $is_type_check):
        $type = $tree.(#$tree)
        if (($type.type == "Text") and (#$type == 1)):
            if (($type.1 == "a List") or ($type.1 == "a Dict")):
                $new = ("Action" tree from $tree.source with (unpack $tree))
                $new.(#$new) = ("Text" tree from $type.source with "a Table")
                return $new

### Lambdas are written with (->) and take {$x, $y} for several arguments, instead
    of (for $x: ...) or (($x $y) -> ...)
(lambda arguments $args) means:
    ### (\$args -> \$body) in quoted code isn't a list of arguments
    $is_escape = (($args.type == "Action") and (($args, get stub) == "\\"))
    if ((($args.type == "Action") or ($args.type == "MethodCall")) and (not $is_escape)):
        return ("Table" tree from $args.source with (unpack ($args, get args)))
    return $args

upgrade action "for" to "8" via
    $tree ->:
        return
            "Action" tree from $tree.source with (lambda arguments $tree.2) "->" $tree.3

upgrade action "1 ->" to "8" via
    $tree ->:
        $new = (SyntaxTree ($k = $v for ($k = $v) in $tree))
        $new.1 = (lambda arguments $tree.1)
        return $new

### Names that were removed because they duplicated another name for the same thing.
    Pairs of an English name and standard notation, like (sine $) and (sin $), remain.
upgrade action (inf) to "8" as (infinity)
upgrade action (nan) to "8" as (NaN)
upgrade action {Pi, PI} to "8" as (pi)
upgrade action {nothing, null} to "8" as (nil)
upgrade action ($a if $c otherwise $b) to "8" as ($a if $c else $b)
upgrade action ($a unless $c then $b) to "8" as ($a unless $c else $b)
upgrade action (if $x is $body) to "8" as (when $x is $body)
upgrade action (remove last from $list) to "8" as (pop from $list)
upgrade action (test that $condition) to "8" as (assume $condition)
upgrade action (assume $a == $b) to "8" as (assume ($a == $b))
upgrade action (assume $a != $b) to "8" as (assume ($a != $b))
upgrade action (assume $a < $b) to "8" as (assume ($a < $b))
upgrade action (assume $a > $b) to "8" as (assume ($a > $b))
upgrade action (assume $a <= $b) to "8" as (assume ($a <= $b))
upgrade action (assume $a >= $b) to "8" as (assume ($a >= $b))
upgrade action (assume $a is $b) to "8" as (assume ($a is $b))
upgrade action (assume $a isn't $b) to "8" as (assume ($a isn't $b))
upgrade action (assume $a is not $b) to "8" as (assume ($a is not $b))
upgrade action (assume $a deeply equals $b) to "8" as
    assume ($a deeply equals $b)
upgrade action (command line args) to "8" as (the command line arguments)
upgrade action (keys of $table) to "8" as (keys in $table)
upgrade action (values of $table) to "8" as (values in $table)
upgrade action (reversed $list) to "8" as ($list reversed)
upgrade action (sorted $items) to "8" as ($items sorted)
upgrade action ($'metatable) to "8" as ($'s metatable)
upgrade action (round $) to "8" as ($ rounded)
upgrade action (all $items) to "8" as (all of $items)
upgrade action (any $items) to "8" as (any of $items)
upgrade action (none $items) to "8" as (none of $items)
upgrade action (not all $items) to "8" as (not all of $items)
upgrade action (sum $items) to "8" as (sum of $items)
upgrade action (product $items) to "8" as (product of $items)
upgrade action (avg of $items) to "8" as (average of $items)
upgrade action (lowest of $items) to "8" as (smallest of $items)
upgrade action {biggest of $items, highest of $items} to "8" as (largest of $items)
upgrade action (rand) to "8" as (random number)
upgrade action {random int $n, randint $n} to "8" as (random integer $n)
upgrade action {random from $low to $high, rand $low $high} to "8" as
    random number from $low to $high
upgrade action (random choice $items) to "8" as (random choice from $items)

### (random) and (random $items) have the same stub
upgrade action "random" to "8" via
    $tree ->:
        if (#$tree == 1):
            return ("Action" tree from $tree.source with "random" "number")
        return ("Action" tree from $tree.source with "random" "choice" "from" $tree.2)

upgrade action (absolute value $x) to "8" as (absolute value of $x)
upgrade action (square root $x) to "8" as (square root of $x)
upgrade action ($x as number) to "8" as ($x as a number)
upgrade action ($a not= $b) to "8" as ($a != $b)
upgrade action ($a wrapped around $b) to "8" as ($a % $b)

### (try) no longer has an (if it succeeds) part, so that code moves to the end of the
    (try) block, where a failure in it is now handled by the (if it fails) part
(statements of $a then $b) means:
    $statements = {}
    for $part in {$a, $b}:
        for $s in (($ for $ in $part) if ($part.type == "Block") else {$part}):
            add $s to $statements

    return ("Block" tree from $a.source with (unpack $statements))

for $pattern in {
    \(try $action if it succeeds $success)
    \(try $action if it succeeds $success if it fails $fallback)
    \(try $action if it fails $fallback if it succeeds $success)
    \(try $action if it succeeds $success if it fails with $msg $fallback)
    \(try $action if it fails with $msg $fallback if it succeeds $success)
}:
    upgrade action ($pattern, get stub) to "8" via
        $tree ->:
            $m = ($tree, matching $pattern)
            $body = (statements of $m.action then $m.success)
            if $m.msg:
                return \(try \$body if it fails with \$m.msg \$m.fallback)

            if $m.fallback:
                return \(try \$body if it fails \$m.fallback)

            return \(try \$body)

### Removed actions
upgrade action {tau, Tau, TAU} to "8" as (2 * (pi))
upgrade action (golden ratio) to "8" as ((1 + (sqrt 5)) / 2)
upgrade action ($spec とは $body) to "8" as ($spec means $body)
upgrade action (wrap $var around $n) to "8" as ($var = ($var % $n))
upgrade action (NOT $x) to "8" as ~$x
upgrade action ($a OR $b) to "8" as ($a | $b)
upgrade action ($a XOR $b) to "8" as ($a ~ $b)
upgrade action ($a AND $b) to "8" as ($a & $b)
upgrade action ($a LSHIFT $b) to "8" as ($a << $b)
upgrade action ($a RSHIFT $b) to "8" as ($a >> $b)
upgrade action {$'s id, $'id} to "8" as (id of $)
upgrade action ($x as lua identifier) to "8" as ($x as lua id)