Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is writing a compiler Hello World for F#? [closed]

Tags:

f#

I don't believe seeing this. It says:

For April, Chris Smith will be presenting on writing a Java to x86 Compiler in F#.

The presentation may go on for an hour or two which definitely is not enough to write a compiler. I've heard that F# is powerful, but this powerful?

Well, all I wanted to ask is this: Can you write a compiler in F# that quickly?

like image 843
Abdulsattar Mohammed Avatar asked Nov 26 '22 21:11

Abdulsattar Mohammed


1 Answers

Let's first start with a few corrections:

  • It's not a Java compiler, it's a compiler for a small subset of Java.
  • It doesn't say anywhere that the compiler will be written in the time, only that it will be explained.
  • In fact, it doesn't even say that, it says, it will be presented. Heck, I can present GCC in 3 minutes. It's not gonna be a very useful presentation, but it's gonna be a presentation.

That said, explaining a well-structured, simple compiler for a simple language implemented in a language which is well-suited for writing compilers within an hour is definitely feasible.

F# is a member of the ML family of languages (specifically, a close cousin of OCaml), and those are indeed well-suited for writing compilers. In fact, the reason why Robin Milner chose the name ML (meta language) for his language, was because it is specifically designed for writing compilers. A compiler is basically a big function (thus making it very natural to implement in a functional language) that detects patterns (thus making it very natural to implement in a language with pattern matching) and executes a little bit of code for each pattern it detects (thus making it very natural to implement in a language with first-class functions). And whaddayaknow? F# is a functional language with very sophisticated pattern matching facilities. Another nice feature is an expressive type system with algebraic data types and discriminated unions which makes it very easy to represent Abstract Syntax Trees.

At the Lang.NET Symposium Jason Olson gave a talk on F#, during which he showed some pieces of an interpreter that he is currently working on that demonstrate these features very well.

Fredrik Holmström is currently working on IronJS, an ECMAScript 3 implementation for the Dynamic Language Runtime. Take a look at the code, specifically the AST types and some of the analysis and parsing code.

Jonathan Tang's Write Yourself a Scheme in 48 Hours is another good example of writing an interpreter, this time in Haskell which shares many features with F#.

The 90 Minute Scheme to C compiler by Marc Feeley is a presentation about a Scheme compiler written in Scheme.

In Implementing Scheme in Ruby, James Coglan teaches the audience Scheme, live-codes and explains a Scheme interpreter in Ruby and writes a couple of sample Scheme programs, all in 15 minutes.

like image 71
Jörg W Mittag Avatar answered Jun 11 '23 15:06

Jörg W Mittag