basic code implemented, but there's a memory problem somehwere :-(

This commit is contained in:
Vern Paxson 2018-06-20 09:49:29 -07:00
parent 344382ee7b
commit 21614cd30d
3 changed files with 41 additions and 45 deletions

View file

@ -665,6 +665,9 @@ Val* BinaryExpr::Fold(Val* v1, Val* v2) const
if ( it == TYPE_INTERNAL_STRING )
return StringFold(v1, v2);
if ( v1->Type()->Tag() == TYPE_PATTERN )
return PatternFold(v1, v2);
if ( it == TYPE_INTERNAL_ADDR )
return AddrFold(v1, v2);
@ -851,6 +854,21 @@ Val* BinaryExpr::StringFold(Val* v1, Val* v2) const
return new Val(result, TYPE_BOOL);
}
Val* BinaryExpr::PatternFold(Val* v1, Val* v2) const
{
const RE_Matcher* re1 = v1->AsPattern();
const RE_Matcher* re2 = v2->AsPattern();
if ( tag != EXPR_AND && tag != EXPR_OR )
BadTag("BinaryExpr::PatternFold");
RE_Matcher* res = tag == EXPR_AND ?
RE_Matcher_conjunction(re1, re2) :
RE_Matcher_disjunction(re1, re2);
return new PatternVal(res);
}
Val* BinaryExpr::AddrFold(Val* v1, Val* v2) const
{
IPAddr a1 = v1->AsAddr();
@ -1696,9 +1714,6 @@ BoolExpr::BoolExpr(BroExprTag arg_tag, Expr* arg_op1, Expr* arg_op2)
SetType(base_type(TYPE_BOOL));
}
else if ( bt1 == TYPE_PATTERN && bt2 == bt1 )
SetType(base_type(TYPE_PATTERN));
else
ExprError("requires boolean operands");
}
@ -1708,22 +1723,6 @@ Val* BoolExpr::DoSingleEval(Frame* f, Val* v1, Expr* op2) const
if ( ! v1 )
return 0;
if ( Type()->Tag() == TYPE_PATTERN )
{
Val* v2 = op2->Eval(f);
if ( ! v2 )
return 0;
RE_Matcher* re1 = v1->AsPattern();
RE_Matcher* re2 = v2->AsPattern();
RE_Matcher* res = tag == EXPR_AND_AND ?
RE_Matcher_conjunction(re1, re2) :
RE_Matcher_disjunction(re1, re2);
return new PatternVal(res);
}
if ( tag == EXPR_AND_AND )
{
if ( v1->IsZero() )
@ -1885,6 +1884,16 @@ BitExpr::BitExpr(BroExprTag arg_tag, Expr* arg_op1, Expr* arg_op2)
SetType(base_type(TYPE_COUNT));
}
if ( bt1 == TYPE_PATTERN )
{
if ( bt2 != TYPE_PATTERN )
ExprError("cannot mix pattern and non-pattern operands");
else if ( tag == EXPR_XOR )
ExprError("'^' operator does not apply to patterns");
else
SetType(base_type(TYPE_PATTERN));
}
else
ExprError("requires \"count\" operands");
}

View file

@ -327,6 +327,9 @@ protected:
// Same for when the constants are strings.
virtual Val* StringFold(Val* v1, Val* v2) const;
// Same for when the constants are patterns.
virtual Val* PatternFold(Val* v1, Val* v2) const;
// Same for when the constants are addresses or subnets.
virtual Val* AddrFold(Val* v1, Val* v2) const;
virtual Val* SubNetFold(Val* v1, Val* v2) const;

View file

@ -2,7 +2,7 @@
// See the file "COPYING" in the main distribution directory for copyright.
%}
%expect 78
%expect 77
%token TOK_ADD TOK_ADD_TO TOK_ADDR TOK_ANY
%token TOK_ATENDIF TOK_ATELSE TOK_ATIF TOK_ATIFDEF TOK_ATIFNDEF
@ -49,13 +49,12 @@
%left '$' '[' ']' '(' ')' TOK_HAS_FIELD TOK_HAS_ATTR
%type <b> opt_no_test opt_no_test_block opt_deprecated
%type <str> TOK_ID TOK_PATTERN_TEXT single_pattern
%type <str> TOK_ID TOK_PATTERN_TEXT
%type <id> local_id global_id def_global_id event_id global_or_event_id resolve_id begin_func
%type <id_l> local_id_list
%type <ic> init_class
%type <expr> opt_init
%type <val> TOK_CONSTANT
%type <re> pattern
%type <expr> expr opt_expr init anonymous_function
%type <event_expr> event
%type <stmt> stmt stmt_list func_body for_head
@ -720,11 +719,15 @@ expr:
$$ = new ConstExpr($1);
}
| pattern
| '/' { begin_RE(); } TOK_PATTERN_TEXT { end_RE(); } '/'
{
set_location(@1);
$1->Compile();
$$ = new ConstExpr(new PatternVal($1));
set_location(@3);
RE_Matcher* re = new RE_Matcher($3);
delete [] $3;
re->Compile();
$$ = new ConstExpr(new PatternVal(re));
}
| '|' expr '|' %prec '('
@ -754,25 +757,6 @@ opt_expr_list:
{ $$ = new ListExpr(); }
;
pattern:
pattern '|' single_pattern
{
$1->AddPat($3);
delete [] $3;
}
| single_pattern
{
$$ = new RE_Matcher($1);
delete [] $1;
}
;
single_pattern:
'/' { begin_RE(); } TOK_PATTERN_TEXT { end_RE(); } '/'
{ $$ = $3; }
;
enum_body:
enum_body_list
{