1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-22 22:15:19 +02:00

b2/: introduce dimensionless values (field syntax: name=##)

This commit is contained in:
Werner Almesberger 2012-06-01 00:20:31 -03:00
parent 4fb5f26043
commit d48a9030e0
7 changed files with 72 additions and 14 deletions

View File

@ -141,9 +141,11 @@ static void dump_field_decl(FILE *file, const struct field *field)
else if (field->fmt->ops == &param_ops_set) else if (field->fmt->ops == &param_ops_set)
dump_set_decl(file, field->fmt->u.set); dump_set_decl(file, field->fmt->u.set);
else if (field->fmt->ops == &param_ops_abs) else if (field->fmt->ops == &param_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 == &param_ops_rel) else if (field->fmt->ops == &param_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 else
abort(); abort();
} }

View File

@ -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) 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); v->u.rel.minus*100, v->u.rel.plus*100);
else else
fprintf(file, "-%g/+%g%s", 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 : "");
} }

View File

@ -92,11 +92,13 @@ static int strip_unit(const char *s, const char **stop, const char *unit,
{ {
int n; int n;
if (unit) {
n = strlen(unit); n = strlen(unit);
if (*stop-n < s) if (*stop-n < s)
return 0; return 0;
if (!strcmp(*stop-n, unit)) if (!strcmp(*stop-n, unit))
*stop -= n; *stop -= n;
}
if (*stop > s) { if (*stop > s) {
if (decode_mult((*stop)[-1], mult)) if (decode_mult((*stop)[-1], mult))
(*stop)--; (*stop)--;

View File

@ -393,6 +393,12 @@ format:
$$->ops = &param_ops_abs; $$->ops = &param_ops_abs;
$$->u.abs = $2; $$->u.abs = $2;
} }
| '#' '#'
{
$$ = alloc_type(struct format);
$$->ops = &param_ops_abs;
$$->u.abs = NULL;
}
| '%' WORD | '%' WORD
{ {
$$ = alloc_type(struct format); $$ = alloc_type(struct format);

View File

@ -45,7 +45,7 @@ struct format {
const struct param_ops *ops; const struct param_ops *ops;
union { union {
const struct names *set; const struct names *set;
const char *abs; /* unit name */ const char *abs; /* unit name; NULL if dimensionless */
const struct format *rel; const struct format *rel;
} u; } u;
}; };

View File

@ -41,7 +41,7 @@ EOF
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
tst "hierarchy: absolute value field" <<EOF tst "hierarchy: absolute value field (with unit)" <<EOF
{ X=#foo }; { X=#foo };
EOF EOF
@ -51,7 +51,17 @@ EOF
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
tst_fail "hierarchy: absolute value field (omit type name)" <<EOF tst "hierarchy: absolute value field (without unit)" <<EOF
{ X=## };
EOF
expect <<EOF
{ X=## }
EOF
#------------------------------------------------------------------------------
tst_fail "hierarchy: absolute value field (omit unit name)" <<EOF
{ X=# }; { X=# };
EOF EOF

View File

@ -79,7 +79,7 @@ EOF
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
tst "hierarchy: absolute value rule" <<EOF tst "hierarchy: absolute value rule (with unit)" <<EOF
<predation> = mouse<cat<dog; <predation> = mouse<cat<dog;
N=#R { N=#R {
10R: { X=* }; 10R: { X=* };
@ -98,6 +98,25 @@ EOF
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
tst "hierarchy: absolute value rule (without unit)" <<EOF
<predation> = mouse<cat<dog;
N=## {
2: { X=* };
5k: { Y=* };
3.1: { Z=* };
};
EOF
expect <<EOF
N=## {
2: { X=* }
5000: { Y=* }
3.1: { Z=* }
}
EOF
#------------------------------------------------------------------------------
tst_fail "hierarchy: absolute value rule (different unit)" <<EOF tst_fail "hierarchy: absolute value rule (different unit)" <<EOF
<predation> = mouse<cat<dog; <predation> = mouse<cat<dog;
N=#R { N=#R {
@ -142,7 +161,7 @@ EOF
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
tst "hierarchy: relative value rule (absolute)" <<EOF tst "hierarchy: relative value rule (absolute, with unit)" <<EOF
{ V=#R } { V=#R }
TOL=%V { TOL=%V {
1R: { X=* }; 1R: { X=* };
@ -160,6 +179,24 @@ EOF
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
tst "hierarchy: relative value rule (absolute, without unit)" <<EOF
{ V=## }
TOL=%V {
1: { X=* };
1k: { Y=* };
};
EOF
expect <<EOF
{ V=## }
TOL=%# {
-1/+1: { X=* }
-1000/+1000: { Y=* }
}
EOF
#------------------------------------------------------------------------------
tst "hierarchy: relative value rule (plus/minus percent)" <<EOF tst "hierarchy: relative value rule (plus/minus percent)" <<EOF
{ V=#R } { V=#R }
TOL=%V { TOL=%V {