forked from mthom/cl-psoatransrun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscryer_server.pl
85 lines (73 loc) · 2.23 KB
/
scryer_server.pl
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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The Prolog server for PSOATransRun implementations, for Scryer Prolog.
Written in October 2020 by Mark Thom ([email protected])
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(psoatransrun_server, []).
:- use_module(library(charsio)).
:- use_module(library(dcgs)).
:- use_module(library(format)).
:- use_module(library(iso_ext)).
:- use_module(library(lists)).
:- use_module(library(sockets)).
start_server :-
socket_server_open('127.0.0.1':Port, ServerSocket),
nl, write(Port), nl,
socket_server_accept(ServerSocket, _, Stream, [eof_action(eof_code)]),
eval_loop(Stream),
close(Stream),
socket_server_close(ServerSocket).
eval_loop(Stream) :-
read_term(Stream, Term, [variable_names(VNNames)]),
( Term == end_of_file ->
true
;
read_term(Stream, _, [variable_names(UVNNames)]),
split_vars(VNNames, UVNNames, RVNNames),
catch(call(Term), _, false),
phrase(compile_solution_string(RVNNames, 0), VarString),
( RVNNames == [] ->
write_term(Stream, 'Yes', [])
;
write_term(Stream, VarString, [])
),
write_term(Stream, '\n', []),
flush_output(Stream),
false
;
write_term(Stream, 'No\n', []),
flush_output(Stream),
eval_loop(Stream)
).
split_vars([], _, []).
split_vars([VN | VNs], UVNNames, RVNs) :-
( member(VN, UVNNames) ->
split_vars(VNs, UVNNames, RVNs)
;
RVNs = [VN | RVNs1],
split_vars(VNs, UVNNames, RVNs1)
).
compile_solution_string([], _) --> [].
compile_solution_string([VN=Term|VNs], VarCount0) -->
( { var(Term) } ->
{ write_term_to_chars(VN, [], VNChars),
phrase(format_("'Var~d'", [VarCount0]), TermChars),
VarCount is VarCount0 + 1
}
;
{ write_term_to_chars(VN, [], VNChars),
write_term_to_chars(Term, [quoted(true)], TermChars),
VarCount = VarCount0
}
),
"'='(",
VNChars,
",",
TermChars,
")",
( { VNs \== [] } ->
", "
;
{ true }
),
compile_solution_string(VNs, VarCount).
:- initialization(start_server).