1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-22 20:14:04 +02:00

b2/: assigments to can express inequalities

This commit is contained in:
Werner Almesberger 2012-05-20 23:16:51 -03:00
parent a34702cd8d
commit 6fd5313917
8 changed files with 42 additions and 20 deletions

View File

@ -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
}

View File

@ -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);
}

View File

@ -119,19 +119,23 @@ PAT "\\"[^\t\n]|[^ \t\n\\]
}
%}
<INITIAL,ID>{ID}({ID}|"%")* { yylval.s = unique(yytext);
<INITIAL,ID>{ID}({ID}|"%")* { yylval.s = unique(yytext);
return WORD; }
<PAT>{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;

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -17,6 +17,8 @@
#include <sys/types.h>
#include <regex.h>
#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);