Transpile ;# into your language

25

2

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): \'`

PhiNotPi

Posted 2017-05-24T20:50:33.660

Reputation: 26 739

34My computer came with ;# preinstalled... – programmer5000 – 2017-05-24T20:56:21.787

1Another one? Why? – caird coinheringaahing – 2017-05-24T22:37:20.833

Does the program have to stop execution? Also, can it print no-ops infinitely after the actual code? – totallyhuman – 2017-05-24T23:50:41.597

Will the program always have at least one # character in it? – FryAmTheEggman – 2017-05-25T00:54:26.183

2@totallyhuman I'm gonna say that all programs must eventually halt. – PhiNotPi – 2017-05-25T03:33:00.413

1Please could we get a test case where the input contains a character (or characters) that aren't ; or #? – streetster – 2017-08-10T20:55:58.523

@streetster Just spam random characters in the already-existing testcases as comments – MilkyWay90 – 2019-06-14T17:08:01.270

Answers

7

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.

Adnan

Posted 2017-05-24T20:50:33.660

Reputation: 41 965

5

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.
  ]
,]

Nitrodon

Posted 2017-05-24T20:50:33.660

Reputation: 9 181

5

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
...

CensoredUsername

Posted 2017-05-24T20:50:33.660

Reputation: 951

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. – aschepler – 2017-08-10T22:29:22.823

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

– CensoredUsername – 2017-08-10T23:10:34.810

4

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.

nmjcman101

Posted 2017-05-24T20:50:33.660

Reputation: 3 274

3

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 should work, but it doesn't for some reason)
               J # Join stack and output implicitly

Riley

Posted 2017-05-24T20:50:33.660

Reputation: 11 345

';¢ can be g, žy pushes 128, may work somehow and why not just spell the full word and surround it by quotes? – Magic Octopus Urn – 2017-05-25T21:54:47.400

1I 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. – Riley – 2017-05-25T22:12:20.487

@carusocomputing I forgot to ping you... – Riley – 2017-05-25T22:21:23.120

1

@carusocomputing and Riley: ƵQ is a compressed version of 127.

– Adnan – 2017-05-25T22:38:46.303

@Adnan why/how? – Magic Octopus Urn – 2017-06-01T16:36:04.233

Also "çJ"} technically works instead of "ç"}'J. Odd how that ends up working but „çJ} is also a bust. – Magic Octopus Urn – 2017-06-01T16:37:39.413

@carusocomputing That does work. Thanks! – Riley – 2017-06-01T16:42:29.227

@Riley if you remove the footer }J can also just be J. Or just change the footer to D.V» – Magic Octopus Urn – 2017-06-01T16:45:16.453

@carusocomputing Q converted to base 255 with an offset of 101 results in 26 + 101 = 127. – Adnan – 2017-06-01T21:05:33.130

You've used ƵQ in your explanation, but not in your actual code, byte-count, or TIO-link. Also, „çJ works in the new version of 05AB1E. (And your footer can be 'golfed' to }=.V,. ;p) Try it online 16 bytes

– Kevin Cruijssen – 2019-03-27T12:40:52.443

2

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"+...).

Mego

Posted 2017-05-24T20:50:33.660

Reputation: 32 998

2

Jelly,  25 24  16 bytes

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

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!.

ṣ”#Ṗċ€”;%127;€”Ọ - 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)
            ;€   - concatenate for €ach - making a list of lists like [[72,'Ọ],[101,'Ọ'],...]
                 - implicit print (this smashes, printing something like: 72Ọ101Ọ...)

A note regarding input: Jelly takes string input in Python format. The empty program could be input as "", and the hash-only programs as "#", "##", etc. Other manipulation may be required for input containing backslashes and quotes.

Jonathan Allan

Posted 2017-05-24T20:50:33.660

Reputation: 67 804

2

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
...

TehPers

Posted 2017-05-24T20:50:33.660

Reputation: 899

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. – MD XF – 2017-10-08T21:53:40.687

1

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)

steenbergh

Posted 2017-05-24T20:50:33.660

Reputation: 7 772

Weird example to use (printing only white space) that confused me. – Jonathan Allan – 2017-05-24T21:35:28.827

1@JonathanAllan Added examples with links to TIO. – steenbergh – 2017-05-24T21:40:01.443

1

JavaScript (ES6), 101 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.

Neil

Posted 2017-05-24T20:50:33.660

Reputation: 95 035

1

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!");}

MD XF

Posted 2017-05-24T20:50:33.660

Reputation: 11 605

2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;# 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." – hvd – 2017-05-25T07:47:36.193

@hvd Fixing.... – MD XF – 2017-05-25T17:25:31.533

1

><>, 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' <

Peter Lenkefi

Posted 2017-05-24T20:50:33.660

Reputation: 1 577

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 ;) – Aaron – 2017-06-02T09:51:06.587

