Intro to programming language development series in JS

综合技术 2018-12-08 阅读原文

Take this article as a sort-of short introductory post. So, recently I've come up with an idea to create a programming language . Seams easy, isn't it? :sweat_smile: Is it possible? - Yes. Is it worth it? - Kind-of. You see, by creating a language you can learn certain concepts that you wouldn't otherwise. It just makes you a better programmer . Also, it's fun. 😉 So, without further ado, let's create a programming language !

Photo by
Glenn Carstens-Peters /
Unsplash

Let's get prepared!

Well, I think it's gonna be an interesting (and quite long) series. Keep in mind, that even this will be some new experience even for me. Like many others, I'm just an ordinary guy without deeper knowledge of language creation. I just plan on creating something from nothing (or rather from other sources on the web). Now, some basic information. The language I'm going to make will be named AIM (because I'm aiming at my goal of creating a language and it's a nice shortcut). It's meant to be multi-paradigm, statically typed, compiled language. For this, I'll be using LLVM (more on that later) and Node.js . As you might know, programming languages and similar lower-level stuff are done using C/C++, so I take Node.js - the unusual project is being made using unusual tools. Also, I'm a bit more experienced with Node.js that C/C++. :+1:

So, if you're ready, let's first create a TODO list:

  1. Find a cool name ( done! ) :grinning:
  2. Create / imagine language's syntax.
  3. Create lexer.
  4. Create parser.
  5. Create compiler.

These are the most basic and definitely required steps. Next, going forward in the future without much order:

  • Standard library implementation
  • Runtime library implementation
  • Self-hosting/compiler bootstrapping
  • Specification/documentation
  • Examples/other libraries/first app

Now, let's get back to the ordered list and discuss each point for a second or two. Also, I'll be discussing each of these points in the following post, so I'll be brief.

Photo by
Fabian Grohs /
Unsplash

Create language syntax

Syntax and the general idea of it is one of, if not the most important when designing a language. Many programming languages have similar syntaxes. They base on what has already been proven to work and that's fine. That's what you should consider when creating a programming language of your own. But, as I'm not really planning on creating this particular language for general purpose (but who knows :joy:) I most likely won't follow this advice in order to create something new and fresh. Of course, it won't be as radical as this , so don't panic, just a little bit different. Still, I don't know exactly how will it look like but I'm sure it won't be so standard.

Create lexer

Nothing fancy here. A lexer is just a software that takes your code and creates a series of tokens with additional metadata for it. At least that's what I know for now. Of course there's a plan for posts about each step of language development, so be patient.

Create parser

A parser is much more important that lexer. Parser, from a list of tokens created by the lexer, creates what's called AST (Abstract Syntax Tree). It's basically a representation of parsed code in a form of the data tree, which allows interacting with it programmatically. So, it's important for it to be fast and well-design since it'll be used in things like linters, pretty-printers and so on. Generally by software that's meant to interact with the syntax of your language directly. Also, AST is what will be used in the stage of compiling the language to its machine code representation. Which brings us to the last, most important step...

Create compiler

This is what all the programming languages are about (the compiled ones, of course) - to just being able to execute. The compiler just takes your code and outputs (usually) the machine code. I've decided to implement the compiler with the help from LLVM (Low-Level Virtual Machine), so-called compiler infrastructure library. This piece of software has been used to create languages like (most notably) Rust and Swift together with one of the most popular C/C++ compiler front-ends - Clang , so it must be good enough for this project. 😉 It's much easier to use the C API of the LLVM to generate machine code than to use Assembly, obviously. Still, it's very big and complicated, so I'm going to spend a fair bit of time with its documentation, which is a very well-written one. Here the Node.js C binding may be necessary.

Photo by
Vlad Bagacian /
Unsplash

Journey begins...

So here you go with what's coming next. I'm not covering the other points as only time will tell what will come in the future. After this little intro, I hope you and me myself will enjoy this journey. Again, keep in mind that I'm not an expert, I'll just share my own experience, so if you got any tips for me, I'll be grateful. Now, the next post is coming about the syntax of this creation, so if you want to follow me on Twitter for any updates. Once more, thank you for reading this intro and let the journey begin...:+1:

责编内容by:Areknawo 【阅读原文】。感谢您的支持!

您可能感兴趣的

JavaScript面向对象轻松入门之概述(demo by ES5、ES6、TypeScript)... 写在前面的话 这是一个JavaScript面向对象系列的文章,本篇文章主要讲概述,介绍面向对象,后面计划还会有5 篇文章, 讲抽象、封装、继承、多态,最后再来一个综合。 说实话,写JavaScript面向对象...
JavaScript Promises, road to async/await Introduction Asynchronous and synchronous are kinda complex concepts for beginners. Even some experienced developers f...
using commander.js in node for command line apps? im trying to build a command line app using commander.js which is node module? Commander.js ...
Remixing other people’s websites Why would you want to change the UI of a site you visit? Well, there're loads of reasons: Personalise your exper...
Nim Improves Async There's a new release of Nim, the systems programming language that focuses on performance, portability and expressivene...