Added underscores for numbers (e.g. 1_000, 0xDEAD_BEEF), and fixed some

compatibility and decompiling issues.
This commit is contained in:
Bruce Hill 2019-02-06 14:31:14 -08:00
parent 5d5dea4639
commit 9e9bcad6fa
6 changed files with 42 additions and 33 deletions

View File

@ -17,7 +17,7 @@ external:
$ACTION_UPGRADES.$version.$stub = $upgrade_fn $ACTION_UPGRADES.$version.$stub = $upgrade_fn
(upgrade $tree to $version as $body) parses as (upgrade $tree to $version as $body) parses as
upgrade to $version via (($ $end_version) -> ($ with $tree -> $body)) upgrade to $version via (($ $end_version) -> ($, with ($tree -> $body)))
(upgrade action $actions to $version as $body) compiles to: (upgrade action $actions to $version as $body) compiles to:
if ($actions is "Action" syntax tree): if ($actions is "Action" syntax tree):
@ -70,12 +70,12 @@ external:
$tree upgraded to $end_version from $start_version $tree upgraded to $end_version from $start_version
] all mean: ] all mean:
unless ($tree is syntax tree): return $tree unless ($tree is syntax tree): return $tree
($ver as list) means (($ as number) for $ in $ver matching "[0-9]+") ($ver as version list) means (($ as number) for $ in $ver matching "[0-9]+")
(Ver $) means: (Ver $) means:
[$lib, $ver] = ($, match "(.*)/([0-9.]+)") [$lib, $ver] = ($, match "(.*)/([0-9.]+)")
if $lib: if $lib:
return {.lib = $lib, .version = ($ver as list)} return {.lib = $lib, .version = ($ver as version list)}
return {.version = ($ as list)} return {.version = ($ as version list)}
$start = (Ver $start_version) $start = (Ver $start_version)
$end = (Ver $end_version) $end = (Ver $end_version)
$end.lib or= $start.lib $end.lib or= $start.lib
@ -89,20 +89,21 @@ external:
$versions.$v = (yes) $versions.$v = (yes)
$versions = [: for $v = $ in $versions: if ((Ver $v).lib == $start.lib): add $v] $versions = [: for $v = $ in $versions: if ((Ver $v).lib == $start.lib): add $v]
sort $versions by $ -> ($ as list) sort $versions by $ -> ($ as version list)
for $ver in $versions: for $ver in $versions:
if (($ver as list) <= $start.version): do next $ver if (($ver as version list) <= $start.version): do next $ver
if (($ver as list) > $end.version): stop $ver if (($ver as version list) > $end.version): stop $ver
if $ACTION_UPGRADES.$ver: if $ACTION_UPGRADES.$ver:
$tree = $tree =
$tree with $ ->: $tree, with
if (($ is "Action" syntax tree) and $ACTION_UPGRADES.$ver.($.stub)): $ ->:
$with_upgraded_args = { if (($ is "Action" syntax tree) and $ACTION_UPGRADES.$ver.($.stub)):
: for $k = $v in $: $with_upgraded_args = {
add $k = ($v upgraded from $start_version to $end_version) : for $k = $v in $:
} add $k = ($v upgraded from $start_version to $end_version)
set $with_upgraded_args's metatable to ($'s metatable) }
return ($ACTION_UPGRADES.$ver.($.stub) $with_upgraded_args $end_version) set $with_upgraded_args's metatable to ($'s metatable)
return ($ACTION_UPGRADES.$ver.($.stub) $with_upgraded_args $end_version)
if $UPGRADES.$ver: if $UPGRADES.$ver:
$with_upgraded_args = { $with_upgraded_args = {

View File

@ -180,14 +180,13 @@ number <-
hex_integer / real_number / integer hex_integer / real_number / integer
integer (Number) <- integer (Number) <-
(((%at_break "-")? [0-9]+)-> tonumber) { (%at_break "-")? [0-9]+ (("_"+->"") [0-9]+)* }
hex_integer (Number) <- hex_integer (Number) <-
(((%at_break "-")? "0x" [0-9a-fA-F]+)-> tonumber) { (%at_break "-")? "0x" [0-9a-fA-F]+ (("_"+->"") [0-9a-fA-F]+)* }
{:hex: '' -> 'yes' :}
real_number (Number) <- real_number (Number) <-
(((%at_break "-")? [0-9]+ "." [0-9]+)-> tonumber) { (%at_break "-")? [0-9]+ (("_"+->"") [0-9]+)* "." [0-9]+ (("_"+->"") [0-9]+)* }
variable (Var) <- "$" ({ident_char+} / "(" ws* (inline_action / variable) ws* ")" / {''}) variable (Var) <- "$" ({ident_char+} / "(" ws* (inline_action / variable) ws* ")" / {''})

View File

@ -406,7 +406,8 @@ compile = function(self, tree)
end end
return lua return lua
elseif "Number" == _exp_0 then elseif "Number" == _exp_0 then
return LuaCode:from(tree.source, tostring(tree[1])) local number = tostring(tree[1]):gsub("_", "")
return LuaCode:from(tree.source, number)
elseif "Var" == _exp_0 then elseif "Var" == _exp_0 then
return LuaCode:from(tree.source, tree:as_var():as_lua_id()) return LuaCode:from(tree.source, tree:as_var():as_lua_id())
elseif "FileChunks" == _exp_0 then elseif "FileChunks" == _exp_0 then

View File

@ -326,7 +326,8 @@ compile = (tree)=>
return lua return lua
when "Number" when "Number"
return LuaCode\from(tree.source, tostring(tree[1])) number = tostring(tree[1])\gsub("_", "")
return LuaCode\from(tree.source, number)
when "Var" when "Var"
return LuaCode\from(tree.source, tree\as_var!\as_lua_id!) return LuaCode\from(tree.source, tree\as_var!\as_lua_id!)

View File

@ -19,11 +19,11 @@ local operator_patt = operator_char ^ 1 * -1
local identifier_patt = (R("az", "AZ", "09") + P("_") + (-operator_char * utf8_char_patt)) ^ 1 * -1 local identifier_patt = (R("az", "AZ", "09") + P("_") + (-operator_char * utf8_char_patt)) ^ 1 * -1
local is_operator local is_operator
is_operator = function(s) is_operator = function(s)
return type(s) == 'string' and operator_patt:match(s) return type(s) == 'string' and not not operator_patt:match(s)
end end
local is_identifier local is_identifier
is_identifier = function(s) is_identifier = function(s)
return type(s) == 'string' and identifier_patt:match(s) return type(s) == 'string' and not not identifier_patt:match(s)
end end
local can_be_unary local can_be_unary
can_be_unary = function(t) can_be_unary = function(t)
@ -71,7 +71,7 @@ tree_to_inline_nomsu = function(tree)
if type(tree[i - 1]) == 'string' then if type(tree[i - 1]) == 'string' then
clump_words = is_operator(bit) ~= is_operator(tree[i - 1]) clump_words = is_operator(bit) ~= is_operator(tree[i - 1])
else else
clump_words = bit == "'" clump_words = bit == "'" and type(tree[i - 1]) ~= 'string'
end end
if i > 1 and not clump_words then if i > 1 and not clump_words then
nomsu:add(" ") nomsu:add(" ")
@ -235,13 +235,16 @@ tree_to_inline_nomsu = function(tree)
end end
return nomsu return nomsu
elseif "Number" == _exp_0 then elseif "Number" == _exp_0 then
local n = tostring(tree[1])
local s local s
if tree.hex and tree[1] < 0 then if n:match("^-*0x") then
s = n:upper():gsub("0X", "0x")
elseif tree.hex and tonumber((n:gsub("_", ""))) < 0 then
s = ("-0x%X"):format(-tree[1]) s = ("-0x%X"):format(-tree[1])
elseif tree.hex then elseif tree.hex then
s = ("0x%X"):format(tree[1]) s = ("0x%X"):format(tree[1])
else else
s = tostring(tree[1]) s = n
end end
return NomsuCode:from(tree.source, s) return NomsuCode:from(tree.source, s)
elseif "Var" == _exp_0 then elseif "Var" == _exp_0 then
@ -449,7 +452,7 @@ tree_to_nomsu = function(tree)
if #word_buffer > 0 then if #word_buffer > 0 then
local words = table.concat(word_buffer) local words = table.concat(word_buffer)
if next_space == " " then if next_space == " " then
if nomsu:trailing_line_len() + #words > MAX_LINE and nomsu:trailing_line_len() > 8 then if nomsu:trailing_line_len() + #words > MAX_LINE + 8 and nomsu:trailing_line_len() > 8 then
next_space = "\n.." next_space = "\n.."
elseif word_buffer[1] == "'" then elseif word_buffer[1] == "'" then
next_space = "" next_space = ""

View File

@ -16,10 +16,10 @@ operator_patt = operator_char^1 * -1
identifier_patt = (R("az","AZ","09") + P("_") + (-operator_char*utf8_char_patt))^1 * -1 identifier_patt = (R("az","AZ","09") + P("_") + (-operator_char*utf8_char_patt))^1 * -1
is_operator = (s)-> is_operator = (s)->
return type(s) == 'string' and operator_patt\match(s) return type(s) == 'string' and not not operator_patt\match(s)
is_identifier = (s)-> is_identifier = (s)->
return type(s) == 'string' and identifier_patt\match(s) return type(s) == 'string' and not not identifier_patt\match(s)
can_be_unary = (t)-> can_be_unary = (t)->
t.type == "Action" and #t == 2 and is_operator(t[1]) and type(t[2]) != 'string' and t[2].type != "Block" t.type == "Action" and #t == 2 and is_operator(t[1]) and type(t[2]) != 'string' and t[2].type != "Block"
@ -51,7 +51,7 @@ tree_to_inline_nomsu = (tree)->
num_words += 1 num_words += 1
clump_words = if type(tree[i-1]) == 'string' clump_words = if type(tree[i-1]) == 'string'
is_operator(bit) != is_operator(tree[i-1]) is_operator(bit) != is_operator(tree[i-1])
else bit == "'" else bit == "'" and type(tree[i-1]) != 'string'
nomsu\add " " if i > 1 and not clump_words nomsu\add " " if i > 1 and not clump_words
nomsu\add bit nomsu\add bit
else else
@ -175,11 +175,15 @@ tree_to_inline_nomsu = (tree)->
return nomsu return nomsu
when "Number" when "Number"
s = if tree.hex and tree[1] < 0 -- Preserve original formatting, just make sure 0xdead_beef -> 0xDEAD_BEEF
n = tostring(tree[1])
s = if n\match("^-*0x")
n\upper!\gsub("0X", "0x")
elseif tree.hex and tonumber((n\gsub("_",""))) < 0
("-0x%X")\format(-tree[1]) ("-0x%X")\format(-tree[1])
elseif tree.hex elseif tree.hex
("0x%X")\format(tree[1]) ("0x%X")\format(tree[1])
else tostring(tree[1]) else n
return NomsuCode\from(tree.source, s) return NomsuCode\from(tree.source, s)
when "Var" when "Var"
@ -334,7 +338,7 @@ tree_to_nomsu = (tree)->
if #word_buffer > 0 if #word_buffer > 0
words = table.concat(word_buffer) words = table.concat(word_buffer)
if next_space == " " if next_space == " "
if nomsu\trailing_line_len! + #words > MAX_LINE and nomsu\trailing_line_len! > 8 if nomsu\trailing_line_len! + #words > MAX_LINE + 8 and nomsu\trailing_line_len! > 8
next_space = "\n.." next_space = "\n.."
elseif word_buffer[1] == "'" elseif word_buffer[1] == "'"
next_space = "" next_space = ""