np/numparser2.h

Sat, 26 Feb 2005 21:38:55 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 26 Feb 2005 21:38:55 +0100
changeset 90
f5a392131875
parent 62
aae5facf9fc5
permissions
-rw-r--r--

Renamed Symlist PtrList.

60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
1 /*
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
2 * libtu/numparser2.h
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
3 *
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2002.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
5 *
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
6 * You may distribute and modify this library under the terms of either
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
8 */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
9
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
10 #define MAX_MANTISSA 10 /* should be enough for our needs */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
11 #define ULONG_SIZE (sizeof(ulong)*8)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
12
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
13 enum{
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
14 NPNUM_INT,
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
15 NPNUM_FLOAT
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
16 };
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
17
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
18 #ifdef NP_SIMPLE_IMPL
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
19
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
20 typedef struct _NPNum{
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
21 int type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
22 int base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
23 bool negative;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
24 double fval;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
25 ulong ival;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
26 } NPNum;
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
27
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
28 #define NUM_INIT {0, 0, 0, 0.0, 0}
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
29
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
30 static int npnum_mulbase_add(NPNum *num, long base, long v)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
31 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
32 double iold=num->ival;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
33
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
34 num->fval=num->fval*base+(double)v;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
35
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
36 num->ival*=base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
37
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
38 if(num->ival<iold)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
39 num->type=NPNUM_FLOAT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
40
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
41 num->ival+=v;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
42
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
43 return 0;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
44 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
45
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
46 #else /* NP_SIMPLE_IMPL */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
47
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
48 typedef struct _NPNum{
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
49 unsigned char nmantissa;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
50 int type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
51 int base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
52 bool negative;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
53 ulong mantissa[MAX_MANTISSA];
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
54 long exponent;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
55 } NPNum;
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
56
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
57 #define NUM_INIT {0, 0, 0, 0, {0,}, 0}
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
58
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
59 #define ADD_EXP(NUM, X) (NUM)->exponent+=(X);
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
61 #if defined(__GNUG__) && defined(i386) && !defined(NP_NO_I386_ASM)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
62 #define NP_I386_ASM
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
63 #endif
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
64
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
65 static int npnum_mulbase_add(NPNum *num, long base, long v)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
66 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
67 long i, j;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
68 ulong overflow;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
69 #ifndef NP_I386_ASM
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
70 ulong val;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
71 #endif
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
72
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
73 for(i=num->nmantissa;i>=0;i--){
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
74 #ifdef NP_I386_ASM
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
75 __asm__("mul %4\n"
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
76 : "=a" (num->mantissa[i]), "=d" (overflow)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
77 : "0" (num->mantissa[i]), "1" (0), "q" (base)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
78 : "eax", "edx");
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
79 #else
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
80 overflow=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
81 val=num->mantissa[i];
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
82
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
83 if(val<ULONG_MAX/base){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
84 val*=base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
85 }else if(val){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
86 ulong tmp=val;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
87 ulong old=val;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
88 for(j=0; j<base; j++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
89 val+=tmp;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
90 if(val<=old)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
91 overflow++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
92 old=val;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
93 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
94 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
95 num->mantissa[i]=val;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
96 #endif
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
97 if(overflow){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
98 if(i==num->nmantissa){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
99 if(num->nmantissa==MAX_MANTISSA)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
100 return E_TOKZ_TOOBIG;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
101 num->nmantissa++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
102 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
103 num->mantissa[i+1]+=overflow;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
104 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
105 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
106 num->mantissa[0]+=v;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
107
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
108 return 0;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
109 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
110
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
111 #undef NP_I386_ASM
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
112
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
113 #endif /* NP_SIMPLE_IMPL */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
114
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
115
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
116 /* */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
117
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
118
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
119 static bool is_end(int c)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
120 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
121 /* oops... EOF was missing */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
122 return (c==EOF || (c!='.' && ispunct(c)) || isspace(c) || iscntrl(c));
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
123 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
124
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
125
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
126 /* */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
127
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
128
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
129 static int parse_exponent(NPNum *num, Tokenizer *tokz, int c)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
130 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
131 long exp=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
132 bool neg=FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
133 int err=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
134
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
135 c=GETCH();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
136
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
137 if(c=='-' || c=='+'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
138 if(c=='-')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
139 neg=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
140 c=GETCH();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
141 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
142
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
143 for(; 1; c=GETCH()){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
144 if(isdigit(c)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
145 exp*=10;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
146 exp+=c-'0';
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
147 }else if(is_end(c)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
148 UNGETCH(c);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
149 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
150 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
151 err=E_TOKZ_NUMFMT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
152 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
153 }
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
154
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
155 if(neg)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
156 exp*=-1;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
157
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
158 #ifndef NP_SIMPLE_IMPL
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
159 ADD_EXP(num, exp);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
160 #else
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
161 num->fval*=pow(num->base, exp);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
162 #endif
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
163 return err;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
164 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
165
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
166
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
167 static int parse_number(NPNum *num, Tokenizer *tokz, int c)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
168 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
169 int base=10;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
170 int dm=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
171 int err=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
172 int tmp;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
173 #ifdef NP_SIMPLE_IMPL
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
174 double divisor=base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
175 #endif
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
176
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
177 if(c=='-' || c=='+'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
178 if(c=='-')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
179 num->negative=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
180 c=GETCH();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
181 if(!isdigit(c))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
182 err=E_TOKZ_NUMFMT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
183 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
184
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
185 if(c=='0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
186 dm=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
187 c=GETCH();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
188 if(c=='x' || c=='X'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
189 base=16;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
190 c=GETCH();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
191 }else if(c=='b' || c=='B'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
192 base=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
193 c=GETCH();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
194 }else if('0'<=c && c<='7'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
195 base=8;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
196 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
197 dm=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
198 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
199 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
200
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
201 num->base=base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
202
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
203 for(; 1; c=GETCH()){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
204 if((c=='e' || c=='E') && dm!=0){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
205 if(dm<2){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
206 err=E_TOKZ_NUMFMT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
207 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
208 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
209 tmp=parse_exponent(num, tokz, c);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
210 if(err==0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
211 err=tmp;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
212 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
213 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
214
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
215 if(isxdigit(c)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
216 if('0'<=c && c<='9')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
217 c-='0';
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
218 else if(isupper(c))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
219 c-='A'-10;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
220 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
221 c-='a'-10;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
222
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
223 if(c>=base)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
224 err=E_TOKZ_NUMFMT;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
225
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
226 #ifdef NP_SIMPLE_IMPL
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
227 if(dm==3){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
228 num->fval+=(double)c/divisor;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
229 divisor*=base;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
230 }else
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
231 #endif
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
232 {
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
233 tmp=npnum_mulbase_add(num, base, c);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
234 if(err==0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
235 err=tmp;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
236 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
237
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
238 if(dm==1)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
239 dm=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
240 #ifndef NP_SIMPLE_IMPL
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
241 else if(dm==3)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
242 ADD_EXP(num, -1);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
243 #endif
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
244 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
245 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
246
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
247 if(c=='.'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
248 if(dm!=2){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
249 err=E_TOKZ_NUMFMT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
250 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
251 dm=3;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
252 #ifdef NP_SIMPLE_IMPL
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
253 num->type=NPNUM_FLOAT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
254 divisor=base;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
255 #endif
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
256 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
257 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
258
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
259 if(is_end(c)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
260 UNGETCH(c);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
261 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
262 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
263
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
264 err=E_TOKZ_NUMFMT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
265 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
266
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
267 #ifndef NP_SIMPLE_IMPL
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
268 num->type=(num->exponent==0 ? NPNUM_INT : NPNUM_FLOAT);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
269 #endif
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
270
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
271 return err;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
272 }

mercurial