ASCII art diagram converter: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added/changed comments and whitespace, used a template for the output section, used a better glyph for echoed output.) |
|||
Line 440: | Line 440: | ||
NSCOUNT, 16 bits: 0001101111110001 |
NSCOUNT, 16 bits: 0001101111110001 |
||
ARCOUNT, 16 bits: 0110100110100100</pre> |
ARCOUNT, 16 bits: 0110100110100100</pre> |
||
=={{header|Phix}}== |
|||
Should work on any width, but didn't actually test, or verify width is 8/16/32/64. |
|||
<lang Phix>function interpret(sequence lines) |
|||
if remainder(length(lines),2)!=1 then |
|||
crash("missing header/footer?") |
|||
end if |
|||
string l1 = lines[1] |
|||
integer w = length(l1) -- sug: check this is 8/16/32/64 |
|||
integer bits = (w-1)/3 |
|||
if l1!=join(repeat("+",bits+1),"--") then |
|||
crash("malformed header?") |
|||
end if |
|||
sequence res = {} |
|||
integer offset = 0 |
|||
for i=1 to length(lines) do |
|||
string li = lines[i] |
|||
if remainder(i,2) then |
|||
if li!=l1 then |
|||
crash("missing separator (line %d)?",{i}) |
|||
end if |
|||
else |
|||
if li[1]!='|' or li[w]!='|' then |
|||
crash("missing separator on line %d",{i}) |
|||
end if |
|||
integer k = 1 |
|||
while true do |
|||
integer l = find('|',li,k+1) |
|||
string desc = trim(li[k+1..l-1]) |
|||
{k,l} = {l,(l-k)/3} |
|||
res = append(res,{desc,l,offset}) |
|||
offset += l |
|||
if k=w then exit end if |
|||
end while |
|||
end if |
|||
end for |
|||
res = append(res,{"total",0,offset}) |
|||
return res |
|||
end function |
|||
procedure unpack(string data, sequence res) |
|||
if length(data)*8!=res[$][3] then |
|||
crash("wrong length") |
|||
end if |
|||
string bin = "" |
|||
for i=1 to length(data) do |
|||
bin &= sprintf("%08b",data[i]) |
|||
end for |
|||
printf(1,"\n\nTest bit string:\n%s\n\nUnpacked:\n",{bin}) |
|||
for i=1 to length(res)-1 do |
|||
{string name, integer bits, integer offset} = res[i] |
|||
printf(1,"%7s, %02d bits: %s\n",{name,bits,bin[offset+1..offset+bits]}) |
|||
end for |
|||
end procedure |
|||
function trimskip(string diagram) |
|||
-- |
|||
-- split's ",no_empty:=true)" is not quite enough here. |
|||
-- Note that if copy/paste slips in any tab characters, |
|||
-- it will most likely trigger a length mismatch error. |
|||
-- |
|||
sequence lines = split(diagram,'\n') |
|||
integer prevlli = 0 |
|||
for i=length(lines) to 1 by -1 do |
|||
string li = trim(lines[i]) |
|||
integer lli = length(li) |
|||
if lli then |
|||
if prevlli=0 then |
|||
prevlli = lli |
|||
elsif lli!=prevlli then |
|||
crash("mismatching lengths") |
|||
end if |
|||
lines[i] = li |
|||
else |
|||
lines[i..i] = {} |
|||
end if |
|||
end for |
|||
return lines |
|||
end function |
|||
constant diagram = """ |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
| ID | |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
|QR| Opcode |AA|TC|RD|RA| Z | RCODE | |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
| QDCOUNT | |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
| ANCOUNT | |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
| NSCOUNT | |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
| ARCOUNT | |
|||
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
|||
""" |
|||
sequence lines = trimskip(diagram) |
|||
sequence res = interpret(lines) |
|||
printf(1,"--Name-- Size Offset\n") |
|||
for i=1 to length(res) do |
|||
printf(1," %-7s %2d %5d\n",res[i]) |
|||
end for |
|||
unpack(x"78477bbf5496e12e1bf169a4",res)</lang> |
|||
{{out}} |
|||
<pre> |
|||
--Name-- Size Offset |
|||
ID 16 0 |
|||
QR 1 16 |
|||
Opcode 4 17 |
|||
AA 1 21 |
|||
TC 1 22 |
|||
RD 1 23 |
|||
RA 1 24 |
|||
Z 3 25 |
|||
RCODE 4 28 |
|||
QDCOUNT 16 32 |
|||
ANCOUNT 16 48 |
|||
NSCOUNT 16 64 |
|||
ARCOUNT 16 80 |
|||
total 0 96 |
|||
Test bit string: |
|||
011110000100011101111011101111110101010010010110111000010010111000011011111100010110100110100100 |
|||
Unpacked: |
|||
ID, 16 bits: 0111100001000111 |
|||
QR, 01 bits: 0 |
|||
Opcode, 04 bits: 1111 |
|||
AA, 01 bits: 0 |
|||
TC, 01 bits: 1 |
|||
RD, 01 bits: 1 |
|||
RA, 01 bits: 1 |
|||
Z, 03 bits: 011 |
|||
RCODE, 04 bits: 1111 |
|||
QDCOUNT, 16 bits: 0101010010010110 |
|||
ANCOUNT, 16 bits: 1110000100101110 |
|||
NSCOUNT, 16 bits: 0001101111110001 |
|||
ARCOUNT, 16 bits: 0110100110100100 |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |