3
votes

I am intrigued by lower level concepts such as operating systems, algorithms, mathematically proving software correctness, etc.

What intrigued me most are compilers. I started learning about them and wish to implement a toy compiler for the sake of learning and maybe it turns out to be something big (you never know, right?).

My goal is to implement lua-like statically typed, thread-aware programming language (first goal is interpreter with basic stack operations of course). My problem though is that I have little to no interest in lower level languages such as c,c++ or pascal and I was wondering whether it would be possible to implement a compiler for statically typed language in a dynamically typed environment?

I am interested to implement such a language in lua or python (lua suits me better though).

3
Sure, that's possible. A compiler is just a program that translates strings to other strings, usually via syntax trees, which is something most high-level languages can do. However, you will have to understand the language you are compiling to... - Fred Foo
I would advise you to just try it, see what you can figure out in a day, and then a week. Once you've done it you'll have much more answers to your questions than you'd ever have been able to obtain from anywhere else. - Keldon Alleyne

3 Answers

6
votes

Easy question: yes, absolutely, it's done all the time. Most compiled languages are ultimately "bootstrapped" such that their own compilers are written in the language itself: for example, javac, the standard Java compiler, is written in Java.

2
votes

Yes it is. In fact, the compiler for the latest version of C# will be implemented... in C#: http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx

(I'm talking about the Roslyn C# library:)

The foundation of this work is a new C# compiler, written in C# (and a new VB compiler written in VB too, see the VB Team blog for details).

Edit

Check this out: https://bitbucket.org/pypy/pypy

It's an implementation of Python written in Python.

-1
votes

As other people have point out,yes this is possible,but...

1- With a compiler, usually the coupling between the programming language being implemented and the one used to do the implementation is pretty thin. On the other hand, various languages have various communities and various "domain of expertise". For writing compiler, the fore-front functional programming languages (Haskell,Ocaml, etc...) have much more rich library of used cases and examples, you might want to explore that.Also their community are full of programming language geek that are more than happy to share their experiences.

2-If you are interested on writing an interpreter (or a translator) then the story is different.Some of the characteristics of the host langague strongly influence the ease of witch some feature in your interpreted language can be implemented.For example, application order, dynamic scoping and closures are much easier to implement in the host and interpreted languages follow the same rules.