7 ms·
How to implement a programming language in JavaScript
- avmich 10y agoImmediately thought about another awesome resource about how to write a language in JavaScript: http://nathansuniversity.com/ http://nathansuniversity.com/
- pier25 10y agoSo this is how magic happens
- tumurinn 10y agoJust wondering if it is true that most of the modern language parsers are written in C?
- lifthrasiir 10y agoProbably true, but only because the implementation language for them is also C. Some of them also frequently use a mix of the implementation language and grammar specification DSL like yacc.
- sebcat 10y agoUse of DSLs and tools like flex and bison makes the implementation a whole lot more reliable (but only if they're used, and some commonly run languages have a very implementation-specific grammar). consider: input: /* empty string */ | input line ; line: '\n' | exp '\n' { printf ("\t%.10g\n", $1); } ; exp: NUM { $$ = $1; } | exp '+' exp { $$ = $1 + $3; } | exp '-' exp { $$ = $1 - $3; } | exp '*' exp { $$ = $1 * $3; } | exp '/' exp { $$ = $1 / $3; } | '-' exp %prec NEG { $$ = -$2; } | exp '^' exp { $$ = pow ($1, $3); } | '(' exp ')' { $$ = $2; } ; http://dinosaur.compilertools.net/bison/bison_5.html#SEC27 http://dinosaur.compilertools.net/bison/bison_5.html#SEC27
- matiasb 10y agoGo is now written in Go
- spriggan3 10y agoIt used to be written in C before bootstrapping.
- rtpg 10y agoI don't think this is true. Loads of FP languages are self-hosted.
- sjrd 10y agoNot only FP. Lots of languages are self-hosted. So actually most modern parsers are not written in C, since they are written in the language they parse.
- pcr0 10y agoPython, 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.
- nv-vn 10y agoI 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.
- zachrose 10y agoIs 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 that? *Ok really it was a little bit of munging and then eval.
- nostrademons 10y agoWell, at some level every language compiles to a value rather than an executable program - the parse tree of a source module is a data structure describing the program, and the executable is a sequence of bytes that is interpreted by the processor. There's a continuum between this and what we think of as a "value" in programming terms (eg. an int or a string), but the boundary is fuzzy. It was pretty common at Google to write DSLs that compiled to a protobuf that'd be loaded into the server as configuration, for example. CUDA compiles programs into a buffer that is then sent to the GPU. HTML compiles into the DOM, which is a value that can be manipulated with Javascript.
- khedoros 10y agoConsider a run-length encoded image. One possible implementation is to have a command, a length value, then a sequence of 1 or more pixels. A "run" command's sequence would just be the color to repeat, while a "dump" command's sequence would be a number of pixels to output directly into the image. In a sense, that's a piece of source code that would "compile" into an image, if you take a loose interpretation of what it means to "compile". For other examples that may count, LaTeX and Postscript are both Turing-complete languages, usually compiled into a typeset document and a vector graphic image, respectively. You might call them Domain-Specific Languages, in general (the DSL mentioned by the sibling comment).
- tikhonj 10y agoSure. I would describe that as a domain-specific language (DSL), although not all domain-specific languages look like that. DSLs that don't encode any computation are sometimes described as "configuration" languages, but I'm not sure that term is particularly well-defined. Domain-specific languages are one of the most powerful yet underused techniques in programming—especially if the language can be embedded inside a rich, general-purpose language. We get to bear all the abstractive capability of language at our domain while also having general programming language techniques at our disposal (static analysis, optimization, type checking... etc). If we're really doing a good job we can even make sure our DSL is easy to reason about and has good semantics, using the theoretical tools developed to analyze general-purpose programs. One neat trick is that a lot of tools that are difficult to use on general-purpose languages like automatic verification or program synthesis can become far easier to apply and more practical when working with a small DSL. A lot of tools in this vein would be a multi-year research project to implement for a language like C but only a multi-week project for sufficiently constrained, well-behaved DSLs. And if you're designing your DSL, you can design it in tandem with this tooling, making the problem even more tractable.
- passiveobserver 10y agoThe dude writes good code. He's the author of Uglify.
- mikekchar 10y agoIt 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 regular grammars. The problem is that most grammars for programming languages, file formats, communication protocols, etc, are not regular grammars. With a regular grammar you can look at the current state and the next input symbol to determine the next state. With context free grammars you need to have a stack of states. With context sensitive grammars you need to have a stack of stacks states. With unrestricted grammars you are essentially screwed ;-) So your first task when you are designing a language or file format or communication protocol or whatever is to choose a simple grammar. If you choose a regular grammar then you can (and probably should) use regular expressions to parse it. With context free or context sensitive grammars you can often do lexical analysis (creating symbols that you then pass to your parser) with regular expressions, but you need something more complex for parsing the stream of symbols (i.e., you need to be able to put parser states on a stack). The problem that you often see is that people design things and have absolutely no idea what kind of grammar it is. They use regular expressions (or some ad-hoc code) and then try to keep track of state in global variables. If they happen to have a context sensitive grammar then chances are their parser simply will not work correctly no matter what they do. You may be wondering why people choose to use more complex grammars if it is harder to parse. The main reason is that complex grammars give you more options for expression. Sometimes it is extremely difficult or even impossible to represent something with a regular grammar. Having said that, though, you should almost always try to keep your grammars at least context free. Once you get into context sensitive grammars, the difficulty of parsing will either make your parser very difficult to implement or buggy as hell (usually both). Usually it is better to remove functionality than it is to move from a context sensitive to context free grammar. In the past I have often seen file formats that have unrestricted grammars (converting file formats used to be my job). People who do this should be replaced with programmers who know what they are doing :-P (1) - https://en.wikipedia.org/wiki/Chomsky_hierarchy https://en.wikipedia.org/wiki/Chomsky_hierarchy
- 10y ago
- moretti 10y agoThis is also a really good resource: https://github.com/thejameskyle/the-super-tiny-compiler https://github.com/thejameskyle/the-super-tiny-compiler
- anqurvanillapy 10y agoIt would be nice to read some books like Language Implementation Patterns for deeper thoughts after following some easy tutorials. That book is good for designing both DSL and general-purpose languages and explains multiple patterns with pros and cons, tradeoffs, etc. And also, it's better to check out some metasyntax notations, e.g. EBNF https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_F...
- chvid 10y agoIf anyone is interested in making a language in JS I can recommend the "Nearley" parser/lexer: http://nearley.js.org 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 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.
- joshmarinacci 10y agoEmunotes is really cool. A magical word processor. Is there a way to make functions that connect to expanded services?
- chvid 10y agoThanks; if you by expanded services mean integration with external pieces of user-defined javascript or data sources (such as a YQL query) it is a good and very potent idea. It is something I like to do a little further down the road. In the near future I am focusing on graphics and collaborative features.
- ralphc 10y agoThe new hotness seems to be transpiling, turning an enhanced language into Javascript. Any good resources for this?
- ehaliewicz2 10y agoIt's the same as any other compiler, just generate javascript out of the backend.
- matthewtoast 10y agoCool! 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 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.
- peter303 10y agoUNIX/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.
- eudox 10y agoParsing is the smallest part of writing a compiler.
- deadowl 10y agoIt's also one of the most conceptually difficult parts, which makes it a good learning opportunity.
- greglindahl 10y agoOne common criticism of compiler classes is that the first semester spends all of its time on parsing, so the students don't learn very much about compilers. And btw, there's an infinite number of conceptually difficult parts when it comes to optimization.
- Ace17 10y ago"It's also one of the most conceptually difficult parts" ... except all the others.
- ehaliewicz2 10y agoConceptually? I'd say it can be tedious, if done by hand, but the concepts are pretty simple.
- nnq 10y agoCool... 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-combinators http://theorangeduck.com/page/you-could-have-invented-parser... .