Created
January 18, 2012 13:31
-
-
Save hisui/1633016 to your computer and use it in GitHub Desktop.
use of cyclic term
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bf_memget([K:X|_], K, V) :- !, X = V. | |
bf_memget([ _|T], K, V) :- !, bf_memget(T, K, V). | |
bf_memget([], _, 0). | |
bf_memset([K:V|T], [K:_|T], K, V) :- !. | |
bf_memset([ H|U], [ H|T], K, V) :- !, bf_memset(U, T, K, V). | |
bf_memset([K:V], _, K, V). | |
bf_exec([], _, _) :- !. | |
bf_exec([H/J|T], Ptr, Mem) :- | |
H='[', !, (bf_memget(Mem, Ptr, 0), !, X=J; X=T), bf_exec(X, Ptr, Mem) | |
; H=']', !, (bf_memget(Mem, Ptr, 0), !, X=T; X=J), bf_exec(X, Ptr, Mem) | |
; H='>', !, Ptr1 is Ptr + 1, bf_exec(T, Ptr1, Mem) | |
; H='<', !, Ptr1 is Ptr - 1, bf_exec(T, Ptr1, Mem) | |
; H=',', !, get_byte(Val), bf_memset(Mem1, Mem, Ptr, Val), bf_exec(T, Ptr, Mem1) | |
; bf_memget(Mem, Ptr, Val), | |
( H='.', !, put_byte(Val), bf_exec(T, Ptr, Mem) | |
; ( H='+', !, Val1 is Val+ 1 | |
; H='-', !, Val1 is Val- 1) | |
, bf_memset(Mem1, Mem, Ptr, Val1) | |
, bf_exec(T, Ptr, Mem1)). | |
bf_link(L, ['['|T], LS) :- !, | |
L = ['['/R|N], | |
bf_link(N, T, [L|LS]). | |
bf_link(X, [']'|T], [L|LS]) :- !, | |
L = ['['/X|_], | |
X = [']'/L|N], | |
bf_link(N, T, LS). | |
bf_link([H/_|N], [H|T], LS) :- !, | |
bf_link(N, T, LS). | |
bf_link([], [], []). | |
bf_exec(Code) :- | |
atom_chars(Code, Chars), | |
bf_link(Prog, Chars, []), | |
bf_exec(Prog,0,[]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment