summaryrefslogtreecommitdiffstats
path: root/src/rule.c
diff options
context:
space:
mode:
authorPatrick McHardy <kaber@trash.net>2009-03-20 08:12:18 +0100
committerPatrick McHardy <kaber@trash.net>2009-03-20 08:12:18 +0100
commit5c40780440c0e8661fc7cfcec72dab48b934631d (patch)
treec6f334da0a6a2bc2fca4e48497408f477fb80fa9 /src/rule.c
parent2ae0ab5fb3eabc149bad1250c681a7a5f2aea694 (diff)
Add support for scoping and symbol binding
As a first step towards stand-alone sets, add support for scoping and binding symbols. This will be used for user-defined constants, as well as declarations of modifiable (stand-alone) sets once the kernel side is ready. Scopes are currently limited to three nesting levels: the global scope, table block scopes and chain block scopes. Signed-off-by: Patrick McHardy <kaber@trash.net>
Diffstat (limited to 'src/rule.c')
-rw-r--r--src/rule.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/rule.c b/src/rule.c
index e86c78aa..8efbd887 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -68,6 +68,38 @@ void rule_print(const struct rule *rule)
printf("\n");
}
+struct scope *scope_init(struct scope *scope, const struct scope *parent)
+{
+ scope->parent = parent;
+ init_list_head(&scope->symbols);
+ return scope;
+}
+
+void symbol_bind(struct scope *scope, const char *identifier, struct expr *expr)
+{
+ struct symbol *sym;
+
+ sym = xzalloc(sizeof(*sym));
+ sym->identifier = xstrdup(identifier);
+ sym->expr = expr;
+
+ list_add_tail(&sym->list, &scope->symbols);
+}
+
+struct symbol *symbol_lookup(const struct scope *scope, const char *identifier)
+{
+ struct symbol *sym;
+
+ while (scope != NULL) {
+ list_for_each_entry(sym, &scope->symbols, list) {
+ if (!strcmp(sym->identifier, identifier))
+ return sym;
+ }
+ scope = scope->parent;
+ }
+ return NULL;
+}
+
struct chain *chain_alloc(const char *name)
{
struct chain *chain;