ASCII art diagram converter: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output section, used a better glyph for echoed output.
(→‎{{header|Perl 6}}: Make grammar more tolerant of whitespace, add some whitespace to demo; rearrange to make more modular)
m (→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output section, used a better glyph for echoed output.)
Line 691:
 
=={{header|REXX}}==
Some code was added to validate the input file.
<lang rexx>/*REXX pgmprogram intreprets an ASCII art diagram for names & and their bit length(s). */
numeric digits 100 /*be able to handle large numbers*/
parsenumeric digits 100 arg iFID test . /*optionalbe input─FIDable &to test─datahandle large numbers. */
parse arg iFID test . /*obtain optional input─FID & test─data*/
if iFID=='' | iFID==',' then iFID= 'ASCIIART.TXT' /*use default iFID*/
if testiFID=='' | testiFID=='",'" then testiFID= 'cafe8050800000808080000aASCIIART.TXT' /*datause the default iFID.*/
if test=='' | test=="," then test= 'cafe8050800000808080000a' /* " " " data.*/
e = '***error*** illegal input txt' /*literal used for error messages*/
w=0; e wb=0; '***error*** !.=0;illegal input $= txt' /*Wa (maxliteral widthused for error messages. name), bits, names*/
p.w=0; p.0 wb=10; !.=0; $= /*P.αW is structure(max bitwidth positionname), bits, names. */
p.=0; p.0=1 /*P.α [↓] readis thestructure inputbit text fileposition. */
/*stick a[↓] fork inread it,the we'reinput donetext file. */</lang>
do j=1 while lines(iFID)\==0; q=linein(iFID); say '═════text=■■■■■text►'q
q=strip(q) /*strip leading & trailing blanks*/
_Lq=leftstrip(q,1); _R=right(q,1) /*getstrip extremeleading leftand &trailing right charsblanks. */
if q=_L=''left(q, 1); then_R=right(q, 1) iterate /*skip ifget thisextreme isleft aand blankright record.characters*/
if q=='' then iterate /*skip if this is a blank record. */
 
if _L=='+' then do /*is this record an "in-between" ? */
if verify(q, '+-')\==0 then say e ' "(invalid grid):'" q
iterate /*skip this record, it's a single "+". */
end
 
if _L\=='|' | _R\=='"|'" then do; say e '(boundry): ' q; iterate; end
 
do until q=='|' /* [↓] parse a record for names. */
parse var q '|' n ' "|'" -1 q; x=n; n=strip(n); w=max(w, length(n))
if n=='' then leave /*Is N is null? We're done scanning. */
if words(n)\==1 then do; say e'(invalid name): ' n; iterate j; end
/* [↑] add more name validations. */
$$=$; nn=n; upper $$ nnn /*N can be a mixed─case name. */
if wordpos(nn, $$)\==0 then do; say e '(dup name):' n; iterate j; end
$=$ n /*add the N (name) to the $ list. */
#=words($); !.#= (length(x) + 1) % 3 /*assign the number of bits for N. */
wb=max(wb, !.#) /*find the maximum number of bits. */
prev= # - 1 /*#number of names previous to this name. */
p.#= p.prev + !.prev /*calculate the structure bit posposition.*/
end /*until q==···*/
end /*j*/
 
if j==1 then do; say e '(file not found): ' iFID; exit 12; end
say
do k=1 for words($)
say right( word($, k), w)right(!.k, 4) "bits, bit position:"right(p.k, 5)
end /*k*/
say /* [↓] Any (hex) data to test? */
if test=='' then exit /*stick a fork in it, we're all done. */
bits=x2b(test); L=length(test) /*convert test data to a bit strstring. */
wm=length( x2d( b2x( copies(1, wb) ) ) ) + 1 /*used for displaying maximum #snumbers. */
say 'test (hex)=' test " length="L 'hexadecimal digits.'
say
do r=1 by 8+8 to L*4
_1=substr(bits, r, 8, 0); _2=substr(bits, r+8, 8, 0)
say 'test (bit)=' _1 _2 '" hex='" b2x(_1) b2x(_2)
end /*r*/
say
do m=1 for words($) /* [↓] showdisplay hex strstring in lowercase*/
_=translate( b2x( substr( bits, p.m, !.m) ), 'abcdef', "ABCDEF" )
say right( word($, m), w+2) ' decimal='right( x2d(_), wm) ' hex=' _
end /*m*/ /*stick a fork in it, we're all done. */</lang>
end /*m*/
'''{{out|output'''|text=&nbsp; when using the default input file:}}
/*stick a fork in it, we're done.*/</lang>
'''output''' when using the default input file:
<pre>
■■■■■text►
═════text=
■■■■■text►
═════text=
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
═════text=■■■■■text► | ID |
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
═════text=■■■■■text► |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
═════text=■■■■■text► | QDCOUNT |
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
═════text=■■■■■text► | ANCOUNT |
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
═════text=■■■■■text► | NSCOUNT |
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
═════text=■■■■■text► | ARCOUNT |
═════text=■■■■■text► +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
■■■■■text►
═════text=
■■■■■text►
═════text=
 
ID 16 bits, bit position: 1
QR 1 bits, bit position: 17
OpcodeOPCODE 4 bits, bit position: 18
AA 1 bits, bit position: 22
TC 1 bits, bit position: 23
Line 793:
ID decimal= 51966 hex= cafe
QR decimal= 1 hex= 1
OpcodeOPCODE decimal= 0 hex= 0
AA decimal= 0 hex= 0
TC decimal= 0 hex= 0
Line 803:
ANCOUNT decimal= 128 hex= 0080
NSCOUNT decimal= 32896 hex= 8080
ARCOUNT decimal= 10 hex= 000a
</pre>