30
\$\begingroup\$

Related to: Make a ;# interpreter and Generate ;# code

;# - A Whirlwind Guide

This is a simple language with two commands. Its only data structure is an accumulator, which is initialized to 0.

  1. ; Increment the accumulator

  2. # Calculate the value of the accumulator modulo 127, and print the corresponding ASCII character. Then, reset the accumulator to 0.

The source code may contain additional characters (printable ASCII + whitespace), but these are treated as comments and have no effect on program execution.

Challenge

Since most computers do not come with ;# preinstalled, it would be very useful to have a tool that can convert ;# code into a different language. In this challenge, you shall write a program to accomplish this.

Input

Some ;# source code, taken via argument or STDIN. This source code may contain (comment) characters other than ; or #.

Output

Code, in the same language as your submission, which, when executed, prints/returns the same string as the original ;# code. This resultant code may output a trailing newline after the target string, if that is more convenient for your language.

Notes

One thing to look out for is escape sequences, such as code that prints backslashes or prints quote marks. Also look out for ;# code that could contain things that look like keywords or commands in your language.

Additional restrictions

All programs must terminate (I normally consider this a default, but someone asked about it so I'm stating it here).

Examples

input: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
output (python): print(";#")
input: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
output (element): \'`
\$\endgroup\$
6
  • 45
    \$\begingroup\$ My computer came with ;# preinstalled... \$\endgroup\$
    – user58826
    May 24, 2017 at 20:56
  • \$\begingroup\$ Does the program have to stop execution? Also, can it print no-ops infinitely after the actual code? \$\endgroup\$ May 24, 2017 at 23:50
  • \$\begingroup\$ Will the program always have at least one # character in it? \$\endgroup\$ May 25, 2017 at 0:54
  • 3
    \$\begingroup\$ @totallyhuman I'm gonna say that all programs must eventually halt. \$\endgroup\$
    – PhiNotPi
    May 25, 2017 at 3:33
  • 1
    \$\begingroup\$ Please could we get a test case where the input contains a character (or characters) that aren't ; or #? \$\endgroup\$
    – mkst
    Aug 10, 2017 at 20:55

44 Answers 44

9
\$\begingroup\$

brainfuck, 126 bytes

+++[->+++++<]>[->++++>+++<<],[+<++++++[-<++++>>------<]>[<]<<[>>>>>[--<--.++>]<+.-<.<<<<]>[->>-<<]>>[[-]<]>[>>++++[-->]<[<]],]

Try it online!

The output program will fail in the TIO implementation if the ;# output exceeds 65536 characters. I also made a 130-byte version which outputs [+] instead of <, avoiding this problem:

++[------>++>+<<]>+++++<,[+<++++++[-<++++>>------<]>[<]<<[>>>>>[--<.>]<+++.---<.>.<++.--<<<<]>[->>-<<]>>[[-]<]>[>>++++[-->]<[<]],]

Explanation

+++[->+++++<]>[->++++>+++<<]        initialize tape with 60 and 45
,[                                  for each input byte:
  +<++++++[-<++++>>------<]         subtract 35 (#) from byte
  >[<]<<[                           if byte was #
         >>>>>[--<--.++>]           output + (43) a number of times equal to accumulator
         <+.-<.<<<<                 output . (46) and < (60)
  ]
  >[->>-<<]>>                       subtract 24 from what's left of byte
  [[-]<]                            check difference and clear if nonzero
  >[                                if byte was originally 59 (;)
   >>++++[-->]<[<]                  add 4 to accumulator cell, then subtract 2 if nonzero. Since BF cells are mod 256, this creates an accumulator mod 127.
  ]
,]
\$\endgroup\$
6
\$\begingroup\$

Python 2, 76 69 bytes

Code

Input is surrounded by quotes.

for y in input('print').split("#")[:-1]:print`chr(y.count(";")%127)`,

Try it online!

Explanation

The first part of the output is essentially done by the input, using input('print'). We split the input on hashtags and discard the last element. We print the representation of ord(y%127), where y is the number of occurrences of the semicolon. We append the , at the end of the print to make sure that this does not print a newline.

This would give the following Python code for the Hello, World!-program:

print'H' 'e' 'l' 'l' 'o' ',' ' ' 'W' 'o' 'r' 'l' 'd' '!'

Which can be tried online.

\$\endgroup\$
5
\$\begingroup\$

Whitespace, 291 bytes

NSSTTNNSTNNSTNNSTTNNSSTSNSSNSNSTNTSTTTSSSTSSSTTNTSSTSNSNTSSSSNSSSTTSSSNTSSTNTSSSTNNSNTSNNSSSSSNSNNNSTSSNNSTSTNNSTSTNNSTSNNSTTNNSTSNNSTNNSTSTNNSTTNNSTNNSTNNSNTTNNSSSSTNNSTSSNNSTSNNSTTNNSTSNNSTSSNNSNTSNNSSTNSSSTSTSNTNSSNTNNSSSSNNSTNNSTNNSSNSSSTSSSSSNTNSSNTNNSSSTNNSTSNNSTSNNSSSNSSSTSSTNTNSSNTN

Replace S by space, T by tab and N by a newline.

Generating whitespace in whitespace is not the most efficient thing in the world. Generating any kind of dynamic code requires significant bit-twiddling which, in a language without bitwise operations, would cause the code size to explode. Therefore, this program does not attempt to do something smart, instead opting for just translating the source program one to one. It disassembles to the following:

early:
    call S
    call S
    call N
start:
    push  0
    dup
    ichr
    get
    push  35
    sub
    dup
    jz    hash
    push  24
    sub
    jz    semi
    jmp   start
hash:
    pop
    call SSS
    call TTT
    call TTT
    call T
    call N
    call T
    call S
    call TTT
    call N
    call S
    call S
    jmp   early
semi:
    call SSS
    call T
    call N
    call T
    call SSS
    jmp   start
N:
    push 10
    pchr
    ret
SSS:
    call S
    call S
S:
    push 32
    pchr
    ret
TTT:
    call T
    call T
T:
    push 9
    pchr
    ret

The code generated by the program looks like:

push  0
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  1
add
push  127
mod
pchr
push  0
push  1
add
...
\$\endgroup\$
2
  • \$\begingroup\$ Doesn't work for me. In the original whitespace interpreter written in Haskell, the sign bit may not be omitted from a number, so "SSN" is not a valid way to push zero. \$\endgroup\$
    – aschepler
    Aug 10, 2017 at 22:29
  • 1
    \$\begingroup\$ Due to the vagueness of the specification of whitespace and the differences between the original reference interpreter and the actual specification, it is hard to judge what was to be the intended behaviour. As far as I remember several example programs listed on the original site actually did require the signless behaviour, and furthermore many other implementations do have it. I ran into these issues several times while building my own ws JIT compiler, and in the end I decided to stick with it for compatability with other implementations \$\endgroup\$ Aug 10, 2017 at 23:10
4
\$\begingroup\$

V, 19 20 28 bytes

Bugfix, broke if there was no # at the end

Bugfix, implemented mod 127

Í;û127}
éiA0Í#/0
ò/;
x

Try it online!

Try Generated Code

Explanation:

Í;û127}                            ' Delete runs of 127 `;`s (mod 127)
éi                                 ' Insert an `i` in front of input
  A<C-v><esc>0<esc>                ' Append <esc>0 to input
                   Í#/<C-v><C-v>0  ' Replace all `#` with `<C-v>0`
