-
Notifications
You must be signed in to change notification settings - Fork 3
/
smlbuild-include.ps1
100 lines (64 loc) · 2.09 KB
/
smlbuild-include.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$libdir = "C:\mlton-20201002-1.amd64-mingw-gmp-dynamic\lib\mlton\sml"
#$mlton = (Get-Command mlton -ErrorAction SilentlyContinue).Path
#if ($mlton -ne "") {
# $libdir = "$mlton\..\lib\mlton\sml"
#}
$libdir = $libdir -Replace "\\","/"
"Using ""$libdir"" as SML_LIB path"
function script:listMLB($m) {
if (! (Test-Path $m)) {
"Error: file not found: " + $m
return
}
$lines = @(Get-Content $m)
# remove incompatible Basis lib, MLton lib, and unneeded call to main
$lines = $lines -notmatch "basis[.]mlb" -notmatch "mlton[.]mlb" -notmatch "main[.]sml"
# remove ML-style comments
$lines = $lines -replace "\(\*[^\*]*\*\)",""
# expand library path
$lines = $lines -replace "\$\(SML_LIB\)",$libdir
# remove leading whitespace
$lines = $lines -replace "^ *",""
# remove trailing whitespace
$lines = $lines -replace " *$",""
# remove empty lines
$lines = $lines -notmatch "^$"
# remove lines with double-quotes in them, e.g. annotation
$lines = $lines -notmatch """"
$expanded = @()
foreach ($line in $lines) {
$path = $line
if (!([System.IO.Path]::IsPathRooted($path))) {
if (Split-Path -parent $m) {
# resolve path relative to containing mlb file
$path = (Join-Path (Split-Path -parent $m) $path)
}
}
if ($path -match "[.]mlb$") {
# recurse to expand included mlb
$expanded += @(listMLB $path)
} elseif ($path -match "[.](sml|sig)$") {
# use forward slashes for path separators
$path = $path -replace "\\","/"
$expanded += $path
} else {
Write-Warning "*** Warning: unsupported syntax or file in ${m}: ${line}"
}
}
$expanded
}
function script:processMLB($m) {
$lines = @(listMLB $mlb)
if ($lines -match "^Error: ") {
$lines -match "^Error: "
} else {
$expanded = @()
foreach ($line in $lines) {
$path = $line
# add use declaration
$path = $path -replace "^(.*)$",'use "$1";'
$expanded += $path
}
$expanded
}
}