test-token.c 1.01 KB
Newer Older
1 2
#include <gnumeric-config.h>
#include <gnumeric.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
#include "token.h"

char *tests [] = {
	"5+4",
	"SUM(A1..A5)",
	"F(56.4+A)",
	NULL
};

int
main ()
{
	token_result_t res;
	char *s;
	int i, v;
	mp_exp_t exp;
22

Arturo Espinosa's avatar
Arturo Espinosa committed
23 24 25
	for (i = 0; tests [i]; i++){
		char *test = tests [i];
		int v;
26

Arturo Espinosa's avatar
Arturo Espinosa committed
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
		printf ("Expression: %s\n", test);

		while ((v = token_get_next (&test, &res)) != 0){
			switch (v){
			case TOKEN_IDENTIFIER:
				printf ("id=%s, ", res.v.str_value);
				break;

			case TOKEN_NUMBER:
				if (res.type == R_FLOAT){
					s = mpf_get_str (NULL, &exp, 0, 10, res.v.float_value);
					printf ("float=%c.%sE%d, ", s [0], &s[1], exp);
				} else {
					s = mpz_get_str (NULL, 10, res.v.int_value);
					printf ("int=%s, ", s);
				}
				free (s);
				break;

			case TOKEN_ELLIPSIS:
				printf ("(..)");
				break;

			case TOKEN_ERROR:
				printf ("ERROR!\n");

			default:
				printf ("char[%c] ", v);
			}
			token_done (&res);
			printf ("\n");
		}
		printf ("\n");
	}
61

Arturo Espinosa's avatar
Arturo Espinosa committed
62 63
	return 0;
}