TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How to Implement a Programming Language in JavaScript

99 点作者 cristiantincu大约 10 年前

6 条评论

chubot大约 10 年前
I&#x27;ve written multiple parsers&#x2F;interpreters in both JS and Python -- Python being my favorite language. From that experience, I&#x27;ve come around to the fact that they&#x27;re both the wrong language for writing languages -- lexers, parsers, interpreter loops, compilers.<p>I have a good analogy to explain this. Take this OCaml program.<p><pre><code> let sum_file filename = let file = In_channel.create filename in let numbers = List.map ~f:Int.of_string (In_channel.input_lines file) in let sum = List.fold ~init:0 ~f:(+) numbers in In_channel.close file; sum ;; </code></pre> This is roughly equivalent to:<p><pre><code> def sum_file(filename): with open(filename) as f: return sum([int(x) for x in f]) </code></pre> Now think of the degree to which OCaml is awkward here. That&#x27;s exactly how awkward Python and JS are for writing languages :)<p>OCaml is based around ML-style typed records, which are exactly what you need for manipulating languages.<p>I am not a type safety guy, but when you write languages, there are tons of nested conditionals. In Python in JS or C, you end up with bugs in those corners. It is quite easy to crash any non-trivial parser, like the CPython parser, or Clang&#x27;s parsers, etc. The type safety that ML offers helps a lot with this.<p>The code for writing parsers and interpreters is just SHORTER in OCaml than in Python. I used to think Python was the most concise language. But no, it depends on the problem domain. Try it and you&#x27;ll see.<p>Someone saying the same thing here:<p><a href="http://flint.cs.yale.edu/cs421/case-for-ml.html" rel="nofollow">http:&#x2F;&#x2F;flint.cs.yale.edu&#x2F;cs421&#x2F;case-for-ml.html</a>
评论 #9192329 未加载
评论 #9193026 未加载
评论 #9191827 未加载
评论 #9196697 未加载
评论 #9192196 未加载
arcatek大约 10 年前
Another really good course to learn the basic about programming language design:<p><a href="http://nathansuniversity.com/" rel="nofollow">http:&#x2F;&#x2F;nathansuniversity.com&#x2F;</a><p>And if you want to actually build a &quot;real&quot; programming language, I advise you to try llvm - it really takes away the pain of generating bytecode, and gives you everything you need to deal with the actual design of your language.
fredkelly大约 10 年前
Interesting read.<p>Possibly more digestable, I recommend the walkthrough of building &quot;Egg&quot; in the fantastic (and free!) Eloquent JavaScript by Marijn Haverbeke: <a href="http://eloquentjavascript.net/11_language.html" rel="nofollow">http:&#x2F;&#x2F;eloquentjavascript.net&#x2F;11_language.html</a>
tinco大约 10 年前
Given the domain name I was hoping they&#x27;d first implement a Lisp in 12 lines of Javascript, and then implement the new language in that lisp, that would&#x27;ve been an interesting tutorial.<p>I get that it&#x27;s a beginners tutorial and they have some constraints, but it starts off with &quot;let&#x27;s dream up a language&quot; and then presents a super standard language, it&#x27;s basically just Javascript with obligatory semicolons..
评论 #9196325 未加载
thomasfoster96大约 10 年前
I&#x27;m glad they didn&#x27;t write a tutorial that made another Lisp - I&#x27;d your audience is javacript developers, a lisp isn&#x27;t all that appealing usually.<p>Anyways, this is such a detailed series of tutorials I may be distracted for days bringing my half finished programming language back from the dead.
评论 #9191260 未加载
noiv大约 10 年前
I remember reading this the first time as a JS rookie. Every time the author mentions &quot;this is same in JS&quot; a light went on in my head and all the JS quirks found earlier became just natural. It still deserves its place in the Christmas tree bookmark section.