-2001-03-16 Bruno Haible <haible@clisp.cons.org>
+2001-03-17 Bruno Haible <haible@clisp.cons.org>
* gettextP.h (struct expression): Add operators lnot, less_than,
- greater_than, less_or_equal, greater_or_equal. Add args1 to union.
- * plural.y ('?' ':'): Make right-associative.
- ('<', '>', LE, GE, '!'): New operators.
- (EQ): Renamed from '='.
- (NE): Renamed from '!'.
- (exp): Add rules with these operators.
+ greater_than, less_or_equal, greater_or_equal. Replace args2/args3
+ union by a 'nargs' counter and an 'args[]' array.
+ * plural.y: Don't include stdarg.h.
+ (new_exp): New function.
+ (new_exp_0, new_exp_2, new_exp_3): Rewritten to call new_exp.
(new_exp_1): New function.
- (FREE_EXPRESSION): Recognize these operators.
- (yylex): Don't skip "\\n". Recognize '<', '>', LE, GE, '!' operators.
- * dcigettext.c (plural_eval): Recognize these operators.
+ ('?' ':'): Make right-associative.
+ (EQUOP2): New token, replaces '=' and '!'.
+ (CMPOP2): New token.
+ (ADDOP2): New token, replaces '+' and '-'.
+ (MULOP2): New token, replaces '*', '/' and '%'.
+ ('!'): New token.
+ (exp): Add rules for CMPOP2 and '!'. Don't call YYABORT.
+ (start): Call YYABORT here.
+ (FREE_EXPRESSION): Update.
+ (yylex): Don't skip "\\n". Recognize comparison and '!' operators.
+ Update for new token symbols.
+ * loadmsgcat.c (plvar, plone, germanic_plural, init_germanic_plural):
+ Update.
+ * dcigettext.c (_nl_find_msg): Optimize for space.
+ (plural_eval): Recognize comparison and '!' operators. Optimize for
+ space.
+
+ * dcigettext.c (transcmp): New declaration.
2001-03-10 Bruno Haible <haible@clisp.cons.org>
# endif
/* Function to compare two entries in the table of known translations. */
+static int transcmp PARAMS ((const void *p1, const void *p2));
static int
transcmp (p1, p2)
const void *p1;
nls_uint32 hash_val = hash_string (msgid);
nls_uint32 idx = hash_val % domain->hash_size;
nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
- nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
-
- if (nstr == 0)
- /* Hash table entry is empty. */
- return NULL;
while (1)
{
+ nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
+
+ if (nstr == 0)
+ /* Hash table entry is empty. */
+ return NULL;
+
/* Compare msgid with the original string at index nstr-1.
We compare the lengths with >=, not ==, because plural entries
are represented by strings with an embedded NUL. */
idx -= domain->hash_size - incr;
else
idx += incr;
-
- nstr = W (domain->must_swap, domain->hash_tab[idx]);
- if (nstr == 0)
- /* Hash table entry is empty. */
- return NULL;
}
/* NOTREACHED */
}
struct expression *pexp;
unsigned long int n;
{
- switch (pexp->operation)
+ switch (pexp->nargs)
{
- case var:
- return n;
- case num:
- return pexp->val.num;
- case lnot:
- return ! plural_eval (pexp->val.args1.right, n);
- case mult:
- return (plural_eval (pexp->val.args2.left, n)
- * plural_eval (pexp->val.args2.right, n));
- case divide:
- return (plural_eval (pexp->val.args2.left, n)
- / plural_eval (pexp->val.args2.right, n));
- case module:
- return (plural_eval (pexp->val.args2.left, n)
- % plural_eval (pexp->val.args2.right, n));
- case plus:
- return (plural_eval (pexp->val.args2.left, n)
- + plural_eval (pexp->val.args2.right, n));
- case minus:
- return (plural_eval (pexp->val.args2.left, n)
- - plural_eval (pexp->val.args2.right, n));
- case less_than:
- return (plural_eval (pexp->val.args2.left, n)
- < plural_eval (pexp->val.args2.right, n));
- case greater_than:
- return (plural_eval (pexp->val.args2.left, n)
- > plural_eval (pexp->val.args2.right, n));
- case less_or_equal:
- return (plural_eval (pexp->val.args2.left, n)
- <= plural_eval (pexp->val.args2.right, n));
- case greater_or_equal:
- return (plural_eval (pexp->val.args2.left, n)
- >= plural_eval (pexp->val.args2.right, n));
- case equal:
- return (plural_eval (pexp->val.args2.left, n)
- == plural_eval (pexp->val.args2.right, n));
- case not_equal:
- return (plural_eval (pexp->val.args2.left, n)
- != plural_eval (pexp->val.args2.right, n));
- case land:
- return (plural_eval (pexp->val.args2.left, n)
- && plural_eval (pexp->val.args2.right, n));
- case lor:
- return (plural_eval (pexp->val.args2.left, n)
- || plural_eval (pexp->val.args2.right, n));
- case qmop:
- return (plural_eval (pexp->val.args3.bexp, n)
- ? plural_eval (pexp->val.args3.tbranch, n)
- : plural_eval (pexp->val.args3.fbranch, n));
+ case 0:
+ switch (pexp->operation)
+ {
+ case var:
+ return n;
+ case num:
+ return pexp->val.num;
+ default:
+ break;
+ }
+ /* NOTREACHED */
+ break;
+ case 1:
+ {
+ /* pexp->operation must be lnot. */
+ unsigned long int arg = plural_eval (pexp->val.args[0], n);
+ return ! arg;
+ }
+ case 2:
+ {
+ unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
+ if (pexp->operation == lor)
+ return leftarg || plural_eval (pexp->val.args[1], n);
+ else if (pexp->operation == land)
+ return leftarg && plural_eval (pexp->val.args[1], n);
+ else
+ {
+ unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
+
+ switch (pexp->operation)
+ {
+ case mult:
+ return leftarg * rightarg;
+ case divide:
+ return leftarg / rightarg;
+ case module:
+ return leftarg % rightarg;
+ case plus:
+ return leftarg + rightarg;
+ case minus:
+ return leftarg - rightarg;
+ case less_than:
+ return leftarg < rightarg;
+ case greater_than:
+ return leftarg > rightarg;
+ case less_or_equal:
+ return leftarg <= rightarg;
+ case greater_or_equal:
+ return leftarg >= rightarg;
+ case equal:
+ return leftarg == rightarg;
+ case not_equal:
+ return leftarg != rightarg;
+ default:
+ break;
+ }
+ }
+ /* NOTREACHED */
+ break;
+ }
+ case 3:
+ {
+ /* pexp->operation must be qmop. */
+ unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
+ return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
+ }
}
/* NOTREACHED */
return 0;
%union {
unsigned long int num;
+ enum operator op;
struct expression *exp;
}
%{
/* Prototypes for local functions. */
-static struct expression *new_exp_0 PARAMS ((enum operator op));
-static struct expression *new_exp_1 PARAMS ((enum operator op,
- struct expression *right));
+static struct expression *new_exp PARAMS ((int nargs, enum operator op,
+ struct expression * const *args));
+static inline struct expression *new_exp_0 PARAMS ((enum operator op));
+static inline struct expression *new_exp_1 PARAMS ((enum operator op,
+ struct expression *right));
static struct expression *new_exp_2 PARAMS ((enum operator op,
struct expression *left,
struct expression *right));
-static struct expression *new_exp_3 PARAMS ((enum operator op,
- struct expression *bexp,
- struct expression *tbranch,
- struct expression *fbranch));
+static inline struct expression *new_exp_3 PARAMS ((enum operator op,
+ struct expression *bexp,
+ struct expression *tbranch,
+ struct expression *fbranch));
static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
static void yyerror PARAMS ((const char *str));
+
+/* Allocation of expressions. */
+
+static struct expression *
+new_exp (nargs, op, args)
+ int nargs;
+ enum operator op;
+ struct expression * const *args;
+{
+ int i;
+ struct expression *newp;
+
+ /* If any of the argument could not be malloc'ed, just return NULL. */
+ for (i = nargs - 1; i >= 0; i--)
+ if (args[i] == NULL)
+ goto fail;
+
+ /* Allocate a new expression. */
+ newp = (struct expression *) malloc (sizeof (*newp));
+ if (newp != NULL)
+ {
+ newp->nargs = nargs;
+ newp->operation = op;
+ for (i = nargs - 1; i >= 0; i--)
+ newp->val.args[i] = args[i];
+ return newp;
+ }
+
+ fail:
+ for (i = nargs - 1; i >= 0; i--)
+ FREE_EXPRESSION (args[i]);
+
+ return NULL;
+}
+
+static inline struct expression *
+new_exp_0 (op)
+ enum operator op;
+{
+ return new_exp (0, op, NULL);
+}
+
+static inline struct expression *
+new_exp_1 (op, right)
+ enum operator op;
+ struct expression *right;
+{
+ struct expression *args[1];
+
+ args[0] = right;
+ return new_exp (1, op, args);
+}
+
+static struct expression *
+new_exp_2 (op, left, right)
+ enum operator op;
+ struct expression *left;
+ struct expression *right;
+{
+ struct expression *args[2];
+
+ args[0] = left;
+ args[1] = right;
+ return new_exp (2, op, args);
+}
+
+static inline struct expression *
+new_exp_3 (op, bexp, tbranch, fbranch)
+ enum operator op;
+ struct expression *bexp;
+ struct expression *tbranch;
+ struct expression *fbranch;
+{
+ struct expression *args[3];
+
+ args[0] = bexp;
+ args[1] = tbranch;
+ args[2] = fbranch;
+ return new_exp (3, op, args);
+}
+
%}
/* This declares that all operators have the same associativity and the
precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
- There is no unary minus and no bitwise operators. */
-%right '?' /* ? */
-%left '|' /* || */
-%left '&' /* && */
-%left EQ, NE /* == != */
-%left '<', '>', LE, GE /* < > <= >= */
-%left '+', '-' /* + - */
-%left '*', '/', '%' /* * / % */
-%right '!' /* ! */
-
+ There is no unary minus and no bitwise operators.
+ Operators with the same syntactic behaviour have been merged into a single
+ token, to save space in the array generated by bison. */
+%right '?' /* ? */
+%left '|' /* || */
+%left '&' /* && */
+%left EQUOP2 /* == != */
+%left CMPOP2 /* < > <= >= */
+%left ADDOP2 /* + - */
+%left MULOP2 /* * / % */
+%right '!' /* ! */
+
+%token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
%token <num> NUMBER
%type <exp> exp
start: exp
{
+ if ($1 == NULL)
+ YYABORT;
((struct parse_args *) arg)->res = $1;
}
;
exp: exp '?' exp ':' exp
{
- if (($$ = new_exp_3 (qmop, $1, $3, $5)) == NULL)
- YYABORT
+ $$ = new_exp_3 (qmop, $1, $3, $5);
}
| exp '|' exp
{
- if (($$ = new_exp_2 (lor, $1, $3)) == NULL)
- YYABORT
+ $$ = new_exp_2 (lor, $1, $3);
}
| exp '&' exp
{
- if (($$ = new_exp_2 (land, $1, $3)) == NULL)
- YYABORT
+ $$ = new_exp_2 (land, $1, $3);
}
- | exp EQ exp
+ | exp EQUOP2 exp
{
- if (($$ = new_exp_2 (equal, $1, $3)) == NULL)
- YYABORT
+ $$ = new_exp_2 ($2, $1, $3);
}
- | exp NE exp
+ | exp CMPOP2 exp
{
- if (($$ = new_exp_2 (not_equal, $1, $3)) == NULL)
- YYABORT
+ $$ = new_exp_2 ($2, $1, $3);
}
- | exp '<' exp
+ | exp ADDOP2 exp
{
- if (($$ = new_exp_2 (less_than, $1, $3)) == NULL)
- YYABORT
+ $$ = new_exp_2 ($2, $1, $3);
}
- | exp '>' exp
+ | exp MULOP2 exp
{
- if (($$ = new_exp_2 (greater_than, $1, $3)) == NULL)
- YYABORT
- }
- | exp LE exp
- {
- if (($$ = new_exp_2 (less_or_equal, $1, $3)) == NULL)
- YYABORT
- }
- | exp GE exp
- {
- if (($$ = new_exp_2 (greater_or_equal, $1, $3)) == NULL)
- YYABORT
- }
- | exp '+' exp
- {
- if (($$ = new_exp_2 (plus, $1, $3)) == NULL)
- YYABORT
- }
- | exp '-' exp
- {
- if (($$ = new_exp_2 (minus, $1, $3)) == NULL)
- YYABORT
- }
- | exp '*' exp
- {
- if (($$ = new_exp_2 (mult, $1, $3)) == NULL)
- YYABORT
- }
- | exp '/' exp
- {
- if (($$ = new_exp_2 (divide, $1, $3)) == NULL)
- YYABORT
- }
- | exp '%' exp
- {
- if (($$ = new_exp_2 (module, $1, $3)) == NULL)
- YYABORT
+ $$ = new_exp_2 ($2, $1, $3);
}
| '!' exp
{
- if (($$ = new_exp_1 (lnot, $2)) == NULL)
- YYABORT
+ $$ = new_exp_1 (lnot, $2);
}
| 'n'
{
- if (($$ = new_exp_0 (var)) == NULL)
- YYABORT
+ $$ = new_exp_0 (var);
}
| NUMBER
{
- if (($$ = new_exp_0 (num)) == NULL)
- YYABORT;
- $$->val.num = $1
+ if (($$ = new_exp_0 (num)) != NULL)
+ $$->val.num = $1;
}
| '(' exp ')'
{
- $$ = $2
+ $$ = $2;
}
;
%%
-static struct expression *
-new_exp_0 (op)
- enum operator op;
-{
- struct expression *newp = (struct expression *) malloc (sizeof (*newp));
-
- if (newp != NULL)
- newp->operation = op;
-
- return newp;
-}
-
-static struct expression *
-new_exp_1 (op, right)
- enum operator op;
- struct expression *right;
-{
- struct expression *newp = NULL;
-
- if (right != NULL)
- newp = (struct expression *) malloc (sizeof (*newp));
-
- if (newp != NULL)
- {
- newp->operation = op;
- newp->val.args1.right = right;
- }
- else
- {
- FREE_EXPRESSION (right);
- }
-
- return newp;
-}
-
-static struct expression *
-new_exp_2 (op, left, right)
- enum operator op;
- struct expression *left;
- struct expression *right;
-{
- struct expression *newp = NULL;
-
- if (left != NULL && right != NULL)
- newp = (struct expression *) malloc (sizeof (*newp));
-
- if (newp != NULL)
- {
- newp->operation = op;
- newp->val.args2.left = left;
- newp->val.args2.right = right;
- }
- else
- {
- FREE_EXPRESSION (left);
- FREE_EXPRESSION (right);
- }
-
- return newp;
-}
-
-static struct expression *
-new_exp_3 (op, bexp, tbranch, fbranch)
- enum operator op;
- struct expression *bexp;
- struct expression *tbranch;
- struct expression *fbranch;
-{
- struct expression *newp = NULL;
-
- if (bexp != NULL && tbranch != NULL && fbranch != NULL)
- newp = (struct expression *) malloc (sizeof (*newp));
-
- if (newp != NULL)
- {
- newp->operation = op;
- newp->val.args3.bexp = bexp;
- newp->val.args3.tbranch = tbranch;
- newp->val.args3.fbranch = fbranch;
- }
- else
- {
- FREE_EXPRESSION (bexp);
- FREE_EXPRESSION (tbranch);
- FREE_EXPRESSION (fbranch);
- }
-
- return newp;
-}
-
void
internal_function
FREE_EXPRESSION (exp)
return;
/* Handle the recursive case. */
- switch (exp->operation)
+ switch (exp->nargs)
{
- case qmop:
- FREE_EXPRESSION (exp->val.args3.fbranch);
- /* FREE_EXPRESSION (exp->val.args3.tbranch); */
- /* FREE_EXPRESSION (exp->val.args3.bexp); */
- /* break; */
- /* instead: FALLTHROUGH */
-
- case mult:
- case divide:
- case module:
- case plus:
- case minus:
- case less_than:
- case greater_than:
- case less_or_equal:
- case greater_or_equal:
- case equal:
- case not_equal:
- case land:
- case lor:
- FREE_EXPRESSION (exp->val.args2.right);
- /* FREE_EXPRESSION (exp->val.args2.left); */
- /* break; */
- /* instead: FALLTHROUGH */
-
- case lnot:
- FREE_EXPRESSION (exp->val.args1.right);
- break;
-
+ case 3:
+ FREE_EXPRESSION (exp->val.args[2]);
+ /* FALLTHROUGH */
+ case 2:
+ FREE_EXPRESSION (exp->val.args[1]);
+ /* FALLTHROUGH */
+ case 1:
+ FREE_EXPRESSION (exp->val.args[0]);
+ /* FALLTHROUGH */
default:
break;
}
if (exp[0] == '=')
{
++exp;
- result = EQ;
+ lval->op = equal;
+ result = EQUOP2;
}
else
result = YYERRCODE;
if (exp[0] == '=')
{
++exp;
- result = NE;
+ lval->op = not_equal;
+ result = EQUOP2;
}
break;
if (exp[0] == '=')
{
++exp;
- result = LE;
+ lval->op = less_or_equal;
}
+ else
+ lval->op = less_than;
+ result = CMPOP2;
break;
case '>':
if (exp[0] == '=')
{
++exp;
- result = GE;
+ lval->op = greater_or_equal;
}
+ else
+ lval->op = greater_than;
+ result = CMPOP2;
break;
- case 'n':
case '*':
+ lval->op = mult;
+ result = MULOP2;
+ break;
+
case '/':
+ lval->op = divide;
+ result = MULOP2;
+ break;
+
case '%':
+ lval->op = module;
+ result = MULOP2;
+ break;
+
case '+':
+ lval->op = plus;
+ result = ADDOP2;
+ break;
+
case '-':
+ lval->op = minus;
+ result = ADDOP2;
+ break;
+
+ case 'n':
case '?':
case ':':
case '(':