Skip to content

RyanMarcus/basicaf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BASIC AF

A (Dartmouth) BASIC to Brainf**k Compiler

crates.io badge Codeship Status for RyanMarcus/basicaf

This is a compiler from a variant of Dartmouth BASIC (see notes below) to Brainf**k. It was inspired by Peter Norvig's BASIC interpreter "pytude" -- what easier way to interpret a language than first compiling it into an esoteric one?

The compiler is written in Rust, and uses nom parser combinators, the clap command line option parser, and Saghm Rossi's unescape crate.

To install:

cargo install basicaf

To compile a BASIC program:

basicaf input.db

To execute a Brainf**k program:

basicaf -e program.bf

For more options, see:

basicaf --help

The BASIC variant

This program:

5  REM Compute the first 20 Fibonacci numbers 
10 DIM F(20)
15 LET F(0) = 0
20 LET F(1) = 1
25 FOR I = 2 TO 20
30 LET F(I) = F(I-1) + F(I-2)
35 NEXT I
40 FOR X = 0 TO 20
45 PRINT "F(", X, ") = ", F(X), "\n"
50 NEXT X
55 END

Get compiled into:

[-]>[-]<[>+<-][-]>[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]<[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[-]>[>>>[-<<<<+>>>>]<[->+<]<[->+<]<[->+<]>-]>>>[-]<[->+<]<[[-<+>]<<<[->>>>+<<<<]>>-]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<[-]+>[-]<[>+<-][-]+>[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]<[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[-]>[>>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[<<+>>>+<-]>[<+>-]<<[>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]<[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>>>>>>>>>>>>>[-]>[>>>[-<<<<+>>>>]<[->+<]<[->+<]<[->+<]>-]>>>[-]<[->+<]<[[-<+>]<<<[->>>>+<<<<]>>-]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>[-]>[-]<>+++++[<++++>-]<>[-]<<[-]>>>[-]>[-]<<<<<[>>>+<<+<-]>[<+>-][-]>>[<[<+>>>+<<-]>>[<<+>>-]+<<<[>>>-<<-<[-]]>>>[>+<[-]]<-]>>[<<+>>-]<<[<<+>>-]<[<+>-]<]>[-]<[-]>[<+>-]>[-]>[-]<>+++++[<++++>-]<>[-]<<[-]>>>[-]>[-]<<<<<[>>>+<<+<-]>[<+>-][-]>>[<[<+>>>+<<-]>>[<<+>>-]+<<<[>>>-<<-<[-]]>>>[>+<[-]]<-]>>[<<+>>-]<<[<<+>>-]<[<+>-]<[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------.[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<<<<<<<[>+<-]>[>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]++++++++[<++++++>-]>[<<+>>-]>[<<+>>-]<<]>]<[->>++++++++[<++++++>-]]<[.[-]<]<<[-]+++++++++++++++++++++++++++++++++++++++++.---------.+++++++++++++++++++++++++++++.-----------------------------.[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]<[>+<-][-]>[>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[-]<<<[-]>[>>>[-<<<<+>>>>]<<[->+<]<[->+<]>-]>>>[-<+<<+>>>]<<<[->>>+<<<]>[[-<+>]>[-<+>]<<<<[->>>>+<<<<]>>-]<<>>>[<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<<<<<<<[>+<-]>[>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]++++++++[<++++++>-]>[<<+>>-]>[<<+>>-]<<]>]<[->>++++++++[<++++++>-]]<[.[-]<]<<[-]++++++++++.<<+>>[-]>[-]<>+++++[<++++>-]<>[-]<<[-]>>>[-]>[-]<<<<<[>>>+<<+<-]>[<+>-][-]>>[<[<+>>>+<<-]>>[<<+>>-]+<<<[>>>-<<-<[-]]>>>[>+<[-]]<-]>>[<<+>>-]<<[<<+>>-]<[<+>-]<]

This variant of BASIC is very similar to Norvig's and the original, with a few notable exceptions:

  • Multi-letter variable names are allowed. As a result, Ai is a variable, not the ith element of the A array. To index an array, use A(i).
  • Commas are required between elements of a PRINT statement
  • The PRINT statement does not automatically append a newline
  • Semicolons are not allowed at the end of non-comment lines
  • Non-reducible control flow graphs are not supported (not a problem for most code)
  • No support for floating point or negative values. As a result, TAN, COS, SIN, ABS, etc. are unavailable.

Other than that, all major features are supported, including multi-dimensional arrays, GOTO, GOSUB, etc.

Examples

See the examples/ directory for sample BASIC (.db) inputs and Brainf**k (.bf) outputs. You can run the output online here, or using basicaf -e. Note that the last two examples require you to select the 32-bit cell size option.

About

A BASIC to Brainf**k compiler written in Rust

Resources

License

GPL-3.0, GPL-3.0 licenses found

Licenses found

GPL-3.0
LICENSE
GPL-3.0
COPYING

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages