10
\$\begingroup\$

Commentscript is a variant on Javascript that I made up for the purpose of this question. Only commented-out code is evaluated.

Javascript has two types of comments:

// this is a single line comment, which starts with `//` and ends on a newline.

/*
This is a multiline comment.
It starts with /* and ends with */

Example:

//console.log('goodbye world')
console.log('hello world')

will output goodbye world to the console.

Your challenge is to compile Commentscript code into regular Javascript.

Scoring

This is code-golf, so shortest bytes wins!

Clarifications

Nested comments like // comment // nested comment should just have the top-level comment removed, for example comment // nested comment.

To avoid complications, comments in strings should be read as comments, e.g. 'abc //' def //ghi => ' def //ghi

Newlines from single line comments should be kept, but ones by multiline comments shouldn't.

Ignore shebangs, e.g. #! /usr/bin/env node

//a
//b
=>
a
b


/*a*/


/*b*/
=>
ab

Test cases

//console.log('hello world') => console.log('hello world')

//console.log('hello world')//do stuff => console.log('hello world')//do stuff

/*var a = 5;*/function f(){}/*console.log(a)*/ => var a = 5; console.log(a)

/////////// => /////////

//console.log('hello world')/*More nested comments*/ => console.log('hello world')/*More nested comments*/

"abc //" def //ghi => " def //ghi

//a
//b
=>
a
b

/*a*//*b*/ => ab

//a



//b

//c
=>
a
b
c

/*
for(var x of array){
    console.log(x)
}
*/
=> 
for(var x of array){
    console.log(x)
}


/*
//this is a comment
*/

=>

//this is a comment


#!/usr/bin/env node =>  [NOTHING]
\$\endgroup\$
17
  • 6
    \$\begingroup\$ "To avoid complications, comments in strings should be read as comments," doesn't sound right. Are you sure? I'd say that comments cannot appear in strings. Otherwise this is solvable with boringly simple regex. \$\endgroup\$ – Adám Mar 17 at 10:06
  • 3
    \$\begingroup\$ I think the above questions are a good indicator that this question could use some time in the sandbox first. \$\endgroup\$ – Etheryte Mar 17 at 13:11
  • 10
    \$\begingroup\$ @Etheryte It was in the sandbox during about 1 month. So I'd say that the above questions are a good indicator that the sandbox doesn't work very well. \$\endgroup\$ – Arnauld Mar 17 at 14:15
  • 1
    \$\begingroup\$ I'd say that neither is the case (or rather, the questions above do not indicate either); almost all challenges - sandboxed or not - have some clarifications in the comments, and a fair number of them are simplified beyond what the challenge author, or anyone who saw the challenge in the Sandbox, notified or intended \$\endgroup\$ – caird coinheringaahing Mar 17 at 14:32
  • 2
    \$\begingroup\$ What about // comments nested in /**/ comments? should they remain commented or not? And the other way round? Also what is the goal of remplacing multiple newlines by a single one? This rule doesn't seem to add anything interesting to the challenge IMO \$\endgroup\$ – Kaddath Mar 17 at 15:36
2
\$\begingroup\$

Retina 0.8.2, 63 bytes

(//.*|/\*(.|¶)*?\*/|¶)|.
$1
//(.*)|/\*((.|¶)*?)\*/|(¶)¶*
$1$2$4

Try it online! Link includes test cases, but the newline-collapsing rule means that they're not double-separated on output. Explanation:

(//.*|/\*(.|¶)*?\*/|¶)|.
$1

Delete anything not a newline or comment.

//(.*)|/\*((.|¶)*?)\*/|(¶)¶*
$1$2$4

Keep the content of comments and one newline of consecutive newlines.

\$\endgroup\$
1
  • \$\begingroup\$ Nice job! We'll have to see what others get up to! \$\endgroup\$ – A username Mar 23 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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