Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
mpangburn committed Dec 18, 2017
0 parents commit a3c17685f65993b46d15f5bd148460a46f40b6de
Showing with 2,520 additions and 0 deletions.
  1. +68 βˆ’0 .gitignore
  2. +19 βˆ’0 Expression.playground/Pages/ArithmeticExpression.xcplaygroundpage/Contents.swift
  3. +24 βˆ’0 Expression.playground/Pages/SingleType.xcplaygroundpage/Contents.swift
  4. +7 βˆ’0 Expression.playground/contents.xcplayground
  5. +7 βˆ’0 Expression.xcodeproj/Expression.xcworkspace/contents.xcworkspacedata
  6. +659 βˆ’0 Expression.xcodeproj/project.pbxproj
  7. +7 βˆ’0 Expression.xcodeproj/project.xcworkspace/contents.xcworkspacedata
  8. +10 βˆ’0 Expression.xcworkspace/contents.xcworkspacedata
  9. +19 βˆ’0 Expression/Expression.h
  10. +24 βˆ’0 Expression/Info.plist
  11. +36 βˆ’0 ExpressionTests/ExpressionTests.swift
  12. +22 βˆ’0 ExpressionTests/Info.plist
  13. +49 βˆ’0 Sources/ArithmeticExpression.swift
  14. +16 βˆ’0 Sources/BinaryOperatorAssociativity.swift
  15. +25 βˆ’0 Sources/Extensions/Numeric.swift
  16. +29 βˆ’0 Sources/FixedWidthIntegerOperator.swift
  17. +49 βˆ’0 Sources/FloatingPointArithmeticExpression.swift
  18. +29 βˆ’0 Sources/FloatingPointBinaryOperator.swift
  19. +20 βˆ’0 Sources/NumericBinaryOperatorPrecedence.swift
  20. +159 βˆ’0 Sources/Protocols/ArithmeticExpressionProtocol.swift
  21. +12 βˆ’0 Sources/Protocols/BinaryOperatorPrecedenceProtocol.swift
  22. +27 βˆ’0 Sources/Protocols/BinarySearchTreeProtocol.swift
  23. +29 βˆ’0 Sources/Protocols/BinaryTreeProtocol.swift
  24. +38 βˆ’0 Sources/Protocols/Divisible.swift
  25. +15 βˆ’0 Sources/Protocols/Evaluatable.swift
  26. +17 βˆ’0 Sources/Protocols/OperandProtocol.swift
  27. +30 βˆ’0 Sources/Protocols/Operator Protocols/BinaryIntegerBinaryOperatorProtocol.swift
  28. +18 βˆ’0 Sources/Protocols/Operator Protocols/BinaryOperatorProtocol.swift
  29. +20 βˆ’0 Sources/Protocols/Operator Protocols/DivisibleBinaryOperatorProtocol.swift
  30. +28 βˆ’0 Sources/Protocols/Operator Protocols/FixedWidthIntegerBinaryOperatorProtocol.swift
  31. +12 βˆ’0 Sources/Protocols/Operator Protocols/FloatingPointBinaryOperatorProtocol.swift
  32. +24 βˆ’0 Sources/Protocols/Operator Protocols/NumericBinaryOperatorProtocol.swift
  33. +28 βˆ’0 Sources/Protocols/Operator Protocols/OperatorProtocol.swift
  34. +45 βˆ’0 Sources/Protocols/SingleTypeBinaryTreeProtocol.swift
  35. +46 βˆ’0 Sources/Protocols/SingleTypeTreeProtocol.swift
  36. +63 βˆ’0 Sources/Protocols/TreeProtocol.swift
  37. +44 βˆ’0 Sources/RedBlackTree.swift
  38. +31 βˆ’0 Sources/Utilities/Either.swift
  39. +49 βˆ’0 Sources/Visualization/BinaryTreeProtocol+CustomPlaygroundQuickLookable.swift
  40. +175 βˆ’0 Sources/Visualization/BinaryTreeProtocol+Drawable.swift
  41. +38 βˆ’0 Sources/Visualization/CustomPlaygroundQuickLookableBinaryTreeProtocol.swift
  42. +51 βˆ’0 Sources/Visualization/Extensions/CGContext.swift
  43. +33 βˆ’0 Sources/Visualization/Extensions/CGRect.swift
  44. +19 βˆ’0 Sources/Visualization/Extensions/String.swift
  45. +28 βˆ’0 Sources/Visualization/Extensions/UIBezierPath.swift
  46. +36 βˆ’0 Sources/Visualization/Extensions/UIColor.swift
  47. +45 βˆ’0 Sources/Visualization/NodeVisualAttributes.swift
  48. +162 βˆ’0 Sources/Visualization/PositionedBinaryTree.swift
  49. +15 βˆ’0 Sources/Visualization/Protocols/Drawable.swift
  50. +41 βˆ’0 Sources/Visualization/Protocols/Renderer.swift
  51. +23 βˆ’0 Sources/Visualization/SingleTypeCustomPlaygroundQuickLookableBinaryTreeProtocol.swift
@@ -0,0 +1,68 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
@@ -0,0 +1,19 @@
import UIKit
import PlaygroundSupport
import Expression


