ObjFW  Check-in [f5dd9b7f73]

Overview
Comment:OFLocale: Add . operator to check for real numbers
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f5dd9b7f73e67908cb9aa812c0ff88828aff026282012e44c7e4abebc8ff4c9a
User & Date: js on 2020-04-12 15:57:42
Other Links: manifest | tags
Context
2020-04-12
22:09
OFLocale: Switch to infix notation check-in: fe77fef5ec user: js tags: trunk
15:57
OFLocale: Add . operator to check for real numbers check-in: f5dd9b7f73 user: js tags: trunk
15:39
OFLocale: Add a stack machine for plurals check-in: 411b99fea8 user: js tags: trunk
Changes

Modified src/OFLocale.m from [c6430c7378] to [279230af16].

107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
			TYPE_LESS_EQUAL,
			TYPE_GREATER,
			TYPE_GREATER_EQUAL,
			TYPE_ADD,
			TYPE_MODULO,
			TYPE_AND,
			TYPE_OR,
			TYPE_NOT

		} type;
		id var, first, second;
		size_t stackSize;

		if ([atom isEqual: @"=="]) {
			type = TYPE_EQUAL;
		} else if ([atom isEqual: @"!="]) {







|
>







107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
			TYPE_LESS_EQUAL,
			TYPE_GREATER,
			TYPE_GREATER_EQUAL,
			TYPE_ADD,
			TYPE_MODULO,
			TYPE_AND,
			TYPE_OR,
			TYPE_NOT,
			TYPE_IS_REAL
		} type;
		id var, first, second;
		size_t stackSize;

		if ([atom isEqual: @"=="]) {
			type = TYPE_EQUAL;
		} else if ([atom isEqual: @"!="]) {
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
			type = TYPE_MODULO;
		} else if ([atom isEqual: @"&&"]) {
			type = TYPE_AND;
		} else if ([atom isEqual: @"||"]) {
			type = TYPE_OR;
		} else if ([atom isEqual: @"!"]) {
			type = TYPE_NOT;


		} else {
			of_unichar_t firstCharacter =
			    [atom characterAtIndex: 0];

			if (firstCharacter >= '0' && firstCharacter <= '9')

				type = TYPE_LITERAL;
			else
				type = TYPE_VARIABLE;
		}

		switch (type) {
		case TYPE_LITERAL:
			[stack addObject:
			    [OFNumber numberWithIntMax: atom.decimalValue]];
			break;
		case TYPE_VARIABLE:
			if ((var = [variables objectForKey: atom]) == nil)
				@throw [OFInvalidFormatException exception];

			if ([var isKindOfClass: [OFString class]])
				var = [OFNumber numberWithIntMax:
				    [var decimalValue]];

			[stack addObject: var];
			break;
		case TYPE_EQUAL:
		case TYPE_NOT_EQUAL:
		case TYPE_LESS:
		case TYPE_LESS_EQUAL:







>
>




|
>








|






|
|







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
			type = TYPE_MODULO;
		} else if ([atom isEqual: @"&&"]) {
			type = TYPE_AND;
		} else if ([atom isEqual: @"||"]) {
			type = TYPE_OR;
		} else if ([atom isEqual: @"!"]) {
			type = TYPE_NOT;
		} else if ([atom isEqual: @"."]) {
			type = TYPE_IS_REAL;
		} else {
			of_unichar_t firstCharacter =
			    [atom characterAtIndex: 0];

			if ((firstCharacter >= '0' && firstCharacter <= '9') ||
			    firstCharacter == '-')
				type = TYPE_LITERAL;
			else
				type = TYPE_VARIABLE;
		}

		switch (type) {
		case TYPE_LITERAL:
			[stack addObject:
			    [OFNumber numberWithDouble: atom.doubleValue]];
			break;
		case TYPE_VARIABLE:
			if ((var = [variables objectForKey: atom]) == nil)
				@throw [OFInvalidFormatException exception];

			if ([var isKindOfClass: [OFString class]])
				var = [OFNumber numberWithDouble:
				    [var doubleValue]];

			[stack addObject: var];
			break;
		case TYPE_EQUAL:
		case TYPE_NOT_EQUAL:
		case TYPE_LESS:
		case TYPE_LESS_EQUAL:
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
				    compare: second] == OF_ORDERED_DESCENDING];
				break;
			case TYPE_GREATER_EQUAL:
				var = [OFNumber numberWithBool: [first
				    compare: second] != OF_ORDERED_ASCENDING];
				break;
			case TYPE_ADD:
				var = [OFNumber numberWithIntMax:
				    [first intMaxValue] + [second intMaxValue]];
				break;
			case TYPE_MODULO:
				var = [OFNumber numberWithIntMax:
				    [first intMaxValue] % [second intMaxValue]];
				break;
			case TYPE_AND:
				var = [OFNumber numberWithBool:







|
|







203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
				    compare: second] == OF_ORDERED_DESCENDING];
				break;
			case TYPE_GREATER_EQUAL:
				var = [OFNumber numberWithBool: [first
				    compare: second] != OF_ORDERED_ASCENDING];
				break;
			case TYPE_ADD:
				var = [OFNumber numberWithDouble:
				    [first doubleValue] + [second doubleValue]];
				break;
			case TYPE_MODULO:
				var = [OFNumber numberWithIntMax:
				    [first intMaxValue] % [second intMaxValue]];
				break;
			case TYPE_AND:
				var = [OFNumber numberWithBool:
224
225
226
227
228
229
230

231




232
233









234
235
236
237
238
239
240
241
242

			[stack replaceObjectAtIndex: stackSize - 2
					 withObject: var];
			[stack removeLastObject];

			break;
		case TYPE_NOT:

			stackSize = stack.count;




			first = [OFNumber numberWithBool:
			    ![stack.lastObject boolValue]];









			[stack replaceObjectAtIndex: stackSize - 1
					 withObject: first];
			break;
		}
	}

	if (stack.count != 1)
		@throw [OFInvalidFormatException exception];








>

>
>
>
>
|
|
>
>
>
>
>
>
>
>
>

|







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

			[stack replaceObjectAtIndex: stackSize - 2
					 withObject: var];
			[stack removeLastObject];

			break;
		case TYPE_NOT:
		case TYPE_IS_REAL:
			stackSize = stack.count;
			first = stack.lastObject;

			switch (type) {
			case TYPE_NOT:
				var = [OFNumber numberWithBool:
				    ![first boolValue]];
				break;
			case TYPE_IS_REAL:
				var = [OFNumber numberWithBool:
				    [first doubleValue] != [first intMaxValue]];
				break;
			default:
				OF_ENSURE(0);
			}

			[stack replaceObjectAtIndex: stackSize - 1
					 withObject: var];
			break;
		}
	}

	if (stack.count != 1)
		@throw [OFInvalidFormatException exception];