ASCII art diagram converter: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 47:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program asciiDiagram64.s */
Line 557:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI or android 32 bits */
/* program asciiDiagram.s */
Line 1,015:
=={{header|C}}==
interpret text diagram as data structure
<syntaxhighlight lang=C"c">
#include <stdlib.h>
#include <stdio.h>
Line 1,166:
The ASCII diagram is parsed at compile time into a data structure that is used at runtime to encode and decode the fields.
 
<syntaxhighlight lang="cpp">#include <array>
#include <bitset>
#include <iostream>
Line 1,350:
 
An invalid table will cause a compilation error - here the ANCOUNT field is missing.
<syntaxhighlight lang="cpp"> static constexpr char missingFieldArt[] = R"(
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
Line 1,380:
=={{header|D}}==
This solution generates anonymous struct code at compile-time, that can be mixed-in inside a struct or class.
<syntaxhighlight lang="d">string makeStructFromDiagram(in string rawDiagram) pure @safe {
import std.conv: text;
import std.format: format;
Line 1,594:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,787:
===Variant based on parsing===
 
<syntaxhighlight lang="haskell">import Text.ParserCombinators.ReadP
import Control.Monad (guard)
 
Line 1,917:
===List-based interpretation with validation===
 
<syntaxhighlight lang="haskell">import Data.List (nub)
import Data.List.Split (splitOn)
import Control.Monad (unless)
Line 1,949:
=={{header|J}}==
 
<syntaxhighlight lang=J"j">require'strings'
 
soul=: -. {.
Line 1,986:
Sample definition (note the deliberate introduction of extraneous whitespace in locations the task requires us to ignore it.
 
<syntaxhighlight lang="j">sample=: 0 :0
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
Line 2,011:
Example data for sample definition:
 
<syntaxhighlight lang=J"j">
4095 13 5 6144 4096 'ID Opcode RCODE ARCOUNT QDCOUNT' set start
4095 0 13 0 0 0 0 0 5 4096 0 0 6144
Line 2,035:
Separate methods to validate, display, decode ASCII art, and decode hex value.
 
<syntaxhighlight lang=Java"java">
import java.math.BigInteger;
import java.util.ArrayList;
Line 2,252:
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">// ------------------------------------------------------------[ Boilerplate ]--
const trimWhitespace = s => s.trim();
const isNotEmpty = s => s !== '';
Line 2,412:
=={{header|Julia}}==
The validator() function can be customized. The one used only checks length.
<syntaxhighlight lang="julia">diagram = """
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
Line 2,517:
=={{header|Lua}}==
Provided mainly to illustrate the string-parsing aspect, not necessarily the bit-structure aspect...
<syntaxhighlight lang="lua">local function validate(diagram)
local lines = {}
for s in diagram:gmatch("[^\r\n]+") do
Line 2,599:
Of course, the second mechanism may be used for all cases. The first mechanism is mainly interesting to show how it is possible, using Nim powerful macro system, to create a type ''ex nihilo''.
 
<syntaxhighlight lang=Nim"nim">import macros
import strutils
import tables
Line 3,137:
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(import (owl parse))
 
Line 3,238:
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 3,317:
=={{header|Phix}}==
Should work on any width, but didn't actually test, or verify width is 8/16/32/64.
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">interpret</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
Line 3,461:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
"""
http://rosettacode.org/wiki/ASCII_art_diagram_converter
Line 3,690:
<b><code>ascii-art-parser.rkt</code></b>
Note that this is in the <code>racket/base</code> language so it doesn't overburden the modules that import it, especially since they're at the suntax phase.
<syntaxhighlight lang="racket">#lang racket/base
(require (only-in racket/list drop-right)
(only-in racket/string string-trim))
Line 3,751:
 
<b><code>ascii-art-reader.rkt</code></b>
<syntaxhighlight lang="racket">#lang racket
(require (for-syntax "ascii-art-parser.rkt"))
(require (for-syntax racket/syntax))
Line 3,821:
 
<b><code>test-ascii-art-reader.rkt</code></b>
<syntaxhighlight lang="racket">#lang racket
(require "ascii-art-reader.rkt")
(require "ascii-art-parser.rkt")
Line 3,932:
{{works with|Rakudo|2018.05}}
 
<syntaxhighlight lang=perl6"raku" line>grammar RFC1025 {
rule TOP { <.line-separator> [<line> <.line-separator>]+ }
rule line-separator { <.ws> '+--'+ '+' }
Line 4,028:
=={{header|REXX}}==
Some code was added to the REXX program to validate the input file.
<syntaxhighlight lang="rexx">/*REXX program interprets an ASCII art diagram for names and their bit length(s).*/
numeric digits 100 /*be able to handle large numbers. */
er= '***error*** illegal input txt' /*a literal used for error messages. */
Line 4,141:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">header = <<HEADER
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
Line 4,211:
See the output below the source code.
 
<syntaxhighlight lang=Rust"rust">use std::{borrow::Cow, io::Write};
 
pub type Bit = bool;
Line 4,675:
 
In this implementation, '''parse''' produces a dictionary from names to bit-lengths. '''encode''' and '''decode''' use these to produce appropriate binary format strings, and then do what they say on the tin. As implemented, this is limited to unsigned numeric values in fields. Supporting unsigned values, strings and enums would require parsing a more complex annotation than only the ASCII art packet structure, but ought not take much more code.
<syntaxhighlight lang=Tcl"tcl">
namespace eval asciipacket {
proc assert {expr} { ;# for "static" assertions that throw nice errors
Line 4,755:
</syntaxhighlight>
And here is how to use it with the original test data:
<syntaxhighlight lang=Tcl"tcl">
proc test {} {
set header {
Line 4,816:
=={{header|Vlang}}==
{{trans|go}}
<syntaxhighlight lang="vlang">import math.big
import strings
 
Line 4,949:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascript">import "/dynamic" for Tuple
import "/fmt" for Fmt
import "/big" for BigInt
10,333

edits