3 releases
Uses old Rust 2015
0.1.2 | Dec 22, 2017 |
---|---|
0.1.1 | Dec 22, 2017 |
0.1.0 | Dec 15, 2017 |
#7 in #bf
135KB
3K
SLoC
BASIC AF
A (Dartmouth) BASIC to Brainf**k Compiler
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 thei
th element of theA
array. To index an array, useA(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.
game_of_life.db
(output) prints 10 generations of Conway's Game of Life.fib.db
(output) computes the first 20 Fibonacchi numbers.collatz.db
(output) prints the first 25 hailstone sequences.
Dependencies
~5.5MB
~110K SLoC