Execute Brain****: Difference between revisions

(Add CLU)
Line 5,387:
 
=={{header|Ol}}==
 
(without input operator ",")
<lang olscheme>
(define (bf program stack-length)
(let ((program (string-append program "]")); end
(program-counter 0)
(stack (make-vectorbytevector stack-length 0))
(stack-pointer 0))
(letrec ((skip (lambda (PC sp in)
(let loop ((pc PC) (sp sp) (in in))
(let ((ch (string-ref program pc))
(pc (+ pc 1)))
(case ch
(#\] (list pc sp in))
(#\[ (apply loop (skip pc sp in)))
(else
(loop pc sp in)))))))
(step (lambda (PC SP IN)
(let loop ((pc PC) (sp SP) (in IN))
(let ((ch (string-ref program pc))
(pc (+ pc 1)))
(case ch
(#\] (list (- PC 1) sp in)) ; the end
(#\[ (if (eq? (vector-ref stack sp) 0)
(apply loop (skip pc sp in))
(apply loop (step pc sp in))))
(#\+ (set-ref! stack sp (mod (+ (vector-ref stack sp) 1257) 256))
(loop pc sp in))
(#\- (set-ref! stack sp (-mod (+ (vector-ref stack sp) 1255) 256))
(loop pc sp in))
(#\> (loop pc (+ sp 1) in))
(#\< (loop pc (- sp 1) in))
(#\. (display (make-string 1 (vector-ref stack sp)))
(loop pc sp in))
(else#\, (let this ((in in))
(loop pc sp)))))))) (cond
((pair? in)
(step 0 0))))
(set-ref! stack sp (car in))
 
(loop pc sp (cdr in)))
; testing:
((null? in)
; (bf ",++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." 30000)
(set-ref! stack sp 0)
; ==> Hello World!
(loop pc sp in))
; (bf ">>++++[<++++[<++++>-]>-]<<.[-]++++++++++." 30000)
(else
; ==> @
(this (force in))))))
(else ; skip any invalid character
(loop pc sp in))))))))
(step 0 0 (port->bytestream stdin)))))
</lang>
{{Out}}
<pre>
;> (bf ",++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." 30000) ; (print "Hello World!")
; ==> Hello World!
 
> (bf ">>++++[<++++[<++++>-]>-]<<." 30000) ; (display "@")
@
 
> (bf "----[---->+<]>+." 30000) ; another (display "@")
@
 
;> (bf ">>++++[<++++[<++++>-]>-]<<.[-]++++++++++." 30000) ; this time (print "@")
@
 
; brainfuck interpreter in brainfuck (c) Daniel B Cristofani (cristofdathevanetdotcom)
; use stdin to input a brainfuck program and its input, separated by an exclamation point.
;
; provided program makes +2 to every character and print the line,
; ^D means pressing "Ctrl+D" (an 4, or EOT, or end-of-xmit ANSI control character).
> (bf ">>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>++>++++++<<-]+>>>,<++[[>[
->>]<[>>]<<-]<[<]<+>>[>]>[<+>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<
]<]<[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]+<<[+>+<<-[>-->+<<-[>
+<[>>+<<-]]]>[<+>-]<]++>>-->[>]>>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-
[<->[<<+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>>]<<[>.>>>>>>>]<<[
>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]" 30000)
>,[>,]<[+<]>[----.>]!Khoor#Eudlqixfn$^D
Hello Brainfuck!
 
</pre>
 
=={{header|PARI/GP}}==