Crates.io | basicaf |
lib.rs | basicaf |
version | 0.1.2 |
source | src |
created_at | 2017-12-15 05:36:32.165289 |
updated_at | 2017-12-22 19:19:45.789975 |
description | A (Dartmouth) BASIC to Brainf**k compiler |
homepage | https://github.com/RyanMarcus/basicaf |
repository | https://github.com/RyanMarcus/basicaf |
max_upload_size | |
id | 43146 |
size | 239,058 |
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
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:
Ai
is a variable, not the i
th element of the A
array. To index an array, use A(i)
.PRINT
statementPRINT
statement does not automatically append a newlineTAN
, COS
, SIN
, ABS
, etc. are unavailable.Other than that, all major features are supported, including multi-dimensional arrays, GOTO
, GOSUB
, etc.
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.