AZHenley / teenytinycompiler

A small BASIC-to-C compiler written in Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

teenytinycompiler

This is a small compiler to demonstrate how compilers work to my students. It compiles our own dialect of BASIC to C, while being written in Python.

Read the tutorial: Let's make a Teeny Tiny compiler, part 1 as well as part 2 and part 3

The code is split into folders (part1, part2, part3) that correspond with the complete code from the parts of the tutorial. See part3 for the finished compiler.

It supports:

  • Numerical variables
  • Basic arithmetic
  • If statements
  • While loops
  • Print text and numbers
  • Input numbers
  • Labels and goto
  • Comments

Example code:

PRINT "How many fibonacci numbers do you want?"
INPUT nums
PRINT ""

LET a = 0
LET b = 1
WHILE nums > 0 REPEAT
    PRINT a
    LET c = a + b
    LET a = b
    LET b = c
    LET nums = nums - 1
ENDWHILE

About

A small BASIC-to-C compiler written in Python.

License:MIT License


Languages

Language:Python 99.0%Language:Shell 1.0%