go>> bast>> 返回
项目作者: varbhat

项目描述 :
Compiler for BAST(Basic Tiny) - A Tiny Toy Basic Dialect
高级语言: Go
项目地址: git://github.com/varbhat/bast.git
创建时间: 2020-07-28T16:15:45Z
项目社区:https://github.com/varbhat/bast

开源协议:MIT License

下载


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.

  1. 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 :

  1. 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

  1. PRINT "How many fibonacci numbers do you want?"
  2. INPUT nums
  3. PRINT ""
  4. LET a = 0
  5. LET b = 1
  6. WHILE nums > 0 REPEAT
  7. PRINT a
  8. LET c = a + b
  9. LET a = b
  10. LET b = c
  11. LET nums = nums - 1
  12. 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