1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-26 11:31:54 +02:00

b2/: use (general) "struct param" to replace (specialized) "struct var"

This commit is contained in:
Werner Almesberger 2012-05-21 19:19:04 -03:00
parent 061e8f5872
commit c1b6b2a5e4
4 changed files with 40 additions and 43 deletions

View File

@ -20,7 +20,7 @@
#include "subex.h" #include "subex.h"
static struct var *vars = NULL, **last_var = &vars; static struct param *vars = NULL, **last_var = &vars;
static void add_var(const char *arg) static void add_var(const char *arg)
@ -36,19 +36,20 @@ static void add_var(const char *arg)
*eq = 0; *eq = 0;
*last_var = make_var(tmp, rel_eq, eq+1); *last_var = make_var(tmp, rel_eq, eq+1);
last_var = &(*last_var)->next; last_var = &(*last_var)->next;
free(tmp);
} }
static void do_substitutions(void) static void do_substitutions(void)
{ {
struct var *out; struct param *out;
const struct var *var; const struct param *var;
out = substitute(substitutions, vars); out = substitute(substitutions, vars);
for (var = out; var; var = var->next) { for (var = out; var; var = var->next) {
printf("%s", var->name); printf("%s", var->u.name);
dump_relop(stdout, var->op); dump_relop(stdout, var->op);
printf("%s\n", var->value); printf("%s\n", var->value.u.s);
} }
free_vars(out); free_vars(out);
} }

View File

@ -62,12 +62,18 @@ struct value {
} u; } u;
}; };
/*
* When using "struct param" to store (string) variables not associated to a
* field, then u.name contains the variable name, "op" contains the relational
* operator (default rel_eq), and value.u.s contains the variable's value.
*/
struct param { struct param {
union { union {
const char *name; const char *name;
const struct field *field; const struct field *field;
} u; } u;
enum relop op; /* always rel_eq for now */ enum relop op;
struct value value; struct value value;
struct param *next; struct param *next;
}; };

View File

@ -87,7 +87,7 @@ static char *canonicalize(const char *s, char unit)
static char *compose(const struct chunk *c, static char *compose(const struct chunk *c,
const struct var *in, const struct var *out, const struct param *in, const struct param *out,
const char *s, const regmatch_t *match, const char *units) const char *s, const regmatch_t *match, const char *units)
{ {
char *res = stralloc(""); char *res = stralloc("");
@ -135,38 +135,36 @@ static char *compose(const struct chunk *c,
} }
struct var *make_var(const char *name, enum relop op, const char *val) struct param *make_var(const char *name, enum relop op, const char *val)
{ {
struct var *var; struct param *var;
var = alloc_type(struct var); var = alloc_type(struct param);
var->name = unique(name); var->u.name = unique(name);
var->op = op; var->op = op;
var->value = unique(val); var->value.u.s = unique(val);
var->next = NULL; var->next = NULL;
return var; return var;
} }
static void do_assign(const char *name, struct var **out, enum relop op, static void do_assign(const char *name, struct param **out, enum relop op,
const char *val) const char *val)
{ {
while (*out) { while (*out) {
if ((*out)->name == name) if ((*out)->u.name == name)
break; break;
out = &(*out)->next; out = &(*out)->next;
} }
if (*out) { if (*out)
free((void *) (*out)->value); (*out)->value.u.s = val;
(*out)->value = val; else
} else {
*out = make_var(name, op, val); *out = make_var(name, op, val);
}
} }
static int do_match_1(const char *var, const regex_t *re, static int do_match_1(const char *var, const regex_t *re,
const struct var *in, const struct var *out, const struct param *in, const struct param *out,
const char **val, regmatch_t *match) const char **val, regmatch_t *match)
{ {
*val = var_lookup(out, var); *val = var_lookup(out, var);
@ -179,7 +177,7 @@ static int do_match_1(const char *var, const regex_t *re,
static int do_match(const char *var, const regex_t *re, static int do_match(const char *var, const regex_t *re,
const struct var *in, const struct var *out, const struct param *in, const struct param *out,
const char **val, regmatch_t *match) const char **val, regmatch_t *match)
{ {
int i; int i;
@ -194,8 +192,8 @@ static int do_match(const char *var, const regex_t *re,
static const struct subst *recurse_sub(const struct subst *sub, static const struct subst *recurse_sub(const struct subst *sub,
const struct var *in, const char *s, const regmatch_t *match, const struct param *in, const char *s, const regmatch_t *match,
const char *units, struct var **out) const char *units, struct param **out)
{ {
const struct subst *jump; const struct subst *jump;
regmatch_t m_tmp[10]; regmatch_t m_tmp[10];
@ -236,9 +234,9 @@ static const struct subst *recurse_sub(const struct subst *sub,
} }
struct var *substitute(const struct subst *sub, const struct var *in) struct param *substitute(const struct subst *sub, const struct param *in)
{ {
struct var *out = NULL; struct param *out = NULL;
int i; int i;
char tmp[4]; char tmp[4];
@ -254,20 +252,20 @@ struct var *substitute(const struct subst *sub, const struct var *in)
} }
const char *var_lookup(const struct var *vars, const char *name) const char *var_lookup(const struct param *vars, const char *name)
{ {
while (vars) { while (vars) {
if (vars->name == name) if (vars->u.name == name)
return vars->value; return vars->value.u.s;
vars = vars->next; vars = vars->next;
} }
return NULL; return NULL;
} }
void free_vars(struct var *vars) void free_vars(struct param *vars)
{ {
struct var *next; struct param *next;
while (vars) { while (vars) {
next = vars->next; next = vars->next;

View File

@ -13,21 +13,13 @@
#ifndef SUBEX_H #ifndef SUBEX_H
#define SUBEX_H #define SUBEX_H
#include "relop.h" #include "param.h"
#include "subst.h" #include "subst.h"
struct var { struct param *substitute(const struct subst *sub, const struct param *in);
const char *name; struct param *make_var(const char *name, enum relop op, const char *val);
enum relop op; const char *var_lookup(const struct param *vars, const char *name);
const char *value; void free_vars(struct param *vars);
struct var *next;
};
struct var *substitute(const struct subst *sub, const struct var *in);
struct var *make_var(const char *name, enum relop op, const char *val);
const char *var_lookup(const struct var *vars, const char *name);
void free_vars(struct var *vars);
#endif /* !SUBEX_H */ #endif /* !SUBEX_H */