User talk:IanOsgood: Difference between revisions
Content added Content deleted
(→Forth: sign while logged in) |
(→Forth: BF to Forth compiler) |
||
Line 14: | Line 14: | ||
:There are already plenty of brainfuck interpreters in the world. If I were to do one, I'd do it in JavaScript so folks could run in their browser. I've already done this for a number of [http://www.quirkster.com/iano/js/ other obscure languages]. |
:There are already plenty of brainfuck interpreters in the world. If I were to do one, I'd do it in JavaScript so folks could run in their browser. I've already done this for a number of [http://www.quirkster.com/iano/js/ other obscure languages]. |
||
:If I ''did'' do BF for Forth, I would use meta-compilation, directly compiling BF into one big Forth definition. --[[User:IanOsgood|IanOsgood]] 10:32, 27 November 2007 (MST) |
:If I ''did'' do BF for Forth, I would use meta-compilation, directly compiling BF into one big Forth definition. --[[User:IanOsgood|IanOsgood]] 10:32, 27 November 2007 (MST) |
||
: OK, that was easier than I thought. --[[User:IanOsgood|IanOsgood]] 11:46, 27 November 2007 (MST) |
|||
\ brainfuck compiler |
|||
1024 constant size |
|||
: init ( -- p *p ) here size erase here 0 ; |
|||
: right ( p *p -- p+1 *p ) over c! 1+ dup c@ ; |
|||
: left ( p *p -- p-1 *p ) over c! 1- dup c@ ; \ range check? |
|||
: compile-bf-char ( c -- ) |
|||
case |
|||
[char] [ of postpone begin |
|||
postpone dup |
|||
postpone while endof |
|||
[char] ] of postpone repeat endof |
|||
[char] + of postpone 1+ endof |
|||
[char] - of postpone 1- endof |
|||
[char] > of postpone right endof |
|||
[char] < of postpone left endof |
|||
[char] , of postpone key endof |
|||
[char] . of postpone dup |
|||
postpone emit endof |
|||
\ ignore all other characters |
|||
endcase ; |
|||
: :bf" ( name bfcode" -- ) |
|||
: |
|||
[char] " parse \ " |
|||
postpone init |
|||
bounds do i c@ compile-bf-char loop |
|||
postpone 2drop |
|||
postpone ; |
|||
; |
|||
Testing in [[GNU Forth]]: |
|||
:bf" test [,->+<.]" |
|||
see test |
|||
: test |
|||
init |
|||
BEGIN dup |
|||
WHILE key 1- right 1+ left dup emit |
|||
REPEAT |
|||
2drop ; |