TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An ultra-simplified example of a modern compiler written in JavaScript

93 pointsby alokraiabout 5 years ago

10 comments

mrkeenabout 5 years ago
9&#x2F;10 times when I see a &quot;compiler&quot; on GitHub, it&#x27;s just an interpreter, or the compilation phase is completely missing.<p>In this case I believe the &quot;compiler&quot; is an &quot;ast printer&quot;.
评论 #22523750 未加载
评论 #22524790 未加载
评论 #22523654 未加载
评论 #22524007 未加载
评论 #22524775 未加载
neilparikhabout 5 years ago
If anyone is curious, I threw together a similar (although not commented yet) example of how I would write this using parser combinators in Haskell: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;neilparikh&#x2F;b8a9c758ebbbb5a3ee9b2ed999740b0d" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;neilparikh&#x2F;b8a9c758ebbbb5a3ee9b2ed99...</a><p>The JS example wrote everything from scratch, so to do the same, I wrote my own parser combinator library (based on <a href="https:&#x2F;&#x2F;www.cs.nott.ac.uk&#x2F;~pszgmh&#x2F;monparsing.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.nott.ac.uk&#x2F;~pszgmh&#x2F;monparsing.pdf</a>), but you could use an existing library, and get rid of `Parser.hs`.
评论 #22523214 未加载
ducaaleabout 5 years ago
There was a person who began rewriting this using reason [1]. Sadly, he never got to complete it.<p>I began tinkering with F# recently and found that ML languages are perfect for parsing. Is there a version of this in F# or ocaml?<p>[1] <a href="https:&#x2F;&#x2F;medium.com&#x2F;@javierwchavarri&#x2F;building-the-super-tiny-compiler-with-reason-part-1-21460cd4ae7b" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@javierwchavarri&#x2F;building-the-super-tiny-...</a>
评论 #22538267 未加载
评论 #22533041 未加载
masswerkabout 5 years ago
Maybe interesting for a comparison: 21 years ago I wrote my own tiny demo compiler in JS, much inspired by Niklaus Wirth.<p><a href="https:&#x2F;&#x2F;www.masswerk.at&#x2F;demospace&#x2F;eal&#x2F;eal.htm" rel="nofollow">https:&#x2F;&#x2F;www.masswerk.at&#x2F;demospace&#x2F;eal&#x2F;eal.htm</a><p>See the page source for the code. (Probably fun, old syntax both for HTML and JS, pre-ECMA JavaScript [meaning, the semicolon isn&#x27;t a delimiter, but a separator], even the indentation style is outdated. Mind that back then, even the support of JS object literals wasn&#x27;t guaranteed.)<p>Syntax, etc: <a href="https:&#x2F;&#x2F;www.masswerk.at&#x2F;demospace&#x2F;eal&#x2F;syntax.htm" rel="nofollow">https:&#x2F;&#x2F;www.masswerk.at&#x2F;demospace&#x2F;eal&#x2F;syntax.htm</a><p>Byte code reference: <a href="https:&#x2F;&#x2F;www.masswerk.at&#x2F;demospace&#x2F;eal&#x2F;pcode.htm" rel="nofollow">https:&#x2F;&#x2F;www.masswerk.at&#x2F;demospace&#x2F;eal&#x2F;pcode.htm</a>
Kwantuumabout 5 years ago
Reminds me of this old screencast about writing a compiler in ruby:<p><a href="https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;screencasts&#x2F;catalog&#x2F;a-compiler-from-scratch" rel="nofollow">https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;screencasts&#x2F;catalog&#x2F;a-com...</a>
mrkeenabout 5 years ago
I want to dispute the &#x27;modern&#x27; claim.<p>I don&#x27;t know my dates well enough. But is there anything in here that isn&#x27;t at least 50 years old?<p>The high-level is tokenize-&gt;parse-&gt;ast_transform-&gt;codegen.<p>The low-level looks like `char = input[++current];`
tobrabout 5 years ago
What does “modern” mean in this context?
评论 #22525234 未加载
评论 #22524132 未加载
MR4Dabout 5 years ago
Petty cool. Very well documented too (I only read through a few hundred lines before bookmarking it for later).
alisweabout 5 years ago
Wouldn&#x27;t this be closer to a &quot;transpiler&quot;?
lostmsuabout 5 years ago
Does this parse in linear time?