diff options
HW3.2 Brainf interpreter (#43)
* Create README.md
* Initial commit
* Initial commit
* Create README.md
* Rename homework/81281/c2html.pl to homework/81281/c2html/c2html.pl
* Rename homework/81281/test.c to homework/81281/c2html/test.c
* Update README.md
* Update README.md
* Update c2html.pl
* Update c2html.pl
* Update c2html.pl
* Update README.md
* Update c2html.pl
* Update test.c
* Create executeProgram.sh
* Update README.md
* Update c2html.pl
* Initial commit of updated code + prettify C
* Rename secret.c to messyCode.c
* Update and rename test.c to initialTest.c
* Update README.md
* Update README.md
* Update README.md
* Update README.md
* Rename executeProgram.sh to c2htmlrun.sh
* Update README.md
* Update prettify.pl
* Update prettify.pl
* Update messyCode.c
* Fixed some bugs.
* Update README.md
* Update c2html.pl
* Added collapse and fixed matrix representation.
* Updated flatten and added predicate for function identification.
* Update initialTest.c
* Update messyCode.c
* Bug fixing, added is_functionAndBody predicate.
* Update print.pl
* Result after applying prettify and added collapse functions
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Update messyCode.c
* Update README.md
* Update prettify.pl
* Update README.md
* Update prettify.pl
* Update messyCode.c
* Update messyCode.c
* Update page.html
* Fix bug typedef
* Update page.html
* Added choices
* Update c2html.pl
* Update prettify.pl
* Update prettify.pl
* Update README.md
* Update README.md
* Update c2html.pl
* Update c2html.pl
* Added forgotten do-while structure format
* Update messyCode.c
* Removing unknown tokens from stream
* Rename homework/81281/c2html/page.html to homework/81281/c2html/exampleCodeAndTestPage/page.html
* Rename homework/81281/c2html/messyCode.c to homework/81281/c2html/exampleCodeAndTestPage/messyCode.c
* Rename homework/81281/c2html/initialTest.c to homework/81281/c2html/exampleCodeAndTestPage/initialTest.c
* Update prettify.pl
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Fixed named of predicates
* Update messyCode.c
* Update page.html
* Update README.md
* Changed execTAB to "\s\s\s\s"
* Changed execTAB to "\s\s\s\s"
* Changed execTAB to "\s\s\s\s"
* Update page.html
* Added new tokens (forgotten operators) and added rules for them
* Added new tokens (forgotten operators) and fixed some old predicates
* Fixed bug with "else '\n' if (..)" in addNewLines predicate
* Fixed regular expression for a string and removed token ttypesPrintF
* Added clauses for handling enum and fixed handling of chars
* Fixed typos
* Added forgotten token to clause
* Fixed "case smt :'\n'" where smt is more than one char
* Fixed some bugs and divided code in modules
* New code module and fixed bugs
* Fixed some bugs with tabulation
* Divided code in modules
* Delete executeProgram.sh
* Delete test.c
* Initial version, still very buggy.
* Finished with bug killing.
* Fixed readme and added new program for tests.
* Forgotten line added.
* Organizing stuff in different folders.
* Forgot to delete files.
* Fixed typo.
1 files changed, 132 insertions, 0 deletions
| -rw-r--r-- | homework/81281/brainf***Interpreter/brainySource.pl | 132 |
diff --git a/homework/81281/brainf***Interpreter/brainySource.pl b/homework/81281/brainf***Interpreter/brainySource.pl new file mode 100644 index 0000000..6c73192 --- /dev/null +++ b/homework/81281/brainf***Interpreter/brainySource.pl @@ -0,0 +1,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). |
