
Make Your Own Programming Language - Pirate-of-SV
http://mattias-.github.io/2015-01-03/DIY-Make-Your-Own-Programming-language/
======
anindyabd
Great post. Recently I wrote an interpreter for Scheme using Haskell using the
guide "Write Yourself a Scheme" [1]. It was great fun, and it seems that it
will be fairly straightforward to write a new domain-specific language using
Haskell, using the same principles. That said, it was never clear to me how
something like that would work in a non-purely-functional language, and so the
fact that the OP used Rust is very exciting.

[1]
[http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_H...](http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours)

~~~
rasur
This is a great tutorial, although the one complaint I had was the errors in
the pdf version (can't remember the specifics).. took me a while to realise
the html version was far superior.

Regardless of my complaints, it was an interesting experience.

~~~
anindyabd
Yes, I came to the same realization. One of the problems I had with the pdf
version was that the flags given for compiling the program did not work
correctly for my version of GHC.

~~~
rasur
That rings bells, but I think there were some others too - _sigh_ my memory :/

------
thomasfoster96
I started making a language a few months ago. I found Douglas Crockford's
parser from his Top Down Operator Precedence paper pretty useful, which is
here:
[http://javascript.crockford.com/tdop/index.html](http://javascript.crockford.com/tdop/index.html)

When looking for resources on making toy languages, most of them said not to
bother with a parser and use a pre-built solution. I think they're probably
wrong - the parser is actually pretty easy (relative to the rest of the
process of getting a language working) and at a stretch, fun.

~~~
ioddly
The more you do yourself, the more you will learn. If you're just learning and
not trying to build the next big programming language, try and do some of the
low level stuff yourself. Write a parser, write a garbage collector and a
virtual machine, re-write the compiler in the language itself. Go hog wild.

~~~
JadeNB
> The more you do yourself, the more you will learn.

This is true _once you complete the project_ , but it seems like it can be
dangerous: if you start off your mission to make a programming language by
soldering components on a breadboard, say, then, you will learn something, to
be sure, but you will probably get so embroiled in preliminaries that you will
never learn what you _intended_ to learn. (Of course, sometimes learning
something other than what you intended is a good thing ….)

------
wldlyinaccurate
The author appears to have added a CNAME since this was posted to HN. The
updated URL is [http://blog.ppelgren.se/2015-01-03/DIY-Make-Your-Own-
Program...](http://blog.ppelgren.se/2015-01-03/DIY-Make-Your-Own-Programming-
language/)

------
fcanela
Server not found error when connecting. Tried removing the "-" but didn't work
neither.

~~~
Hemospectrum
Apparently the hyphen isn't there by mistake. Here's the blog source repo:
[https://github.com/Mattias-/Mattias-.github.io](https://github.com/Mattias-/Mattias-.github.io)

Are you perhaps using a browser or a DNS stack that rejects this type of
domain name?

~~~
th0br0
The question should probably be: which OS are you using?

It's quite interesting... dig seems to resolve the domain just fine, but
neither chrome, curl nor firefox seem to be able to open the page. (tested on
Fedora/CentOS/Android)

~~~
blablablaat
Same here. My dnsmasq server responds with the domain name when I try it with
'host', but curl and firefox fail on Debian and Ubuntu with only that 1 DNS
server in resolv.conf. Curl says "curl: (6) Could not resolve host:
mattias-.github.io".

However, curl and firefox seem to be RFC-compliant. RFC952 states: "The last
character must not be a minus sign or period."

------
Tenjou
Nice article, inspired me to invest time in the future to learn Rust.

Also working on JavaScript native compiler + developing a new language on top
as subset for JavaScript:
[https://github.com/InfiniteFoundation/dopple](https://github.com/InfiniteFoundation/dopple)
(front page info is outdated though).

------
nadams
After writing a lexer, parser, compiler and virtual machine in C++ and ported
parts to Python, Java and FreeBASIC - I think this is a good starting point.

Most of the concepts are pretty simple - it's when you get into if/while/for
statements. Especially if you have nested if/while/for statements.
Conceptually easy to implement using recursion but during the compilation
phase keeping track of where you are at can be...difficult. I'll be the first
to admit my implementation sucked but it worked.

------
alediaferia
Let's add the next thing I won't have time to play with. Anyway, this is a
great post and I look forward to the whole guide. Keep it up!

~~~
Pirate-of-SV
Thank you! If I can at least make one person interested I'm happy to continue.

------
dochtman
Fun!

I've been working on Python-like systems language with a compiler in Python
targeting LLVM IR, and it's been cool (though so much work to get a halfway
usable language!).

[https://github.com/djc/runa](https://github.com/djc/runa) if anyone's
interested.

~~~
Pirate-of-SV
Did you create an interpreter for the language before you started on the
compiler? I think it's not much overhead and you can run tests with it to make
sure you get all the lexing and parsing done correctly. Then you just replace
the evaluation with code generation and BAM! you've got a compiler.

~~~
dochtman
No, I didn't. For my language, being compiled down to machine code has been
one of the design goals. Also, it doesn't seem to me like building an
interpreter is significantly simpler than building a compiler (at least given
the fact that I've left a bunch of the hard stuff to LLVM magic). LLVM IR
isn't that hard to write.

------
jwatte
I would argue that this statement is a mistake that should have been foreseen:
"negative integers isn't literals!" Specifically, twos complement signed
integers will be a bitch of you continue down that path.

~~~
dezgeg
On the other hand, having negative integers as literals will cause even harder
problems with subtraction (as in "2-2")

~~~
Pirate-of-SV
In a language like C the parser is going to try to parse Expr - Expr but not
Expr Expr since there's no such construct in the language to put two
expressions after each other.

------
0942v8653
Wow. I'm sure I've seen Rust before (in fact, I have it installed) but I'm
just surprised how similar it is to Swift.

EDIT: s/is/looks/. I meant the syntax.

~~~
Hemospectrum
The design of Swift was influenced by Rust. However, most of the resemblances
are superficial or relate only to areas where Rust in turn was heavily
influenced by other languages. For example, Rust's memory management model
could not work in Swift (because this is an area where Swift remains
compatible with Objective-C).

------
loop2
I started one a while ago, but I became fascinated by the "middle end" and
bought a compiler textbook. Now I have SSA optmizations but no strings still.

------
marcofiset
Very interesting read. I am myself writing a series on the same subject and I
was pretty inspired by your way of writing. Your article is paced a lot faster
than what I have written so far and I really like the style.

I submitted my own posts to HN also, but I did not get the same attention as
you did, congratulations!

I'm looking forward to the next iteration ;)

------
gpsarakis
Great read! An effort on a DSL using PLY (Python LEX-YACC implementation):
[https://github.com/georgepsarakis/quick-
bash](https://github.com/georgepsarakis/quick-bash) .

It is actually a transcompilation to Bash from a functional language, using
Python for the intermediate processing.

------
Gurrewe
I started creating a language by myself a few days ago, this is a great read.
Thanks!

~~~
Pirate-of-SV
Nice, what kind of language? I think nice beginner languages are Query
languages (like SQL) or markup languages (like Markdown).

~~~
Gurrewe
I'm aming for a scripting language that is a sort of mix between Lua and Go.
The project is currently very overambitions, but it is fun! :)

------
jmcdonald-ut
This was a really cool read :). I'm working on my own DSL for generating code.
I'll make a Show HN soon, but it was really cool to see someone else doing
something so similar.

~~~
Pirate-of-SV
Nice what kind of code is it going to be used for to generate? A specific
language or something more generic.

It would be cool with a DSL to generate low level code like X86 ASM, LLVM IR
or JVM bytecode. That would be so meta!

~~~
jmcdonald-ut
Heh, not so complicated. It's just used to generate boiler plate code. Kind of
like data object files in C#, Java, PHP, etc. That would be really cool though
:).

------
rtpg
whitespace sensitivity is a pretty big pain to deal with in the current
toolkits available for building out a programming language, unfortunately.

The pain is compared to the absolute and total simplicity of defining a
grammar when you're in a whitespace-insensitive setting (parser generators are
easy to express and efficient in deterministic grammars).

We have a lot of good parsing tools out there, but we might be missing the
right tools for building out good lexers

------
mataug
I'm getting started with LLVM, Just downloaded the source and now wondering
how to go about things.

------
msie
It would be cool if we could add a "Rust" tag to this entry. I almost didn't
give this link a second look because I thought it was an older link. Glad I
did give it a second look.

