Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81281 / brainfsckInterpreter / interpreterMain.pl
blob: ff51f3433f3696b4f50946ad9ab938b63f719f34 (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
:- 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)).