YAP 7.1.0
args.c
Go to the documentation of this file.
1
13#include "Yap.h"
14#include "Yatom.h"
15#include "amiops.h"
16
26static xarg *matchKey(Atom key, xarg *e0, int n, const param_t *def) {
27 int i;
28 for (i = 0; i < n; i++) {
29 if (!strcmp((char *)def->name, (char *)RepAtom(key)->StrOfAE)) {
30 return e0;
31 }
32 def++;
33 e0++;
34 }
35 return NULL;
36}
37
42int Yap_ArgKey(Atom key, const param_t *def, int n) {
43 int i;
44 for (i = 0; i < n; i++) {
45 if (!strcmp((char *)def->name, (char *)RepAtom(key)->StrOfAE)) {
46 return i;
47 }
48 def++;
49 }
50 return -1;
51}
52
53#define YAP_XARGINFO(Error, Message)
54
55
56xarg *Yap_ArgListToVector__(const char *file, const char *function, int lineno,
57 Term listl, const param_t *def, int n,
58 xarg *a,
59 yap_error_number err) {
60 CACHE_REGS
61 listl = Deref(listl);
62 if (IsVarTerm(listl)) {
63 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, listl,
64 "while opening listl = ArgOfTerm(2, listl ,k)");
65 }
66 if (!a)
67 a = calloc(n, sizeof(xarg));
68
69 if (!IsPairTerm(listl) && listl != TermNil) {
70 if (IsAtomTerm(listl)) {
71 xarg *na = matchKey(AtomOfTerm(listl), a, n, def);
72 if (!na) {
73 Yap_ThrowError__(file, function, lineno, err, listl, "match key");
74 }
75 } else if (IsApplTerm(listl)) {
76 Functor f = FunctorOfTerm(listl);
77 if (IsExtensionFunctor(f)) {
78 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_LIST, listl, "callable");
79 }
80 arity_t arity = ArityOfFunctor(f);
81 if (arity != 1) {
82 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_LIST, listl, "bad arity");
83 }
84 xarg *na = matchKey(NameOfFunctor(f), a, n, def);
85 if (!na) {
86 Yap_ThrowError__(file, function, lineno, err, listl, "no match");
87 }
88 na->used = true;
89 na->tvalue = ArgOfTerm(1, listl);
90 } else {
91 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_ATOM, listl, "not atom");
92 }
93 listl = MkPairTerm(listl, TermNil);
94 }
95 while (IsPairTerm(listl)) {
96 Term hd = HeadOfTerm(listl);
97 listl = TailOfTerm(listl);
98 if (IsVarTerm(hd)) {
99 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, hd, "sub-element");
100 }
101 if (IsVarTerm(listl)) {
102 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, listl, "sub-list");
103 }
104 if (IsAtomTerm(hd)) {
105 xarg *na = matchKey(AtomOfTerm(hd), a, n, def);
106 if (!na)
107 Yap_ThrowError__(file, function, lineno, err, hd, "bad match in list");
108 na->used = true;
109 na->tvalue = TermNil;
110 continue;
111 } else if (IsApplTerm(hd)) {
112 Functor f = FunctorOfTerm(hd);
113 if (IsExtensionFunctor(f)) {
114 Yap_ThrowError__(file, function, lineno, err, hd, "bad compound");
115 }
116 arity_t arity = ArityOfFunctor(f);
117 if (arity != 1) {
118 Yap_ThrowError__(file, function, lineno, DOMAIN_ERROR_OUT_OF_RANGE, hd,
119 "high arity");
120 }
121 xarg *na = matchKey(NameOfFunctor(f), a, n, def);
122 if (!na) {
123 Yap_ThrowError__(file, function, lineno, err, hd, "no match");
124 }
125 na->used = true;
126 na->tvalue = ArgOfTerm(1, hd);
127 } else {
128 Yap_ThrowError__(file, function, lineno, err, hd, "bad type");
129 }
130 }
131 if (IsVarTerm(listl)) {
132 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, listl, "unbound");
133 } else if (listl != TermNil) {
134 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_LIST, listl, "bad list");
135 }
136 return a;
137 }
138
139static xarg *matchKey2(Atom key, xarg *e0, int n, const param2_t *def) {
140 int i;
141 for (i = 0; i < n; i++) {
142 if (!strcmp((char *)def->name, (char *)RepAtom(key)->StrOfAE)) {
143 return e0;
144 }
145 def++;
146 e0++;
147 }
148 return NULL;
149}
153xarg *Yap_ArgList2ToVector__(const char *file, const char *function, int lineno,Term listl, const param2_t *def, int n, yap_error_number err) {
154 CACHE_REGS
155 xarg *a = calloc(n, sizeof(xarg));
156 if (!IsPairTerm(listl) && listl != TermNil) {
157 if (IsVarTerm(listl)) {
158 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, listl, "unbound");
159 }
160 if (IsAtomTerm(listl)) {
161 xarg *na = matchKey2(AtomOfTerm(listl), a, n, def);
162 if (!na) {
163 Yap_ThrowError__(file, function, lineno, err,
164 listl, "bad match");
165 }
166 }
167 if (IsApplTerm(listl)) {
168 Functor f = FunctorOfTerm(listl);
169 if (IsExtensionFunctor(f)) {
170 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_PARAMETER, listl,
171 "bad compound");
172 }
173 arity_t arity = ArityOfFunctor(f);
174 if (arity != 1) {
175 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_LIST, listl, "bad arity");
176 }
177 xarg *na = matchKey2(NameOfFunctor(f), a, n, def);
178 if (!na) {
179 Yap_ThrowError__(file, function, lineno, DOMAIN_ERROR_GENERIC_ARGUMENT,
180 listl, "bad match");
181 }
182 } else {
183 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_LIST, listl, "");
184 }
185 listl = MkPairTerm(listl, TermNil);
186 }
187 while (IsPairTerm(listl)) {
188 Term hd = HeadOfTerm(listl);
189 if (IsVarTerm(hd)) {
190 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, hd, "");
191 }
192 if (IsAtomTerm(hd)) {
193 xarg *na = matchKey2(AtomOfTerm(hd), a, n, def);
194 if (!na) {
195 Yap_ThrowError__(file, function, lineno, DOMAIN_ERROR_GENERIC_ARGUMENT,
196 hd, "bad match");
197 }
198 na->used = true;
199 na->tvalue = TermNil;
200 continue;
201 } else if (IsApplTerm(hd)) {
202 Functor f = FunctorOfTerm(hd);
203 if (IsExtensionFunctor(f)) {
204 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_PARAMETER, hd, "bad compound");
205 }
206 arity_t arity = ArityOfFunctor(f);
207 if (arity != 1) {
208 Yap_ThrowError__(file, function, lineno, DOMAIN_ERROR_GENERIC_ARGUMENT,
209 hd, "bad arity");
210 }
211 xarg *na = matchKey2(NameOfFunctor(f), a, n, def);
212 if (na) {
213 na->used = 1;
214 na->tvalue = ArgOfTerm(1, hd);
215 } else {
216 Yap_ThrowError__(file, function, lineno, err,
217 hd, "bad key");
218 }
219 return a;
220 } else {
221 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, hd, "unbound");
222 }
223 listl = TailOfTerm(listl);
224 }
225 if (IsVarTerm(listl)) {
226 Yap_ThrowError__(file, function, lineno, INSTANTIATION_ERROR, listl, "");
227 }
228 if (TermNil != listl) {
229 Yap_ThrowError__(file, function, lineno, TYPE_ERROR_LIST, listl, "");
230 }
231 return a;
232}
233
Main definitions.
int Yap_ArgKey(Atom key, const param_t *def, int n)
Returns the index of an argument key, or -1 if not found.
Definition: args.c:42
xarg * Yap_ArgList2ToVector__(const char *file, const char *function, int lineno, Term listl, const param2_t *def, int n, yap_error_number err)
Yap_ArgList2ToVector is much the same as before, but assumes parameters also have something called a ...
Definition: args.c:153
void Yap_ThrowError__(const char *file, const char *function, int lineno, yap_error_number type, Term where, const char *msg,...)
Throw an error directly to the error handler.
Definition: errors.c:789
Definition: YapFlags.h:152