1

PHP, 72 bytes

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

user63956

Posted 2017-05-24T20:50:33.660

Reputation: 1 571

1

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+"\");");
    }
}

App-Devon

Posted 2017-05-24T20:50:33.660

Reputation: 11

1

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)

sudee

Posted 2017-05-24T20:50:33.660

Reputation: 551

1

Brachylog, 33 bytes

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

Try it online!

Feeling a bit too tired to explain this at the moment, if anyone sees this and wonders how it works drop a comment to remind me.

Unrelated String

Posted 2017-05-24T20:50:33.660

Reputation: 5 300

1

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.

Sanchises

Posted 2017-05-24T20:50:33.660

Reputation: 8 530

1

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.

maxb

Posted 2017-05-24T20:50:33.660

Reputation: 5 754

Forgot how to create a chat.. Anyway, (discard everything except for the top of the stack) is currently bugged. It gives a Python FileNotFoundError.

– Kevin Cruijssen – 2019-03-28T08:49:23.877

@KevinCruijssen Check the README! I switched that character in the code page, to avoid having two space characters. The new character is Þ. – maxb – 2019-03-28T09:26:00.753

Yeah, @JoKing indeed said it was changed to Þ. (The docs I use still state the old value however.)

– Kevin Cruijssen – 2019-03-28T09:38:31.970

1

@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.

– maxb – 2019-03-28T13:06:20.330

0

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

Mego

Posted 2017-05-24T20:50:33.660

Reputation: 32 998

0

shortC, 48 bytes

c,a;AR"AP\"");O;~c;c=G)c==59?a++:c==35?a=!R&a):a

MD XF

Posted 2017-05-24T20:50:33.660

Reputation: 11 605

Can you add the expanded form? – CalculatorFeline – 2017-05-25T23:57:42.710

@CalculatorFeline already done.

– MD XF – 2017-05-25T23:58:18.047

0

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.

KarlKastor

Posted 2017-05-24T20:50:33.660

Reputation: 2 352

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. – FryAmTheEggman – 2017-05-25T00:55:00.993

0

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)

Beta Decay

Posted 2017-05-24T20:50:33.660

Reputation: 21 478

0

Ruby, 47+1=48 bytes

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

+1 byte for -p.

Try it online!

-30 bytes thanks to @manatwork!

Pavel

Posted 2017-05-24T20:50:33.660

Reputation: 8 585

Unfortunately this not plays nice with the “This source code may contain (comment) characters other than ; or #.” part of requirement. – manatwork – 2017-05-25T09:38:15.927

@manatwork Fixed, will make golfier later. – Pavel – 2017-05-25T14:32:54.867

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!

– manatwork – 2017-05-25T14:48:42.407

@manatwork thank you so much! I think your comment has doubled my ruby knowledge. – Pavel – 2017-05-25T16:16:38.233

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

– Value Ink – 2017-08-15T21:10:42.040

0

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

Esolanging Fruit

Posted 2017-05-24T20:50:33.660

Reputation: 13 542

0

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

marinus

Posted 2017-05-24T20:50:33.660

Reputation: 30 224

You can remove the as the actual output to STDOUT is the same. – Adám – 2017-05-25T12:13:36.433

0

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

epimatech

Posted 2017-05-24T20:50:33.660

Reputation: 21

0

braingasm, 40 bytes

,[35-z["[127-s[127+.>]]<0*".]24-z[43.],]

Gotta say, that's surprisingly short.

,[                                    ,]     Read a byte until end of input
  35-                                          Subtract 35.
     z[                     ]                  If the result is zero,
       "[127-s[127+.>]]<0*".                     print the code for `#`.
                             24-               Subtract 24.
                                z[   ]         If the result is zero,
                                  43.            print the code for `;`

The generated code for ; is a +, which increments the current cell.

The generated code for # contains an akward, manual modulo operation

[127-s[127+.>]]<0*
[             ]       While current cell is not zero
 127-                   Subtract 127
     s[      ]          If result is negative
       127+.              Add 127 again and print current cell
            >             Step to next cell to break the loop
               <0*      Step back to first cell and clear it

daniero

Posted 2017-05-24T20:50:33.660

Reputation: 17 193

0

Braingolf, 55 bytes

V#0VR&,l1-MvMR[R.#;e"+1"MM|.##e"@%+1~#"MMMMMM|$_vv]Rv&@

Try it online!

Basically replaces ; with 1+, # with #~1+%@ and pre-pends the whole thing with a 0 because monadic + operator is broken right now.

