Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: A self-hosted js compiler in 1k loc (github.com/maierfelix)
47 points by _s47s on Feb 10, 2017 | hide | past | favorite | 13 comments



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.


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


Compiler:

a program that converts instructions into a machine-code or lower-level form so that they can be read and executed by a computer

Since they add features, I guess this would qualify as it outputs a "lower level form", but man it's really pushing the definition.


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


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


'Pretty-printer' is not quite right either. Nobody bootstraps pretty-printers by repeatedly running them through themselves and extending them which I think was kind of the point of the exercise here.


What is the best way known to implement the semantics of a language? Generating an abstract syntax tree is nice, but what is the state of the art in abstract semantics ?


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!


I prefer "source-to-source compiler": https://en.wikipedia.org/wiki/Source-to-source_compiler


Yeah, it was the same question I had in mind. The README.md does not do a good job of explaining it.


I can make Chrome V8 in one LOC.

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

Boom!


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


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




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: