RosettaCodeData/Task/Abbreviations-simple/Pluto/abbreviations-simple.pluto
2026-04-30 12:34:36 -04:00

102 lines
4.2 KiB
Text

do -- Abbreviatiions Simple - expand command name abbreviations
-- returns the table of space delimited words in s
local function getWords( s : string ) : table
local t, cmds, w = s, {}, s
while t:contains( " " ) do
w, t = t:partition( " " )
if w != "" then
cmds[ # cmds + 1 ] = w
end
end
if t != "" then
cmds[ # cmds + 1 ] = t
end
return cmds
end
local commandStrings
= "add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3"
.. " compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate"
.. " 3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2"
.. " forward 2 get help 1 hexType 4 input 1 powerInput 3 join 1 split 2 spltJOIN load"
.. " locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2 macro merge 2 modify 3 move 2"
.. " msg next 1 overlay 1 parse preserve 4 purge 3 put putD query 1 quit read recover 3"
.. " refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left"
.. " 2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1"
local function expandEasyAbbreviations( cmds : string, input : string ) : table
local cmdTable, inputTable, outputTable = getWords( cmds ), getWords( input ), {}
local function isCommand( cmd : string, word : string ) : boolean
local result, uCmd, uWord = false, cmd:upper(), word:upper()
if # cmd >= # word then -- word is not longer than the command
if uCmd == uWord then -- the word is the command
result = true
else -- word is shorter than the command
local ab = cmd:gsub( "%l", "" ) -- remove lowercase
if # ab <= # word then -- the minimum abbreviation isn't longer than the word
result = uWord == uCmd:sub( 1, # word )
end
end
end
return result
end
for i = 1, # inputTable do
local w, u = inputTable[ i ], "*error*"
for c = 1, # cmdTable do
if isCommand( cmdTable[ c ], w ) then
u = cmdTable[ c ]:upper()
break
end
end
outputTable[ # outputTable + 1 ] = u
end
return { inputTable, outputTable }
end
local function printExpansion( expansion : table )
local fmt, input, output = {}, expansion[ 1 ], expansion[ 2 ]
for i, v in ipairs( output ) do
fmt[ i ] = " %" .. # v .. "s"
end
io.write( "Input: " )
for i, v in ipairs( input ) do
io.write( string.format( fmt[ i ], v ) )
end
io.write( "\nOutput:" )
for i, v in ipairs( output ) do
io.write( string.format( fmt[ i ], v ) )
end
io.write( "\n" )
end
-- maps a "simple" format command list to "easy" format
local function simpleToEasyFormat( cmds : string ) : string
local words <const> = getWords( cmds )
local wPos, wMax, easy = 1, # words, ""
while wPos <= wMax do
local w = words[ wPos ]
local lenStr <const> = if wPos == wMax then "" else words[ wPos + 1 ] end
if not lenStr:match( "^%d+$" ) then
easy ..= " " .. w:upper()
else
-- have a minimum length
local len <const> = tonumber( lenStr )
easy ..= " " .. w:sub( 1, len ):upper()
if len < # w then easy ..= w:sub( len + 1, -1 ):lower() end
++ wPos
end
++ wPos
end
return easy
end
printExpansion( expandEasyAbbreviations( simpleToEasyFormat( commandStrings )
, ( "riG rePEAT copies put mo rest "
.. "types fup. 6 poweRin"
)
)
)
end