You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
13KB

  1. # E03-S01: run the parser over every fixture in x128 and check the ERR files
  2. # and the recorded top-level types against the fixtures' expectations.
  3. [CmdletBinding()]
  4. param([int]$Port = 6535, [int]$TimeoutSeconds = 300)
  5. $ErrorActionPreference = "Stop"
  6. $project = Split-Path $PSScriptRoot -Parent
  7. Import-Module (Join-Path $PSScriptRoot "lib\Nova128Test.psm1") -Force
  8. . (Join-Path $PSScriptRoot "build-parser-test.ps1") # defines $ParserFixtures
  9. $out = Join-Path $project "build\parser-test"
  10. $disk = Join-Path $out "parser-test.d81"
  11. # Catalog: symbol -> number.
  12. $codes = @{}
  13. foreach ($line in Get-Content (Join-Path $project "src\common\diagcat.inc")) {
  14. $m = [regex]::Match($line, '^\s*DIAGNOSTIC\s+(\w+),\s*(\d+),')
  15. if ($m.Success) { $codes[$m.Groups[1].Value] = [int]$m.Groups[2].Value }
  16. }
  17. $session = Start-Nova128X128 -Autostart $disk -Drive8Image $disk -Port $Port `
  18. -Screenshot (Join-Path $out "parser-test-vicii.png")
  19. try {
  20. $mb = Wait-Nova128Mailbox $session -TimeoutSeconds $TimeoutSeconds
  21. $dump = Read-Nova128Memory $session ($mb[8] + 256 * $mb[9]) ($mb[10] + 256 * $mb[11])
  22. } finally {
  23. Stop-Nova128X128 $session
  24. }
  25. if ($mb[3] -ne 0xA5) { throw ("C128 reported FAIL (code `${0:X2})" -f $mb[4]) }
  26. # ------------------------------------------------------------ decode dump
  27. function ConvertFrom-Spelling([byte[]]$bytes) {
  28. return -join ($bytes | ForEach-Object {
  29. if ($_ -ge 0x41 -and $_ -le 0x5A) { [char]($_ + 0x20) } elseif ($_ -ge 0xC1 -and $_ -le 0xDA) { [char]($_ - 0x80) } else { [char]$_ } })
  30. }
  31. # A name is a length and its spelling; a type is base, name, length lo/hi.
  32. # Both are rendered lowercase, as the oracle below writes them.
  33. function Read-DumpName {
  34. $len = $dump[$script:p]; $script:p++
  35. if ($len -eq 0) { return "" }
  36. $s = ConvertFrom-Spelling $dump[$script:p..($script:p + $len - 1)]
  37. $script:p += $len
  38. return $s.ToLower()
  39. }
  40. function Read-DumpType {
  41. $base = $dump[$script:p]; $script:p++
  42. $name = Read-DumpName
  43. $n = $dump[$script:p] + 256 * $dump[$script:p + 1]; $script:p += 2
  44. $t = @("none", "boolean", "byte", "char", "integer", "word", $name)[$base]
  45. if ($n) { $t += "[$n]" }
  46. return $t
  47. }
  48. $records = @(); $script:p = 0
  49. while ($dump[$p] -ne 0xFE) {
  50. if ($dump[$p] -ne 0xFF) { throw "dump out of sync at $p" }
  51. $r = [pscustomobject]@{ Index = $dump[$p + 1]; Load = $dump[$p + 2]; Count = $dump[$p + 3]
  52. MinSp = $dump[$p + 4]; Types = @(); Members = @(); Trace = $null }
  53. $p += 5
  54. while ($dump[$p] -ne 0xFD -and $dump[$p] -ne 0xFC) {
  55. $len = $dump[$p + 3]
  56. $spell = ConvertFrom-Spelling $dump[($p + 4)..($p + 3 + $len)]
  57. $r.Types += ("{0} {1} {2}" -f @("", "interface", "class", "program")[$dump[$p]], $spell, ($dump[$p + 1] + 256 * $dump[$p + 2]))
  58. $p += 4 + $len
  59. }
  60. if ($dump[$p] -eq 0xFC) {
  61. $p++
  62. while ($dump[$p] -ne 0xFD -and $dump[$p] -ne 0xFB) {
  63. $owner = Read-DumpName
  64. $name = Read-DumpName
  65. $flags = $dump[$p]; $p++
  66. $type = Read-DumpType
  67. $count = $dump[$p]; $p++
  68. $params = @(for ($k = 0; $k -lt $count; $k++) { Read-DumpType })
  69. $r.Members += ("{0}.{1} {2} {3} {4} ({5})" -f $owner, $name,
  70. $(if ($flags -band 1) { "method" } else { "field" }),
  71. $(if ($flags -band 2) { "private" } else { "public" }), $type, ($params -join ","))
  72. }
  73. if ($dump[$p] -eq 0xFB) {
  74. $n = $dump[$p + 1] + 256 * $dump[$p + 2]
  75. $r.Trace = if ($n) { [byte[]]$dump[($p + 3)..($p + 2 + $n)] } else { [byte[]]@() }
  76. $p += 3 + $n
  77. }
  78. }
  79. $p++
  80. $records += $r
  81. }
  82. # Expression trace -> "line: postfix" strings. Operands are named by the
  83. # source text at their column; operators by their symbol (parser.inc TR_*).
  84. $traceOps = @{ 0x17 = "="; 0x1E = "<>"; 0x1C = "<"; 0x1D = ">"; 0x1F = "<="; 0x20 = ">="
  85. 0x18 = "+"; 0x19 = "-"; 0x1A = "*"; 0x1B = "/"; 0x40 = "and"; 0x53 = "or" }
  86. function ConvertFrom-Trace([byte[]]$trace, [string[]]$lines) {
  87. $out = @(); $k = 0; $cur = $null
  88. while ($k -lt $trace.Length) {
  89. $b = $trace[$k]
  90. if ($b -eq 0xFF) {
  91. if ($cur) { $out += $cur }
  92. $line = $trace[$k + 1] + 256 * $trace[$k + 2]; $k += 3
  93. $cur = "${line}:"; $src = $lines[$line - 1]; continue
  94. }
  95. if ($b -eq 0xF0) { $cur += " neg" }
  96. elseif ($b -eq 0xF1) { $cur += " not" }
  97. elseif ($b -ge 0x80) { $cur += " " + $traceOps[$b - 0x80] }
  98. else {
  99. $m = [regex]::Match($src.Substring($b - 1), '^(\w+|"[^"]*"|''.'')')
  100. $cur += " " + $m.Value.ToLower()
  101. }
  102. $k++
  103. }
  104. if ($cur) { $out += $cur }
  105. return ,$out
  106. }
  107. # Members the source declares, derived independently by regex: per class or
  108. # interface in order; access defaults (fields private, methods public);
  109. # a second declaration of a name in the same namespace is not recorded.
  110. $typeRx = '(boolean|byte|char|integer|word|\w+)(\[(\d+)\])?'
  111. function Format-SourceType([string]$base, [string]$n) {
  112. $t = $base.ToLower(); if ($n) { $t += "[$n]" }; return $t
  113. }
  114. function Get-SourceMembers([string]$path) {
  115. $members = @(); $seen = @{}; $owner = $null; $inBody = $false
  116. foreach ($line in Get-Content -LiteralPath $path) {
  117. if ($line -match '^\s*(interface|class)\s+(\w+)') { $ownerKind = $Matches[1]; $owner = $Matches[2].ToLower(); continue }
  118. if ($line -match '^\s*end\s+(interface|class)\b') { $owner = $null; continue }
  119. if ($line -match '^\s*end\s+method\b') { $inBody = $false; continue }
  120. if (-not $owner -or $inBody) { continue }
  121. $m = [regex]::Match($line, "^\s*(public|private)?\s*method\s+(\w+)\s*\((.*)\)\s*(as\s+$typeRx)?\s*$")
  122. if ($m.Success) {
  123. $kind = "method"; $name = $m.Groups[2].Value.ToLower()
  124. $access = if ($m.Groups[1].Value -eq "private") { "private" } else { "public" }
  125. $type = if ($m.Groups[4].Success) { Format-SourceType $m.Groups[5].Value $m.Groups[7].Value } else { "none" }
  126. $params = @(foreach ($part in ($m.Groups[3].Value -split ',' | Where-Object { $_.Trim() })) {
  127. $pm = [regex]::Match($part.Trim(), "^\w+\s+as\s+$typeRx$")
  128. Format-SourceType $pm.Groups[1].Value $pm.Groups[3].Value })
  129. $inBody = ($ownerKind -eq "class") # interface methods have no body
  130. } else {
  131. $m = [regex]::Match($line, "^\s*(public|private)?\s*$typeRx\s+(\w+)\s*$")
  132. if (-not $m.Success) { continue }
  133. $kind = "field"; $name = $m.Groups[5].Value.ToLower()
  134. $access = if ($m.Groups[1].Value -eq "public") { "public" } else { "private" }
  135. $type = Format-SourceType $m.Groups[2].Value $m.Groups[4].Value
  136. $params = @()
  137. }
  138. $key = "$owner.$kind.$name"
  139. if ($seen[$key]) { continue }
  140. $seen[$key] = $true
  141. $members += ("{0}.{1} {2} {3} {4} ({5})" -f $owner, $name, $kind, $access, $type, ($params -join ","))
  142. }
  143. return ,$members
  144. }
  145. # Fixtures whose member tables are compared (headers all well formed).
  146. $memberChecked = @("members.n8", "lifecycle.n8", "scopes.n8", "private.n8")
  147. $c1541 = Get-ViceTool "c1541"
  148. $listing = (& $c1541 -attach $disk -list 2>&1) | Out-String
  149. function Get-ErrLines([string]$base) {
  150. $dest = Join-Path $out "$base.err.bin"
  151. if (Test-Path $dest) { Remove-Item $dest -Force }
  152. if ($listing -notmatch ('"' + [regex]::Escape("$base.err") + '"')) { return $null }
  153. & $c1541 -attach $disk -read "$base.err,s" $dest 2>&1 | Out-Null
  154. # The leading comma keeps a one-line file an array (PowerShell unrolls it).
  155. # Names keep their source spelling: shifted PETSCII letters ($C1-$DA) are
  156. # capitals; messages are compared case-insensitively.
  157. $text = -join ([IO.File]::ReadAllBytes($dest) | ForEach-Object {
  158. if ($_ -ge 0xC1 -and $_ -le 0xDA) { [char]($_ - 0x80) } else { [char]$_ } })
  159. return ,@(($text.TrimEnd("`r")) -split "`r")
  160. }
  161. function Parse-Err([string]$line) {
  162. $m = [regex]::Match($line, '^E(\d{3}) \S+ (\d+) (\d+) (.*)$')
  163. return [pscustomobject]@{ Code = [int]$m.Groups[1].Value; Line = [int]$m.Groups[2].Value
  164. Col = [int]$m.Groups[3].Value; Message = $m.Groups[4].Value }
  165. }
  166. # ---------------------------------------------------------------- check
  167. $failures = @(); $i = 0; $minSp = 0xFF
  168. foreach ($name in $ParserFixtures.Keys) {
  169. $src, $group, $expect = $ParserFixtures[$name]
  170. $base = $name -replace '\.n8$', ''
  171. $r = $records[$i]; $i++
  172. $errLines = Get-ErrLines $base
  173. $problems = @()
  174. if ($r.Load -ne 0) { $problems += "src_load returned $($r.Load)" }
  175. switch ($group) {
  176. "accepted" {
  177. if ($r.Count -ne 0) { $problems += "$($r.Count) diagnostic(s): $($errLines -join ' | ')" }
  178. if ($null -ne $errLines) { $problems += "an ERR file exists" }
  179. $want = @()
  180. $n = 0
  181. foreach ($line in Get-Content -LiteralPath $src) {
  182. $n++
  183. $m = [regex]::Match($line, '^\s*(interface|class|program)\s+(\w+)')
  184. if ($m.Success) { $want += ("{0} {1} {2}" -f $m.Groups[1].Value, $m.Groups[2].Value, $n) }
  185. }
  186. if ((($r.Types | Sort-Object) -join ';') -ne (($want | Sort-Object) -join ';')) {
  187. $problems += "types [$($r.Types -join '; ')] expected [$($want -join '; ')]"
  188. }
  189. $detail = "{0} types" -f $r.Types.Count
  190. }
  191. "rejected" {
  192. $text = Get-Content -Raw -LiteralPath $expect
  193. $sym = [regex]::Match($text, '(?m)^diagnostic: (\w+)').Groups[1].Value
  194. $wantLine = [int][regex]::Match($text, '(?m)^line: (\d+)').Groups[1].Value
  195. $colMatch = [regex]::Match($text, '(?m)^column: (\d+)')
  196. if ($null -eq $errLines) { $problems += "no ERR file" }
  197. else {
  198. $first = Parse-Err $errLines[0]
  199. if ($first.Code -ne $codes[$sym] -or $first.Line -ne $wantLine -or
  200. ($colMatch.Success -and $first.Col -ne [int]$colMatch.Groups[1].Value)) {
  201. $problems += "first diagnostic '$($errLines[0])' expected $sym (E$($codes[$sym])) at line $wantLine"
  202. }
  203. }
  204. $detail = if ($errLines) { $errLines[0] } else { "" }
  205. }
  206. "exact" {
  207. # SYMBOL line column [message]; a message is compared when given.
  208. $rows = @(Get-Content -LiteralPath $expect | Where-Object { $_ -and -not $_.StartsWith("//") } | ForEach-Object {
  209. $f = $_ -split ' ', 4
  210. [pscustomobject]@{ Key = "{0} {1} {2}" -f $codes[$f[0]], $f[1], $f[2]; Message = $f[3] } })
  211. $want = @($rows | ForEach-Object { $_.Key })
  212. $errs = @($errLines | Where-Object { $_ } | ForEach-Object { Parse-Err $_ })
  213. $got = @($errs | ForEach-Object { "{0} {1} {2}" -f $_.Code, $_.Line, $_.Col })
  214. if (($got -join ';') -ne ($want -join ';')) {
  215. $problems += "diagnostics [$($got -join '; ')] expected [$($want -join '; ')]"
  216. } else {
  217. for ($k = 0; $k -lt $rows.Count; $k++) {
  218. if ($rows[$k].Message -and $rows[$k].Message.ToUpper() -ne $errs[$k].Message.ToUpper()) {
  219. $problems += "E$($errs[$k].Code) at $($errs[$k].Line):$($errs[$k].Col) says '$($errs[$k].Message)', expected '$($rows[$k].Message)'"
  220. }
  221. }
  222. }
  223. $detail = "{0} diagnostics" -f $got.Count
  224. }
  225. }
  226. if ($group -eq "accepted" -or $memberChecked -contains $name) {
  227. $wantMembers = Get-SourceMembers $src
  228. if (($r.Members -join ';') -ne ($wantMembers -join ';')) {
  229. $problems += "members [$($r.Members -join '; ')] expected [$($wantMembers -join '; ')]"
  230. }
  231. $detail += ", {0} members" -f $r.Members.Count
  232. }
  233. if ($group -eq "accepted" -and $expect) { # expression trace golden
  234. $want = @(Get-Content -LiteralPath $expect | Where-Object { $_ -and -not $_.StartsWith("//") })
  235. if ($null -eq $r.Trace) { $problems += "no expression trace" }
  236. else {
  237. $got = ConvertFrom-Trace $r.Trace @(Get-Content -LiteralPath $src)
  238. for ($k = 0; $k -lt [Math]::Max($got.Count, $want.Count); $k++) {
  239. if ($got[$k] -ne $want[$k]) { $problems += "trace '$($got[$k])' expected '$($want[$k])'" }
  240. }
  241. $detail += ", {0} traced statements" -f $got.Count
  242. }
  243. }
  244. if ($r.MinSp -ne 0xFF -and $r.MinSp -lt $minSp) { $minSp = $r.MinSp }
  245. $status = if ($problems) { "FAIL" } else { "ok" }
  246. Write-Host (" {0,-13} {1,-9} {2} {3}" -f $name, $group, $status, $detail)
  247. if ($problems) { $failures += "${name}: " + ($problems -join "; ") }
  248. }
  249. # The expression bound must leave hardware stack to spare (KERNAL calls and
  250. # interrupts run below the parser).
  251. if ($minSp -lt 0x60) { $failures += ("expression parsing took the stack down to `${0:X2}" -f $minSp) }
  252. if ($failures) { throw ("parser verification failed:`n " + ($failures -join "`n ")) }
  253. Write-Host ("verified parser: {0} fixtures; lowest stack pointer in expressions `${1:X2}" -f $ParserFixtures.Count, $minSp)

Powered by TurnKey Linux.