在 PowerShell 中,查看如何解析带有逻辑运算符的语句:
> $ast = [System.Management.Automation.Language.Parser]::ParseInput("($true) -and ($false)", [ref]$null, [ref]$null)
> $ast.EndBlock.Statements[0].PipelineElements[0].GetType().Name
CommandExpressionAst
> $ast.EndBlock.Statements[0].PipelineElements[0].Expression
运营商:和
左:(真)
正确:(错误)
错误位置:-和
静态类型:System.Boolean
范围:(真)-和(假)
家长:(真)-和(假)
正如预期的那样,AST 将其视为二进制表达式。但是,如果删除括号,它将被解析为命令。
> $true - 或 $false
真的
> $ast = [System.Management.Automation.Language.Parser]::ParseInput("$true -or $false", [ref]$null, [ref]$null)
> $ast.EndBlock.Statements[0].PipelineElements[0].Gettype().Name
CommandAst
> $ast.EndBlock.Statements[0].PipelineElements[0].CommandElements
StringConstantType : 裸字
值:真
静态类型:System.String
程度:真
父级:真或假
参数名称:或
争论 :
错误位置:-或
范围:-或
父级:真或假
StringConstantType : 裸字
值:假
静态类型:System.String
范围:错误
父级:真或假
我研究了官方 PowerShell 语言规范中的语言语法,但我没有看到它 - 为什么这是一个命令,而不是一个表达式?