IfStatement 条件分支
1. 结构声明
- TS Declaration
- JSON Schema
class IfStatement extends LogicItem {
test: LogicItem;
consequent: Array<LogicItem>;
alternate: Array<LogicItem>;
}
{
"type": "object",
"properties": {
"composedBy": {
"type": "array",
"items": {
"type": "string"
}
},
"label": {
"type": "string"
},
"description": {
"type": "string"
},
"folded": {
"type": "boolean"
},
"offsetX": {
"type": "number"
},
"offsetY": {
"type": "number"
},
"test": {
"$ref": "#/definitions/LogicItem"
},
"consequent": {
"type": "array",
"items": {
"$ref": "#/definitions/LogicItem"
}
},
"alternate": {
"type": "array",
"items": {
"$ref": "#/definitions/LogicItem"
}
}
},
"required": [
"test",
"consequent",
"alternate"
],
"additionalProperties": false
}
2. 节点示例
(1) 示例
AST 如下:
- JSON
- YAML
{
"concept": "IfStatement",
"test": {
"concept": "BinaryExpression",
"left": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "length"
}
},
"right": {
"concept": "NumericLiteral",
"value": "0",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
},
"operator": "=="
},
"consequent": [
{
"concept": "Assignment",
"label": "赋值",
"left": {
"concept": "Identifier",
"namespace": "",
"name": "result"
},
"right": {
"concept": "NullLiteral"
}
}
],
"alternate": [
{
"concept": "Assignment",
"label": "赋值",
"left": {
"concept": "Identifier",
"namespace": "",
"name": "result"
},
"right": {
"concept": "BinaryExpression",
"left": {
"concept": "CallFunction",
"label": "内置函数",
"calleeNamespace": "nasl.util",
"calleeName": "ListSum",
"typeArguments": [],
"arguments": [
{
"concept": "Argument",
"keyword": "list",
"expression": {
"concept": "Identifier",
"namespace": "",
"name": "list"
}
}
]
},
"right": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "length"
}
},
"operator": "/"
}
}
]
}
concept: IfStatement
test:
concept: BinaryExpression
left:
concept: MemberExpression
object:
concept: Identifier
namespace: ""
name: list
property:
concept: Identifier
namespace: ""
name: length
right:
concept: NumericLiteral
value: "0"
typeAnnotation:
concept: TypeAnnotation
typeKind: primitive
typeNamespace: nasl.core
typeName: Long
inferred: false
ruleMap: {}
operator: ==
consequent:
- concept: Assignment
label: 赋值
left:
concept: Identifier
namespace: ""
name: result
right:
concept: NullLiteral
alternate:
- concept: Assignment
label: 赋值
left:
concept: Identifier
namespace: ""
name: result
right:
concept: BinaryExpression
left:
concept: CallFunction
label: 内置函数
calleeNamespace: nasl.util
calleeName: ListSum
typeArguments: []
arguments:
- concept: Argument
keyword: list
expression:
concept: Identifier
namespace: ""
name: list
right:
concept: MemberExpression
object:
concept: Identifier
namespace: ""
name: list
property:
concept: Identifier
namespace: ""
name: length
operator: /
对应的代码如下:
- 文本化 NASL
if (list.length == 0) {
result = null
} else {
result = nasl::util::ListSum(list) / list.length
}