Entry 975

brainfuck

   

Submitted by Dark-Side on Aug. 27, 2008 at 5:36 p.m.
Language: Io. Code size: 1.1 KB.

brainfuck := Object clone do(
  stdin := File clone standardInput

  change := method(nb,
    newValue := (256 + mem at(id) + nb) %256
    mem atPut(id, newValue)
  )

  get := method(mem atPut(id, (stdin readLine at(0))))

  init := method(
    self mem := list(0)
    self id := 0
    self temp := 0
    self string := str
    self i := 0
  )

  isNull := method(mem at(id) == 0)

  move := method(nb,
    id = id + nb
    if(id < 0) then(mem atInsert(0, 0)
    ) elseif(id >= mem size) then(mem append(0))
  )

  show := method(mem at(id) asCharacter print)

  skip := method(toAdd, start, end,
    nb := 1
    while(nb > 0,
      i = i + toAdd
      string at(i) asCharacter switch(
        start, nb = nb + 1,
        end, nb = nb - 1)
    )
  )

  parse := method(str,
    init
    while(i >= 0 and i < str size,
      string at(i) asCharacter switch(
        "+", change(1),
        "-", change(-1),
        ">", move(1),
        "<", move(-1),
        ".", show,
        ",", get,
        "[", isNull ifTrue(skip(1, "[", "]")),
        "]", isNull ifFalse(skip(-1, "]", "["))
      )
      i = i+1
    )
  )
)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).