Profile cover photo
Profile photo
Daniel Fabian
56 followers
56 followers
About
Posts

Because F# really can be used for useful things...

open System.Text
open Core.Printf

let compile prog =
    let indent = ref 0
    let compiled = StringBuilder()
    let gen fmt =
        compiled.Append(String.replicate !indent "    ") |> ignore
        kbprintf (compiled.AppendLine >> ignore) compiled fmt
    
    gen "open System"
    gen "let tape : int [] = Array.zeroCreate (1024*1024)"
    gen "let mutable pos = 0"

    for c in prog do
        match c with
        | '>' > gen "pos < pos + 1"
        | '<' > gen "pos < pos - 1"
        | '+' > gen "tape.[pos] < tape.[pos] + 1"
        | '-' > gen "tape.[pos] < tape.[pos] - 1"
        | '.' -> gen "Console.Write(char tape.[pos])"
        | ',' > gen "tape.[pos] < Console.Read()"
        | '[' -> gen "while tape.[pos] <> 0 do"; incr indent
        | ']' -> decr indent
        | _ -> ()

    compiled.ToString()
Add a comment...

Post has attachment
New post about the Y-Combinator, using F#, Haskell and Scheme.
Y-Combinator
Y-Combinator
typedandnerdy.blogspot.com
Add a comment...

Post has attachment
Wait while more posts are being loaded