let integerExpression: ArithmeticExpression<Int> = 3*(1+2)-8
let view = binaryTreeView(of: integerExpression)
integerExpression.traverseInOrder { kind in
switch kind {
case let .leaf(operand): print(operand)
case let .node(`operator`): print(`operator`)
}
}
//view.setNeedsDisplay()
//PlaygroundPage.current.liveView = view
let expression: ArithmeticExpression<UInt64> = 80<<32&+15/3

let fExpression: FloatingPointArithmeticExpression<Double> = 1.3-10.8/3.3*1.5+4
@@ -0,0 +1,24 @@
import UIKit
import PlaygroundSupport
import Expression


let tree: SingleTypeBinaryTree<Int> =
.node(
left: .leaf(5),
value: 9,
right: .node(
left: .node(
left: .leaf(8),
value: 4,
right: .leaf(5)
),
value: 3,
right: nil
)
)

tree.traverseInOrder {
print($0)
}

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='SingleType'/>
<page name='ArithmeticExpression'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,19 @@
//
// Expression.h
// Expression
//
// Created by Michael Pangburn on 12/16/17.
// Copyright Β© 2017 Michael Pangburn. All rights reserved.
//

#import <UIKit/UIKit.h>

//! Project version number for Expression.
FOUNDATION_EXPORT double ExpressionVersionNumber;

//! Project version string for Expression.
FOUNDATION_EXPORT const unsigned char ExpressionVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <Expression/PublicHeader.h>


@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
@@ -0,0 +1,36 @@
//
// ExpressionTests.swift
// ExpressionTests
//
// Created by Michael Pangburn on 12/16/17.
// Copyright Β© 2017 Michael Pangburn. All rights reserved.
//
import XCTest
@testable import Expression

class ExpressionTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
@@ -0,0 +1,49 @@
//
// ArithmeticExpression.swift
// Expression
//
// Created by Michael Pangburn on 12/16/17.
// Copyright Β© 2017 Michael Pangburn. All rights reserved.
//
import Foundation


public typealias ArithmeticExpression<T: FixedWidthIntegerOperandProtocol> = _ArithmeticExpression<FixedWidthIntegerOperator<T>>

public enum _ArithmeticExpression<Operator: NumericBinaryOperatorProtocol>: ArithmeticExpressionProtocol {
public typealias Operand = Operator.Operand

case operand(Operand)
indirect case expression(left: _ArithmeticExpression<Operator>, operator: Operator, right: _ArithmeticExpression<Operator>)
}

// MARK: - Boilerplate conformance
extension _ArithmeticExpression {
public var kind: Either<Operand, Operator> {
switch self {
case let .operand(operand):
return .leaf(operand)
case let .expression(_, `operator`, _):
return .node(`operator`)
}
}

public var left: _ArithmeticExpression<Operator>? {
guard case let .expression(left, _, _) = self else { return nil }
return left
}

public var right: _ArithmeticExpression<Operator>? {
guard case let .expression(_, _, right) = self else { return nil }
return right
}

public static func makeOperand(_ operand: Operand) -> _ArithmeticExpression<Operator> {
return .operand(operand)
}

public static func makeExpression(left: _ArithmeticExpression<Operator>, operator: Operator, right: _ArithmeticExpression<Operator>) -> _ArithmeticExpression<Operator> {
return .expression(left: left, operator: `operator`, right: right)
}
}
@@ -0,0 +1,16 @@
//
// BinaryOperatorAssociativity.swift
// Expression
//
// Created by Michael Pangburn on 12/16/17.
// Copyright Β© 2017 Michael Pangburn. All rights reserved.
//
import Foundation


public enum BinaryOperatorAssociativity {
case left
case right
case none
}
@@ -0,0 +1,25 @@
//
// Numeric.swift
// Expression
//
// Created by Michael Pangburn on 12/16/17.
// Copyright Β© 2017 Michael Pangburn. All rights reserved.
//
import Foundation


extension Numeric {
// Would expect these to have default implementations
public static func += (lhs: inout Self, rhs: Self) {
lhs = lhs + rhs
}

public static func -= (lhs: inout Self, rhs: Self) {
lhs = lhs - rhs
}

public static func *= (lhs: inout Self, rhs: Self) {
lhs = lhs * rhs
}
}
@@ -0,0 +1,29 @@
//
// FixedWidthIntegerOperator.swift
// Expression
//
// Created by Michael Pangburn on 12/16/17.
// Copyright Β© 2017 Michael Pangburn. All rights reserved.
//
import Foundation


public struct FixedWidthIntegerOperator<T: FixedWidthIntegerOperandProtocol>: FixedWidthIntegerBinaryOperatorProtocol {
public typealias Operand = T
public typealias Result = T

public var identifier: String
public var apply: (Operand, Operand) -> Operand
public var precedence: NumericBinaryOperatorPrecedence
public var associativity: BinaryOperatorAssociativity
public var isCommutative: Bool

public init(identifier: String, apply: @escaping (Operand, Operand) -> Operand, precedence: NumericBinaryOperatorPrecedence, associativity: BinaryOperatorAssociativity, isCommutative: Bool) {
self.identifier = identifier
self.apply = apply
self.precedence = precedence
self.associativity = associativity
self.isCommutative = isCommutative
}
}

0 comments on commit a3c1768

Please sign in to comment.
You can’t perform that action at this time.