mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2024-11-26 06:54:03 +02:00
b2/: use (general) "struct param" to replace (specialized) "struct var"
This commit is contained in:
parent
061e8f5872
commit
c1b6b2a5e4
11
b2/boom.c
11
b2/boom.c
@ -20,7 +20,7 @@
|
||||
#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)
|
||||
@ -36,19 +36,20 @@ static void add_var(const char *arg)
|
||||
*eq = 0;
|
||||
*last_var = make_var(tmp, rel_eq, eq+1);
|
||||
last_var = &(*last_var)->next;
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
|
||||
static void do_substitutions(void)
|
||||
{
|
||||
struct var *out;
|
||||
const struct var *var;
|
||||
struct param *out;
|
||||
const struct param *var;
|
||||
|
||||
out = substitute(substitutions, vars);
|
||||
for (var = out; var; var = var->next) {
|
||||
printf("%s", var->name);
|
||||
printf("%s", var->u.name);
|
||||
dump_relop(stdout, var->op);
|
||||
printf("%s\n", var->value);
|
||||
printf("%s\n", var->value.u.s);
|
||||
}
|
||||
free_vars(out);
|
||||
}
|
||||
|
@ -62,12 +62,18 @@ struct value {
|
||||
} 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 {
|
||||
union {
|
||||
const char *name;
|
||||
const struct field *field;
|
||||
} u;
|
||||
enum relop op; /* always rel_eq for now */
|
||||
enum relop op;
|
||||
struct value value;
|
||||
struct param *next;
|
||||
};
|
||||
|
46
b2/subex.c
46
b2/subex.c
@ -87,7 +87,7 @@ static char *canonicalize(const char *s, char unit)
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
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->name = unique(name);
|
||||
var = alloc_type(struct param);
|
||||
var->u.name = unique(name);
|
||||
var->op = op;
|
||||
var->value = unique(val);
|
||||
var->value.u.s = unique(val);
|
||||
var->next = NULL;
|
||||
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)
|
||||
{
|
||||
while (*out) {
|
||||
if ((*out)->name == name)
|
||||
if ((*out)->u.name == name)
|
||||
break;
|
||||
out = &(*out)->next;
|
||||
}
|
||||
if (*out) {
|
||||
free((void *) (*out)->value);
|
||||
(*out)->value = val;
|
||||
} else {
|
||||
if (*out)
|
||||
(*out)->value.u.s = val;
|
||||
else
|
||||
*out = make_var(name, op, val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
*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,
|
||||
const struct var *in, const struct var *out,
|
||||
const struct param *in, const struct param *out,
|
||||
const char **val, regmatch_t *match)
|
||||
{
|
||||
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,
|
||||
const struct var *in, const char *s, const regmatch_t *match,
|
||||
const char *units, struct var **out)
|
||||
const struct param *in, const char *s, const regmatch_t *match,
|
||||
const char *units, struct param **out)
|
||||
{
|
||||
const struct subst *jump;
|
||||
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;
|
||||
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) {
|
||||
if (vars->name == name)
|
||||
return vars->value;
|
||||
if (vars->u.name == name)
|
||||
return vars->value.u.s;
|
||||
vars = vars->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void free_vars(struct var *vars)
|
||||
void free_vars(struct param *vars)
|
||||
{
|
||||
struct var *next;
|
||||
struct param *next;
|
||||
|
||||
while (vars) {
|
||||
next = vars->next;
|
||||
|
18
b2/subex.h
18
b2/subex.h
@ -13,21 +13,13 @@
|
||||
#ifndef SUBEX_H
|
||||
#define SUBEX_H
|
||||
|
||||
#include "relop.h"
|
||||
#include "param.h"
|
||||
#include "subst.h"
|
||||
|
||||
|
||||
struct var {
|
||||
const char *name;
|
||||
enum relop op;
|
||||
const char *value;
|
||||
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);
|
||||
struct param *substitute(const struct subst *sub, const struct param *in);
|
||||
struct param *make_var(const char *name, enum relop op, const char *val);
|
||||
const char *var_lookup(const struct param *vars, const char *name);
|
||||
void free_vars(struct param *vars);
|
||||
|
||||
#endif /* !SUBEX_H */
|
||||
|
Loading…
Reference in New Issue
Block a user