From d48a9030e085bc15880c4ebba6db46b6a438bf9f Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 1 Jun 2012 00:20:31 -0300 Subject: [PATCH] b2/: introduce dimensionless values (field syntax: name=##) --- b2/chr.c | 6 ++++-- b2/dump.c | 5 +++-- b2/eval.c | 12 +++++++----- b2/lang.y | 6 ++++++ b2/param.h | 2 +- b2/test/hierfld | 14 ++++++++++++-- b2/test/hierrul | 41 +++++++++++++++++++++++++++++++++++++++-- 7 files changed, 72 insertions(+), 14 deletions(-) diff --git a/b2/chr.c b/b2/chr.c index 1b68572..ecad525 100644 --- a/b2/chr.c +++ b/b2/chr.c @@ -141,9 +141,11 @@ static void dump_field_decl(FILE *file, const struct field *field) else if (field->fmt->ops == ¶m_ops_set) dump_set_decl(file, field->fmt->u.set); else if (field->fmt->ops == ¶m_ops_abs) - fprintf(file, "#%s", field->fmt->u.abs); + fprintf(file, "#%s", + field->fmt->u.abs ? field->fmt->u.abs : "#"); else if (field->fmt->ops == ¶m_ops_rel) - fprintf(file, "%%%s", field->fmt->u.rel->u.abs); + fprintf(file, "%%%s", + field->fmt->u.rel->u.abs ? field->fmt->u.rel->u.abs : "#"); else abort(); } diff --git a/b2/dump.c b/b2/dump.c index 31d3f8b..b4e9f2a 100644 --- a/b2/dump.c +++ b/b2/dump.c @@ -43,7 +43,7 @@ void dump_set(FILE *file, const struct format *fmt, const struct value *v) void dump_abs(FILE *file, const struct format *fmt, const struct value *v) { - fprintf(file, "%g%s", v->u.abs, fmt->u.abs); + fprintf(file, "%g%s", v->u.abs, fmt->u.abs ? fmt->u.abs : ""); } @@ -54,7 +54,8 @@ void dump_rel(FILE *file, const struct format *fmt, const struct value *v) v->u.rel.minus*100, v->u.rel.plus*100); else fprintf(file, "-%g/+%g%s", - v->u.rel.minus, v->u.rel.plus, fmt->u.rel->u.abs); + v->u.rel.minus, v->u.rel.plus, + fmt->u.rel->u.abs ? fmt->u.rel->u.abs : ""); } diff --git a/b2/eval.c b/b2/eval.c index 9cfa4c1..5163c77 100644 --- a/b2/eval.c +++ b/b2/eval.c @@ -92,11 +92,13 @@ static int strip_unit(const char *s, const char **stop, const char *unit, { int n; - n = strlen(unit); - if (*stop-n < s) - return 0; - if (!strcmp(*stop-n, unit)) - *stop -= n; + if (unit) { + n = strlen(unit); + if (*stop-n < s) + return 0; + if (!strcmp(*stop-n, unit)) + *stop -= n; + } if (*stop > s) { if (decode_mult((*stop)[-1], mult)) (*stop)--; diff --git a/b2/lang.y b/b2/lang.y index b1e87c3..3f79991 100644 --- a/b2/lang.y +++ b/b2/lang.y @@ -393,6 +393,12 @@ format: $$->ops = ¶m_ops_abs; $$->u.abs = $2; } + | '#' '#' + { + $$ = alloc_type(struct format); + $$->ops = ¶m_ops_abs; + $$->u.abs = NULL; + } | '%' WORD { $$ = alloc_type(struct format); diff --git a/b2/param.h b/b2/param.h index a4d884a..9f74bd3 100644 --- a/b2/param.h +++ b/b2/param.h @@ -45,7 +45,7 @@ struct format { const struct param_ops *ops; union { const struct names *set; - const char *abs; /* unit name */ + const char *abs; /* unit name; NULL if dimensionless */ const struct format *rel; } u; }; diff --git a/b2/test/hierfld b/b2/test/hierfld index 3de01f8..b40a32f 100755 --- a/b2/test/hierfld +++ b/b2/test/hierfld @@ -41,7 +41,7 @@ EOF #------------------------------------------------------------------------------ -tst "hierarchy: absolute value field" < = mouse = mouse = mouse