Allow named vector constructors. Addresses #983.

This commit is contained in:
Jon Siwek 2013-05-30 10:57:28 -05:00
parent bcf5c41786
commit a66b7380b6
5 changed files with 61 additions and 17 deletions

View file

@ -3717,12 +3717,26 @@ bool SetConstructorExpr::DoUnserialize(UnserialInfo* info)
return true; return true;
} }
VectorConstructorExpr::VectorConstructorExpr(ListExpr* constructor_list) VectorConstructorExpr::VectorConstructorExpr(ListExpr* constructor_list,
BroType* arg_type)
: UnaryExpr(EXPR_VECTOR_CONSTRUCTOR, constructor_list) : UnaryExpr(EXPR_VECTOR_CONSTRUCTOR, constructor_list)
{ {
if ( IsError() ) if ( IsError() )
return; return;
if ( arg_type )
{
if ( arg_type->Tag() != TYPE_VECTOR )
{
Error("bad vector constructor type", arg_type);
SetError();
return;
}
SetType(arg_type->Ref());
}
else
{
if ( constructor_list->Exprs().length() == 0 ) if ( constructor_list->Exprs().length() == 0 )
{ {
// vector(). // vector().
@ -3731,17 +3745,22 @@ VectorConstructorExpr::VectorConstructorExpr(ListExpr* constructor_list)
} }
BroType* t = merge_type_list(constructor_list); BroType* t = merge_type_list(constructor_list);
if ( t ) if ( t )
{ {
SetType(new VectorType(t->Ref())); SetType(new VectorType(t->Ref()));
if ( ! check_and_promote_exprs_to_type(constructor_list, t) )
ExprError("inconsistent types in vector constructor");
Unref(t); Unref(t);
} }
else else
{
SetError(); SetError();
return;
}
}
if ( ! check_and_promote_exprs_to_type(constructor_list,
type->AsVectorType()->YieldType()) )
ExprError("inconsistent types in vector constructor");
} }
Val* VectorConstructorExpr::Eval(Frame* f) const Val* VectorConstructorExpr::Eval(Frame* f) const

View file

@ -818,7 +818,7 @@ protected:
class VectorConstructorExpr : public UnaryExpr { class VectorConstructorExpr : public UnaryExpr {
public: public:
VectorConstructorExpr(ListExpr* constructor_list); VectorConstructorExpr(ListExpr* constructor_list, BroType* arg_type = 0);
Val* Eval(Frame* f) const; Val* Eval(Frame* f) const;

View file

@ -555,6 +555,9 @@ expr:
break; break;
case TYPE_VECTOR: case TYPE_VECTOR:
$$ = new VectorConstructorExpr($4, ctor_type);
break;
default: default:
$1->Error("constructor type not implemented"); $1->Error("constructor type not implemented");
YYERROR; YYERROR;

View file

@ -0,0 +1,3 @@
[one, two, three]
[1.0, 2.0, 3.0]
[[min=<uninitialized>, max=1], [min=<uninitialized>, max=2], [min=<uninitialized>, max=3]]

View file

@ -0,0 +1,19 @@
# @TEST-EXEC: bro -b %INPUT >out
# @TEST-EXEC: btest-diff out
type MyRec: record {
min: count &optional;
max: count;
};
type FooVector: vector of string;
type FooVectorD: vector of double;
type FooVectorRec: vector of MyRec;
global myvec: FooVector = FooVector("one", "two", "three");
global myvecd: FooVectorD = FooVectorD(1, 2, 3);
global myvecrec: FooVectorRec = FooVectorRec([$max=1], [$max=2], [$max=3]);
print myvec;
print myvecd;
print myvecrec;