basicaf

Crates.iobasicaf
lib.rsbasicaf
version0.1.2
sourcesrc
created_at2017-12-15 05:36:32.165289
updated_at2017-12-22 19:19:45.789975
descriptionA (Dartmouth) BASIC to Brainf**k compiler
homepagehttps://github.com/RyanMarcus/basicaf
repositoryhttps://github.com/RyanMarcus/basicaf
max_upload_size
id43146
size239,058
Ryan Marcus (RyanMarcus)

documentation

README

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.

Commit count: 20

cargo fmt