From 5288b0429fcdac891be3bced8e21787bd452d44a Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Tue, 27 Nov 2018 22:31:13 +0200 Subject: Fixed cloning the repo on Windows --- homework/81281/brainf***Interpreter/README.md | 21 ---- .../81281/brainf***Interpreter/brainySource.pl | 132 --------------------- .../81281/brainf***Interpreter/interpreterMain.pl | 35 ------ .../81281/brainf***Interpreter/runMeChooseMode.sh | 15 --- .../brainf***Interpreter/testPrograms/bigProgram.b | 32 ----- .../brainf***Interpreter/testPrograms/input.txt | 1 - .../81281/brainf***Interpreter/testPrograms/pr.b | 21 ---- .../testPrograms/smallProgram.b | 33 ------ homework/81281/brainfsckInterpreter/README.md | 21 ++++ .../81281/brainfsckInterpreter/brainySource.pl | 132 +++++++++++++++++++++ .../81281/brainfsckInterpreter/interpreterMain.pl | 35 ++++++ .../81281/brainfsckInterpreter/runMeChooseMode.sh | 15 +++ .../brainfsckInterpreter/testPrograms/bigProgram.b | 32 +++++ .../brainfsckInterpreter/testPrograms/input.txt | 1 + .../81281/brainfsckInterpreter/testPrograms/pr.b | 21 ++++ .../testPrograms/smallProgram.b | 33 ++++++ 16 files changed, 290 insertions(+), 290 deletions(-) delete mode 100644 homework/81281/brainf***Interpreter/README.md delete mode 100644 homework/81281/brainf***Interpreter/brainySource.pl delete mode 100644 homework/81281/brainf***Interpreter/interpreterMain.pl delete mode 100755 homework/81281/brainf***Interpreter/runMeChooseMode.sh delete mode 100644 homework/81281/brainf***Interpreter/testPrograms/bigProgram.b delete mode 100644 homework/81281/brainf***Interpreter/testPrograms/input.txt delete mode 100644 homework/81281/brainf***Interpreter/testPrograms/pr.b delete mode 100644 homework/81281/brainf***Interpreter/testPrograms/smallProgram.b create mode 100644 homework/81281/brainfsckInterpreter/README.md create mode 100644 homework/81281/brainfsckInterpreter/brainySource.pl create mode 100644 homework/81281/brainfsckInterpreter/interpreterMain.pl create mode 100755 homework/81281/brainfsckInterpreter/runMeChooseMode.sh create mode 100644 homework/81281/brainfsckInterpreter/testPrograms/bigProgram.b create mode 100644 homework/81281/brainfsckInterpreter/testPrograms/input.txt create mode 100644 homework/81281/brainfsckInterpreter/testPrograms/pr.b create mode 100644 homework/81281/brainfsckInterpreter/testPrograms/smallProgram.b diff --git a/homework/81281/brainf***Interpreter/README.md b/homework/81281/brainf***Interpreter/README.md deleted file mode 100644 index 29d4b63..0000000 --- a/homework/81281/brainf***Interpreter/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# Homework 3.2 - -## Create a brainf*** interpreter - -_The interpreter reads from a file if there is input._ - -### How to install and use SWI-Prolog - -+ [How to install SWI-Prolog](https://wwu-pi.github.io/tutorials/lectures/lsp/010_install_swi_prolog.html) - -+ [Quick start on how to use it](http://www.swi-prolog.org/pldoc/man?section=quickstart) - -+ [Command line options](http://www.swi-prolog.org/pldoc/man?section=cmdline) - -### Added bash script for executing program after successful installation of SWI-Prolog on a Unix or Unix-like variants - -[Script](./runMeChooseMode.sh) - -_Mode 0 is for interactive interpretation and 1 is for interpretation of a file._ - -**The interpreter is a bit slow if the program has a lot of loops with the current method of handling loops. Also Prolog isn't the fastest PL of them all xD.** diff --git a/homework/81281/brainf***Interpreter/brainySource.pl b/homework/81281/brainf***Interpreter/brainySource.pl deleted file mode 100644 index 6c73192..0000000 --- a/homework/81281/brainf***Interpreter/brainySource.pl +++ /dev/null @@ -1,132 +0,0 @@ -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). diff --git a/homework/81281/brainf***Interpreter/interpreterMain.pl b/homework/81281/brainf***Interpreter/interpreterMain.pl deleted file mode 100644 index ff51f34..0000000 --- a/homework/81281/brainf***Interpreter/interpreterMain.pl +++ /dev/null @@ -1,35 +0,0 @@ -:- use_module(library(dcg/basics)). -:- use_module(library(readutil)). -:- use_module(library(quintus)). -:- [brainySource]. - -:- set_prolog_flag(verbose, silent). -:- initialization(main, main). - -main:- - current_prolog_flag(argv, Argv), - Argv = [H1|_], - atom_string(H1, Filename), - ((Filename == "", readyToReadAndInterpret("Hello, how can I help you?\nExit me by typing \"quit\".")); - (Filename \= "", readyToReadAndInterpret("Ready to interpret your file.\n", Filename))), - halt. -main:- - halt(1). - -readyToReadAndInterpret(Message):- - write(Message), - startCycleTillQuit. - -readyToReadAndInterpret(Message, File):- - write(Message), - open(File, read, RFile), - read_stream_to_codes(RFile, BFSource), - close(RFile), - interpret(BFSource). - -startCycleTillQuit:- - nl, - current_stream(0, read, Stream), - read_line_to_codes(Stream, BFProgram), - ((string_codes("quit", BFProgram), write("Bye!"), nl); - (interpret(BFProgram), startCycleTillQuit)). diff --git a/homework/81281/brainf***Interpreter/runMeChooseMode.sh b/homework/81281/brainf***Interpreter/runMeChooseMode.sh deleted file mode 100755 index 4162db4..0000000 --- a/homework/81281/brainf***Interpreter/runMeChooseMode.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -echo -ne "Please enter 0 for live interpreter or 1 for interpretation of a file: " -read mode - -base=`pwd`"/interpreterMain.pl" - -if [ $mode == 0 ] -then - exec swipl -q -f "$base" "" -else - echo -n "Please enter the name of the file: " - read fileName - exec swipl -q -f "$base" "$fileName" -fi diff --git a/homework/81281/brainf***Interpreter/testPrograms/bigProgram.b b/homework/81281/brainf***Interpreter/testPrograms/bigProgram.b deleted file mode 100644 index 2f311a7..0000000 --- a/homework/81281/brainf***Interpreter/testPrograms/bigProgram.b +++ /dev/null @@ -1,32 +0,0 @@ -99 bottles in 1752 brainfuck instructions -by jim crawford (http://www (dot) goombas (dot) org/) ->++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+ ->+<<-]>>[-<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>> ->>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>-]+>[[-]<->]<<<<<<<< -<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<< -<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]< -<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]+ -+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[< -<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-] ->.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[- ->>+<<]<++++++++[<++++>-]<.>+++++++[>+++++++++<-]>+++.<+++++[ ->+++++++++<-]>.+++++..--------.-------.++++++++++++++>>[>>>+ ->+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<< -[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+> ->]<<<[>>+<<[-]]>[>+<[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]< -+<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<.>>----.---------.<< -.>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+< -[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++ ->+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+> ->[-]]<<[[-]>++++++++[<++++>-]<.>++++++++++[>+++++++++++<-]>+ -.-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++[ ->----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>> -[-<<+>>]+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++. ->+++++++++[>+++++++++<-]>+++.+++++++++++++.++++++++++.------ -.<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++ -++++.++++++++.---------.>.<-------------.+++++++++++++.----- ------.>.<++++++++++++.---------------.<+++[>++++++<-]>..>.<- ----------.+++++++++++.>.<<+++[>------<-]>-.+++++++++++++++++ -.---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[- -<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.[ --]<[-]>]<+<] diff --git a/homework/81281/brainf***Interpreter/testPrograms/input.txt b/homework/81281/brainf***Interpreter/testPrograms/input.txt deleted file mode 100644 index 341cc98..0000000 --- a/homework/81281/brainf***Interpreter/testPrograms/input.txt +++ /dev/null @@ -1 +0,0 @@ -example 123 diff --git a/homework/81281/brainf***Interpreter/testPrograms/pr.b b/homework/81281/brainf***Interpreter/testPrograms/pr.b deleted file mode 100644 index 1dce918..0000000 --- a/homework/81281/brainf***Interpreter/testPrograms/pr.b +++ /dev/null @@ -1,21 +0,0 @@ -Calculate the value 256 and test if it's zero -If the interpreter errors on overflow this is where it'll happen -++++++++[>++++++++<-]>[<++++>-] -+<[>-< - Not zero so multiply by 256 again to get 65536 - [>++++<-]>[<++++++++>-]<[>++++++++<-] - +>[> - # Print "32" - ++++++++++[>+++++<-]>+.-.[-]< - <[-]<->] <[>> - # Print "16" - +++++++[>+++++++<-]>.+++++.[-]< -<<-]] >[> - # Print "8" - ++++++++[>+++++++<-]>.[-]< -<-]< -# Print " bit cells\n" -+++++++++++[>+++>+++++++++>+++++++++>+<<<<-]>-.>-.+++++++.+++++++++++.<. ->>.++.+++++++..<-.>>- -Clean up used cells. -[[-]<] diff --git a/homework/81281/brainf***Interpreter/testPrograms/smallProgram.b b/homework/81281/brainf***Interpreter/testPrograms/smallProgram.b deleted file mode 100644 index e080b7f..0000000 --- a/homework/81281/brainf***Interpreter/testPrograms/smallProgram.b +++ /dev/null @@ -1,33 +0,0 @@ -[ This program prints Sierpinski triangle on 80-column display. ] - > - + + - + + - [ < + + - + + - + + + + - > - ] > - + + + + + + + + - [ > - + + + + - < - ] > - > + + > > > + > - > > + < - < < < < < < < < - < [ - [ - > + < - ] > [ - < + > > > . < < ] > > > - [ [ - - > + + - + + + + - + + [ > + + + + - < - ] > - . < < [ - > + < - ] + > [ - > + + - + + + + + + + + < < + > ] > . [ - - ] > ] - ] + < < < [ - [ - - > + < ] + > [ - - < + > > > - [ - > + < ] + + > - [ - < - > ] < < - < ] < < < < ] + + + + + + + + + - + . + + + . [ - ] < ] + + + + + - * * * * * M a d e * B y : * N Y Y R I K K I * 2 0 0 2 * * * * * diff --git a/homework/81281/brainfsckInterpreter/README.md b/homework/81281/brainfsckInterpreter/README.md new file mode 100644 index 0000000..29d4b63 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/README.md @@ -0,0 +1,21 @@ +# Homework 3.2 + +## Create a brainf*** interpreter + +_The interpreter reads from a file if there is input._ + +### How to install and use SWI-Prolog + ++ [How to install SWI-Prolog](https://wwu-pi.github.io/tutorials/lectures/lsp/010_install_swi_prolog.html) + ++ [Quick start on how to use it](http://www.swi-prolog.org/pldoc/man?section=quickstart) + ++ [Command line options](http://www.swi-prolog.org/pldoc/man?section=cmdline) + +### Added bash script for executing program after successful installation of SWI-Prolog on a Unix or Unix-like variants + +[Script](./runMeChooseMode.sh) + +_Mode 0 is for interactive interpretation and 1 is for interpretation of a file._ + +**The interpreter is a bit slow if the program has a lot of loops with the current method of handling loops. Also Prolog isn't the fastest PL of them all xD.** diff --git a/homework/81281/brainfsckInterpreter/brainySource.pl b/homework/81281/brainfsckInterpreter/brainySource.pl new file mode 100644 index 0000000..6c73192 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/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). diff --git a/homework/81281/brainfsckInterpreter/interpreterMain.pl b/homework/81281/brainfsckInterpreter/interpreterMain.pl new file mode 100644 index 0000000..ff51f34 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/interpreterMain.pl @@ -0,0 +1,35 @@ +:- use_module(library(dcg/basics)). +:- use_module(library(readutil)). +:- use_module(library(quintus)). +:- [brainySource]. + +:- set_prolog_flag(verbose, silent). +:- initialization(main, main). + +main:- + current_prolog_flag(argv, Argv), + Argv = [H1|_], + atom_string(H1, Filename), + ((Filename == "", readyToReadAndInterpret("Hello, how can I help you?\nExit me by typing \"quit\".")); + (Filename \= "", readyToReadAndInterpret("Ready to interpret your file.\n", Filename))), + halt. +main:- + halt(1). + +readyToReadAndInterpret(Message):- + write(Message), + startCycleTillQuit. + +readyToReadAndInterpret(Message, File):- + write(Message), + open(File, read, RFile), + read_stream_to_codes(RFile, BFSource), + close(RFile), + interpret(BFSource). + +startCycleTillQuit:- + nl, + current_stream(0, read, Stream), + read_line_to_codes(Stream, BFProgram), + ((string_codes("quit", BFProgram), write("Bye!"), nl); + (interpret(BFProgram), startCycleTillQuit)). diff --git a/homework/81281/brainfsckInterpreter/runMeChooseMode.sh b/homework/81281/brainfsckInterpreter/runMeChooseMode.sh new file mode 100755 index 0000000..4162db4 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/runMeChooseMode.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +echo -ne "Please enter 0 for live interpreter or 1 for interpretation of a file: " +read mode + +base=`pwd`"/interpreterMain.pl" + +if [ $mode == 0 ] +then + exec swipl -q -f "$base" "" +else + echo -n "Please enter the name of the file: " + read fileName + exec swipl -q -f "$base" "$fileName" +fi diff --git a/homework/81281/brainfsckInterpreter/testPrograms/bigProgram.b b/homework/81281/brainfsckInterpreter/testPrograms/bigProgram.b new file mode 100644 index 0000000..2f311a7 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/testPrograms/bigProgram.b @@ -0,0 +1,32 @@ +99 bottles in 1752 brainfuck instructions +by jim crawford (http://www (dot) goombas (dot) org/) +>++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+ +>+<<-]>>[-<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>> +>>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>-]+>[[-]<->]<<<<<<<< +<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<< +<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]< +<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]+ ++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[< +<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-] +>.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[- +>>+<<]<++++++++[<++++>-]<.>+++++++[>+++++++++<-]>+++.<+++++[ +>+++++++++<-]>.+++++..--------.-------.++++++++++++++>>[>>>+ +>+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<< +[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+> +>]<<<[>>+<<[-]]>[>+<[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]< ++<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<.>>----.---------.<< +.>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+< +[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++ +>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+> +>[-]]<<[[-]>++++++++[<++++>-]<.>++++++++++[>+++++++++++<-]>+ +.-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++[ +>----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>> +[-<<+>>]+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++. +>+++++++++[>+++++++++<-]>+++.+++++++++++++.++++++++++.------ +.<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++ +++++.++++++++.---------.>.<-------------.+++++++++++++.----- +-----.>.<++++++++++++.---------------.<+++[>++++++<-]>..>.<- +---------.+++++++++++.>.<<+++[>------<-]>-.+++++++++++++++++ +.---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[- +<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.[ +-]<[-]>]<+<] diff --git a/homework/81281/brainfsckInterpreter/testPrograms/input.txt b/homework/81281/brainfsckInterpreter/testPrograms/input.txt new file mode 100644 index 0000000..341cc98 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/testPrograms/input.txt @@ -0,0 +1 @@ +example 123 diff --git a/homework/81281/brainfsckInterpreter/testPrograms/pr.b b/homework/81281/brainfsckInterpreter/testPrograms/pr.b new file mode 100644 index 0000000..1dce918 --- /dev/null +++ b/homework/81281/brainfsckInterpreter/testPrograms/pr.b @@ -0,0 +1,21 @@ +Calculate the value 256 and test if it's zero +If the interpreter errors on overflow this is where it'll happen +++++++++[>++++++++<-]>[<++++>-] ++<[>-< + Not zero so multiply by 256 again to get 65536 + [>++++<-]>[<++++++++>-]<[>++++++++<-] + +>[> + # Print "32" + ++++++++++[>+++++<-]>+.-.[-]< + <[-]<->] <[>> + # Print "16" + +++++++[>+++++++<-]>.+++++.[-]< +<<-]] >[> + # Print "8" + ++++++++[>+++++++<-]>.[-]< +<-]< +# Print " bit cells\n" ++++++++++++[>+++>+++++++++>+++++++++>+<<<<-]>-.>-.+++++++.+++++++++++.<. +>>.++.+++++++..<-.>>- +Clean up used cells. +[[-]<] diff --git a/homework/81281/brainfsckInterpreter/testPrograms/smallProgram.b b/homework/81281/brainfsckInterpreter/testPrograms/smallProgram.b new file mode 100644 index 0000000..e080b7f --- /dev/null +++ b/homework/81281/brainfsckInterpreter/testPrograms/smallProgram.b @@ -0,0 +1,33 @@ +[ This program prints Sierpinski triangle on 80-column display. ] + > + + + + + + + [ < + + + + + + + + + + + > - ] > + + + + + + + + + + [ > + + + + + + < - ] > + > + + > > > + > + > > + < + < < < < < < < < + < [ - [ - > + < + ] > [ - < + > > > . < < ] > > > + [ [ + - > + + + + + + + + + + [ > + + + + + < - ] > + . < < [ - > + < + ] + > [ - > + + + + + + + + + + + < < + > ] > . [ + - ] > ] + ] + < < < [ - [ + - > + < ] + > [ + - < + > > > - [ - > + < ] + + > + [ - < - > ] < < + < ] < < < < ] + + + + + + + + + + + . + + + . [ - ] < ] + + + + + + * * * * * M a d e * B y : * N Y Y R I K K I * 2 0 0 2 * * * * * -- cgit v1.3.1-23-gd82d