Subscribe to The Madman Scribblings        RSS Feed
-----

BrainF*ck Interpreter In Just 3 LoC

Icon 1 Comments
I've managed to squeeze a BrainF*ck Interpreter into just 3 line of vb.net code.

Public Sub BF(Code As String)
  If Code.Aggregate(Of Integer)(0, Function(s, i) If(s < 0, s, If(i = "["c, s + 1, If(i = "]"c, s - 1, s)))) <> 0 Then Exit Sub
  Dim PC, Ptr As Integer : Dim Mem(30000) As Integer : Dim JMP As Func(Of Integer, Integer, Integer, Char,Char,Integer) = Function(PCx As Integer, NStep As Integer, Level As Integer,PushC As Char, PullC As Char) If(Code(PCx) = PushC, JMP(PCx + NStep, NStep, Level + 1,PushC,PullC), If(Code(PCx) = PullC, If(Level = 1, PCx, JMP(PCx + NStep, NStep, Level - 1,PushC,PullC)), JMP(PCx + NStep, NStep, Level,PushC,PullC))) :  Dim Instructions As New Dictionary(Of Char, Action) From {{"+"c, Sub() Mem(Ptr) = If(Mem(Ptr) = 255, 255, Mem(Ptr) + 1)}, {"-"c, Sub() Mem(Ptr) = If(Mem(Ptr) = 0, 0, Mem(Ptr) - 1)}, {"<"c, Sub() Ptr = If(Ptr = 0, Mem.Length, Ptr - 1)}, {">"c, Sub() Ptr = If(Ptr = Mem.Length - 1, 0, Ptr + 1)}, {"["c, Sub() PC = If(Mem(Ptr) = 0, JMP(PC, +1, 0, "["c, "]"c), PC)}, {"]"c, Sub() PC = If(Mem(Ptr) = 0,PC, JMP(PC, -1, 0, "]"c, "["c))}, {","c, Sub() Mem(Ptr) = Console.Read()}, {"."c, Sub() Console.Write(Convert.ToChar(Mem(Ptr)))}}
  For PC=0 To Code.Length-1 :Dim q=If( Instructions.ContainsKey(Code(PC)),Sub() Instructions(Code(PC))(),Sub() Exit Sub  ): Q(): Next
End Sub




Hello World!

Option Strict On
Module Module1

  Sub Main()
    BF("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.")
  End Sub
End Sub

1 Comments On This Entry

Page 1 of 1

AdamSpeight2008 Icon

13 September 2013 - 10:27 AM
:bananaman: 1 LoC BrainF*ck Interepter :shuriken:
  Public Sub BF(Code As String)
    If Code.Aggregate(Of Integer)(0, Function(s, i) If(s < 0, s, If(i = "["c, s + 1, If(i = "]"c, s - 1, s)))) = 0 Then Dim PC=0, Ptr=0, Mem(30000) As Integer : Dim JMP As Func(Of Integer, Integer, Integer, Char, Char, Integer) = Function(PCx As Integer, NStep As Integer, Level As Integer, PushC As Char, PullC As Char) If(Code(PCx) = PushC, JMP(PCx + NStep, NStep, Level + 1, PushC, PullC), If(Code(PCx) = PullC, If(Level = 1, PCx, JMP(PCx + NStep, NStep, Level - 1, PushC, PullC)), JMP(PCx + NStep, NStep, Level, PushC, PullC))) : Dim Instructions As New Dictionary(Of Char, Action) From {{"+"c, Sub() Mem(Ptr) = If(Mem(Ptr) = 255, 255, Mem(Ptr) + 1)}, {"-"c, Sub() Mem(Ptr) = If(Mem(Ptr) = 0, 0, Mem(Ptr) - 1)}, {"<"c, Sub() Ptr = If(Ptr = 0, Mem.Length, Ptr - 1)}, {">"c, Sub() Ptr = If(Ptr = Mem.Length - 1, 0, Ptr + 1)}, {"["c, Sub() PC = If(Mem(Ptr) = 0, JMP(PC, +1, 0, "["c, "]"c), PC)}, {"]"c, Sub() PC = If(Mem(Ptr) = 0, PC, JMP(PC, -1, 0, "]"c, "["c))}, {","c, Sub() Mem(Ptr) = Console.Read()}, {"."c, Sub() Console.Write(Convert.ToChar(Mem(Ptr)))}}: For PC = 0 To Code.Length - 1 : Dim q = If(Instructions.ContainsKey(Code(PC)), Sub() Instructions(Code(PC))(), Sub() Exit Sub) : q() : Next
 End Sub

0
Page 1 of 1

Search My Blog

Recent Entries

Recent Comments