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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*-----------------------------------------------------------------------
File : cte_functypes.c
Author: Stephan Schulz (schulz@eprover.org)
Contents
Simple code for handling function symbols, both encoded and in
external representation. Most of this is factored out of
cte_signature.c, which is already overbloated.
Copyright 2007 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Changes
<1> Sat Sep 8 01:11:32 EDT 2007
New
-----------------------------------------------------------------------*/
#include "cte_functypes.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
#define ATOMIC_FUNC_SYM_TOK (Identifier | SemIdent | SQString | String)
/* Tokens that always are stand-alone identifiers */
TokenType FuncSymbToken = ATOMIC_FUNC_SYM_TOK;
/* Tokens that may start a (composite or atomic) identifier */
TokenType FuncSymbStartToken = ATOMIC_FUNC_SYM_TOK| PosInt | String | Plus | Hyphen;
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: normalize_int_rep()
//
// Take a string representation of an integer and turn it into a
// normal form. This is done by dropping the optional leading + and
// all leading zeros (except for the case of plain '0', of
// course).
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
static void normalize_int_rep(DStr_p int_rep)
{
char* work, *sign="";
DStr_p tmp = DStrAlloc();
work = DStrView(int_rep);
if(*work=='+')
{
work++;
}
else if(*work=='-')
{
sign = "-";
work++;
}
while(*work == '0')
{
work++;
}
/* Check if there is anything left */
if(*work)
{
DStrAppendStr(tmp, sign);
DStrAppendStr(tmp, work);
DStrSet(int_rep, DStrView(tmp));
}
else
{
DStrSet(int_rep, "0");
}
DStrFree(tmp);
}
/*-----------------------------------------------------------------------
//
// Function: normalize_rational_rep()
//
// Take a string representation of an integer and turn it into a
// normal form. This is done by dropping optional leading +es and
// all leading zeros (except for the case of plain '0', of
// course), and moving any remaining '-' to the very front. Return
// true on success and false if something weird happened.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
static bool normalize_rational_rep(DStr_p int_rep)
{
char *work, *end;
int negative = 1;
long numerator, denominator, gcd;
work = DStrView(int_rep);
numerator = strtoll(work, &end, 10);
if(*end!='/')
{
return false;
}
work = end+1;
denominator = strtoll(work, &end, 10);
if(*end!='\0' || denominator == 0)
{
return false;
}
if(numerator < 0)
{
numerator = numerator*-1;
negative=negative*-1;
}
assert(numerator >= 0);
if(denominator<0)
{
denominator = denominator*-1;
negative=negative*-1;
}
assert(denominator > 0);
gcd = ComputeGCD(numerator, denominator);
numerator=numerator/gcd;
denominator=denominator/gcd;
DStrReset(int_rep);
DStrAppendInt(int_rep, negative*numerator);
DStrAppendChar(int_rep, '/');
DStrAppendInt(int_rep, denominator);
return true;
}
/*-----------------------------------------------------------------------
//
// Function: normalize_float_rep()
//
// Take a string representation of a floating point number and turn
// it into a normal form. The normal form is whatever sprintf()
// makes of it. Over- and underflow are accepted and ingnored (this
// is floating point math, after all - what do you expect?).
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
static void normalize_float_rep(DStr_p float_rep)
{
double value;
char* endptr;
char buff[128];
int res;
value = strtod(DStrView(float_rep), &endptr);
if(fabs(value)>=1000.0)
{
res = snprintf(buff, 128, "%e", value);
}
else
{
res = snprintf(buff, 128, "%f", value);
}
UNUSED(res); assert(res < 128);
DStrSet(float_rep, buff);
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: FuncSymbParse()
//
// Parse a function or predicate symbol (or, currently, variable)
// and store the representation into id.
//
// Operators are now of the types
//
// - Identifier
// - SemIdent
// - String
// - SQString
// - Integers, potentially signed
// - Reals, potentially signed
// - Rationals (fractions)
//
//
// Global Variables: SigIdentToken
//
// Side Effects : Reads input, changes id
//
/----------------------------------------------------------------------*/
FuncSymbType FuncSymbParse(Scanner_p in, DStr_p id)
{
FuncSymbType res = FSNone;
StrNumType numtype;
CheckInpTok(in, FuncSymbStartToken);
if(TestInpTok(in, FuncSymbToken))
{
DStrAppendStr(id, DStrView(AktToken(in)->literal));
if(TestInpTok(in, Identifier))
{
if((isupper(DStrView(AktToken(in)->literal)[0])
||
DStrView(AktToken(in)->literal)[0] == '_'))
{
res = FSIdentVar;
}
else
{
res = FSIdentFreeFun;
}
}
else
{
switch(AktTokenType(in))
{
case SemIdent:
res = FSIdentInterpreted;
break;
case SQString:
res = FSIdentFreeFun;
break;
case String:
res = FSIdentObject;
break;
default:
assert(false && "Unexpected token in FuncSymbParse()");
break;
}
}
AcceptInpTok(in, FuncSymbToken);
}
else
{
CheckInpTok(in, PosInt|Plus|Hyphen);
numtype = ParseNumString(in);
switch(numtype)
{
case SNInteger:
normalize_int_rep(in->accu);
DStrAppendStr(id, DStrView(in->accu));
res = FSIdentInt;
break;
case SNRational:
/* Division by zero is caught in ParseNumString() */
normalize_rational_rep(in->accu);
DStrAppendStr(id, DStrView(in->accu));
res = FSIdentRational;
break;
case SNFloat:
normalize_float_rep(in->accu);
DStrAppendStr(id, DStrView(in->accu));
res = FSIdentFloat;
break;
default:
assert(false);
break;
}
}
return res;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|