Skip to content

Commit 945b24d

Browse files
author
James Brundage
committed
Adding TypeExpression Transpiler (#6)
1 parent 87db421 commit 945b24d

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<#
2+
.SYNOPSIS
3+
The PipeScript TypeExpression Transpiler
4+
.DESCRIPTION
5+
Type Expressions may be transpiled.
6+
7+
.Example
8+
{
9+
[include[a.ps1]]
10+
} | .>PipeScript
11+
#>
12+
param(
13+
# The attributed expression
14+
[Parameter(Mandatory,ParameterSetName='AttributedExpressionAst',ValueFromPipeline)]
15+
[Management.Automation.Language.TypeExpressionAst]
16+
$TypeExpressionAst
17+
)
18+
19+
begin {
20+
if (-not $script:TypeAcceleratorsList) {
21+
$script:TypeAcceleratorsList = [PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get.Keys
22+
}
23+
function TypeConstraintToArguments {
24+
param (
25+
[Parameter(ValueFromPipeline)]
26+
$TypeName
27+
)
28+
begin {
29+
$TypeNameArgs = @()
30+
$TypeNameParams = @{}
31+
}
32+
process {
33+
34+
if ($TypeName.IsGeneric) {
35+
$TypeNameParams[$typeName.Name] =
36+
$typeName.GenericArguments |
37+
UnpackTypeConstraintArgs
38+
} elseif (-not $TypeName.IsArray) {
39+
$TypeNameArgs += $TypeName.Name
40+
}
41+
}
42+
end {
43+
[PSCustomObject]@{
44+
ArgumentList = $TypeNameArgs
45+
Parameter = $TypeNameParams
46+
}
47+
}
48+
}
49+
}
50+
51+
process {
52+
$IsRealType = $TypeExpressionAst.TypeName.GetReflectionType()
53+
if ($IsRealType) { return }
54+
55+
$transpilerStepName =
56+
if ($TypeExpressionAst.TypeName.TypeName) {
57+
$TypeExpressionAst.TypeName.TypeName.Name
58+
} else {
59+
$TypeExpressionAst.TypeName.Name
60+
}
61+
62+
if ($transpilerStepName -eq 'ordered') { return }
63+
64+
$foundTranspiler =
65+
if ($currentInput -isnot [string]) {
66+
Get-Transpiler -CouldPipe $currentInput -TranspilerName "$transpilerStepName"
67+
} else {
68+
Get-Transpiler -TranspilerName "$transpilerStepName"
69+
}
70+
71+
$argList = @()
72+
$parameters = @{}
73+
74+
if ($TypeExpressionAst.TypeName.IsGeneric) {
75+
$TypeExpressionAst.TypeName.GenericArguments |
76+
TypeConstraintToArguments |
77+
ForEach-Object {
78+
if ($_.ArgumentList) {
79+
$arglist += $_.ArgumentList
80+
}
81+
if ($_.Parameter.Count) {
82+
$parameters += $_.Parameter
83+
}
84+
}
85+
}
86+
87+
88+
if ($foundTranspiler -and $currentInput -isnot [string]) {
89+
Invoke-PipeScript -CommandInfo $foundTranspiler -ArgumentList $arglist -Parameter $parameters
90+
} elseif ($foundTranspiler -and $currentInput -is [string]) {
91+
Invoke-PipeScript -CommandInfo $foundTranspiler -ArgumentList $arglist -Parameter $parameters
92+
} elseif ($script:TypeAcceleratorsList -notcontains $transpilerStepName -and $transpilerStepName -notin 'Ordered') {
93+
Write-Error "Unable to find a transpiler for [$TranspilerStepName]"
94+
}
95+
}

0 commit comments

Comments
 (0)