/
RuntimeError.swift
70 lines (67 loc) · 2.6 KB
/
RuntimeError.swift
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
//
// RuntimeError.swift
// slox
//
// Created by Danielle Kefford on 2/26/24.
//
import Foundation
enum RuntimeError: CustomStringConvertible, Equatable, LocalizedError {
case unaryOperandMustBeNumber
case unsupportedUnaryOperator
case binaryOperandsMustBeNumbers
case binaryOperandsMustBeNumbersOrStringsOrLists
case unsupportedBinaryOperator
case undefinedVariable(String)
case notAFunctionDeclaration
case notACallableObject
case notAnInstance
case notAList
case notANumber
case onlyInstancesHaveProperties
case undefinedProperty(String)
case wrongArity(Int, Int)
case notALambda
case couldNotFindAncestorEnvironmentAtDepth(Int)
case superclassMustBeAClass
case indexMustBeAnInteger
var description: String {
switch self {
case .unaryOperandMustBeNumber:
return "Error: operand must be a number"
case .unsupportedUnaryOperator:
return "Error: unsupported unary operator"
case .binaryOperandsMustBeNumbers:
return "Error: operands must be both numbers"
case .binaryOperandsMustBeNumbersOrStringsOrLists:
return "Error: operands must be either both numbers, strings, or lists"
case .unsupportedBinaryOperator:
return "Error: unsupported binary operator"
case .undefinedVariable(let name):
return "Error: undefined variable '\(name)'"
case .notAFunctionDeclaration:
return "Error: expected function declaration in class"
case .notACallableObject:
return "Error: expected a callable object"
case .notAnInstance:
return "Error: expected an instance"
case .notAList:
return "Error: expected a list"
case .notANumber:
return "Error: expected a number"
case .onlyInstancesHaveProperties:
return "Error: can only get/set properties of instances"
case .undefinedProperty(let name):
return "Error: undefined property '\(name)'"
case .wrongArity(let expected, let actual):
return "Error: incorrect number of arguments; expected \(expected), got \(actual)"
case .notALambda:
return "Error: expected lambda as body of function declaration"
case .couldNotFindAncestorEnvironmentAtDepth(let depth):
return "Error: could not find ancestor environment at depth \(depth)."
case .superclassMustBeAClass:
return "Error: superclass must be a class"
case .indexMustBeAnInteger:
return "Error: index must be a number"
}
}
}