From ea9932557af5d30dc652d27edc3548664cb879c0 Mon Sep 17 00:00:00 2001 From: Rob Shearman Date: Sun, 20 Apr 2008 22:16:10 +0100 Subject: [PATCH] widl: Add support for '%' operator in expressions. --- tools/widl/header.c | 2 ++ tools/widl/parser.y | 10 +++++++++- tools/widl/typegen.c | 3 +++ tools/widl/widltypes.h | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/widl/header.c b/tools/widl/header.c index 6989149ca1f..e1dcccea9eb 100644 --- a/tools/widl/header.c +++ b/tools/widl/header.c @@ -520,6 +520,7 @@ void write_expr(FILE *h, const expr_t *e, int brackets) break; case EXPR_SHL: case EXPR_SHR: + case EXPR_MOD: case EXPR_MUL: case EXPR_DIV: case EXPR_ADD: @@ -533,6 +534,7 @@ void write_expr(FILE *h, const expr_t *e, int brackets) switch (e->type) { case EXPR_SHL: fprintf(h, " << "); break; case EXPR_SHR: fprintf(h, " >> "); break; + case EXPR_MOD: fprintf(h, " %% "); break; case EXPR_MUL: fprintf(h, " * "); break; case EXPR_DIV: fprintf(h, " / "); break; case EXPR_ADD: fprintf(h, " + "); break; diff --git a/tools/widl/parser.y b/tools/widl/parser.y index 32752bf15da..31f631f64a8 100644 --- a/tools/widl/parser.y +++ b/tools/widl/parser.y @@ -293,7 +293,7 @@ static void add_explicit_handle_if_necessary(func_t *func); %left '|' %left '&' %left '-' '+' -%left '*' '/' +%left '*' '/' '%' %left SHL SHR %left '.' MEMBERPTR '[' ']' %right '~' @@ -632,6 +632,7 @@ expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); } | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); } | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); } | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); } + | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); } | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); } | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); } | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); } @@ -1238,6 +1239,13 @@ static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2) case EXPR_SUB: e->cval = expr1->cval - expr2->cval; break; + case EXPR_MOD: + if (expr2->cval == 0) { + error_loc("divide by zero in expression\n"); + e->cval = 0; + } else + e->cval = expr1->cval % expr2->cval; + break; case EXPR_MUL: e->cval = expr1->cval * expr2->cval; break; diff --git a/tools/widl/typegen.c b/tools/widl/typegen.c index da5c95aa262..575f06c05de 100644 --- a/tools/widl/typegen.c +++ b/tools/widl/typegen.c @@ -352,6 +352,7 @@ static int compare_expr(const expr_t *a, const expr_t *b) case EXPR_AND: case EXPR_ADD: case EXPR_SUB: + case EXPR_MOD: case EXPR_MUL: case EXPR_DIV: case EXPR_SHL: @@ -3148,6 +3149,7 @@ static void write_struct_expr(FILE *h, const expr_t *e, int brackets, break; case EXPR_SHL: case EXPR_SHR: + case EXPR_MOD: case EXPR_MUL: case EXPR_DIV: case EXPR_ADD: @@ -3161,6 +3163,7 @@ static void write_struct_expr(FILE *h, const expr_t *e, int brackets, switch (e->type) { case EXPR_SHL: fprintf(h, " << "); break; case EXPR_SHR: fprintf(h, " >> "); break; + case EXPR_MOD: fprintf(h, " %% "); break; case EXPR_MUL: fprintf(h, " * "); break; case EXPR_DIV: fprintf(h, " / "); break; case EXPR_ADD: fprintf(h, " + "); break; diff --git a/tools/widl/widltypes.h b/tools/widl/widltypes.h index 31c7599a554..879c7a93a1a 100644 --- a/tools/widl/widltypes.h +++ b/tools/widl/widltypes.h @@ -164,6 +164,7 @@ enum expr_type EXPR_MEMBERPTR, EXPR_MEMBER, EXPR_ARRAY, + EXPR_MOD, }; enum type_kind