1 /* Part of XPCE --- The SWI-Prolog GUI toolkit
2
3 Author: Jan Wielemaker and Anjo Anjewierden
4 E-mail: jan@swi.psy.uva.nl
5 WWW: http://www.swi.psy.uva.nl/projects/xpce/
6 Copyright (c) 1985-2002, University of Amsterdam
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in
18 the documentation and/or other materials provided with the
19 distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <h/kernel.h>
36
37 static status
initialiseOrv(Or or,int argc,Any * argv)38 initialiseOrv(Or or, int argc, Any *argv)
39 { int n;
40
41 initialiseCode((Code) or);
42 assign(or, members, newObject(ClassChain, EAV));
43
44 for(n=0; n<argc; n++)
45 appendChain(or->members, argv[n]);
46
47 succeed;
48 }
49
50
51 /********************************
52 * EXECUTION *
53 ********************************/
54
55 static status
ExecuteOr(Or or)56 ExecuteOr(Or or)
57 { Cell cell;
58
59 for_cell(cell, or->members)
60 { if ( executeCode(cell->value) != FAIL )
61 succeed;
62 }
63
64 fail;
65 }
66
67 /********************************
68 * TERM REPRESENTATION *
69 ********************************/
70
71 static Int
getArityOr(Or or)72 getArityOr(Or or)
73 { answer(getArityChain(or->members));
74 }
75
76
77 static Any
getArgOr(Or or,Int n)78 getArgOr(Or or, Int n)
79 { answer(getArgChain(or->members, n));
80 }
81
82
83 /*******************************
84 * CLASS DECLARATION *
85 *******************************/
86
87 /* Type declarations */
88
89
90 /* Instance Variables */
91
92 static vardecl var_or[] =
93 { IV(NAME_members, "chain", IV_GET,
94 NAME_statement, "One of these must succeed")
95 };
96
97 /* Send Methods */
98
99 static senddecl send_or[] =
100 { SM(NAME_Execute, 0, NULL, ExecuteOr,
101 DEFAULT, "Evaluate tests until one succeeds"),
102 SM(NAME_initialise, 1, "test=code ...", initialiseOrv,
103 DEFAULT, "Create from tests")
104 };
105
106 /* Get Methods */
107
108 static getdecl get_or[] =
109 { GM(NAME_Arg, 1, "code", "int", getArgOr,
110 DEFAULT, "Nth-1 argument for term description"),
111 GM(NAME_Arity, 0, "int", NULL, getArityOr,
112 DEFAULT, "Arity for term description")
113 };
114
115 /* Resources */
116
117 #define rc_or NULL
118 /*
119 static classvardecl rc_or[] =
120 {
121 };
122 */
123
124 /* Class Declaration */
125
126 ClassDecl(or_decls,
127 var_or, send_or, get_or, rc_or,
128 ARGC_UNKNOWN, NULL,
129 "$Rev$");
130
131 status
makeClassOr(Class class)132 makeClassOr(Class class)
133 { declareClass(class, &or_decls);
134 delegateClass(class, NAME_members);
135
136 succeed;
137 }
138
139