1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-07-03 00:13:15 +03:00
eda-tools/b2/chr.c

203 lines
4.2 KiB
C
Raw Normal View History

2012-03-18 18:24:12 +02:00
/*
* chr.h - Part characteristics
*
* Copyright 2012 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <assert.h>
2012-03-18 18:24:12 +02:00
#include "util.h"
#include "param.h"
#include "chr.h"
const struct format *field_find(const char *name, const struct field *tail)
2012-03-18 18:24:12 +02:00
{
while (tail) {
if (tail->name == name)
return tail->fmt;
tail = tail->prev;
}
2012-03-18 18:24:12 +02:00
return NULL;
}
/* ----- Construction of the field hierarchy ------------------------------- */
struct field *field_new(const char *name, const struct format *fmt,
const struct field *prev)
2012-03-18 18:24:12 +02:00
{
struct field *field;
if (field_find(name, prev))
2012-03-18 18:24:12 +02:00
yyerrorf("duplicate field \"%s\"", name);
field = alloc_type(struct field);
field->name = name;
field->fmt = fmt;
field->sel = NULL;
field->any.fields = NULL;
field->any.rules = NULL;
field->next = NULL;
field->prev = NULL;
return field;
2012-03-18 18:24:12 +02:00
}
struct selector *field_selector(void)
2012-03-18 18:24:12 +02:00
{
struct selector *sel;
sel = alloc_type(struct selector);
sel->cond = NULL;
sel->act.fields = NULL;
sel->act.rules = NULL;
sel->next = NULL;
return sel;
2012-03-18 18:24:12 +02:00
}
struct condition *field_condition(enum relop relop, const char *word)
2012-03-18 18:24:12 +02:00
{
struct condition *cond;
cond = alloc_type(struct condition);
cond->value.u.name = word;
cond->relop = relop;
cond->next = NULL;
return cond;
2012-03-18 18:24:12 +02:00
}
void field_finalize(struct field *field)
2012-03-18 18:24:12 +02:00
{
const struct selector *sel;
struct condition *cond;
for (sel = field->sel; sel; sel = sel->next)
for (cond = sel->cond; cond; cond = cond->next)
if (!evaluate(field->fmt, cond->value.u.name,
&cond->value))
yyerrorf("invalid value in selection");
/* @@@ indicate exact location */
2012-03-18 18:24:12 +02:00
}
/* ----- Dumping ----------------------------------------------------------- */
#define INDENT 8
static void dump_fields(FILE *file, const struct field *field, int level);
static void dump_set_decl(FILE *file, const struct names *first_name)
2012-03-18 18:24:12 +02:00
{
const char *name;
name = nameset_rev_lookup(first_name);
assert(name);
fprintf(file, "<%s>", name);
2012-03-18 18:24:12 +02:00
}
static void dump_field_decl(FILE *file, const struct field *field)
2012-03-18 18:24:12 +02:00
{
if (field->fmt->ops == &param_ops_name)
fprintf(file, "*");
else if (field->fmt->ops == &param_ops_set)
dump_set_decl(file, field->fmt->u.set);
else if (field->fmt->ops == &param_ops_abs)
fprintf(file, "#%s", field->fmt->u.abs);
else if (field->fmt->ops == &param_ops_rel)
fprintf(file, "%%%s", field->fmt->u.rel->u.abs);
else
abort();
2012-03-18 18:24:12 +02:00
}
static void dump_action(FILE *file, const struct action *act, int level)
{
const struct field *field;
if (act->fields) {
fprintf(file, " {");
for (field = act->fields; field; field = field->next) {
fprintf(file, " %s:", field->name);
dump_field_decl(file, field);
}
fprintf(file, " }\n");
} else {
fprintf(file, "\n");
}
dump_fields(file, act->rules, level+1);
}
static void dump_one_field(FILE *file, const struct field *field, int level)
{
const struct selector *sel;
const struct condition *cond;
fprintf(file, "%*s%s:", level*INDENT, "", field->name);
dump_field_decl(file, field);
fprintf(file, " {\n");
for (sel = field->sel; sel; sel = sel->next) {
fprintf(file, "%*s", level*INDENT+INDENT/2, "");
for (cond = sel->cond; cond; cond = cond->next) {
if (cond != sel->cond)
fprintf(file, "|");
switch (cond->relop) {
case rel_lt:
fprintf(file, "<");
break;
case rel_le:
fprintf(file, "<=");
break;
case rel_eq:
break;
case rel_ge:
fprintf(file, ">");
break;
case rel_gt:
fprintf(file, ">=");
break;
default:
fprintf(file, "?");
}
dump(file, field->fmt, &cond->value);
}
fprintf(file, ": ");
dump_action(file, &sel->act, level);
}
if (field->any.fields || field->any.rules) {
fprintf(file, "%*s*:", level*INDENT+INDENT/2, "");
dump_action(file, &field->any, level);
}
fprintf(file, "%*s}\n", level*INDENT, "");
}
static void dump_fields(FILE *file, const struct field *field, int level)
{
while (field) {
dump_one_field(file, field, level);
field = field->next;
}
}
void field_dump(FILE *file, const struct field *field)
{
dump_fields(file, field, 0);
}