User Tools

Site Tools


useful_programs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
useful_programs [2019/07/31 12:56] – [Bit functions] emozolyakuseful_programs [2019/09/16 15:10] – [Bit functions] emozolyak
Line 47: Line 47:
 end  end 
    
-function outbit(condition, x, b) -- output bool conditoin as 0 /1 into a bit +function outbit(condition, x, b) -- output bool condition as 0 /1 into a bit 
      if condition then           if condition then     
          return hasbit(x, b) and x or x + bw(b)           return hasbit(x, b) and x or x + bw(b) 
Line 53: Line 53:
          return hasbit(x, b) and x - bw(b) or x           return hasbit(x, b) and x - bw(b) or x 
      end       end 
 +end 
 +
 +function outBit(condition, alias, b) -- output bool condition as 0 /1 into a bit of a internal register 
 +     local new_value = outbit(condition, R(alias), b)
 +     return new_value
 end  end 
    
Line 135: Line 140:
  
 <code lua> <code lua>
-function DBG ( ... ) +function DBG( ...) -- ... accepts multiple arguments in a table arg  
-            -- ... means variable number of arguments in lua, passed in the 'arg' table+ 
 +local table_sign = '|' -- if there is divider sign, the values will be grouped in 2 rows 
 +local table_sign_found = false         
 +local tab_s_index = 0 
  
 if ENABLE_DEBUG then -- should be global in the calling script  if ENABLE_DEBUG then -- should be global in the calling script 
 +
 +    -- find divider
 +    for i = 1, #arg do 
 +        if arg[i] == table_sign then 
 +            table_sign_found = true 
 +            tab_s_index = i 
 +            break 
 +        end 
 +    end 
          
-    local ""            -- string buffer to concatenate all arguments +    if not table_sign_found then   
-        for = 1 , #arg do  +        INFO(tabToStr(arg)) -- outputs table values in a single row  
-        -- glue all and add spaces, use tostring to protect from nil argument  +    else  
-            s .. tostring ( arg [i ) .. ' ' +        local header_t, value_t {}, {} -- prepare header and value rows 
 +          
 +        for = 1, tab_s_index - 1 do  
 +            header_t[k] = arg[k]
         end          end 
-    DEBUG +        for j = tab_s_index + 1, #arg do  
-end -- if +            value_t[j - tab_s_index] = tostring(arg[j]
 +        end  
 +         
 +        if (#header_t ~= #value_t) then  
 +             ERROR("column count differs!"
 +        else  
 +            for g = 1, #header_t do  
 +                local delta = #header_t[g] #value_t[g] -- makes aligment by adding spaces to shorter strings... 
 +                if delta ~= 0 then  
 +                    if (delta > 0) then  
 +                        value_t[g] = value_t[g] .. string.rep(' ', delta)  
 +                    else  
 +                        header_t[g] = header_t[g] .. string.rep(' ', math.abs(delta))  
 +                    end  
 +                end  
 +            end -- for  
 +            INFO(tabToStr(header_t)) ; INFO(tabToStr(value_t)) 
 +        end  
 +    end  
 +end -- if ENABLE_DEBUG 
 + 
 +return true  
 +end -- DBG 
 + 
 +function tabToStr(t)  -- glue all and add spaces, use tostring to protect from nil and bool argument  
 +    local s = ""             
 +    for i = 1, #t do  
 +        s = s .. tostring(t[i]) .. ' '  
 +    end -- for  
 +    return s  
 +end 
  
-end -- DBG  
 </code> </code>
 +Then the print output can be enhanced like this:
 +{{ :dbg_table_out.png?direct&600 |}}
  
 The //ENABLE_DEBUG// flag should be global boolean variable in the calling script. You may just to set it when needed and save the script.  The //ENABLE_DEBUG// flag should be global boolean variable in the calling script. You may just to set it when needed and save the script. 
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki