mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 08:08:19 +00:00
Allow local table variables to be initialized with {} list expressions.
This commit is contained in:
parent
f8ec98625d
commit
aae60a6d76
5 changed files with 50 additions and 6 deletions
19
src/Expr.cc
19
src/Expr.cc
|
@ -2435,7 +2435,7 @@ bool RefExpr::DoUnserialize(UnserialInfo* info)
|
|||
}
|
||||
|
||||
AssignExpr::AssignExpr(Expr* arg_op1, Expr* arg_op2, int arg_is_init,
|
||||
Val* arg_val)
|
||||
Val* arg_val, attr_list* arg_attrs)
|
||||
: BinaryExpr(EXPR_ASSIGN,
|
||||
arg_is_init ? arg_op1 : arg_op1->MakeLvalue(), arg_op2)
|
||||
{
|
||||
|
@ -2455,14 +2455,14 @@ AssignExpr::AssignExpr(Expr* arg_op1, Expr* arg_op2, int arg_is_init,
|
|||
|
||||
// We discard the status from TypeCheck since it has already
|
||||
// generated error messages.
|
||||
(void) TypeCheck();
|
||||
(void) TypeCheck(arg_attrs);
|
||||
|
||||
val = arg_val ? arg_val->Ref() : 0;
|
||||
|
||||
SetLocationInfo(arg_op1->GetLocationInfo(), arg_op2->GetLocationInfo());
|
||||
}
|
||||
|
||||
bool AssignExpr::TypeCheck()
|
||||
bool AssignExpr::TypeCheck(attr_list* attrs)
|
||||
{
|
||||
TypeTag bt1 = op1->Type()->Tag();
|
||||
TypeTag bt2 = op2->Type()->Tag();
|
||||
|
@ -2494,6 +2494,19 @@ bool AssignExpr::TypeCheck()
|
|||
return true;
|
||||
}
|
||||
|
||||
if ( bt1 == TYPE_TABLE && op2->Tag() == EXPR_LIST )
|
||||
{
|
||||
attr_list* attr_copy = 0;
|
||||
if ( attrs )
|
||||
{
|
||||
attr_copy = new attr_list;
|
||||
loop_over_list(*attrs, i)
|
||||
attr_copy->append((*attrs)[i]);
|
||||
}
|
||||
op2 = new TableConstructorExpr(op2->AsListExpr(), attr_copy);
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( bt1 == TYPE_VECTOR && bt2 == bt1 &&
|
||||
op2->Type()->AsVectorType()->IsUnspecifiedVector() )
|
||||
{
|
||||
|
|
|
@ -623,7 +623,7 @@ class AssignExpr : public BinaryExpr {
|
|||
public:
|
||||
// If val is given, evaluating this expression will always yield the val
|
||||
// yet still perform the assignment. Used for triggers.
|
||||
AssignExpr(Expr* op1, Expr* op2, int is_init, Val* val = 0);
|
||||
AssignExpr(Expr* op1, Expr* op2, int is_init, Val* val = 0, attr_list* attrs = 0);
|
||||
virtual ~AssignExpr() { Unref(val); }
|
||||
|
||||
Expr* Simplify(SimplifyType simp_type);
|
||||
|
@ -638,7 +638,7 @@ protected:
|
|||
friend class Expr;
|
||||
AssignExpr() { }
|
||||
|
||||
bool TypeCheck();
|
||||
bool TypeCheck(attr_list* attrs = 0);
|
||||
bool TypeCheckArithmetics(TypeTag bt1, TypeTag bt2);
|
||||
|
||||
DECLARE_SERIAL(AssignExpr);
|
||||
|
|
|
@ -202,7 +202,8 @@ Stmt* add_local(ID* id, BroType* t, init_class c, Expr* init,
|
|||
Ref(id);
|
||||
|
||||
Stmt* stmt =
|
||||
new ExprStmt(new AssignExpr(new NameExpr(id), init, 0));
|
||||
new ExprStmt(new AssignExpr(new NameExpr(id), init, 0, 0,
|
||||
id->Attrs() ? id->Attrs()->Attrs() : 0 ));
|
||||
stmt->SetLocationInfo(init->GetLocationInfo());
|
||||
|
||||
return stmt;
|
||||
|
|
10
testing/btest/Baseline/language.table-init/output
Normal file
10
testing/btest/Baseline/language.table-init/output
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
[2] = two,
|
||||
[1] = one
|
||||
}
|
||||
global table default
|
||||
{
|
||||
[4] = four,
|
||||
[3] = three
|
||||
}
|
||||
local table default
|
20
testing/btest/language/table-init.bro
Normal file
20
testing/btest/language/table-init.bro
Normal file
|
@ -0,0 +1,20 @@
|
|||
# @TEST-EXEC: bro %INPUT >output
|
||||
# @TEST-EXEC: btest-diff output
|
||||
|
||||
global global_table: table[count] of string = {
|
||||
[1] = "one",
|
||||
[2] = "two"
|
||||
} &default = "global table default";
|
||||
|
||||
event bro_init()
|
||||
{
|
||||
local local_table: table[count] of string = {
|
||||
[3] = "three",
|
||||
[4] = "four"
|
||||
} &default = "local table default";
|
||||
|
||||
print global_table;
|
||||
print global_table[0];
|
||||
print local_table;
|
||||
print local_table[0];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue