Skip to content

Instantly share code, notes, and snippets.

@takahisa
Created August 17, 2011 22:45
Show Gist options
  • Save takahisa/1152835 to your computer and use it in GitHub Desktop.
Save takahisa/1152835 to your computer and use it in GitHub Desktop.
HeadacheによるBrain F*ckのソースコードだけどどっかの括弧が対応取れてなくてバグってる。あとジャンプ命令あたりも怪しい
let source : "+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+.\0"
let item :
lambda n ->
lambda list ->
if ( = n 0 )
then head list
else item ( - n 1 ) ( tail list )
let state :
lambda src ->
lambda ptr ->
lambda dat ->
lambda idx -> ( src , ptr , dat , idx )
let move :
lambda n ->
lambda s -> ( item 0 s , ( + n ( item 1 s ) ) , item 2 s , item 3 s , item 4 s )
let increment :
lambda v ->
lambda n ->
lambda list -> update list n ( + ( item n list ) v )
let decrement :
lambda v ->
lambda n ->
lambda list -> update list n ( + ( item n list ) ( neg v ) )
let peek :
lambda s -> ( item ( item 1 s ) ( item 0 s ) , s )
let read :
lambda s -> ( item ( item 1 s ) ( item 0 s ) , move 1 s )
let back :
lambda s -> ( item ( - ( item 1 s ) 1 ) ( item 0 s ) , move ( neg 1 ) s )
let jump_] :
lambda s ->
if ( = ( item 0 ( read s ) ) "]" )
then ( item 1 ( read s ) )
else ( jump_] ( move 1 s ) )
let jump_[ :
lambda s ->
if ( = ( item 0 ( back s ) ) "[" )
then ( item 1 ( back s ) )
else ( jump_[ ( move ( neg 1 ) s ) )
let command :
lambda x ->
if ( = ( head x ) "+" ) then command ( read ( increment 1 3 ( item 1 x ) ) ) else
if ( = ( head x ) "-" ) then command ( read ( decrement 1 3 ( item 1 x ) ) ) else
if ( = ( head x ) "<" ) then command ( read ( move ( neg 1 ) ( item 1 x ) ) ) else
if ( = ( head x ) ">" ) then command ( read ( move 1 ( item 1 x ) ) ) else
if ( = ( head x ) "." ) then head ( command ( read ( item 1 x ) ) , puts ( item ( item 3 ( item 1 x ) ) ( item 2 ( item 1 x ) ) ) ) else
if ( = ( head x ) "[" ) then ( if ( = ( item ( item 3 ( item 1 x ) ) ( item 2 ( item 1 x ) ) ) 0 ) then ( command ( read ( jump_] ( item 1 x ) ) ) ) else ( command ( read ( item 1 x ) ) ) ) else
if ( = ( head x ) "]" ) then ( if ( = ( item ( item 3 ( item 1 x ) ) ( item 2 ( item 1 x ) ) ) 0 ) then ( command ( read ( item 1 x ) ) ) else ( command ( read ( jump_[ ( item 1 x ) ) ) ) ) else
if ( = ( head x ) "\0" ) then nil else
( command ( read ( item 1 x ) ) )
let memory :
lambda n -> if ( = n 0 ) then 0 else cons 0 ( memory ( - n 1 ) )
let [] : state source 0 ( memory 1000 ) 0
let eval : command ( peek [] )
let main : puts ( eval )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment