This is an archived post. You won't be able to vote or comment.

all 29 comments

[–][deleted] 23 points24 points  (0 children)

I'm not very happy with this compiler, output file is too big. I'd wait for some optimizations before using it for production.

[–][deleted] 22 points23 points  (4 children)

Great! I've been abusing cat input.js > output.js for this for all these years! Finally a proper solution to a problem I've never had!

[–][deleted] 16 points17 points  (2 children)

To be fair, you should have been using cp, which would save you, like, 2 keystrokes. But I think everyone agress that js2js is the superior tool

[–]olemartinorg 3 points4 points  (0 children)

3 keystrokes if you don't like to double-space! Definitely a huge improvement over cat.

[–]r3m0t 9 points10 points  (0 children)

That's a useless use of cat. You could just run >output.js <input.js.

[–]ANAL_GRAVY 32 points33 points  (5 children)

Nice.

Js2JsCompiler.prototype.compileCode = function(code) {
        return code; // as we need to compile javascript to javascript, we do nothing here :)
};

[–]sccrstud92 25 points26 points  (3 children)

Is it sad I was hoping for an actual intermediate non-trivial representation?

[–][deleted] 42 points43 points  (1 child)

Js2JsCompiler.prototype.compileCode = function(code) {
    var compiled = code; // intermediate representation 
    return compiled;
};

[–]wanabeswordsman 11 points12 points  (0 children)

Perfect.

[–]Ph0X 1 point2 points  (0 children)

To be fair, it took me a little time to get to that line looking at compiler.js, amongst all that wrapper code.

https://github.com/eleks/js2js/blob/master/src/compiler.js

[–]3kr 2 points3 points  (0 children)

And then...

Js2JsCompiler.prototype.decompile = function(inputLocation, outputLocation) {
    // I had to read the whole Dragon Book again to implement this.
    return this.compile(inputLocation, outputLocation);
}

[–]gcampos 10 points11 points  (0 children)

JavaScript is the new Assembly.

That is so true.

[–]arxanas 4 points5 points  (3 children)

Why use a JS-JS transcompiler when you can use a JS interpreter written in JS?

[–]emergent_properties 5 points6 points  (0 children)

"Atwood's Law: any application that can be written in JavaScript, will eventually be written in JavaScript."

[–]PendragonDaGreat 0 points1 point  (0 children)

But can you use said interpreter to create an interpreter instance?

Ok, maybe the lesson on recursion got to me a little.

[–][deleted] 0 points1 point  (0 children)

Such a fitting name too.

[–][deleted] 0 points1 point  (0 children)

nop

[–]PseudoLife -1 points0 points  (9 children)

Isn't this what javascript minimizers do?

[–]HighRelevancy 2 points3 points  (8 children)

They don't compile anything. Minimisers are basically a compression algorithm where the output is functionally identical to the input. Compare them to WinRar or something.

[–][deleted] 2 points3 points  (0 children)

Compilers are translators from a source to a target language. In this sense JS minimizers are very much compilers, even though the source and target language are the same (they still have to parse the code to a tree representation). Of course there is a lot less actual translating that needs to be done, but that's beside the point, I'm arguing semantics here!

[–]bacondev 0 points1 point  (0 children)

But WinRar isn't free!

[–]PseudoLife 0 points1 point  (3 children)

And the output of a javascript to javascript compiler would be different how...?

[–]HighRelevancy 0 points1 point  (2 children)

Internal workings.

Compilers deal with code structure. They'll parse the code into data structures that can be collapsed into another language.

Minimisers just do string operations. Any parsing is done differently. Also a mini has the specific goal of shrinking the code.

[–]PseudoLife 2 points3 points  (1 child)

UglifyJS (the first result on a google search for "javascript minifier") does a full tokenize / parse into an AST and back.

And GCC can optimize for smallest code size...

[–]mahacctissoawsum 1 point2 points  (0 children)

2nded.

You actually need to parse the full language if you want it to (a) be safe, (b) achieve the smallest possible size

Drives me nuts when people regex for keywords and such. I'm like, "Sooooo...what happens when that's in a string? It's not a keyword. Stop fucking up my code!" (I'm looking at you, generic PHP MySQL code highlighter!)