Skip to content

Commit

Permalink
Merge pull request #47 from jakerockland/race-conditions-and-swift-4
Browse files Browse the repository at this point in the history
Fix potential race conditions and finish update for Swift 4
  • Loading branch information
jakerockland authored Nov 14, 2018
2 parents 86ffc0c + c4779cb commit ce7bb3e
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 301 deletions.
4 changes: 2 additions & 2 deletions Sources/SwispFramework/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private struct Library {
case let (val as Bool):
return !val
case let (val as NSNumber):
return !Bool(val)
return !Bool(truncating: val)
case let (val as String):
if let bool = Bool(val) {
return !bool
Expand Down Expand Up @@ -691,7 +691,7 @@ private struct Math {
}
switch (args[safe: 0], args[safe: 1]) {
case let (val as Double, exp as Int):
return Foundation.ldexp(val, exp)
return Foundation.scalbn(val, exp)
default:
throw SwispError.SyntaxError(message: "invalid procedure input")
}
Expand Down
32 changes: 16 additions & 16 deletions Sources/SwispFramework/Interpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct Interpreter {
/// Calls the given procedure
public func call(_ args: [Any]) throws -> Any? {
var inner = Env(parms, args, outer: env)
return try Interpreter.eval(&body, with: &inner)
return try Interpreter.eval(body, with: &inner)
}

}
Expand Down Expand Up @@ -184,7 +184,7 @@ public struct Interpreter {
- Returns: The evaluated statement
*/
static func eval(_ x: inout Any, with env: inout Env) throws -> Any? {
static func eval(_ x: Any, with env: inout Env) throws -> Any? {
if let x = x as? Symbol { // variable reference
guard let ref = env.find(x)?[x] else {
return x
Expand All @@ -210,25 +210,25 @@ public struct Interpreter {
}
return lis
} else if x.first as? Symbol == "if" { // conditional
guard var test = x[safe: 1], let conseq = x[safe: 2], let alt = x[safe: 3] else {
guard let test = x[safe: 1], let conseq = x[safe: 2], let alt = x[safe: 3] else {
throw SwispError.SyntaxError(message: "invalid conditional statement")
}
guard let bool = try eval(&test, with: &env) as? Bool else {
guard let bool = try eval(test, with: &env) as? Bool else {
throw SwispError.SyntaxError(message: "invalid conditional statement")
}
var exp = bool ? conseq : alt
return try eval(&exp, with: &env)
let exp = bool ? conseq : alt
return try eval(exp, with: &env)
} else if x.first as? Symbol == "define" { // definition
guard let `var` = x[safe: 1] as? Symbol, var exp = x[safe: 2] else {
guard let `var` = x[safe: 1] as? Symbol, let exp = x[safe: 2] else {
throw SwispError.SyntaxError(message: "invalid definition")
}
env[`var`] = try eval(&exp, with: &env)
env[`var`] = try eval(exp, with: &env)
return nil
} else if x.first as? Symbol == "set!" { // assignment
guard let `var` = x[safe: 1] as? Symbol, var exp = x[safe: 2], let outer = env.find(`var`) else {
guard let `var` = x[safe: 1] as? Symbol, let exp = x[safe: 2], let outer = env.find(`var`) else {
throw SwispError.SyntaxError(message: "invalid assignment")
}
outer[`var`] = try eval(&exp, with: &env)
outer[`var`] = try eval(exp, with: &env)
return nil
} else if x.first as? Symbol == "lambda" { // lambda
guard let parms = x[safe: 1] as? [Symbol], let body = x[safe: 2] as? [Any] else {
Expand All @@ -237,15 +237,15 @@ public struct Interpreter {
return Lambda(parms, body, env)
} else { // procedure call
var args: [Any] = []
guard var exp = x[safe: 0] else {
guard let exp = x[safe: 0] else {
throw SwispError.SyntaxError(message: "invalid procedure called")
}

let proc = try eval(&exp, with: &env)
let proc = try eval(exp, with: &env)

for element in x.dropFirst() {
var element = element
guard let arg = try eval(&element, with: &env) else {
let element = element
guard let arg = try eval(element, with: &env) else {
throw SwispError.SyntaxError(message: "invalid procedure called")
}
args.append(arg)
Expand Down Expand Up @@ -287,8 +287,8 @@ public struct Interpreter {
}

do {
var parsed = try Interpreter.parse(input)
if var val = try Interpreter.eval(&parsed, with: &globalEnv) {
let parsed = try Interpreter.parse(input)
if var val = try Interpreter.eval(parsed, with: &globalEnv) {
print(Interpreter.schemeString(&val))
}
} catch let error as SwispError {
Expand Down
27 changes: 25 additions & 2 deletions Swisp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
OBJ_15 /* EnvironmentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvironmentTests.swift; sourceTree = "<group>"; };
OBJ_16 /* InterpreterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterpreterTests.swift; sourceTree = "<group>"; };
OBJ_17 /* XCTestManifests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestManifests.swift; sourceTree = "<group>"; };
OBJ_19 /* Swisp */ = {isa = PBXFileReference; lastKnownFileType = text; path = Swisp; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_19 /* Swisp */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; path = Swisp; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_20 /* SwispFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwispFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_21 /* SwispTests.xctest */ = {isa = PBXFileReference; lastKnownFileType = file; path = SwispTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_21 /* SwispTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = SwispTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_6 /* Package.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
OBJ_9 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -215,6 +215,17 @@
isa = PBXProject;
attributes = {
LastUpgradeCheck = 9999;
TargetAttributes = {
OBJ_22 = {
LastSwiftMigration = 1000;
};
OBJ_31 = {
LastSwiftMigration = 1000;
};
OBJ_39 = {
LastSwiftMigration = 1000;
};
};
};
buildConfigurationList = OBJ_2 /* Build configuration list for PBXProject "Swisp" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -295,6 +306,8 @@
OTHER_SWIFT_FLAGS = "$(inherited)";
SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES;
SWIFT_FORCE_STATIC_LINK_STDLIB = NO;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TARGET_NAME = Swisp;
};
name = Debug;
Expand All @@ -313,6 +326,8 @@
OTHER_SWIFT_FLAGS = "$(inherited)";
SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES;
SWIFT_FORCE_STATIC_LINK_STDLIB = NO;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TARGET_NAME = Swisp;
};
name = Release;
Expand Down Expand Up @@ -355,6 +370,8 @@
PRODUCT_BUNDLE_IDENTIFIER = SwispFramework;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TARGET_NAME = SwispFramework;
};
name = Debug;
Expand All @@ -375,6 +392,8 @@
PRODUCT_BUNDLE_IDENTIFIER = SwispFramework;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TARGET_NAME = SwispFramework;
};
name = Release;
Expand Down Expand Up @@ -412,6 +431,8 @@
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../Frameworks";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TARGET_NAME = SwispTests;
};
name = Debug;
Expand All @@ -429,6 +450,8 @@
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../Frameworks";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
TARGET_NAME = SwispTests;
};
name = Release;
Expand Down
Loading

0 comments on commit ce7bb3e

Please sign in to comment.