Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81281 / brainfsckInterpreter / brainySource.pl
blob: 6c73192afacc07ed07d223f2e78fcae46f1fac51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
token(tNextCellAddress) --> ">", !.
token(tLastCellAddress) --> "<", !.
token(tPlusPlusCellContent) --> "+", !.
token(tMinusMinusCellContent) --> "-", !.
token(tWriteCellContent) --> ".", !.
token(tReadCellContent) --> ",", !.
token([tWhileLoop|Body]) --> "[", parseLoop(Body), "]".
token(tUnknown) --> allThatIsNotAToken(I), !, {I \= []}.

parseLoop([]) --> "".
parseLoop([H|T]) --> token(H), !, parseLoop(T).

allThatIsNotAToken([C|Cs]) --> [C|Cs], \+ token(C), \+ token([C|Cs]),
    allThatIsNotAToken(Cs).
allThatIsNotAToken([]) --> [].

parceBF([H|T]) --> token(H), !, parceBF(T).
parceBF([]) --> [], !.

cleanUnknownAndCountInput([], [], 0):- !.
cleanUnknownAndCountInput([tUnknown|T], R, InputCnt):-
    cleanUnknownAndCountInput(T, R, InputCnt).
cleanUnknownAndCountInput([tReadCellContent|T], [tReadCellContent|R], InputCnt):-
    cleanUnknownAndCountInput(T, R, TInputCnt),
    InputCnt is TInputCnt + 1.
cleanUnknownAndCountInput([H|T], [H|R], InputCnt):-
    \+ is_list(H),
    H \= tUnknown,
    H \= tReadCellContent,
    cleanUnknownAndCountInput(T, R, InputCnt).
cleanUnknownAndCountInput([H|T], R, InputCnt):-
    is_list(H),
    cleanUnknownAndCountInput(H, R1, InputBitH),
    cleanUnknownAndCountInput(T, R2, InputBitT),
    InputCnt is InputBitH + InputBitT,
    append([R1], R2, R).

interpret(StreamCodes):-
    phrase(parceBF(CommandsU), StreamCodes),
    cleanUnknownAndCountInput(CommandsU, Commands, InputCnt),
    ((InputCnt =\= 0,
    write("\nThere are input symbols, so ready to read from a file.\nPlease enter the name of the file:\s"),
    current_stream(0, read, Stream),
    read_line_to_codes(Stream, Read),
    open(Read, read, ReadStream));
    (ReadStream = [])),
    execute(Commands, [[],[0]], ReadStream),
    !.


execute([], _, _).

execute([[tWhileLoop|Body]|Rest], Memory, ReadStream):-
    Memory = [_,[CurrCellContent|_]],
    CurrCellContent =\= 0,
    append(Body, [[tWhileLoop|Body]|Rest], NewRest),
    % write("While loop not 0: "),
    % write(NewRest), nl,
    !,
    execute(NewRest, Memory, ReadStream).

execute([[tWhileLoop|_]|Rest], Memory, ReadStream):-
    Memory = [_,[CurrCellContent|_]],
    CurrCellContent =:= 0,
    !,
    % write("While loop is 0: "),
    % write(Rest), nl,
    execute(Rest, Memory, ReadStream).

execute([tReadCellContent|Rest], Memory, ReadStream):-
    executeCommand(tReadCellContent, Memory, UpdatedMemory, ReadStream, ResultStream),
    !,
    execute(Rest, UpdatedMemory, ResultStream).

execute([tReadCellContent|Rest], Memory, []):-
    !,
    execute(Rest, Memory, []).

execute([CurrentCommand|Rest], Memory, ReadStream):-
    executeCommand(CurrentCommand, Memory, UpdatedMemory),
    !,
    execute(Rest, UpdatedMemory, ReadStream).


executeCommand(tNextCellAddress, [PrevAddrs, [CurrAddr|NextAddrs]], [NewPrevAddrs, NewNextAddrs]):-
    append(PrevAddrs, [CurrAddr], NewPrevAddrs),
    ((NextAddrs == [], NewNextAddrs = [0]) ; (NewNextAddrs = NextAddrs)).
    % write("Operation: "), write(tNextCellAddress),
    % write(" Old memory: "), write([PrevAddrs, [CurrAddr|NextAddrs]]),
    % write(" New memory: "), write([NewPrevAddrs, NewNextAddrs]),
    % nl.

executeCommand(tLastCellAddress, [PrevAddrs, CurrAddrs], [MorePrevAddrs, [PrevAddr|CurrAddrs]]):-
    append(MorePrevAddrs, [PrevAddr], PrevAddrs).
    % write("Operation: "), write(tLastCellAddress),
    % write(" Old memory: "), write([PrevAddrs, CurrAddrs]),
    % write(" New memory: "), write([MorePrevAddrs, [PrevAddr|CurrAddrs]]),
    % nl.

executeCommand(tPlusPlusCellContent, CurrentMemory, [PrevAddrs, [NewCurrentCell|RestAddrs]]):-
    CurrentMemory = [PrevAddrs, [CurrentCell|RestAddrs]],
    Temp is CurrentCell + 1,
    ((Temp > 255, NewCurrentCell is 0); (NewCurrentCell is Temp)).
    % write("Operation: "), write(tPlusPlusCellContent),
    % write(" New memory: "), write([PrevAddrs, [NewCurrentCell|RestAddrs]]),
    % nl.

executeCommand(tMinusMinusCellContent, CurrentMemory, [PrevAddrs, [NewCurrentCell|RestAddrs]]):-
    CurrentMemory = [PrevAddrs, [CurrentCell|RestAddrs]],
    Temp is CurrentCell - 1,
    ((Temp < 0, NewCurrentCell is 255); (NewCurrentCell is Temp)).
    % write("Operation: "), write(tMinusMinusCellContent),
    % write(" New memory: "), write([PrevAddrs, [NewCurrentCell|RestAddrs]]),
    % nl.

executeCommand(tWriteCellContent, CurrentMemory, CurrentMemory):-
    CurrentMemory = [_, [CurrentCell|_]],
    char_code(Character, CurrentCell),
    write(Character).

executeCommand(tReadCellContent, [PrevAddrs, [_|RestAddrs]], [PrevAddrs, [NewCellContent|RestAddrs]], ReadStream, ReadStream):-
    is_stream(ReadStream),
    get_code(ReadStream, CurrentInput),
    CurrentInput >= 0,
    ((char_code('\r', CurrentInput), char_code('\n', NL), NewCellContent = NL);
    (NewCellContent = CurrentInput)).

executeCommand(tReadCellContent, [PrevAddrs, [CurrCelContent|RestAddrs]], [PrevAddrs, [CurrCelContent|RestAddrs]], ReadStream, []):-
    is_stream(ReadStream),
    get_code(ReadStream, CurrentInput),
    CurrentInput == -1,
    close(ReadStream).