Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lorrden committed Nov 14, 2023
1 parent 9e541b7 commit 70f80ff
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 36 deletions.
25 changes: 10 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "Sim",
name: "swift-sim",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Sim",
targets: ["Sim"]),
.library(
name: "SimMacro",
targets: ["SimMacro"]
),
name: "Simulator",
targets: ["Simulator"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -28,31 +24,30 @@ let package = Package(
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.macro(
name: "SimMacroLibrary",
name: "SimulatorMacroLibrary",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),

.target(name: "SimMacro", dependencies: ["SimMacroLibrary"]),
.target(
name: "Sim",
name: "Simulator",
dependencies: [
.product(name: "Collections",
package: "swift-collections"),
.product(name: "OrderedCollections",
package: "swift-collections"),
"SimMacro"
"SimulatorMacroLibrary"
]),
.testTarget(
name: "SimTests",
dependencies: ["Sim"]),
name: "SimulatorTests",
dependencies: ["Simulator"]),

.testTarget(
name: "SimMacroTests",
name: "SimulatorMacroTests",
dependencies: [
"SimMacroLibrary",
"SimulatorMacroLibrary",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

@dynamicMemberLookup
open class Component : Model {
var models: [String: Model]

public override init(name: String) {
self.models = [:]
super.init(name: name)
}

public subscript(dynamicMember member: String) -> Model? {
return models[member]
return children[member]
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions Sources/Sim/Model.swift → Sources/Simulator/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,57 @@

import Foundation

public protocol Publisher : Service {
func publishField(name: String, reader: ()->(), writer: ()->())
}

open class Model {
weak var _sim: Simulator!
public weak var sim: Simulator! { get { _sim } }
weak var parent: Model?
var children: [String : Model]
var entrypoints: [String : ()->()] = [:]

var stateWriters: [String : (Any)->()] = [:]
var stateReaders: [String : ()->Any] = [:]

public let name: String

public init(name: String) {
self.name = name
self.children = [:]
}

public func publishFieldReader(name: String, reader: @escaping ()->(Any)) throws {
guard !stateReaders.keys.contains(name) else {
throw SimError.DuplicateName
}

stateReaders[name] = reader
}
public func publishFieldWriter(name: String, writer: @escaping (Any)->()) throws {
guard !stateWriters.keys.contains(name) else {
throw SimError.DuplicateName
}

stateWriters[name] = writer
}
public func getField(name: String) throws -> Any {
guard let reader = stateReaders[name] else {
throw SimError.InvalidFieldName
}

return reader()
}
public func setField(name: String, value: Any) throws
{
guard let writer = stateWriters[name] else {
throw SimError.InvalidFieldName
}

writer(value)
}

public func add(child: Model) throws {
try add(child: child, withName: child.name)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

@attached(member)
public macro model<T>(_ value: T)
= #externalMacro(module: "SimMacroLibrary", type: "ModelMacro")
= #externalMacro(module: "SimulatorMacroLibrary", type: "ModelMacro")

@attached(member)
public macro input<T>(_ value: T)
= #externalMacro(module: "SimMacroLibrary", type: "InputMacro")
= #externalMacro(module: "SimulatorMacroLibrary", type: "InputMacro")

@attached(member)
public macro output<T>(_ value: T)
= #externalMacro(module: "SimMacroLibrary", type: "OutputMacro")
= #externalMacro(module: "SimulatorMacroLibrary", type: "OutputMacro")

@attached(member)
public macro field<T>(_ value: T)
= #externalMacro(module: "SimMacroLibrary", type: "FieldMacro")
= #externalMacro(module: "SimulatorMacroLibrary", type: "FieldMacro")
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest

#if canImport(SimMacroLibrary)
import SimMacroLibrary
#if canImport(SimulatorMacroLibrary)
import SimulatorMacroLibrary

let testMacros: [String: Macro.Type] = [
"model": ModelMacro.self,
Expand All @@ -22,7 +22,7 @@ let testMacros: [String: Macro.Type] = [

final class SimMacroTest: XCTestCase {
func testModelMacro() throws {
#if canImport(SimMacroLibrary)
#if canImport(SimulatorMacroLibrary)
assertMacroExpansion(
"""
@model
Expand All @@ -45,7 +45,7 @@ final class SimMacroTest: XCTestCase {
}

func testInputMacro() throws {
#if canImport(SimMacroLibrary)
#if canImport(SimulatorMacroLibrary)
assertMacroExpansion(
"""
@input
Expand All @@ -64,7 +64,7 @@ final class SimMacroTest: XCTestCase {
}

func testOutputMacro() throws {
#if canImport(SimMacroLibrary)
#if canImport(SimulatorMacroLibrary)
assertMacroExpansion(
"""
@output
Expand All @@ -83,7 +83,7 @@ final class SimMacroTest: XCTestCase {
}

func testFieldMacro() throws {
#if canImport(SimMacroLibrary)
#if canImport(SimulatorMacroLibrary)
assertMacroExpansion(
"""
@field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator

class ClockedFoo : Model, ClockedModel {
var period: Double = 0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator

class MyModel : Model {
@Input public var xInput: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator

final class EventManagerTest: XCTestCase {
func testPublication() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator

fileprivate class NamedModel : Model {
override init(name: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator

class Foo : Model {
override init(name: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

import XCTest
@testable import Sim
@testable import Simulator

final class TimeKeeperTest: XCTestCase {
func testEpochToMissionTime() throws {
Expand Down

0 comments on commit 70f80ff

Please sign in to comment.