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: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
let run (program:string) =
    let b = Array.create 30000 0uy
    let p, pc = ref 0, ref 0
    let rec 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.ReadKey().KeyChar |> byte; incr pc
        | '[' -> if b.[!p] = 0uy then findend -1 else incr pc
        | ']' -> if b.[!p] <> 0uy then findstart -1 else incr pc        
        | _ -> incr pc
    and findend count =
        match program.[!pc] with
        | ']' -> if count > 0 then incr pc; findend (count-1)        
        | '[' -> incr pc; findend (count+1)
        | _ -> incr pc; findend count          
    and findstart count =
        match program.[!pc] with
        | '[' -> if count > 0 then decr pc; findstart (count-1)        
        | ']' -> decr pc; findstart (count+1)
        | _ -> decr pc; findstart count      
    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.ReadKey() : System.ConsoleKeyInfo
System.Console.ReadKey(intercept: bool) : System.ConsoleKeyInfo
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
val findend : (int -> unit)
val findstart : (int -> unit)
val count : int
property System.String.Length: int

More information

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