Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(library(http/thread_httpd)).
- :- use_module(library(http/http_dispatch)).
- :- use_module(library(http/http_parameters)).
- % The predicate server(+Port) starts the server. It simply creates a
- % number of Prolog threads and then returns to the toplevel, so you can
- % (re-)load code, debug, etc.
- server(Port) :-
- http_server(http_dispatch, [port(Port)]).
- % Declare a handler, binding an HTTP path to a predicate.
- % Here our path is / (the root) and the goal we'll query will be
- % say_hi. The third argument is for options
- :- http_handler(/, say_hi, []).
- /* The implementation of /. The single argument provides the request
- details, which we ignore for now. Our task is to write a CGI-Document:
- a number of name: value -pair lines, followed by two newlines, followed
- by the document content, The only obligatory header line is the
- Content-type: <mime-type> header.
- Printing can be done using any Prolog printing predicate, but the
- format-family is the most useful. See format/2. */
- say_hi(Request) :-
- http_parameters(Request, [d(DValue, [default('')])]),
- my_parser(DValue, Msg),
- format('Content-type: text/html~n~n'),
- format('<p>That was ~w</p>', [Msg]),
- format('<form name="input" action="/" method="get">
- Username: <input type="text" name="d"><input type="submit" value="Submit"></form> ').
- my_parser(AtomIn, Tree) :-
- atom_codes(AtomIn, Codes),
- phrase(kumquattish(Tree), Codes).
- my_parser(_, 'Thats not proper kumquattish.').
- kumquattish(sentence(S,V,O)) --> subject(S), w, verb(V), w, object(O).
- subject(noun(S)) --> noun(S).
- subject(adjphrase(A, S)) --> adjective(A), w, subject(S).
- adjective(adjective(A)) --> raw_adj(A).
- raw_adj(big) --> "big".
- raw_adj(bad) --> "bad".
- raw_adj(green) --> "green".
- w --> " ".
- w --> " ", w.
- noun(apple) --> "apple".
- noun(kumquat) --> "kumquat".
- verb(eats) --> "eats".
- verb(knows) --> "knows".
- object(object(N)) --> subject(N).
- any --> [].
- any --> [_], any.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement