Skip to main content
Version: 3.7

ForEachStatement ForEach 循环分支

1. 结构声明

class ForEachStatement extends LogicItem {
each: LogicItem;
item: Param;
index: Param;
start: LogicItem;
end?: LogicItem;
body: Array<LogicItem>;
}

2. 节点示例

(1) 示例

AST 如下:

{
"concept": "ForEachStatement",
"each": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"item": {
"concept": "Param",
"name": "item",
"description": ""
},
"index": {
"concept": "Param",
"name": "index",
"description": ""
},
"start": {
"concept": "NumericLiteral",
"value": "0",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
},
"body": [
{
"concept": "IfStatement",
"test": {
"concept": "BinaryExpression",
"left": {
"concept": "MemberExpression",
"object": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "item"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "productReport"
}
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "accepted"
}
},
"right": {
"concept": "BooleanLiteral",
"value": "true"
},
"operator": "=="
},
"consequent": [
{
"concept": "CallFunction",
"label": "内置函数",
"calleeNamespace": "nasl.util",
"calleeName": "Add",
"typeArguments": [],
"arguments": [
{
"concept": "Argument",
"keyword": "list",
"expression": {
"concept": "Identifier",
"namespace": "",
"name": "fullAcceptedList"
}
},
{
"concept": "Argument",
"keyword": "item",
"expression": {
"concept": "NumericLiteral",
"value": "1",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
}
}
]
}
],
"alternate": [
{
"concept": "CallFunction",
"label": "内置函数",
"calleeNamespace": "nasl.util",
"calleeName": "Add",
"typeArguments": [],
"arguments": [
{
"concept": "Argument",
"keyword": "list",
"expression": {
"concept": "Identifier",
"namespace": "",
"name": "fullAcceptedList"
}
},
{
"concept": "Argument",
"keyword": "item",
"expression": {
"concept": "NumericLiteral",
"value": "0",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
}
}
]
}
]
}
]
}

对应的代码如下:

for (item in list, index) {
if (item.productReport.accepted == true) {
nasl::util::Add(fullAcceptedList, 1)
} else {
nasl::util::Add(fullAcceptedList, 0)
}
}

(2) 示例

AST 如下:

{
"concept": "ForEachStatement",
"each": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"item": {
"concept": "Param",
"name": "item",
"description": ""
},
"index": {
"concept": "Param",
"name": "index",
"description": ""
},
"start": {
"concept": "NumericLiteral",
"value": "3",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
},
"end": {
"concept": "BinaryExpression",
"left": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "length"
}
},
"operator": "-",
"right": {
"concept": "NumericLiteral",
"value": "3",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
}
},
"body": [
{
"concept": "IfStatement",
"test": {
"concept": "BinaryExpression",
"left": {
"concept": "MemberExpression",
"object": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "item"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "productReport"
}
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "accepted"
}
},
"right": {
"concept": "BooleanLiteral",
"value": "true"
},
"operator": "=="
},
"consequent": [
{
"concept": "CallFunction",
"label": "内置函数",
"calleeNamespace": "nasl.util",
"calleeName": "Add",
"typeArguments": [],
"arguments": [
{
"concept": "Argument",
"keyword": "list",
"expression": {
"concept": "Identifier",
"namespace": "",
"name": "fullAcceptedList"
}
},
{
"concept": "Argument",
"keyword": "item",
"expression": {
"concept": "NumericLiteral",
"value": "1",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
}
}
]
}
],
"alternate": [
{
"concept": "CallFunction",
"label": "内置函数",
"calleeNamespace": "nasl.util",
"calleeName": "Add",
"typeArguments": [],
"arguments": [
{
"concept": "Argument",
"keyword": "list",
"expression": {
"concept": "Identifier",
"namespace": "",
"name": "fullAcceptedList"
}
},
{
"concept": "Argument",
"keyword": "item",
"expression": {
"concept": "NumericLiteral",
"value": "0",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
}
}
]
}
]
}
]
}

对应的代码如下:

for (item in list, index in 3 to list.length - 3) {
if (item.productReport.accepted == true) {
nasl::util::Add(fullAcceptedList, 1)
} else {
nasl::util::Add(fullAcceptedList, 0)
}
}