YAP 7.1.0
arith0.c
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: eval.c *
12* Last rev: *
13* mods: *
14* comments: arithmetical expression evaluation *
15* *
16*************************************************************************/
17#ifdef SCCS
18static char SccsId[] = "%W% %G%";
19#endif
20
27
111#include "Yap.h"
112#include "Yatom.h"
113#include "YapHeap.h"
114#include "YapEval.h"
115
116
117
118static Term
119eval0(Int fi) {
120 CACHE_REGS
121 arith0_op fop = fi;
122 switch (fop) {
123 case op_pi:
124 {
125 RFLOAT(PI);
126 }
127 case op_e:
128 {
129 RFLOAT(M_E);
130 }
131 case op_epsilon:
132 {
133 RFLOAT(DBL_EPSILON);
134 }
135 case op_inf:
136 {
137#ifdef _MSC_VER /* Microsoft's Visual C++ Compiler */
138 Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity");
139 P = (yamop *)FAILCODE;
140 RERROR();
141#else
142 if (isoLanguageFlag()) {/* iso */
143 Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity");
144 P = (yamop *)FAILCODE;
145 RERROR();
146 } else {
147 RFLOAT(INFINITY);
148 }
149#endif
150 }
151 case op_nan:
152 {
153#ifdef _MSC_VER /* Microsoft's Visual C++ Compi<ler */
154 Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity");
155 RERROR();
156#else
157 if (isoLanguageFlag()) {/* iso */
158 Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating not-a-number");
159 RERROR();
160 } else {
161 RFLOAT(NAN);
162 }
163#endif
164 }
165 case op_random:
166 {
167 RFLOAT(Yap_random());
168 }
169 case op_cputime:
170 {
171 RFLOAT((Float)Yap_cputime()/1000.0);
172 }
173 case op_heapused:
177 RINT(HeapUsed);
178 case op_localsp:
182#if YAPOR_SBA
183 RINT((Int)ASP);
184#else
185 RINT(LCL0 - ASP);
186#endif
187 case op_b:
191#if YAPOR_SBA
192 RINT((Int)B);
193#else
194 RINT(LCL0 - (CELL *)B);
195#endif
196 case op_env:
200#if YAPOR_SBA
201 RINT((Int)YENV);
202#else
203 RINT(LCL0 - YENV);
204#endif
205 case op_tr:
209#if YAPOR_SBA
210 RINT(TR);
211#else
212 RINT(((CELL *)TR)-LCL0);
213#endif
214 case op_stackfree:
218 RINT(Unsigned(ASP) - Unsigned(HR));
219 case op_globalsp:
223#if YAPOR_SBA
224 RINT((Int)HR);
225#else
226 RINT(HR - H0);
227#endif
228 }
230 RERROR();
231}
232
233Term Yap_eval_atom(Int f)
234{
235 return eval0(f);
236}
237
238typedef struct init_const_eval {
239 char *OpName;
240 arith0_op f;
242
243static InitConstEntry InitConstTab[] = {
244 {"pi", op_pi},
245 {"e", op_e},
246 {"epsilon", op_epsilon},
247 {"inf", op_inf},
248 {"nan", op_nan},
249 {"random", op_random},
250 {"cputime", op_cputime},
251 {"heapused", op_heapused},
252 {"local_sp", op_localsp},
253 {"global_sp", op_globalsp},
254 {"$last_choice_pt", op_b},
255 {"$env", op_env},
256 {"$tr", op_tr},
257 {"stackfree", op_stackfree},
258};
259
260void
261Yap_InitConstExps(void)
262{
263 unsigned int i;
264 ExpEntry *p;
265
266 for (i = 0; i < sizeof(InitConstTab)/sizeof(InitConstEntry); ++i) {
267 AtomEntry *ae = RepAtom(Yap_LookupAtom(InitConstTab[i].OpName));
268 if (ae == NULL) {
269 Yap_EvalError(RESOURCE_ERROR_HEAP,TermNil,"at InitConstExps");
270 return;
271 }
272 WRITE_LOCK(ae->ARWLock);
273 if (Yap_GetExpPropHavingLock(ae, 0)) {
274 WRITE_UNLOCK(ae->ARWLock);
275 break;
276 }
277 p = (ExpEntry *) Yap_AllocAtomSpace(sizeof(ExpEntry));
278 p->KindOfPE = ExpProperty;
279 p->ArityOfEE = 0;
280 p->ENoOfEE = 0;
281 p->FOfEE = InitConstTab[i].f;
282 AddPropToAtom(ae, (PropEntry *)p);
283 WRITE_UNLOCK(ae->ARWLock);
284 }
285}
286
287/* This routine is called from Restore to make sure we have the same arithmetic operators */
288int
289Yap_ReInitConstExps(void)
290{
291 return TRUE;
292}
293
arith0_op
constant operators
Definition: YapEval.h:166
@ op_pi
pi [ISO]
Definition: YapEval.h:173
@ op_epsilon
epsilon
Definition: YapEval.h:187
@ op_e
e
Definition: YapEval.h:180
@ op_inf
inf
Definition: YapEval.h:199
Main definitions.
Definition: amidefs.h:264