1+ adds 1 to the last item in the stack.

#~1+%@ pushes the char value of ~ (126), adds 1 to make 127, modulus with other item on stack, then pop and print as char.

Explanation

V#0VR&,l1-MvMR                         Implicit input of each char to stack
[R.#;e"+1"MM|.##e"@%+1~#"MMMMMM|$_vv]
Rv&@

V#0                                    Create stack2, push char value of 0
   VR                                  Create stack3, return to stack1
     &,                                Reverse stack
       l1-                             Push length of stack - 1
          MvMR                         Move length to stack2, switch to stack 2
                                       Move length to stack3, switch back to stack1
[R................................vv]  Do-While loop, will run l times
                                       where l is length of input
  .#;                                  Duplicate last item and push char value of semicolon
     e                                 Pop last 2 items, if they are equal..
      "+1"                             ..Push char values of +1 in that order
          MM                           ..Move last 2 items to stack2
            |                          endif
             .##                       Duplicate last item and push char value of hash
                e                      Pop last 2 items, if they are equal..
                 "@%+1~#"              ..Push char values of @%+1~# in that order
                         MMMMMM        ..Move last 6 chars to stack2
                               |       endif
                                $_     Silently pop last item
Rv                                     Switch to stack2
  &@                                   Greedy char print, print every item in stack as char

Skidsdev

Posted 2017-05-24T20:50:33.660

Reputation: 9 656

0

Perl 5, 78 bytes

say"say''",map{$_=chr y/;//%127;$_.=$_ if/\\|'/;",'$_'"}(join$",<>)=~/[^#]*#/g

Try it online!

Xcali

Posted 2017-05-24T20:50:33.660

Reputation: 7 671

0

q/kdb+, 42 bytes

Solution:

{-1_"c"$mod[;127](#:)@/:"#"vs x inter"#;"}

Examples:

Note that the , is used to signify a list (versus atom) as "\"" is a list of one element, rather than being an atom.

q){-1_"c"$mod[;127](#:)@/:"#"vs x inter"#;"}";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"
";#"
q){-1_"c"$mod[;127](#:)@/:"#"vs x inter"#;"}";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"
,"\""

Explanation:

Take input string, remove anything that isnt a # or a ;, split into lists on #, count number of elements in each list, perform mod 127 on the result and cast to a string:

{-1_"c"$mod[;127]count each"#"vs x inter"#;"} / ungolfed
{                                           } / lambda function
                                 x inter"#;"  / intersection of input and ';#' to clear out any 'comments'
                           "#"vs              / split string on hash
                 count each                   / length of each sub-list
        mod[;127]                             / 127 modulo result (vectorised)
    "c"$                                      / cast back to string
 -1_                                          / drop last character (assuming terminated string)

Notes:

  • Assumes that input is terminated by a #, otherwise the last chunk will be erroneously dropped by the -1_.
  • Could be 10 bytes shorter if input was guaranteed to only contain ;#.

streetster

Posted 2017-05-24T20:50:33.660

Reputation: 3 635

0

Ly, 52 bytes

0ui[[";"=[pp"+1"oo2$]p"#"=[pp"(127)%o0"&o2$]]i]";"o;

Try it online!

LyricLy

Posted 2017-05-24T20:50:33.660

Reputation: 3 313

0

Common Lisp, 165 156 bytes

(with-output-to-string(v)(princ"(princ\""v)(do((a 0)(c #\ (read-char()())))((not c))(case c(#\;(incf a))(#\#(princ(code-char a)v)(setf a 0))))(princ"\")"v))

Try it online!

For instance, when applied to:

";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"

produces:

(PRINC";#")

that prints ;#

Renzo

Posted 2017-05-24T20:50:33.660

Reputation: 2 260

0

Ahead, 41 bytes

rW"127%o"<
~>jvi:'#=n';=s~
lW~>'@o@~"1+"<

Converts ;# code into its direct Ahead equivalent, so you get long code consisting mostly of 1+. One byte shorter, but output is very long.

Try it online!


Ahead, 42 bytes

roo''%721$<
~> jli:'#=n';=+~
   >"@ooKu"W@

Figures out which characters to print and writes a program that will push and print all of them with the ' function. Generates much shorter output at the cost of 1 byte. This version was surprisingly hard to golf.

Try it online!

snail_

Posted 2017-05-24T20:50:33.660

Reputation: 1 982

0

Microscript II, 50 45 bytes

0`IK#>s{<d59=(`s1+`)35=(128s`%p0`"Kp"p)>}*"h"

SuperJedi224

Posted 2017-05-24T20:50:33.660

Reputation: 11 342