Skip to content

Commit 7cbc78d

Browse files
committed
Implement file-error? and read-error?.
1 parent a454113 commit 7cbc78d

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ _LispKit_ is incompatible or incomplete with respect to the following R7RS featu
6060

6161
- Lists are immutable. Mutable cons-cells are supported in a way similar to
6262
[Racket](https://racket-lang.org)
63-
- Error support is incomplete; the following functions are missing: `read-error?`, `file-error?`
6463
- `current-input-port`, `current-output-port`, and `current-error-port` are functions
6564
(as required by R5RS) and not parameter objects (as required by R7RS)
6665
- Datum comments introduced via `#;` do not always work as expected.

Sources/LispKit/Compiler/EvalError.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import Foundation
2222

23-
2423
///
2524
/// Enumeration `EvalError` represents errors occuring during the evaluation or compilation
2625
/// of LispKit expressions.

Sources/LispKit/Compiler/RuntimeError.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import Foundation
2222

23-
2423
///
2524
/// Class `RuntimeError` defines a universal representation of errors in LispKit. A runtime
2625
/// error consists of the following components:
@@ -411,6 +410,28 @@ public enum ErrorDescriptor: Hashable {
411410
case abortion
412411
case custom(String, String)
413412

413+
414+
public var isFileError: Bool {
415+
guard case .eval(let err) = self else {
416+
return false
417+
}
418+
switch err {
419+
case .cannotOpenFile, .cannotOpenUrl, .cannotWriteToPort:
420+
return true
421+
default:
422+
return false
423+
}
424+
}
425+
426+
public var isReadError: Bool {
427+
switch self {
428+
case .lexical(_), .syntax(_):
429+
return true
430+
default:
431+
return false
432+
}
433+
}
434+
414435
public var typeDescription: String {
415436
switch self {
416437
case .lexical(_):

Sources/LispKit/Compiler/SyntaxError.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import Foundation
2222

23-
2423
///
2524
/// Enumeration `SyntaxError` represents syntactical errors emitted by the parser.
2625
///

Sources/LispKit/Primitives/DynamicControlLibrary.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,17 @@ public final class DynamicControlLibrary: NativeLibrary {
308308
}
309309

310310
private func isReadError(expr: Expr) -> Expr {
311-
return .false
311+
guard case .error(let err) = expr else {
312+
return .false
313+
}
314+
return err.descriptor.isReadError ? .true : .false
312315
}
313316

314317
private func isFileError(expr: Expr) -> Expr {
315-
return .false
318+
guard case .error(let err) = expr else {
319+
return .false
320+
}
321+
return err.descriptor.isFileError ? .true : .false
316322
}
317323

318324
private func triggerExit(args: Arguments) throws -> (Procedure, Exprs) {

0 commit comments

Comments
 (0)