nomsu/compatibility/compatibility.nom

127 lines
4.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env nomsu -V6.14
2018-07-23 15:56:59 -07:00
#
This file contains code for defining ways to upgrade code between different versions
of Nomsu.
use "lib/os.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2018-12-14 20:21:03 -08:00
$UPGRADES = {}
externally (upgrade to $version via $upgrade_fn) means:
$UPGRADES.$version = $upgrade_fn
$ACTION_UPGRADES = ({} with fallback $ -> {})
externally (upgrade action $stub to $version via $upgrade_fn) means:
$ACTION_UPGRADES.$version.$stub = $upgrade_fn
2018-12-30 19:04:34 -08:00
(upgrade $tree to $version as $body) parses as
2018-12-14 20:21:03 -08:00
upgrade to $version via (($ $end_version) -> ($ with $tree -> $body))
2018-12-14 20:21:03 -08:00
(upgrade action $actions to $version as $body) compiles to:
if ($actions is "Action" syntax tree):
$actions = \[$actions]
$lua = (Lua "")
for $action in $actions:
$replacements = {}
for $i in 1 to (size of $action):
if ($action.$i is "Var" syntax tree):
$replacements.($action.$i.1) = "\(\$tree as lua id)[\$i]"
define mangler
2018-12-14 20:21:03 -08:00
(make tree $t) means:
when:
2018-12-14 20:21:03 -08:00
($t is "Var" syntax tree):
if $replacements.($t.1):
return $replacements.($t.1)
..else:
2018-12-14 20:21:03 -08:00
external $needs_mangle = (yes)
2018-12-30 23:58:47 -08:00
return ("
SyntaxTree{type=\(quote $t.type), source=\(quote "\($t.source)"), \(quote (mangle $t.1))}
")
2018-12-14 20:21:03 -08:00
($t is syntax tree):
$args = []
for $k = $v in $t:
if ((type of $k) == "number"):
2018-12-30 19:04:34 -08:00
$args, add (make tree $v)
..else:
2018-12-30 19:04:34 -08:00
$args, add "\($k)=\(make tree $v)"
return "SyntaxTree{\($args, joined with ", ")}"
else:
2018-12-14 20:21:03 -08:00
return (quote $t)
2018-12-14 20:21:03 -08:00
unless ("\$lua" == ""):
2018-12-30 19:04:34 -08:00
$lua, add "\n"
2018-12-14 20:21:03 -08:00
$retval = (make tree $body)
2018-12-30 19:04:34 -08:00
$lua, add
Lua ("
2018-12-30 23:58:47 -08:00
upgrade_action_1_to_2_via(\(quote $action.stub), \($version as lua expr), function(\
..\(\$tree as lua id))
2018-12-14 20:21:03 -08:00
return \$retval
2018-12-30 19:04:34 -08:00
end)
")
2018-12-14 20:21:03 -08:00
return $lua
2018-12-30 19:04:34 -08:00
externally [
2018-12-14 20:21:03 -08:00
$tree upgraded from $start_version to $end_version
$tree upgraded to $end_version from $start_version
2018-12-30 19:04:34 -08:00
] all mean:
2018-12-14 20:21:03 -08:00
unless ($tree is syntax tree): return $tree
($ver as list) means (($ as number) for $ in $ver matching "[0-9]+")
$versions = {}
for $v = $ in $UPGRADES:
$versions.$v = (yes)
2018-12-14 20:21:03 -08:00
for $v = $ in $ACTION_UPGRADES:
$versions.$v = (yes)
2018-12-14 20:21:03 -08:00
$versions = ((keys in $versions) sorted by $ -> ($ as list))
for $ver in $versions:
if (($ver as list) <= ($start_version as list)): do next $ver
if (($ver as list) > ($end_version as list)): stop $ver
if $ACTION_UPGRADES.$ver:
2018-12-30 19:04:34 -08:00
$tree =
2018-12-14 20:21:03 -08:00
$tree with $ ->:
if (($ is "Action" syntax tree) and $ACTION_UPGRADES.$ver.($.stub)):
2018-12-30 19:04:34 -08:00
$with_upgraded_args = {
: for $k = $v in $:
add $k = ($v upgraded from $start_version to $end_version)
2018-12-30 19:04:34 -08:00
}
2018-12-14 20:21:03 -08:00
set $with_upgraded_args's metatable to ($'s metatable)
return ($ACTION_UPGRADES.$ver.($.stub) $with_upgraded_args $end_version)
2018-12-14 20:21:03 -08:00
if $UPGRADES.$ver:
2018-12-30 19:04:34 -08:00
$with_upgraded_args = {
: for $k = $v in $tree:
add $k = ($v upgraded from $start_version to $end_version)
2018-12-30 19:04:34 -08:00
}
2018-12-14 20:21:03 -08:00
set $with_upgraded_args's metatable to ($tree's metatable)
$tree = ($UPGRADES.$ver $with_upgraded_args $end_version)
2018-11-26 16:28:06 -08:00
2018-12-14 20:21:03 -08:00
if ($tree.version != $end_version):
$tree = (SyntaxTree {: for $k = $v in $tree: add $k = $v})
$tree.version = $end_version
if $tree.shebang:
$tree.shebang = "#!/usr/bin/env nomsu -V\$end_version\n"
2018-11-26 16:28:06 -08:00
2018-12-14 20:21:03 -08:00
return $tree
2018-12-30 19:04:34 -08:00
externally ($tree upgraded from $start_version) means
2018-12-14 20:21:03 -08:00
$tree upgraded from $start_version to (Nomsu version)
2018-12-30 19:04:34 -08:00
externally ($tree upgraded to $end_version) means
2018-12-14 20:21:03 -08:00
$tree upgraded from ($tree.version or (Nomsu version)) to $end_version
2018-12-30 19:04:34 -08:00
externally ($tree upgraded) means
2018-12-14 20:21:03 -08:00
$tree upgraded from ($tree.version or (Nomsu version)) to (Nomsu version)
2018-12-14 20:21:03 -08:00
externally (use $path from version $version) means:
for $filename in (files for $path):
if (=lua "LOADED[\$filename]"):
do next $filename
$file = (read file $filename)
$tree = (parse $file from $filename)
$tree = (upgrade $tree from $version)
run tree $tree