Skip to content
/ bast Public

Compiler for Tiny Toy BASIC Dialect

License

Notifications You must be signed in to change notification settings

varbhat/bast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bast

Compiler for BAST(Basic Tiny) - A Tiny Toy Basic Dialect

Introduction

This compiler takes code written in bast and compiles(transpiles) to C code.You need to compile the C code with C compiler (gcc/tcc/clang are tested to work) to produce executable.

This provided the opportunity to learn about lexing,parsing and to learn more Golang. I believe that this toy language will not be used other than for educational purpose(for people like past-me).

Installation

You can download binary for your OS from Releases . Also , if you have Go installed , you can install bast by typing this in terminal.

go install github.com/varbhat/bast@latest

Usage

  • bast --help will print the help.

  • bast -in=filename.bast -out=filename.c will compile bast source filename.bast to C source filename.c.

  • You need to compile the emitted C file with C compiler like GCC/Clang/TCC.

  • cc ./filename.c -o filename (where cc is gcc/clang/tcc)

  • You can combine these steps :

bast -in=filename.bast -out=filename.c && cc filename.c -o filename

And then run the executable binary filename it produced.

Language

Language is small Dialect of BASIC same what AZHenly implemented.I only wrote compiler. Grammer file can be found at grammer.txt

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

You can find more examples at Examples

Acknowledgements

Thanks to AZHenly for their post Let's make a Teeny Tiny compiler which acted as reference. But , instructions in that post are for Python and the compiler is implemented in Python. I who wanted to know more about compiler , read it as reference , wrote the compiler from scratch in Golang.

Although the language for both the compilers are same,bast compiler is written from scratch in Golang ,implemented by instructions of their post.

License

MIT