Skip to content

Commit

Permalink
First cut.
Browse files Browse the repository at this point in the history
  • Loading branch information
quephird committed Mar 22, 2024
1 parent 7c68a60 commit 3cd6662
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 13 additions & 1 deletion slox/Interpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,25 @@ class Interpreter {
return .string(leftString + rightString)
}

if case .instance(let leftList as LoxList) = leftValue,
case .instance(let rightList as LoxList) = rightValue,
case .plus = oper.type {
guard case .instance(let listClass as LoxClass) = try environment.getValue(name: "List") else {
fatalError()
}

let newElements = leftList.elements + rightList.elements
let list = LoxList(elements: newElements, klass: listClass)
return .instance(list)
}

switch oper.type {
case .bangEqual:
return .boolean(!leftValue.isEqual(to: rightValue))
case .equalEqual:
return .boolean(leftValue.isEqual(to: rightValue))
case .plus:
throw RuntimeError.binaryOperandsMustBeNumbersOrStrings
throw RuntimeError.binaryOperandsMustBeNumbersOrStringsOrLists
case .minus, .star, .slash, .greater, .greaterEqual, .less, .lessEqual:
throw RuntimeError.binaryOperandsMustBeNumbers
default:
Expand Down
6 changes: 3 additions & 3 deletions slox/RuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum RuntimeError: CustomStringConvertible, Equatable, LocalizedError {
case unaryOperandMustBeNumber
case unsupportedUnaryOperator
case binaryOperandsMustBeNumbers
case binaryOperandsMustBeNumbersOrStrings
case binaryOperandsMustBeNumbersOrStringsOrLists
case unsupportedBinaryOperator
case undefinedVariable(String)
case notAFunctionDeclaration
Expand All @@ -34,8 +34,8 @@ enum RuntimeError: CustomStringConvertible, Equatable, LocalizedError {
return "Error: unsupported unary operator"
case .binaryOperandsMustBeNumbers:
return "Error: operands must be both numbers"
case .binaryOperandsMustBeNumbersOrStrings:
return "Error: operands must be either both numbers or both strings"
case .binaryOperandsMustBeNumbersOrStringsOrLists:
return "Error: operands must be either both numbers, strings, or lists"
case .unsupportedBinaryOperator:
return "Error: unsupported binary operator"
case .undefinedVariable(let name):
Expand Down

0 comments on commit 3cd6662

Please sign in to comment.