Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

205 lines
10KB

  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]; Types = @(); Members = @() }
  52. $p += 4
  53. while ($dump[$p] -ne 0xFD -and $dump[$p] -ne 0xFC) {
  54. $len = $dump[$p + 3]
  55. $spell = ConvertFrom-Spelling $dump[($p + 4)..($p + 3 + $len)]
  56. $r.Types += ("{0} {1} {2}" -f @("", "interface", "class", "program")[$dump[$p]], $spell, ($dump[$p + 1] + 256 * $dump[$p + 2]))
  57. $p += 4 + $len
  58. }
  59. if ($dump[$p] -eq 0xFC) {
  60. $p++
  61. while ($dump[$p] -ne 0xFD) {
  62. $owner = Read-DumpName
  63. $name = Read-DumpName
  64. $flags = $dump[$p]; $p++
  65. $type = Read-DumpType
  66. $count = $dump[$p]; $p++
  67. $params = @(for ($k = 0; $k -lt $count; $k++) { Read-DumpType })
  68. $r.Members += ("{0}.{1} {2} {3} {4} ({5})" -f $owner, $name,
  69. $(if ($flags -band 1) { "method" } else { "field" }),
  70. $(if ($flags -band 2) { "private" } else { "public" }), $type, ($params -join ","))
  71. }
  72. }
  73. $p++
  74. $records += $r
  75. }
  76. # Members the source declares, derived independently by regex: per class or
  77. # interface in order; access defaults (fields private, methods public);
  78. # a second declaration of a name in the same namespace is not recorded.
  79. $typeRx = '(boolean|byte|char|integer|word|\w+)(\[(\d+)\])?'
  80. function Format-SourceType([string]$base, [string]$n) {
  81. $t = $base.ToLower(); if ($n) { $t += "[$n]" }; return $t
  82. }
  83. function Get-SourceMembers([string]$path) {
  84. $members = @(); $seen = @{}; $owner = $null; $inBody = $false
  85. foreach ($line in Get-Content -LiteralPath $path) {
  86. if ($line -match '^\s*(interface|class)\s+(\w+)') { $ownerKind = $Matches[1]; $owner = $Matches[2].ToLower(); continue }
  87. if ($line -match '^\s*end\s+(interface|class)\b') { $owner = $null; continue }
  88. if ($line -match '^\s*end\s+method\b') { $inBody = $false; continue }
  89. if (-not $owner -or $inBody) { continue }
  90. $m = [regex]::Match($line, "^\s*(public|private)?\s*method\s+(\w+)\s*\((.*)\)\s*(as\s+$typeRx)?\s*$")
  91. if ($m.Success) {
  92. $kind = "method"; $name = $m.Groups[2].Value.ToLower()
  93. $access = if ($m.Groups[1].Value -eq "private") { "private" } else { "public" }
  94. $type = if ($m.Groups[4].Success) { Format-SourceType $m.Groups[5].Value $m.Groups[7].Value } else { "none" }
  95. $params = @(foreach ($part in ($m.Groups[3].Value -split ',' | Where-Object { $_.Trim() })) {
  96. $pm = [regex]::Match($part.Trim(), "^\w+\s+as\s+$typeRx$")
  97. Format-SourceType $pm.Groups[1].Value $pm.Groups[3].Value })
  98. $inBody = ($ownerKind -eq "class") # interface methods have no body
  99. } else {
  100. $m = [regex]::Match($line, "^\s*(public|private)?\s*$typeRx\s+(\w+)\s*$")
  101. if (-not $m.Success) { continue }
  102. $kind = "field"; $name = $m.Groups[5].Value.ToLower()
  103. $access = if ($m.Groups[1].Value -eq "public") { "public" } else { "private" }
  104. $type = Format-SourceType $m.Groups[2].Value $m.Groups[4].Value
  105. $params = @()
  106. }
  107. $key = "$owner.$kind.$name"
  108. if ($seen[$key]) { continue }
  109. $seen[$key] = $true
  110. $members += ("{0}.{1} {2} {3} {4} ({5})" -f $owner, $name, $kind, $access, $type, ($params -join ","))
  111. }
  112. return ,$members
  113. }
  114. # Fixtures whose member tables are compared (headers all well formed).
  115. $memberChecked = @("members.n8", "lifecycle.n8", "scopes.n8", "private.n8")
  116. $c1541 = Get-ViceTool "c1541"
  117. $listing = (& $c1541 -attach $disk -list 2>&1) | Out-String
  118. function Get-ErrLines([string]$base) {
  119. $dest = Join-Path $out "$base.err.bin"
  120. if (Test-Path $dest) { Remove-Item $dest -Force }
  121. if ($listing -notmatch ('"' + [regex]::Escape("$base.err") + '"')) { return $null }
  122. & $c1541 -attach $disk -read "$base.err,s" $dest 2>&1 | Out-Null
  123. # The leading comma keeps a one-line file an array (PowerShell unrolls it).
  124. return ,@(([Text.Encoding]::ASCII.GetString([IO.File]::ReadAllBytes($dest)).TrimEnd("`r")) -split "`r")
  125. }
  126. function Parse-Err([string]$line) {
  127. $m = [regex]::Match($line, '^E(\d{3}) \S+ (\d+) (\d+) ')
  128. return [pscustomobject]@{ Code = [int]$m.Groups[1].Value; Line = [int]$m.Groups[2].Value; Col = [int]$m.Groups[3].Value }
  129. }
  130. # ---------------------------------------------------------------- check
  131. $failures = @(); $i = 0; $pending = @()
  132. foreach ($name in $ParserFixtures.Keys) {
  133. $src, $group, $expect = $ParserFixtures[$name]
  134. $base = $name -replace '\.n8$', ''
  135. $r = $records[$i]; $i++
  136. $errLines = Get-ErrLines $base
  137. $problems = @()
  138. if ($r.Load -ne 0) { $problems += "src_load returned $($r.Load)" }
  139. switch ($group) {
  140. "accepted" {
  141. if ($r.Count -ne 0) { $problems += "$($r.Count) diagnostic(s): $($errLines -join ' | ')" }
  142. if ($null -ne $errLines) { $problems += "an ERR file exists" }
  143. $want = @()
  144. $n = 0
  145. foreach ($line in Get-Content -LiteralPath $src) {
  146. $n++
  147. $m = [regex]::Match($line, '^\s*(interface|class|program)\s+(\w+)')
  148. if ($m.Success) { $want += ("{0} {1} {2}" -f $m.Groups[1].Value, $m.Groups[2].Value, $n) }
  149. }
  150. if ((($r.Types | Sort-Object) -join ';') -ne (($want | Sort-Object) -join ';')) {
  151. $problems += "types [$($r.Types -join '; ')] expected [$($want -join '; ')]"
  152. }
  153. $detail = "{0} types" -f $r.Types.Count
  154. }
  155. "rejected" {
  156. $text = Get-Content -Raw -LiteralPath $expect
  157. $sym = [regex]::Match($text, '(?m)^diagnostic: (\w+)').Groups[1].Value
  158. $wantLine = [int][regex]::Match($text, '(?m)^line: (\d+)').Groups[1].Value
  159. $colMatch = [regex]::Match($text, '(?m)^column: (\d+)')
  160. if ($null -eq $errLines) { $problems += "no ERR file" }
  161. else {
  162. $first = Parse-Err $errLines[0]
  163. if ($first.Code -ne $codes[$sym] -or $first.Line -ne $wantLine -or
  164. ($colMatch.Success -and $first.Col -ne [int]$colMatch.Groups[1].Value)) {
  165. $problems += "first diagnostic '$($errLines[0])' expected $sym (E$($codes[$sym])) at line $wantLine"
  166. }
  167. }
  168. $detail = if ($errLines) { $errLines[0] } else { "" }
  169. }
  170. "pending" {
  171. if ($r.Count -ne 0) { $problems += "false diagnostic(s) before E03-S03: $($errLines -join ' | ')" }
  172. $pending += $name
  173. $detail = "pending E03-S03 (statement grammar); no false errors"
  174. }
  175. "exact" {
  176. $want = @(Get-Content -LiteralPath $expect | Where-Object { $_ -and -not $_.StartsWith("//") } | ForEach-Object {
  177. $f = $_ -split ' '; "{0} {1} {2}" -f $codes[$f[0]], $f[1], $f[2] })
  178. $got = @($errLines | Where-Object { $_ } | ForEach-Object { $e = Parse-Err $_; "{0} {1} {2}" -f $e.Code, $e.Line, $e.Col })
  179. if (($got -join ';') -ne ($want -join ';')) {
  180. $problems += "diagnostics [$($got -join '; ')] expected [$($want -join '; ')]"
  181. }
  182. $detail = "{0} diagnostics" -f $got.Count
  183. }
  184. }
  185. if ($group -eq "accepted" -or $memberChecked -contains $name) {
  186. $wantMembers = Get-SourceMembers $src
  187. if (($r.Members -join ';') -ne ($wantMembers -join ';')) {
  188. $problems += "members [$($r.Members -join '; ')] expected [$($wantMembers -join '; ')]"
  189. }
  190. $detail += ", {0} members" -f $r.Members.Count
  191. }
  192. $status = if ($problems) { "FAIL" } else { "ok" }
  193. Write-Host (" {0,-13} {1,-9} {2} {3}" -f $name, $group, $status, $detail)
  194. if ($problems) { $failures += "${name}: " + ($problems -join "; ") }
  195. }
  196. if ($failures) { throw ("parser verification failed:`n " + ($failures -join "`n ")) }
  197. Write-Host ("verified parser: {0} fixtures ({1} pending E03-S03)" -f $ParserFixtures.Count, $pending.Count)

Powered by TurnKey Linux.