Live data from Hacker News

How to implement a programming language in JavaScript

lisperator.net

21–30 of 39 posts

Re: How to implement a programming language in JavaScript

#21

It would be nice when saying things like "don't use regexps for parsing" that it is accompanied by an explanation (possibly I didn't read far enough to see the explanation...) There are only a few actual computer science theory things that I think all programmers should know and this is one of them. There are classifications of grammars (1). Without going into detail, regular expressions can only be used to parse reg…

This is all true, for sure, but I'd like to point out that PEGs are a formalism that isn't much harder to learn than regular expressions, and totally appropriate for parsing a fairly large class of languages. I've also seen PEG-based (pakrat) parser generators for just about every programming language (some better than others, of course). Again, these tend to be not much harder to learn than regular expressions, and…

Thanks for addending :-) I wasn't aware of PEGs. That definitely does look promising for a lot of cases.

Re: How to implement a programming language in JavaScript

#22
If anyone is interested in making a language in JS I can recommend the "Nearley" parser/lexer:

http://nearley.js.org

Very easy parser/lexer to use, accepts a larger class of grammars than yacc/lex and is reasonably fast.

I use it for the language in my project: http://emunotes.com and Nearley is fast enough to parse stuff as the user is entering text.

It may be tempting to see if you can get away with not using a parser generator (as the author does here) but I found it is not worth the trouble it generates down the road.

Re: How to implement a programming language in JavaScript

#23
Cool! This is a great, clear tutorial.

I strongly recommend this activity to almost any programmer. No matter what, you'll end up with a deeper appreciation for great system design. You realize how almost every design decision boils down to a compromise between convenience and flexibility. For better or worse, you'll also get new respect for the cruel reasoning behind more arcane, but also more flexible APIs. Red pill blue pill kind of thing.

And, just to share: Last year I took a crack at creating a JavaScript-interpreted Lisp and this is the result - https://github.com/matthewtoast/runiq - I haven't added anything since then but I might pick it up again, as it was a helluva lot of fun and also quite mind-expanding.

Re: How to implement a programming language in JavaScript

#25
post #3

Just wondering if it is true that most of the modern language parsers are written in C?

Python, Ruby, PHP: C

JS: no standard parser, but V8(chrome), SpiderMonkey(firefox) and Chakra(edge) are all C++

Go, C, C++: self-hosted (written in the same language)

Java, Erlang: parser/compiler self-hosted, but VMs written in C.

Re: How to implement a programming language in JavaScript

#26
Cool... but nitpick about the parsing part (only glanced at it): why do people always forget about parser combinators and don't present them as obviously 'the first thing to try' when making a new language?!

They seem the simplest and most intuitive tool for an unbiased newbie designing a new language, and you can even introduce them by skipping the theory with something like this: http://theorangeduck.com/page/you-could-have-invented-parser... .

Re: How to implement a programming language in JavaScript

#27
post #3

Just wondering if it is true that most of the modern language parsers are written in C?

I doubt it. If anything, C++, but even that doesn't seem likely. The vast majority of languages seem to be self-hosted. A good portion compilers or interpreters in C or C++ probably use (F)lex and Yacc/Bison so even saying C or C++ are the most popular choice (but not necessarily the majority) sounds like a stretch.

Re: How to implement a programming language in JavaScript

#29
post #28

UNIX/Linux has had off the shelf tools for doing this for nearly 40 years. Two of them are called lexx and yacc. Kind of reinventing thevwheel for those unfamilar with history.

Parsing is the smallest part of writing a compiler.

It's also one of the most conceptually difficult parts, which makes it a good learning opportunity.

Re: How to implement a programming language in JavaScript

#30
post #5

Is there such a thing as a language that compiles to a value rather than an executable program? I've done a really hacky version of something this where a user would input a succinct, high-level description of a sequence of colored lighting, and my program would parse* that and build the tedious, low-level JSON representation of that sequence for yet another program to perform. Is there a fancy or unfancy name for th…

The language in the article is just such a thing!

The difference is essentially between statements (AKA instructions) and expressions (AKA values).

Statements are executed one after another to produce effects(/actions). That is "imperative" programming.

In contrast, expressions represent values, and we evaluate(/reduce/simplify) them over and over. Sometimes they reach a "normal form" (i.e. a "final result"), other times they may end up in a loop. This may be called "value-oriented programming", and pure functional programming is one example. The language in the article is similarly "value-oriented", although it isn't "pure" since it allows effects in its expressions, which requires careful choice of which order to evaluate things in.

In Haskell, for example, "programs" are just expressions representing some sort of value. It just-so-happens that when a Haskell program is 'executed', what actually happens is we look for a value called "main", we evaluate it to get a value, and that value just-so-happens to have the type "imperative program" (technically it's called "IO a"). That "imperative program" value is then handed over to an interpreter to be executed. Well, conceptually at least; those tasks are actually interleaved so the code is executed "on demand", and the whole thing can get smushed together by an aggressive compiler. Still, it sounds very much like your two-stage language.

Post reply on HN