File: calc.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (92 lines) | stat: -rw-r--r-- 2,702 bytes parent folder | download | duplicates (4)
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
86
87
88
89
90
91
92
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        wielemak@science.uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): Public domain
*/

:- use_module(library('http/thread_httpd')).
:- use_module(library('http/html_write')).
:- use_module(library('http/http_session')).
:- use_module(library('http/http_error')).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This demo shows session state  management   in  a very simple calculator
package. It also demonstrates the use of  the html_write library. To use
it, start Prolog, load this file and run

        ?- server.

Now direct your browser to http://localhost:3000/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

server :-
    server(3000, []).

server(Port, Options) :-
    http_server(reply,
                [ port(Port),
                  timeout(20)
                | Options
                ]).

reply(Request) :-
    memberchk(path(Path), Request),
    reply(Path, Request).

reply(/, _Request) :-
    http_session_retractall(formula(_)),
    Formula = 0,
    http_session_assert(formula(Formula)),
    page(Formula).

reply('/calc', Request) :-
    memberchk(search(Search), Request),
    memberchk(operation=Op, Search),
    memberchk(value=AtomVal, Search),
    atom_number(AtomVal, Val),
    http_session_retract(formula(Formula0)),
    debug(calc, 'Formula0 = ~w', [Formula0]),
    Formula =.. [Op, Formula0, Val],
    http_session_assert(formula(Formula)),
    page(Formula).


page(Formula) :-
    reply_page('HTTP Session DEMO',
               [ h2('Simple session demo'),
                 form([ action('/calc'),
                        method('GET')
                      ],
                      table([align(center), border(1)],
                            [ tr(td(\formula(Formula))),
                              tr(td([ \ops,
                                      input([ name(value) ]),
                                      input([ type(submit),
                                              value('Calc!')
                                            ])
                                    ]))
                            ]))
               ]).

formula(Formula) -->
    { sformat(S, '~w', [Formula]),
      Value is Formula
    },
    html([ S, ' = ', Value ]).

ops -->
    html(select(name(operation),
                [ option([selected], +),
                  option([], -),
                  option([], /),
                  option([], *)
                ])).

reply_page(Title, Content) :-
    phrase(page(title(Title), Content), HTML),
    format('Content-type: text/html~n~n'),
    print_html(HTML).