Root/b2/param.h

1/*
2 * param.h - Parameters and values
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12
13#ifndef PARAM_H
14#define PARAM_H
15
16#include <stdio.h>
17
18#include "bitset.h"
19#include "relop.h"
20
21
22/*
23 * Parameter types:
24 *
25 * "name" arbitrary name
26 * "set" names from an ordered list
27 * "abs" absolute numeric value
28 * "rel" numeric value relative to another parameter
29 */
30
31
32struct equiv {
33    const char *name;
34    struct equiv *next;
35};
36
37struct names {
38    struct equiv *equiv; /* equivalent value(s) */
39    struct names *next; /* next/greater value */
40};
41
42struct param_ops;
43
44struct format {
45    const struct param_ops *ops;
46    union {
47        const struct names *set;
48        const char *abs; /* unit name; NULL if dimensionless */
49        const struct format *rel;
50    } u;
51};
52
53struct value {
54    union {
55        const char *s;
56        struct bitset set;
57        double abs;
58        struct rel_value {
59            double plus, minus; /* rel: 100% = 1 */
60            int fract; /* 0: abs. offset; 1: fract. offs. */
61        } rel;
62    } u;
63};
64
65/*
66 * When using "struct param" to store (string) variables not associated to a
67 * field, then u.name contains the variable name, "op" contains the relational
68 * operator (default rel_eq), and value.u.s contains the variable's value.
69 *
70 * If the variable is "$", then u.name is set to NULL.
71 */
72
73struct param {
74    union {
75        const char *name;
76        const struct field *field;
77    } u;
78    enum relop op;
79    struct value value;
80    struct param *next;
81};
82
83struct param_ops {
84    int (*eval)(const struct format *fmt, const char *s,
85        struct value *res);
86    int (*comp)(const struct value *a, enum relop relop,
87        const struct value *b);
88    void (*dump)(FILE *file, const struct format *fmt,
89        const struct value *v);
90};
91
92
93void register_nameset(const char *name, const struct names *set);
94const struct names *find_nameset(const char *name);
95const char *nameset_rev_lookup(const struct names *first_name);
96
97int eval_name(const struct format *fmt, const char *s, struct value *res);
98int eval_set(const struct format *fmt, const char *s, struct value *res);
99int eval_abs(const struct format *fmt, const char *s, struct value *res);
100int eval_rel(const struct format *fmt, const char *s, struct value *res);
101int evaluate(const struct format *fmt, const char *s, struct value *res);
102
103int comp_name(const struct value *a, enum relop relop, const struct value *b);
104int comp_set(const struct value *a, enum relop relop, const struct value *b);
105int comp_abs(const struct value *a, enum relop relop, const struct value *b);
106int comp_rel(const struct value *a, enum relop relop, const struct value *b);
107int compare(const struct format *fmt,
108    const struct value *a, enum relop relop, const struct value *b);
109
110void dump_name(FILE *file, const struct format *fmt, const struct value *v);
111void dump_set(FILE *file, const struct format *fmt, const struct value *v);
112void dump_abs(FILE *file, const struct format *fmt, const struct value *v);
113void dump_rel(FILE *file, const struct format *fmt, const struct value *v);
114void dump_param(FILE *file, const struct format *fmt, const struct value *v);
115
116extern const struct param_ops param_ops_name;
117extern const struct param_ops param_ops_set;
118extern const struct param_ops param_ops_abs;
119extern const struct param_ops param_ops_rel;
120
121struct param *make_var(const char *name, enum relop op, const char *val);
122const char *var_lookup(const struct param *vars, const char *name);
123struct param *merge_vars(const struct param *a, const struct param *b);
124void free_vars(struct param *vars);
125
126#endif /* !PARAM_H */
127

Archive Download this file

Branches:
master



interactive