Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Misc functions #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Sources/SwispFramework/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public let standardEnv = Env([
"tan": Math.tan,

// Angular conversion
"degrees": Math.degrees,
"radians": Math.radians,
"degrees": Math.degrees,
"radians": Math.radians,

// Hyperbolic functions
"acosh": Math.acosh,
Expand All @@ -91,7 +91,7 @@ public let standardEnv = Env([
"erf": Math.erf,
"erfc": Math.erfc,
"gamma": Math.gamma,
"lgamma": Math.lgamma,
"lgamma": Math.lgamma,

// // Misc.
"abs": Library.abs,
Expand All @@ -103,16 +103,16 @@ public let standardEnv = Env([
// "cons": { [$0] + $1 },
// "eq?": { $0 === $1 },
// "equal?": { $0 == $1 },
// "length": { $0.count },
// "list": { List($0) },
// "list?": { $0 is List },
"length": Library.length,
"list": Library.list,
"list?": Library.isList,
// // "map": map, // [TODO](https://www.weheartswift.com/higher-order-functions-map-filter-reduce-and-more/)
// "max": max,
// "min": min,
"max": Library.max,
"min": Library.min,
"not": Library.not,
// "null?": { $0 == nil },
// "number?": { $0 is Number },
"number?": Library.isNumber,
// "procedure?": { String(type(of: $0)).containsString("->") },
// "round": round,
"round": Library.round,
// "symbol?": { $0 is Symbol }
] as [Symbol: Any])
157 changes: 148 additions & 9 deletions Sources/SwispFramework/Environment/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ import Foundation
// "cons": { [$0] + $1 },
// "eq?": { $0 === $1 },
// "equal?": { $0 == $1 },
// "length": { $0.count },
// "list": { List($0) },
// "list?": { $0 is List },
- `length`
- `list`
- `list?`
// // "map": map, // [TODO](https://www.weheartswift.com/higher-order-functions-map-filter-reduce-and-more/)
// "max": max,
// "min": min,
- `max`
- `min`
- `not`
// "null?": { $0 == nil },
// "number?": { $0 is Number },
- `number?`
// "procedure?": { String(type(of: $0)).containsString("->") },
// "round": round,
- `round`
// "symbol?": { $0 is Symbol }
*/
internal struct Library {
Expand Down Expand Up @@ -107,6 +107,109 @@ internal struct Library {
return Array(lis.dropFirst())
}

/**
Static function for `length` operation
*/
static func length(_ args: [Any]) throws -> Any? {
guard args.count == 1 else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}
guard let lis = args[safe: 0] as? [Any] else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}
return lis.count
}

/**
Static function for `list` operation
*/
static func list(_ args: [Any]) throws -> Any? {
return args
}

/**
Static function for `list?` operation
*/
static func isList(_ args: [Any]) throws -> Any? {
guard args.count == 1 else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}
if (args[safe: 0] as? [Any]) != nil {
return true
}
return false
}

/**
Static function for `max` operation
*/
static func max(_ args: [Any]) throws -> Any? {
// Check that there are inputs
guard args.count > 0 else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}

// Calculate maximum double and integer inputs
var tempDouble: Double = -Double.infinity
var tempInt: Int = Int.min
for arg in args {
switch (arg) {
case let (val as Double):
if (val > tempDouble) {
tempDouble = val
}
case let (val as Int):
if (val > tempInt) {
tempInt = val
}
default:
throw SwispError.SyntaxError(message: "invalid procedure input")
}
}

// Find and keep type of maximum between the two maximums
if (tempDouble > Double(tempInt)) {
return tempDouble
} else {
return tempInt
}
}

/**
Static function for `min` operation
*/
static func min(_ args: [Any]) throws -> Any? {
// Check that there are inputs
guard args.count > 0 else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}

// Calculate maximum double and integer inputs
var tempDouble: Double = Double.infinity
var tempInt: Int = Int.max
for arg in args {
switch (arg) {
case let (val as Double):
if (val < tempDouble) {
tempDouble = val
}
case let (val as Int):
if (val < tempInt) {
tempInt = val
}
default:
throw SwispError.SyntaxError(message: "invalid procedure input")
}
}

// Find and keep type of maximum between the two maximums
if (tempDouble < Double(tempInt)) {
return tempDouble
} else {
return tempInt
}
}

/**
Static function for `not` operation
*/
Expand All @@ -117,8 +220,10 @@ internal struct Library {
switch (args[safe: 0]) {
case let (val as Bool):
return !val
case let (val as NSNumber):
return !Bool(truncating: val)
case let (val as Int):
return val == 0
case let (val as Double):
return val == 0
case let (val as String):
if let bool = Bool(val) {
return !bool
Expand All @@ -130,4 +235,38 @@ internal struct Library {
}
}

/**
Static function for `number?` operation
*/
static func isNumber(_ args: [Any]) throws -> Any? {
guard args.count == 1 else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}
switch (args[safe: 0]) {
case (_ as Int):
return true
case (_ as Double):
return true
default:
return false
}
}

/**
Static function for `round` operation
*/
static func round(_ args: [Any]) throws -> Any? {
guard args.count == 1 else {
throw SwispError.SyntaxError(message: "invalid procedure input")
}
switch (args[safe: 0]) {
case let (val as Int):
return val
case let (val as Double):
return Foundation.round(val)
default:
throw SwispError.SyntaxError(message: "invalid procedure input")
}
}

}
Loading