diff --git a/b2/Makefile b/b2/Makefile index e3db6b7..0eca430 100644 --- a/b2/Makefile +++ b/b2/Makefile @@ -83,4 +83,4 @@ spotless: clean # ----- Experiments ----------------------------------------------------------- try: - $(VALGRIND) ./boom HIERARCHY -c CHAR -x CURR -i INV + $(VALGRIND) ./boom HIERARCHY -c CHAR -x CURR -p PROVIDER -i INV diff --git a/b2/PROVIDER b/b2/PROVIDER new file mode 100644 index 0000000..7b5b960 --- /dev/null +++ b/b2/PROVIDER @@ -0,0 +1 @@ +FOO USD 0 0 diff --git a/b2/boom.c b/b2/boom.c index 307ca4e..86bf088 100644 --- a/b2/boom.c +++ b/b2/boom.c @@ -43,6 +43,7 @@ static void usage(const char *name) " -c characteristics\n" " -i inventory\n" " -x currency exchange\n" +" -p providers\n" , name); exit(1); } @@ -61,6 +62,8 @@ int main(int argc, char **argv) parse = parse_inventory; else if (!strcmp(argv[i], "-x")) parse = parse_currencies; + else if (!strcmp(argv[i], "-p")) + parse = parse_providers; else usage(*argv); } else { diff --git a/b2/db.c b/b2/db.c index 432554a..866a8b0 100644 --- a/b2/db.c +++ b/b2/db.c @@ -257,3 +257,42 @@ void currency_exchange(struct currency *from, const struct currency *to, (*ex)->rate = rate; (*ex)->next = NULL; } + + +/* ----- Provider database ------------------------------------------------- */ + + +static struct provider *providers = NULL; + + +struct provider *provider_lookup(const char *name) +{ + struct provider *p; + + for (p = providers; p; p = p->next) + if (p->name == name) + break; + return p; +} + + +struct provider *provider_add(const char *name) +{ + struct provider *p; + + p = provider_lookup(name); + if (p) + return p; + + p = alloc_type(struct provider); + p->name = name; + p->curr = NULL; + p->shipping = 0; + p->minimum = 0; + p->next = providers; + providers = p; + return p; +} + + + diff --git a/b2/db.h b/b2/db.h index 85fb505..caec094 100644 --- a/b2/db.h +++ b/b2/db.h @@ -54,9 +54,9 @@ struct price { struct provider { const char *name; + const struct currency *curr; double shipping; /* S&H cost */ double minimum; /* value of minimum order, before S&H */ - const struct currency *curr; struct provider *next; }; @@ -105,4 +105,7 @@ struct currency *currency_add(const char *name); void currency_exchange(struct currency *from, const struct currency *to, double rate); +struct provider *provider_lookup(const char *name); +struct provider *provider_add(const char *name); + #endif /* !DB_H */ diff --git a/b2/lang.h b/b2/lang.h index 6239d6b..fe0f263 100644 --- a/b2/lang.h +++ b/b2/lang.h @@ -17,6 +17,7 @@ void parse_hierarchy(void); void parse_characteristics(void); void parse_inventory(void); void parse_currencies(void); +void parse_providers(void); void yywarnf(const char *fmt, ...); void yyerrorf(const char *fmt, ...); diff --git a/b2/lang.l b/b2/lang.l index f74ee70..1772ea3 100644 --- a/b2/lang.l +++ b/b2/lang.l @@ -53,6 +53,7 @@ void parse_inventory(void) yyparse(); } + void parse_currencies(void) { start_token = START_EXCHANGE; @@ -61,6 +62,15 @@ void parse_currencies(void) yyparse(); } + +void parse_providers(void) +{ + start_token = START_PROVIDERS; + expose_nl = 1; + lineno = 1; + yyparse(); +} + %} diff --git a/b2/lang.y b/b2/lang.y index 708b8ff..eae69d6 100644 --- a/b2/lang.y +++ b/b2/lang.y @@ -80,11 +80,12 @@ static const struct field *top_field(void) struct param *param; struct price *price; struct stock *stock; + struct provider *prov; }; %token START_HIERARCHY START_CHAR START_INVENTORY -%token START_EXCHANGE +%token START_EXCHANGE START_PROVIDERS %token TOK_LE TOK_GE TOK_NL %token WORD @@ -102,6 +103,7 @@ static const struct field *top_field(void) %type param params %type prices price %type stock +%type providers provider %% @@ -110,6 +112,7 @@ all: | START_CHAR characteristics | START_INVENTORY inventory | START_EXCHANGE exchange + | START_PROVIDERS providers ; @@ -520,3 +523,32 @@ rate: currency_exchange(curr, to, $2); } ; + + +/* ----- Providers --------------------------------------------------------- */ + + +providers: + { + $$ = NULL; + } + | TOK_NL + { + $$ = NULL; + } + | provider providers + { + $$ = $1; + $$->next = $2; + } + ; + +provider: + WORD WORD float float TOK_NL + { + $$ = provider_add($1); + $$->curr = currency_add($2); + $$->shipping = $3; + $$->minimum = $4; + } + ;