GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ssloy/tinycompiler: Writing a compiler in a week-end

BranchesTags
Open more actions menu

Latest commit

 

History

92 Commits
92 Commits

Folders and files

NameName
Last commit message
Last commit date
test-programs
test-programs
 
 
LICENSE.txt
LICENSE.txt
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
analyzer.py
analyzer.py
 
 
compiler.py
compiler.py
 
 
lexer.py
lexer.py
 
 
parser.py
parser.py
 
 
symtable.py
symtable.py
 
 
syntree.py
syntree.py
 
 
transasm.py
transasm.py
 
 
transasm_recipe.py
transasm_recipe.py
 
 

Repository files navigation

TinyCompiler - a 500-ish lines of code compiler in a weekend

Have you ever wondered how a compiler works, but you never found courage to find out? Then this project is for you (N.B.: a detailed description is available here).

I have never had the chance to look under the hood either, but one week-end I have decided to to write a translator from the esoteric programming language wend (short for week-end), which I just invented myself, into regular GNU assembly. The goal is to keep the code as tiny as possible, 500-ish lines of python sounds great.

Here is a program that uses virtually all concepts in Wend:

main() {
    // square root of a fixed-point number
    // stored in a 32 bit integer variable, shift is the precision

    int sqrt(int n, int shift) {
        int x;
        int x_old;
        int n_one;

        if n > 2147483647/shift { // pay attention to potential overflows
            return 2 * sqrt(n / 4, shift);
        }
        x = shift; // initial guess 1.0, can do better, but oh well
        n_one = n * shift; // need to compensate for fixp division
        while true {
            x_old = x;
            x = (x + n_one / x) / 2;
            if abs(x - x_old) <= 1 {
                return x;
            }
        }
    }

    int abs(int x) {
        if x < 0 {
            return -x;
        } else {
            return x;
        }
    }

    // 25735 is approximately equal to pi * 8192;
    // expected value of the output is sqrt(pi) * 8192 approx 14519

    println sqrt(25735, 8192);
}

run tests

make test

Graphics!

It is so dull to compute Fibonacci numbers, so here are more eyecandy examples for our compiler, check test-programs/gfx/*.wend files.

make gfx

Mandelbrot set

Ray tracer

Zero-player breakout game

Fire

Sunset race

Metaballs

About

Writing a compiler in a week-end

Resources

Readme
WTFPL license
Activity

Stars

483 stars

Watchers

3 watching

Forks

29 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.