mirror of
https://github.com/zeek/zeek.git
synced 2025-10-16 05:28:20 +00:00
Allow named vector constructors. Addresses #983.
This commit is contained in:
parent
bcf5c41786
commit
a66b7380b6
5 changed files with 61 additions and 17 deletions
51
src/Expr.cc
51
src/Expr.cc
|
@ -3717,31 +3717,50 @@ 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 ( constructor_list->Exprs().length() == 0 )
|
if ( arg_type )
|
||||||
{
|
{
|
||||||
// vector().
|
if ( arg_type->Tag() != TYPE_VECTOR )
|
||||||
SetType(new ::VectorType(base_type(TYPE_ANY)));
|
{
|
||||||
return;
|
Error("bad vector constructor type", arg_type);
|
||||||
}
|
SetError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
BroType* t = merge_type_list(constructor_list);
|
SetType(arg_type->Ref());
|
||||||
if ( t )
|
|
||||||
{
|
|
||||||
SetType(new VectorType(t->Ref()));
|
|
||||||
|
|
||||||
if ( ! check_and_promote_exprs_to_type(constructor_list, t) )
|
|
||||||
ExprError("inconsistent types in vector constructor");
|
|
||||||
|
|
||||||
Unref(t);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
SetError();
|
{
|
||||||
|
if ( constructor_list->Exprs().length() == 0 )
|
||||||
|
{
|
||||||
|
// vector().
|
||||||
|
SetType(new ::VectorType(base_type(TYPE_ANY)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BroType* t = merge_type_list(constructor_list);
|
||||||
|
|
||||||
|
if ( t )
|
||||||
|
{
|
||||||
|
SetType(new VectorType(t->Ref()));
|
||||||
|
Unref(t);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
3
testing/btest/Baseline/language.named-vector-ctors/out
Normal file
3
testing/btest/Baseline/language.named-vector-ctors/out
Normal 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]]
|
19
testing/btest/language/named-vector-ctors.bro
Normal file
19
testing/btest/language/named-vector-ctors.bro
Normal 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;
|
Loading…
Add table
Add a link
Reference in a new issue