File: pl-flag.c

package info (click to toggle)
swi-prolog 3.1.0-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,772 kB
  • ctags: 12,869
  • sloc: ansic: 43,657; perl: 12,577; lisp: 4,359; sh: 1,534; makefile: 798; awk: 14
file content (154 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download | duplicates (2)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*  $Id: pl-flag.c,v 1.13 1998/07/06 10:29:46 jan Exp $

    Copyright (c) 1990 Jan Wielemaker. All rights reserved.
    See ../LICENCE to find out about your rights.
    jan@swi.psy.uva.nl

    Purpose: implement flag/3
*/

#include "pl-incl.h"

typedef struct flag *	Flag;

#define FLG_ATOM	0
#define FLG_INTEGER	1
#define FLG_REAL	2

struct flag
{ word	key;				/* key to the flag */
  int	type;				/* type (atom, int, real */
  union
  { atom_t a;				/* atom */
    long   i;				/* integer */
    double f;				/* float */
  } value;				/* value of the flag */
};

#define flagTable (GD->flags.table)

forwards Flag lookupFlag(word);

void
initFlags(void)
{ flagTable = newHTable(FLAGHASHSIZE);
}

static Flag
lookupFlag(word key)
{ Symbol symb;
  Flag f;

  if ( (symb = lookupHTable(flagTable, (void *)key)) )
    return (Flag)symb->value;

  f = (Flag) allocHeap(sizeof(struct flag));
  f->key = key;
  f->type = FLG_INTEGER;
  f->value.i = 0;
  addHTable(flagTable, (void *)key, f);

  return f;
}

word
pl_flag(term_t name, term_t old, term_t new)
{ Flag f;
  word key;
  atom_t a;
  number n;

  if ( !(key = getKey(name)) )
    return warning("flag/2: illegal key");

  f = lookupFlag(key);
  switch(f->type)
  { case FLG_ATOM:
      TRY(PL_unify_atom(old, f->value.a));
      break;
    case FLG_INTEGER:
      TRY(PL_unify_integer(old, f->value.i));
      break;
    case FLG_REAL:
    { 
#ifdef DOUBLE_ALIGNMENT
      double v;
      doublecpy(&v, &f->value.f);
      TRY(PL_unify_float(old, v));
#else
      TRY(PL_unify_float(old, f->value.f));
#endif
      break;
    }
    default:
      assert(0);
  }

  if ( PL_get_atom(new, &a) )
  { f->type = FLG_ATOM;
    f->value.a = a;

    succeed;
  } else if ( valueExpression(new, &n) )
  { canoniseNumber(&n);

    if ( n.type == V_INTEGER )
    { f->type = FLG_INTEGER;
      f->value.i = n.value.i;
    } else
    { 
      f->type = FLG_REAL;
#ifdef DOUBLE_ALIGNMENT
      doublecpy(&f->value.f, &n.value.f);
#else
      f->value.f = n.value.f;
#endif
    }

    succeed;
  }

  return warning("flag/2: value should be an atom, integer or expression");
}

word
pl_current_flag(term_t k, term_t h)
{ Symbol symb;

  switch( ForeignControl(h) )
  { case FRG_FIRST_CALL:
    { word key;

      if ( (key = getKey(k)) )
      { if ( lookupHTable(flagTable, (void *)key) )
	  succeed;
	fail;
      }
      if ( PL_is_variable(k) )
      {	symb = firstHTable(flagTable);
	break;
      }
      return warning("current_flag/2: illegal key");
    }
    case FRG_REDO:
      symb = ForeignContextPtr(h);
      break;
    case FRG_CUTTED:
    default:
      succeed;
  }

  for( ; symb; symb = nextHTable(flagTable, symb) )
  { Flag f = (Flag)symb->value;

    if ( !unifyKey(k, f->key) )
      continue;

    if ( !(symb = nextHTable(flagTable, symb)) )
      succeed;

    ForeignRedoPtr(symb);
  }

  fail;
}