6 people like it.

Brainfuck

A minimal Brainfuck Interpreter (<30 lines) running Hello World!

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
let run (program:string) =
    let b = Array.create 30000 0uy
    let p, pc = ref 0, ref 0
    let execute () =
        match program.[!pc] with
        | '>' -> incr p; incr pc
        | '<' -> decr p; incr pc
        | '+' -> b.[!p] <- b.[!p]+1uy; incr pc
        | '-' -> b.[!p] <- b.[!p]-1uy; incr pc
        | '.' -> b.[!p] |> char |> printf "%O"; incr pc
        | ',' -> b.[!p] <- System.Console.Read() |> byte; incr pc
        | '[' -> 
            if b.[!p] = 0uy then while program.[!pc] <> ']' do incr pc
            else incr pc
        | ']' ->
            if b.[!p] <> 0uy then while program.[!pc] <> '[' do decr pc
            else incr pc
        | _ -> incr pc
    while !pc < program.Length do execute ()
run "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
val run : program:string -> unit

Full name: Script.run
val program : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val b : byte []
module Array

from Microsoft.FSharp.Collections
val create : count:int -> value:'T -> 'T []

Full name: Microsoft.FSharp.Collections.Array.create
val p : int ref
val pc : int ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val execute : (unit -> unit)
val incr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.incr
val decr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.decr
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
namespace System
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.Read() : int
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
property System.String.Length: int

More information

Link:http://fssnip.net/ci
Posted:11 years ago
Author:Phillip Trelford
Tags: interpreter