From 6fd5313917db95d97c0e2ea3119acff90e0cbb95 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 20 May 2012 23:16:51 -0300 Subject: [PATCH] b2/: assigments to can express inequalities --- b2/SUBST | 4 ++-- b2/boom.c | 10 +++++++--- b2/lang.l | 14 +++++++++----- b2/lang.y | 6 ++++-- b2/subex.c | 12 ++++++++---- b2/subex.h | 4 +++- b2/subst.c | 7 +++++-- b2/subst.h | 5 ++++- 8 files changed, 42 insertions(+), 20 deletions(-) diff --git a/b2/SUBST b/b2/SUBST index c3811c2..039ada9 100644 --- a/b2/SUBST +++ b/b2/SUBST @@ -1,8 +1,8 @@ REF=R[0-9]* { T=R VAL=(#R) { R=$1 } - TOL = 5% - FN=*% { TOL=$$ } + TOL <= 5% + FN=*% { TOL<=$$ } break REF // end break again } diff --git a/b2/boom.c b/b2/boom.c index 3242169..3e806ab 100644 --- a/b2/boom.c +++ b/b2/boom.c @@ -16,6 +16,7 @@ #include "util.h" #include "lang.h" +#include "param.h" #include "subex.h" @@ -33,7 +34,7 @@ static void add_var(const char *arg) exit(1); } *eq = 0; - *last_var = make_var(tmp, eq+1); + *last_var = make_var(tmp, rel_eq, eq+1); last_var = &(*last_var)->next; } @@ -44,8 +45,11 @@ static void do_substitutions(void) const struct var *var; out = substitute(substitutions, vars); - for (var = out; var; var = var->next) - printf("%s=%s\n", var->name, var->value); + for (var = out; var; var = var->next) { + printf("%s", var->name); + dump_relop(stdout, var->op); + printf("%s\n", var->value); + } free_vars(out); } diff --git a/b2/lang.l b/b2/lang.l index bfd9cee..3751e8e 100644 --- a/b2/lang.l +++ b/b2/lang.l @@ -119,19 +119,23 @@ PAT "\\"[^\t\n]|[^ \t\n\\] } %} -{ID}({ID}|"%")* { yylval.s = unique(yytext); +{ID}({ID}|"%")* { yylval.s = unique(yytext); return WORD; } {PAT}* { BEGIN(ID); yylval.s = stralloc(yytext); return PATTERN; } -"=" { if (pattern) +[<>=] { if (pattern) BEGIN(PAT); - return '='; } + return *yytext; } -"<=" return TOK_LE; -">=" return TOK_GE; +"<=" { if (pattern) + BEGIN(PAT); + return TOK_LE; } +">=" { if (pattern) + BEGIN(PAT); + return TOK_GE; } "//"[^\n]* ; "/*"([^*]|("*"+([^*/])))*"*"+"/" { const char *s = yytext; diff --git a/b2/lang.y b/b2/lang.y index 15743f3..c21a9d4 100644 --- a/b2/lang.y +++ b/b2/lang.y @@ -587,13 +587,15 @@ block: { $$ = NULL; } - | WORD '=' PATTERN opt_block block + | WORD relop PATTERN opt_block block { if ($4) { + if ($2 != rel_eq) + yyerror("only = allow for matching"); $$ = subst_match($1, $3); $$->u.match.block = $4; } else { - $$ = subst_assign($1, $3); + $$ = subst_assign($1, $2, $3); } free((void *) $3); $$->next = $5; diff --git a/b2/subex.c b/b2/subex.c index 5b2b9e2..c608674 100644 --- a/b2/subex.c +++ b/b2/subex.c @@ -21,6 +21,7 @@ #include "util.h" #include "vstring.h" #include "lang.h" +#include "relop.h" #include "subst.h" #include "subex.h" @@ -138,19 +139,21 @@ static char *compose(const struct chunk *c, } -struct var *make_var(const char *name, const char *val) +struct var *make_var(const char *name, enum relop op, const char *val) { struct var *var; var = alloc_type(struct var); var->name = unique(name); + var->op = op; var->value = unique(val); var->next = NULL; return var; } -static void do_assign(const char *name, struct var **out, const char *val) +static void do_assign(const char *name, struct var **out, enum relop op, + const char *val) { while (*out) { if ((*out)->name == name) @@ -161,7 +164,7 @@ static void do_assign(const char *name, struct var **out, const char *val) free((void *) (*out)->value); (*out)->value = val; } else { - *out = make_var(name, val); + *out = make_var(name, op, val); } } @@ -217,7 +220,8 @@ static const struct subst *recurse_sub(const struct subst *sub, case st_assign: tmp = compose(sub->u.assign.pat, in, *out, s, match, units); - do_assign(sub->u.assign.dst, out, tmp); + do_assign(sub->u.assign.dst, out, sub->u.assign.op, + tmp); break; case st_end: return &jump_end; diff --git a/b2/subex.h b/b2/subex.h index 8a84289..8d210af 100644 --- a/b2/subex.h +++ b/b2/subex.h @@ -13,18 +13,20 @@ #ifndef SUBEX_H #define SUBEX_H +#include "relop.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, const char *val); +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); diff --git a/b2/subst.c b/b2/subst.c index 2c85e83..7415419 100644 --- a/b2/subst.c +++ b/b2/subst.c @@ -21,6 +21,7 @@ #include "util.h" #include "vstring.h" #include "lang.h" +#include "relop.h" #include "subst.h" @@ -226,12 +227,13 @@ static struct chunk *parse_pattern(const char *s) } -struct subst *subst_assign(const char *dst, const char *pat) +struct subst *subst_assign(const char *dst, enum relop op, const char *pat) { struct subst *sub; sub = alloc_subst(st_assign); sub->u.assign.dst = dst; + sub->u.assign.op = op; sub->u.assign.pat = parse_pattern(pat); return sub; } @@ -358,7 +360,8 @@ static void recurse_dump(FILE *file, const struct subst *sub, int level) fprintf(file, "%*s}\n", INDENT*level, ""); break; case st_assign: - fprintf(file, "%s=", sub->u.assign.dst); + fprintf(file, "%s", sub->u.assign.dst); + dump_relop(file, sub->u.assign.op); dump_chunks(file, sub->u.assign.pat); fprintf(file, "\n"); break; diff --git a/b2/subst.h b/b2/subst.h index 27f1b8a..f2ca3ad 100644 --- a/b2/subst.h +++ b/b2/subst.h @@ -17,6 +17,8 @@ #include #include +#include "relop.h" + enum chunk_type { ct_string, @@ -53,6 +55,7 @@ struct subst { } match; struct { const char *dst; + enum relop op; struct chunk *pat; } assign; const struct subst *jump; @@ -66,7 +69,7 @@ struct subst { struct subst *subst_match(const char *src, const char *re); -struct subst *subst_assign(const char *dst, const char *pat); +struct subst *subst_assign(const char *dst, enum relop op, const char *pat); struct subst *subst_end(void); struct subst *subst_break(const char *block); struct subst *subst_again(const char *block);