jaredforth / bf

A minimalist programming language, implemented in Racket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bf

This is an implementation of a minimal programming language abbreviated as bf.

This is part of the excellent book Beautiful Racket, which I would highly recommend checking out.

Grammar

The grammar for this language, in Extended Backus–Naur form is as follows:

bf-program : (bf-op | bf-loop)*
bf-op      : ">" | "<" | "+" | "-" | "." | ","
bf-loop    : "[" (bf-op | bf-loop)* "]"

Directory Structure

.
├── expander.rkt
├── main.rkt
├── parser.rkt
└── reader.rkt

This repository contains three Racket modules and a main.rkt, which serves as the package entry point. These modules do what their name suggests. parser.rkt contains the grammar for the language and is imported by reader.rkt, which tokenizes and creates a parse tree of the source string. expander.rkt uses macros to tie everything together and convert the parse tree into Racket functions to implement logic defined by the bf language input.

Usage

Installing the package

  • This requires racket
  • Clone this repository and cd into the bf directory
  • Run raco pkg install

Testing

Once the package is installed, the following programs can be run using #lang bf in DrRacket.

Hello, World

#lang bf
++++++[>++++++++++++<-]>.
>++++++++++[>++++++++++<-]>+.
+++++++..+++.>++++[>+++++++++++<-]>.
<+++[>----<-]>.<<<<<+++[>+++++<-]>.
>>.+++.------.--------.>>+.

Factorial

#lang bf
>++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]
<[>+<-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-]+>>>>>]<<<<<
[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]
<<<<[<[>+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<
[<[>+>+<<-]>.<<<<<]>.>>>>]

About

A minimalist programming language, implemented in Racket

License:MIT License


Languages

Language:Racket 100.0%