Skip to content

Latest commit

 

History

History
73 lines (52 loc) · 2.41 KB

Readme.md

File metadata and controls

73 lines (52 loc) · 2.41 KB

Building a language to do fizzbuzz

The Basics of a Compiler's Job

The job of a compiler is to translate input in one language (usually a high-level language) into a different output language (usually a lower-level language than the input). The main steps are:

  • Do lexical analysis to recognise the lexical elements. In several languages, = is different to ==, so you can't just read a single =. We call these lexical elements tokens.
  • Parse the input, i.e. recognise the syntax and structural elements of the input and ensure that they conform to the grammar of the language. For example, your language might have this decision-making

LLC features

I added statements to the language, specifiacally "print". Usage:

   print 2 + 6 * 5;
   print 18 - 18/3 + 10*2;

Each statement starts with the keyword print and is terminated with a semicolon. I have borrowed some ideas from the SubC compiler written by Nils M Holm. His code can be found online including a very awesome book

Resources

There's a lot of compiler resources out on the Internet. Here are the ones I'll be looking at.

Learning Resources

If you want to start with some books, papers and tools on compilers, I'd highly recommend this list:

Existing Compilers

While I'm going to build my own compiler, I plan on looking at other compilers:

  • SubC by Nils M Holm
  • Swieros C Compiler by Robert Swierczek
  • fbcc by Fabrice Bellard
  • tcc, also by Fabrice Bellard and others
  • catc by Yuichiro Nakada
  • amacc by Jim Huang
  • Small C by Ron Cain, James E. Hendrix, derivatives by others

Setting Up the Development Environment

  $ sudo apt-get install build-essential

install nasm for the asm code.

Trying It Out

The language is very simple, but here try it out:

$ make
$ make test

What's Next

Adding loops, variables and if statments. The goal really is to do fizzbuzz in my language