ò                                  ' Recursively
 /;                                ' Go to the next `;`
<C-a>                              ' Increment the next number come across

In V, in insert mode, any ASCII character can be inserted by code by using <C-v><Code>. The V code replaces all # with <C-v>0, where the zero is a pseudo-accumulator per #. Each # resets the accumulator to 0 so having one per works out fine. Then the code does an increment for each semicolon found, which just increments the next number it finds, which would be the next accumulator. The 0 is appended to the end so that the instruction doesn't fail for ;s without a following #.

Hexdump:

00000000: e969 4116 1b30 1bcd 232f 1616 300a f22f  .iA..0..#/..0../
00000010: 3b0a 7801                                ;.x.
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 101 100 bytes

s=>`_=>'${s.replace(/[^#;]/g,``)}'.replace(/;*(#?)/g,(l,c)=>c&&String.fromCharCode(~-l.length%127))`

Given an input string, deletes all the unnecessary characters, then returns the source of the following function:

_=>'...'.replace(/;*(#?)/g,(l,c)=>c&&String.fromCharCode(~-l.length%127))

Where ... represents the cleaned ;# source. Edit: Saved 1 byte thanks to @l4m2.

\$\endgroup\$
1
  • \$\begingroup\$ /;*(#|$)/ => /;*(#?)/? \$\endgroup\$
    – l4m2
    May 21, 2021 at 5:06
3
\$\begingroup\$

05AB1E, 20 19 18 16 bytes

-1 thanks to Adnan
-2 thanks to carusocomputing
-2 thanks to Kevin Cruijssen

'#¡¨vy';¢ƵQ%„çJJ

Try it online! (includes output of executed 05AB1E code)

'#¡              # Split on #
   ¨             # remove the last element
    vy           # For each...
      ';¢        #   count the number of ;s
         ƵQ%     #   Mod by 127
            „çJ  #   Push çJ
               J # Join stack and output implicitly
\$\endgroup\$
10
  • \$\begingroup\$ ';¢ can be g, žy pushes 128, may work somehow and why not just spell the full word and surround it by quotes? \$\endgroup\$ May 25, 2017 at 21:54
  • 1
    \$\begingroup\$ I need to use ';¢ incase there are characters other than ;. žy< is the same as 127. Printing the word surrounded by quotes will break if one of the characters is a quote. \$\endgroup\$
    – Riley
    May 25, 2017 at 22:12
  • \$\begingroup\$ @carusocomputing I forgot to ping you... \$\endgroup\$
    – Riley
    May 25, 2017 at 22:21
  • 1
    \$\begingroup\$ @carusocomputing and Riley: ƵQ is a compressed version of 127. \$\endgroup\$
    – Adnan
    May 25, 2017 at 22:38
  • \$\begingroup\$ @Adnan why/how? \$\endgroup\$ Jun 1, 2017 at 16:36
2
\$\begingroup\$

Python 2, 75 bytes

lambda s:"print"+`''.join(chr(x.count(';')%127)for x in s.split('#')[:-1])`

Try it online! (includes output from executing the transpiled Python code)

Thanks to ovs for many many bytes!

Explanation

This program transpiles the #; code by splitting on #s (s.split('#')[:-1]), counting the number of semicolons in each chunk mod 127 (x.count(';')%127for x in ...), and converting that into the respective ASCII character (chr(...)). That list is then concatenated (''.join(...)), converted into a Python representation of the string (the backticks) and inserted into a skeleton Python program for printing strings ("print"+...).

\$\endgroup\$
0
2
\$\begingroup\$

Haskell, 106 102 bytes

';'!(x:a)=x+1:a;'#'!a=0:a;_!a=a;h s="main=putStr"++(show$""++(toEnum.(`mod`127)<$>init(foldr(!)[0]s)))

Try it online!

Ungolfed

step ';' (x:acc) = x+1:acc
step '#' acc = 0:acc
step _ acc = acc;

semicolonHash s = toEnum . (`mod` 127) <$> init (foldr step [0] s)

toHaskell s = "main = putStr " ++ (show $ "" ++ semicolonHash s)
\$\endgroup\$
2
\$\begingroup\$

Cubically, 138 137 bytes

+1/1+54@6:5+1/1+5@6/1+52@6:4/1+5@6:1/1+54@6:4/1+5@6:5+2/1+4@6(~-61/1=7&6+5/1+51=7?6{+3/1+5@6}:5+3/1+3=7?6{+52@6:5+1@6-1@6+1@6:5+2/1+4@6})

Try it online!

Note: You may need to replace &6 with ?6& for it to work on TIO. &6 is in the language spec, though.

How it works

+1/1+54@6        Output 'R'
:5+1/1+5@6       Output '3'
/1+52@6          Output 'D'
:4/1+5@6         Output '1'
:1/1+54@6        Output 'R'
:4/1+5@6         Output '1'
:5+2/1+4@6       Output '+'
(                Loop indefinitely
  ~                Get next character
  -61/1=7&6        If input is -1 (EOF), exit program
  +5/1+51=7?6{     If input is `;`
    +3/1+5@6         Output '0'
  }                End if
  :5+3/1+3=7?6{   If input is '#'
    +52@6            Output '@'
    :5+1@6           Output '6'
    -1@6             Output '-'
    +1@6             Output '6'
    :5+2/1+4@6       Output '+'
  }                End if
)                End loop

Output program:

R3D1R1           Set top face to 1
+00...           Add top face to notepad, aka increment notepad
@6               Output notepad as character
-6               Set notepad to 0
...
\$\endgroup\$
1
  • \$\begingroup\$ Save a lot of bytes removing the arguments from @6, %6 and -6. Commands that previously didn't do anything when called implicitly now use the notepad. So @ is the same as @6, % is the same as %6, etc. \$\endgroup\$
    – MD XF
    Oct 8, 2017 at 21:53
2
\$\begingroup\$

Brachylog, 33 bytes

{";"∧"+₁"|"#"∧"%₁₂₇g~ạw0"}ˢ;"0"↔c

Try it online!

I felt too tired to explain this two years ago, but Razetime reminded me.

                           ;"0"↔c    Prepend a 0 to
{                        }ˢ          each element of the input
     "+₁"                            mapped to +₁
 ";"∧                                if it's a semicolon,
         |"#"                        if it's a hash
             ∧"%₁₂₇g~ạw0"            %₁₂₇g~ạw0,
                          ˢ          or nothing otherwise.

      +₁                             add 1

              %₁₂₇                   mod 127,
                  g~ạ                convert from codepoint to character,
                     w               print,
                      0              start over from 0
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I am droppinga comment to remind unrelated string \$\endgroup\$
    – Razetime
    May 21, 2021 at 3:57
2
\$\begingroup\$

Jelly,  25 24 16  15 bytes

-1 thanks to caird coinheringaahing! (Use of p in place of ;€)

ṣ”#Ṗċ€”;%127p”Ọ

A full program printing equivalent Jelly code (as a monadic link it returns a list of lists of mixed types).

The first example is at Try it online! which yields this program.

How?

Counts up the ;s in each run between #s takes each modulo 127 and appends a cast to ordinal instruction, the monadic atom, after each.

Jelly implicitly pipes each value to STDOUT as it runs through a program like that i.e. 72Ọ101Ọ108Ọ108Ọ111Ọ44Ọ32Ọ119Ọ111Ọ114Ọ108Ọ100Ọ33Ọ would print Hello, world!.

ṣ”#Ṗċ€”;%127p”Ọ - Main link: list of characters
 ”#             - literal '#'
ṣ               - split the result at #s
   Ṗ            - pop (remove the last result, list of trailing non-# characters)
      ”;        - literal ';'
    ċ€          - count for €ach
        %127    - modulo 127 (vectorises)
             ”Ọ - literal 'Ọ' (Jelly's cast to ordinal monadic atom)
            p   - Cartesian product - making a list of lists like [[72,'Ọ'],[101,'Ọ'],...]
                - implicit print (this smashes, printing something like: 72Ọ101Ọ...)

A note regarding input: Jelly can take string input in Python format (it will first attempt to evaluate input as Python code). The empty program may be input as "", and the hash-only programs as "#", "##", etc. Escape sequences are required for input containing backslashes and quotes.

\$\endgroup\$
2
1
\$\begingroup\$

C, 98 96 99 98 97 bytes

+3 bytes because I forgot C isn't interpreted :(

c,a;f(){printf("f(){puts(\"");for(;~c;c=getchar())c==59?a++:c==35?a=!printf(&a):a;puts("\");}");}

Running with:

echo ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#" | ./a.out

Will print:

f(){puts("Hello, World!");}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;# produces f(){puts(""");}, which is invalid. The challenge specifically calls out "One thing to look out for is escape sequences, such as code that prints backslashes or prints quote marks." \$\endgroup\$
    – hvd
    May 25, 2017 at 7:47
  • \$\begingroup\$ @hvd Fixing.... \$\endgroup\$
    – MD XF
    May 25, 2017 at 17:25
1
\$\begingroup\$

Jelly, 26 bytes

”‘x
f”;L%127Ç;“Ọø”
ṣ”#Ç€ṙ-

Try it online!

And try that Jelly code here!

ṣ”#Ç€ṙ-      Main link, input is a string of ";;;;;;#lala;;;;;;;;;#"
ṣ”#          Split input on char #
   ǀ        Call helper link 1 for each segment
     ṙ-      Rotate returns 1 to the right (SPLIT introduces an empty element which is moved up front)

f”;L%127Ç;“Ọø”  Helper link 1, determines the number of increments
f”;             Throw out all but semicolons
   L%127        Take the count mod 127
        Ç       Call helper 2
         ;“Ọø”  Add a Jelly link that prints characters and splits print statements

”‘x             Helper 2, receives the count of ;'s
”‘              Return the character ‘ (Jelly's increment command
  x             Once for each ; in the input

The Jelly output becomes code like Ọø‘‘‘‘‘‘‘‘‘‘‘‘‘Ọø‘‘‘‘‘‘‘‘‘‘Ọø, which prints chr(13)+chr(10)

\$\endgroup\$
2
  • \$\begingroup\$ Weird example to use (printing only white space) that confused me. \$\endgroup\$ May 24, 2017 at 21:35
  • 1
    \$\begingroup\$ @JonathanAllan Added examples with links to TIO. \$\endgroup\$
    – steenbergh
    May 24, 2017 at 21:40
1
\$\begingroup\$

PHP, 72 bytes

for(;~$c=$argn[$i++];)echo[Z=>'$n++;',B=>'echo~$n=~chr($n%127);'][a^$c];
\$\endgroup\$
1
\$\begingroup\$

><>, 106 81 77 bytes

This is my first golf in ><> (fish)! A pretty interesting language I have to say. A lot of fun!

0n01.
>i:1+?!v:";"=?v"#"=?v
^ ;o";"<   .37<
^oo"1+"
^oooooooo'"~"1+%o0' <
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the pond ! You can shorten i:1+?! into i:0(?, and I also feel like you could save a few bytes if you constructed the result on the stack and waited for the end of the input to output it. I mean, that's a lot of os ;) \$\endgroup\$
    – Aaron
    Jun 2, 2017 at 9:51
1
\$\begingroup\$

C# 169 Bytes

Golfed:

class P{static int Main(string[] args){var r="Console.Write(";foreach(var y in args[0].Split('#')){r+=(char)(-1+y.Split(';').Length%127);}System.Console.Write(r+");");}}

Human readable version:

class P
{
    static int Main(string[] args)
    {
        var r="Console.Write(\"";
        foreach (var y in args[0].Split('#'))
        {
            r +=(char)(-1+y.Split(';').Length% 127);
        }
        System.Console.Write(r+"\");");
    }
}
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 17 bytes

⌡(¶{gÉ'8=£♣(%$''\

Try it online!

Explanation

⌡                      decrement twice
 (                     decrement
                       this transforms ";;;#;;#" -> "888 88 "
  ¶                    split string on spaces
   {                   foreach...
    gÉ                 filter using next 3 characters
      '8               push single character "8"
        =              pop(a, b), push(a==b)
                       this filters out all non-";"
         £             length of array/string with pop
          ♣            push 128
           (           decrement
            %          modulo
             $         pop(a), push ord(a) or char(a) (gets character with code)
              ''       push single character "'"
                \      swap top elements

Since any character can be put on the stack (and thus the output) using '<char>, this will output a sequence of such code blocks.

\$\endgroup\$
4
  • \$\begingroup\$ Forgot how to create a chat.. Anyway, ` ` (discard everything except for the top of the stack) is currently bugged. It gives a Python FileNotFoundError. \$\endgroup\$ Mar 28, 2019 at 8:49
  • \$\begingroup\$ @KevinCruijssen Check the README! I switched that character in the code page, to avoid having two space characters. The new character is Þ. \$\endgroup\$
    – maxb
    Mar 28, 2019 at 9:26
  • \$\begingroup\$ Yeah, @JoKing indeed said it was changed to Þ. (The docs I use still state the old value however.) \$\endgroup\$ Mar 28, 2019 at 9:38
  • 1
    \$\begingroup\$ @KevinCruijssen Oh, that doc needs to be updated, thanks for the reminder! I'll try to write a script to keep them both updated. I'd suggest using this one until I've gotten that in place. \$\endgroup\$
    – maxb
    Mar 28, 2019 at 13:06
1
\$\begingroup\$

MATL, 32 28 bytes

35lF4$Yb"@g59=z]xv127\!&D99h

Try it online!

Completely different approach based on strsplit rather than an automaton-type program.

      Yb % strsplit
    4$   %  with 4 inputs, namely:
35       %   Split on '#'
  lF     %   Do not (F) collapse delimiters (l).
         %   And of course the implicit program input.

"@g    ] % Loop over strings, convert each to a normal char array
   59=z  % How many equal to 59 ';'?

xv!127\  % Delete last element, concatenate and flip to horizontal, mod 127
&D       % Convert to MATL representation
  99h    % Append a 'c' to output ASCII.
\$\endgroup\$
1
\$\begingroup\$

Vyxal , 18 bytes

\#€Ṫƛ\;O₇‹%\C+\₴+,

Good, but definitely could be improved. Outputs the codepoint, followed by a C to convert to a character, and then a print symbol.

Try it Online!

\$\endgroup\$
2
  • \$\begingroup\$ 15 bytes \$\endgroup\$
    – lyxal
    May 21, 2021 at 4:06
  • 1
    \$\begingroup\$ @lyxal I don't know how I missed the char for removing elements in a that weren't in b, but I have some questions. Does this code not fail for instances with a single ;, ie: ";#" which should print out byte 1, but your code interprets that as two prints of a null byte. Additionally, using "," as your print would output the characters with new lines between them, rather than inline, no? \$\endgroup\$
    – Underslash
    May 21, 2021 at 4:25
1
\$\begingroup\$

Nim, 97 bytes

var a=0
for c in stdin.readAll:
 if c==';':a+=1
 if c=='#':echo "stdout.write chr ",a mod 127;a=0

Try it online!

Example resulting code

\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 16 bytes

';'⁺Ṇ'#"ṇȷ%C¢"WṆ

Try it online! or verify the resulting Thunno 2 code

Explanation

';'⁺Ṇ'#"ṇȷ%C¢"WṆ  '# Implicit input
';'⁺Ṇ              # Transliterate ";" -> "⁺"
   ⁺               #   Increment top of stack
                   #   (which starts at 0)
     '#"ṇȷ%C¢"WṆ  '# Transliterate "#" -> "ṇȷ%C¢"
        ṇȷ         #   Push compressed integer 127
          %        #   And modulo top of stack by that
           C       #   Cast to character using ASCII value
            ¢      #   And print it without a newline
                   # Implicit output
\$\endgroup\$
1
\$\begingroup\$

ARBLE, 57 bytes

"print%q"%gsub(a,".-#",#char(len(gsub(e,"[^;]",""))%128))

Explained

"print%q"%gsub(a,".-#",#char(len(gsub(e,"[^;]",""))%128))
          gsub(a,".-#",                                 )  -- Replace all groups of characters followed by a # in the input with...
                                 gsub(e,"[^;]","")         -- Remove all non-; characters, removing comments
                             len(                 )        -- Effectively count all ; in the group
                        char(                      %128)   -- Modulo 128, convert to a char.
                       #                                   -- As a function. Effectively runs the ;# code.
"print%q"%                                                 -- returns a string consisting of print followed by the string-formatted ;# code.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Actually, 25 bytes

'#@s⌠';@c7╙D@%⌡MdX$"♂cΣ"o

Try it online! (includes output from executing the transpiled Actually code)

Explanation:

'#@s⌠';@c7╙D@%⌡MdX$"♂cΣ"o
'#@s                       split on "#"
    ⌠';@c7╙D@%⌡M           for each chunk:
     ';@c                    count ";"s
         7╙D@%               mod by 127 (2**7-1)
                dX         discard last value
                  $        stringify
                   "♂cΣ"o  append "♂cΣ":
                    ♂c       convert each value to an ASCII character
                      Σ      concatenate
\$\endgroup\$
0
\$\begingroup\$

shortC, 48 bytes

c,a;AR"AP\"");O;~c;c=G)c==59?a++:c==35?a=!R&a):a
\$\endgroup\$
2
  • \$\begingroup\$ Can you add the expanded form? \$\endgroup\$ May 25, 2017 at 23:57
  • \$\begingroup\$ @CalculatorFeline already done. \$\endgroup\$
    – MD XF
    May 25, 2017 at 23:58
0
\$\begingroup\$

Fourier, 32 bytes

$(I~S{59}{`^`}S{35}{`%127a0`}&i)

Try it on FourIDE!

This was quite an easy challenge since Fourier is basically a superset of ;#:

;# command > Fourier equivalent
; > ^ (Increment the accumulator)
# > %127a0 (Modulo accumulator by 127, output corresponding code point and set accumulator to zero)
\$\endgroup\$
0
\$\begingroup\$

CJam, 14 bytes

q'#/);';fe=:c`

Explanation:

q               e# Read input
 '#/            e# Split on '#'
    );          e# Delete last element
      ';fe=     e# Count occurrences of ';' in each
           :c   e# Convert each to character (code point)
             `  e# Escape
\$\endgroup\$
1
  • \$\begingroup\$ This needs a 127% as you should calculate "the value of the accumulator modulo 127" \$\endgroup\$ May 20, 2021 at 22:23
0
\$\begingroup\$

APL, 31 bytes

{⍕'⎕UCS',⌽127|+/¨';'=⍵⊂⍨'#'=⍵}⌽

Output:

      ({⍕'⎕UCS',⌽127|+/¨';'=⍵⊂⍨'#'=⍵}⌽) ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#'
⎕UCS 59 35
      ⍝ evaluate the output
      ⍎({⍕'⎕UCS',⌽127|+/¨';'=⍵⊂⍨'#'=⍵}⌽) ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#'
;#

Explanation:

  • : reverse the input
  • {...}: pass it to this function:
    • ⍵⊂⍨'#'=⍵: partition at each # in the string (from the beginning, which is why it had to be reversed first)
    • +/¨';'=: count the ;s in each partition
    • 127|: modulo 127
    • : reverse it again
    • '⎕UCS',: prepend the string ⎕UCS, which is the Unicode function.
    • : string representation
\$\endgroup\$
1
  • \$\begingroup\$ You can remove the as the actual output to STDOUT is the same. \$\endgroup\$
    – Adám
    May 25, 2017 at 12:13
0
\$\begingroup\$

Ruby, 47+1=48 bytes

$_=gsub(/.*?#/){"$><<#{$&.count(?;)%127}.chr;"}

+1 byte for -p.

Try it online!

-30 bytes thanks to @manatwork!

\$\endgroup\$
6
  • \$\begingroup\$ Unfortunately this not plays nice with the “This source code may contain (comment) characters other than ; or #.” part of requirement. \$\endgroup\$
    – manatwork
    May 25, 2017 at 9:38
  • \$\begingroup\$ @manatwork Fixed, will make golfier later. \$\endgroup\$
    – Pavel
    May 25, 2017 at 14:32
  • \$\begingroup\$ Was enough to change the regular expression /;+#//.*?#/ and the code block s.length-1s.count(?;). BTW, your math is also wrong, as % has higher priority than -, so should be (s.length-1)%127. And in .gsub's code block you can access the captured groups with $&, $1, … so the |s| code block parameter usually is not feasible. And the string interpolation stringifies: {"$><<#{$&.count(?;)%127}.chr;"}. Try it online! \$\endgroup\$
    – manatwork
    May 25, 2017 at 14:48
  • \$\begingroup\$ @manatwork thank you so much! I think your comment has doubled my ruby knowledge. \$\endgroup\$
    – Pavel
    May 25, 2017 at 16:16
  • \$\begingroup\$ It's late, but the code challenge itself was bumped recently by a modified answer so whatever. gsub modifies $_ directly, which means that you don't need to re-assign it. HOWEVER, you have issues if you have comment characters after your last #... see here \$\endgroup\$
    – Value Ink
    Aug 15, 2017 at 21:10
0
\$\begingroup\$

Pyth, 25 23 24 bytes

j\\+"jk["mC%/d\;127Pcw\#

+1 bytes thanks to @FryAmTheEggman

Try it!

handles characters that have to be escaped by only using 1-char-strings.

Sample outputs:

jk[\"

jk[\H\e\l\l\o\,\ \W\o\r\l\d\!

Uses my ;# interpreter.

\$\endgroup\$
1
  • \$\begingroup\$ This doesn't work if there are no # in the input, as it will print 0. You can fix this with jk instead of s. \$\endgroup\$ May 25, 2017 at 0:55
0
\$\begingroup\$

C, 150 bytes

#include<stdio.h>
c;n=0;main(){puts("#include<stdio.h>\nmain(){");while(~(c=getchar()))c-35?c-59?:n++:(printf("putchar(%d);",n%127)&(n=0));puts("}");}

Expanded:

#include<stdio.h>
c;
n=0;
main(){
        puts("#include<stdio.h>\nmain(){");
        while( ~(
            c=getchar()
        ))
                c-35?
                        c-59?
                                :
                                n++
                        :
                        ( printf("putchar(%d);",n%127)
                        &(n=0))
                ;
        puts("}");
}

It's a complete program that (should) terminate, ignore comments and produce always correct output code. I assume EOF=-1

Tested on SystemResque-Cd 4.9.6, compiled with gcc 4.9.4

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.