Skip to content

Commit eaace02

Browse files
author
James Brundage
committed
Adding EqualityComparison (#123)
1 parent 5d4d135 commit eaace02

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<#
2+
.SYNOPSIS
3+
Allows equality comparison.
4+
.DESCRIPTION
5+
Allows most equality comparison using double equals (==).
6+
7+
Many languages support this syntax. PowerShell does not.
8+
9+
This transpiler enables equality comparison with ==.
10+
.NOTES
11+
This will not work if there is a constant on both sides of the expression
12+
13+
14+
if ($null == $null) { "this will work"}
15+
if ('' == '') { "this will not"}
16+
17+
.EXAMPLE
18+
Invoke-PipeScript -ScriptBlock {
19+
$a = 1
20+
if ($a == 1 ) {
21+
"A is $a"
22+
}
23+
}
24+
.EXAMPLE
25+
{
26+
$a == "b"
27+
} | .>PipeScript
28+
#>
29+
[ValidateScript({
30+
# This is valid if the assignment statement's
31+
$AssignmentStatementAST = $_
32+
# right side is followed by an = and at least one space.
33+
$AssignmentStatementAST.Right -match '^=\s{1,}'
34+
})]
35+
param(
36+
# The original assignment statement.
37+
[Parameter(Mandatory,ValueFromPipeline)]
38+
[Management.Automation.Language.AssignmentStatementAst]
39+
$AssignmentStatementAST
40+
)
41+
42+
process {
43+
# This is a very trivial transpiler.
44+
45+
# We create a new script by:
46+
$newScript =
47+
# taking the left side as-is.
48+
$AssignmentStatementAST.Left.Extent.ToString() +
49+
# replacing the = with ' -eq '
50+
' -eq ' +
51+
# And replacing any the = and any trailing whitespace.
52+
($AssignmentStatementAST.Right.Extent.ToString() -replace '^=\s{1,}')
53+
54+
[scriptblock]::Create($newScript)
55+
}
56+

0 commit comments

Comments
 (0)