6 ms·
Show HN: A self-hosted js compiler in 1k loc
- rntz 10y agoThe 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 10y agoI prefer "source-to-source compiler": https://en.wikipedia.org/wiki/Source-to-source_compiler https://en.wikipedia.org/wiki/Source-to-source_compiler
- hasperdi 10y agoYeah, it was the same question I had in mind. The README.md does not do a good job of explaining it.
- ckluis 10y agowould this allow something like - https://ckluis.com/js-template-concept-e6de6912fd35 https://ckluis.com/js-template-concept-e6de6912fd35 - to be feasible?
- peterkelly 10y agoI 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 10y ago> 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 10y agoI'd say transpiler, or at least preprocessor. It does add a language feature, after all.
- Schampu 10y agoI just added a second feature called "inout", which lets you pass variables by reference
- EpicEng 10y agoCompiler: 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.
- pvg 10y ago'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.
- whitten 10y agoWhat 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 ?
- coolspot 10y agoI can make Chrome V8 in one LOC. find ./chrome-v8-src | xargs sed -i ':a;N;$!ba;s/\n//g' Boom!
- aaron-lebo 10y agoI 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