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.
;Increment the accumulator#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): \'`
44 Answers 44
brainfuck, 126 bytes
+++[->+++++<]>[->++++>+++<<],[+<++++++[-<++++>>------<]>[<]<<[>>>>>[--<--.++>]<+.-<.<<<<]>[->>-<<]>>[[-]<]>[>>++++[-->]<[<]],]
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.
]
,]
Python 2, (削除) 76 (削除ここまで) 69 bytes
Code
Input is surrounded by quotes.
for y in input('print').split("#")[:-1]:print`chr(y.count(";")%127)`,
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.
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
...
-
\$\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– aschepler2017年08月10日 22:29:22 +00:00Commented 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\$CensoredUsername– CensoredUsername2017年08月10日 23:10:34 +00:00Commented Aug 10, 2017 at 23:10
V, (削除) 19 (削除ここまで) (削除) 20 (削除ここまで) 28 bytes
Bugfix, broke if there was no # at the end
Bugfix, implemented mod 127
Í;û127}
éiA0Í#/0
ò/;
x
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.
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.
-
\$\begingroup\$
/;*(#|$)/=>/;*(#?)/? \$\endgroup\$l4m2– l4m22021年05月21日 05:06:24 +00:00Commented May 21, 2021 at 5:06
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
-
\$\begingroup\$
';¢can beg,žypushes 128, may work somehow and why not just spell the full word and surround it by quotes? \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年05月25日 21:54:47 +00:00Commented May 25, 2017 at 21:54 -
1\$\begingroup\$ I need to use
';¢incase there are characters other than;.žy<is the same as127. Printing the word surrounded by quotes will break if one of the characters is a quote. \$\endgroup\$Riley– Riley2017年05月25日 22:12:20 +00:00Commented May 25, 2017 at 22:12 -
\$\begingroup\$ @carusocomputing I forgot to ping you... \$\endgroup\$Riley– Riley2017年05月25日 22:21:23 +00:00Commented May 25, 2017 at 22:21
-
1
-
\$\begingroup\$ @Adnan why/how? \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年06月01日 16:36:04 +00:00Commented Jun 1, 2017 at 16:36
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"+...).
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)))
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)
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})
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
...
-
\$\begingroup\$ Save a lot of bytes removing the arguments from
@6,%6and-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– MD XF2017年10月08日 21:53:40 +00:00Commented Oct 8, 2017 at 21:53
Brachylog, 33 bytes
{";"∧"+1"|"#"∧"%127g~ạw0"}s;"0"↔c
I felt too tired to explain this two years ago, but Razetime reminded me.
;"0"↔c Prepend a 0 to
{ }s each element of the input
"+1" mapped to +1
";"∧ if it's a semicolon,
|"#" if it's a hash
∧"%127g~ạw0" %127g~ạw0,
s or nothing otherwise.
+1 add 1
%127 mod 127,
g~ạ convert from codepoint to character,
w print,
0 start over from 0
-
1\$\begingroup\$ I am droppinga comment to remind unrelated string \$\endgroup\$Razetime– Razetime2021年05月21日 03:57:32 +00:00Commented May 21, 2021 at 3:57
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.
-
-
\$\begingroup\$ Nice golf @cairdcoinheringaahing, thanks! \$\endgroup\$Jonathan Allan– Jonathan Allan2021年05月25日 15:01:40 +00:00Commented May 25, 2021 at 15:01
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!");}
-
2\$\begingroup\$
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#producesf(){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– hvd2017年05月25日 07:47:36 +00:00Commented May 25, 2017 at 7:47 -
\$\begingroup\$ @hvd Fixing.... \$\endgroup\$MD XF– MD XF2017年05月25日 17:25:31 +00:00Commented May 25, 2017 at 17:25
Jelly, 26 bytes
"‘x
f";L%127Ç;"Ọø"
ṣ"#Ç€ṙ-
ṣ"#Ç€ṙ- 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)
-
\$\begingroup\$ Weird example to use (printing only white space) that confused me. \$\endgroup\$Jonathan Allan– Jonathan Allan2017年05月24日 21:35:28 +00:00Commented May 24, 2017 at 21:35
-
1\$\begingroup\$ @JonathanAllan Added examples with links to TIO. \$\endgroup\$steenbergh– steenbergh2017年05月24日 21:40:01 +00:00Commented May 24, 2017 at 21:40
PHP, 72 bytes
for(;~$c=$argn[$i++];)echo[Z=>'$n++;',B=>'echo~$n=~chr($n%127);'][a^$c];
><>, (削除) 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' <
-
\$\begingroup\$ Welcome to the pond ! You can shorten
i:1+?!intoi: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 ofos ;) \$\endgroup\$Aaron– Aaron2017年06月02日 09:51:06 +00:00Commented Jun 2, 2017 at 9:51
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+"\");");
}
}
MathGolf, 17 bytes
⌡(¶{gÉ'8=£♣(%$''\
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.
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2019年03月28日 08:49:23 +00:00Commented 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– maxb2019年03月28日 09:26:00 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2019年03月28日 09:38:31 +00:00Commented Mar 28, 2019 at 9:38 -
1
MATL, (削除) 32 (削除ここまで) 28 bytes
35lF4$Yb"@g59=z]xv127\!&D99h
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.
Vyxal Ṫ, 18 bytes
\#€Ṫƛ\;O7‹%\C+\₴+,
Good, but definitely could be improved. Outputs the codepoint, followed by a C to convert to a character, and then a print symbol.
-
-
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– Underslash2021年05月21日 04:25:31 +00:00Commented May 21, 2021 at 4:25
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
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.
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
shortC, 48 bytes
c,a;AR"AP\"");O;~c;c=G)c==59?a++:c==35?a=!R&a):a
-
\$\begingroup\$ Can you add the expanded form? \$\endgroup\$CalculatorFeline– CalculatorFeline2017年05月25日 23:57:42 +00:00Commented May 25, 2017 at 23:57
-
\$\begingroup\$ @CalculatorFeline already done. \$\endgroup\$MD XF– MD XF2017年05月25日 23:58:18 +00:00Commented May 25, 2017 at 23:58
Fourier, 32 bytes
$(I~S{59}{`^`}S{35}{`%127a0`}&i)
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)
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
-
\$\begingroup\$ This needs a
127%as you should calculate "the value of the accumulator modulo 127" \$\endgroup\$2021年05月20日 22:23:49 +00:00Commented May 20, 2021 at 22:23
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 partition127|: modulo 127⌽: reverse it again'⎕UCS',: prepend the string⎕UCS, which is the Unicode function.⍕: string representation
-
\$\begingroup\$ You can remove the
⍕as the actual output to STDOUT is the same. \$\endgroup\$Adám– Adám2017年05月25日 12:13:36 +00:00Commented May 25, 2017 at 12:13
Ruby, 47+1=48 bytes
$_=gsub(/.*?#/){"$><<#{$&.count(?;)%127}.chr;"}
+1 byte for -p.
-30 bytes thanks to @manatwork!
-
\$\begingroup\$ Unfortunately this not plays nice with the "This source code may contain (comment) characters other than
;or#." part of requirement. \$\endgroup\$manatwork– manatwork2017年05月25日 09:38:15 +00:00Commented May 25, 2017 at 9:38 -
\$\begingroup\$ @manatwork Fixed, will make golfier later. \$\endgroup\$Pavel– Pavel2017年05月25日 14:32:54 +00:00Commented May 25, 2017 at 14:32
-
\$\begingroup\$ Was enough to change the regular expression
/;+#/→/.*?#/and the code blocks.length-1→s.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– manatwork2017年05月25日 14:48:42 +00:00Commented May 25, 2017 at 14:48 -
\$\begingroup\$ @manatwork thank you so much! I think your comment has doubled my ruby knowledge. \$\endgroup\$Pavel– Pavel2017年05月25日 16:16:38 +00:00Commented May 25, 2017 at 16:16
-
\$\begingroup\$ It's late, but the code challenge itself was bumped recently by a modified answer so whatever.
gsubmodifies$_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– Value Ink2017年08月15日 21:10:42 +00:00Commented Aug 15, 2017 at 21:10
Pyth, (削除) 25 (削除ここまで) (削除) 23 (削除ここまで) 24 bytes
j\\+"jk["mC%/d\;127Pcw\#
+1 bytes thanks to @FryAmTheEggman
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.
-
\$\begingroup\$ This doesn't work if there are no
#in the input, as it will print0. You can fix this withjkinstead ofs. \$\endgroup\$FryAmTheEggman– FryAmTheEggman2017年05月25日 00:55:00 +00:00Commented May 25, 2017 at 0:55
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
;#preinstalled... \$\endgroup\$#character in it? \$\endgroup\$;or#? \$\endgroup\$