X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Flexer%2Ftoken.c;h=71e98f2d7de44369f4648b498be2569d85978c37;hb=0fde6afee3c995bf264c24c438f43eeb58b859b5;hp=89a5cf0102baf5fa77347ef8051469a8fde8dc77;hpb=fe8dc2171009e90d2335f159d05f7e6660e24780;p=pspp diff --git a/src/language/lexer/token.c b/src/language/lexer/token.c index 89a5cf0102..71e98f2d7d 100644 --- a/src/language/lexer/token.c +++ b/src/language/lexer/token.c @@ -25,25 +25,55 @@ #include "data/identifier.h" #include "libpspp/assertion.h" #include "libpspp/cast.h" +#include "libpspp/misc.h" #include "gl/ftoastr.h" #include "gl/xalloc.h" -/* Initializes TOKEN with an arbitrary type, number 0, and a null string. */ +/* Initializes DST as a copy of SRC. */ void -token_init (struct token *token) +token_copy (struct token *dst, const struct token *src) { - token->type = 0; - token->number = 0.0; - token->string = ss_empty (); + *dst = (struct token) { + .type = src->type, + .number = src->number, + }; + ss_alloc_substring (&dst->string, src->string); } /* Frees the string that TOKEN contains. */ void -token_destroy (struct token *token) +token_uninit (struct token *token) { if (token != NULL) - ss_dealloc (&token->string); + { + ss_dealloc (&token->string); + *token = (struct token) { .type = T_STOP }; + } +} + +/* Returns true if A and B are the same token, false otherwise. */ +bool +token_equal (const struct token *a, const struct token *b) +{ + if (a->type != b->type) + return false; + + switch (a->type) + { + case T_POS_NUM: + case T_NEG_NUM: + return a->number == b->number; + + case T_ID: + case T_MACRO_ID: + case T_MACRO_PUNCT: + case T_STRING: + return ss_equals (a->string, b->string); + + default: + return true; + } } static char * @@ -51,7 +81,7 @@ number_token_to_string (const struct token *token) { char buffer[DBL_BUFSIZE_BOUND]; - dtoastr (buffer, sizeof buffer, 0, 0, fabs (token->number)); + c_dtoastr (buffer, sizeof buffer, 0, 0, fabs (token->number)); return (token->type == T_POS_NUM ? xstrdup (buffer) : xasprintf ("-%s", buffer)); @@ -133,8 +163,6 @@ string_representation (struct substring ss) char * token_to_string (const struct token *token) { - const char *name; - switch (token->type) { case T_POS_NUM: @@ -142,14 +170,15 @@ token_to_string (const struct token *token) return number_token_to_string (token); case T_ID: + case T_MACRO_ID: + case T_MACRO_PUNCT: return ss_xstrdup (token->string); case T_STRING: return string_representation (token->string); default: - name = token_type_to_name (token->type); - return name != NULL ? xstrdup (name) : NULL; + return xstrdup_if_nonnull (token_type_to_string (token->type)); } } @@ -163,7 +192,7 @@ token_print (const struct token *token, FILE *stream) { char s[DBL_BUFSIZE_BOUND]; - dtoastr (s, sizeof s, 0, 0, token->number); + c_dtoastr (s, sizeof s, 0, 0, token->number); fprintf (stream, "\t%s", s); } if (token->type == T_ID || token->type == T_STRING || token->string.length) @@ -171,3 +200,22 @@ token_print (const struct token *token, FILE *stream) (int) token->string.length, token->string.string); putc ('\n', stream); } + +/* Returns true if T is a numeric token for an integer in the range of "long", + except that LONG_MIN is excluded. */ +bool +token_is_integer (const struct token *t) +{ + return (token_is_number (t) + && t->number > LONG_MIN + && t->number <= LONG_MAX + && floor (t->number) == t->number); +} + +/* Returns the "long int" value of T, which must satisfy token_is_integer(T). */ +long +token_integer (const struct token *t) +{ + assert (token_is_integer (t)); + return t->number; +}