
Show HN: A self-hosted js compiler in 1k loc - Schampu
https://github.com/maierfelix/mini-js
======
peterkelly
I think the appropriate term for this is a _pretty-printer_. It isn't really a
compiler, because the target language is the same as the source.

For a JavaScript compiler (or VM) to be genuinely described as _self-hosting_
, it would need to be able to run using _itself_ , without depending on an
existing implementation such as V8, ChakraCore etc. The hard part is not
outputting code that can be evaluated by JS itself, but implementing the
semantics of JS in terms of a lower-level of abstraction, ideally native code.

~~~
munificent
> It isn't really a compiler, because the target language is the same as the
> source.

I think it could still be deemed a "compiler" even if it output the same
language if it was doing some significant transformation or modification of
it.

But, yes, in this case I think you're right and a "pretty-printer" is a better
description of it.

~~~
bbcbasic
I'd say transpiler, or at least preprocessor. It does add a language feature,
after all.

~~~
Schampu
I just added a second feature called "inout", which lets you pass variables by
reference

------
rntz
The immediate question I had was: "compiles JS to _what_ "?

Apparently the answer is: to Javascript. It's what some call a transpiler: it
compiles Javascript plus some extra features down to a more "vanilla"
Javascript (es5?). I tend to dislike the term transpiler (transpilers are just
compilers that target high-level languages), but I guess in this case the term
would have helped me understand what was meant!

~~~
ThisIs_MyName
I prefer "source-to-source compiler": [https://en.wikipedia.org/wiki/Source-
to-source_compiler](https://en.wikipedia.org/wiki/Source-to-source_compiler)

------
coolspot
I can make Chrome V8 in one LOC.

find ./chrome-v8-src | xargs sed -i ':a;N;$!ba;s/\n//g'

Boom!

------
aaron-lebo
I really like Douglas Crockford's Pratt parser which is similar. It doesn't
compile down to JS but it's trivial to go from the AST to JS either manually
or using something like escodegen.

Very elegant and easily extensible.

[http://javascript.crockford.com/tdop/index.html](http://javascript.crockford.com/tdop/index.html)

------
ckluis
would this allow something like - [https://ckluis.com/js-template-
concept-e6de6912fd35](https://ckluis.com/js-template-concept-e6de6912fd35) \-
to be feasible?

