1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 18:48:12 +03:00
mal/swift/step6_file.swift

421 lines
14 KiB
Swift
Raw Normal View History

2015-03-18 09:32:35 +03:00
//******************************************************************************
// MAL - step 6 - file
//******************************************************************************
// This file is automatically generated from templates/step.swift. Rather than
// editing it directly, it's probably better to edit templates/step.swift and
// regenerate this file. Otherwise, your change might be lost if/when someone
// else performs that process.
//******************************************************************************
import Foundation
// The number of times EVAL has been entered recursively. We keep track of this
// so that we can protect against overrunning the stack.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private var EVAL_level = 0
2015-03-18 09:32:35 +03:00
// The maximum number of times we let EVAL recurse before throwing an exception.
// Testing puts this at some place between 1800 and 1900. Let's keep it at 500
// for safety's sake.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private let EVAL_leval_max = 500
2015-03-18 09:32:35 +03:00
// Control whether or not tail-call optimization (TCO) is enabled. We want it
// `true` most of the time, but may disable it for debugging purposes (it's
// easier to get a meaningful backtrace that way).
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private let TCO = true
2015-03-18 09:32:35 +03:00
// Control whether or not we emit debugging statements in EVAL.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private let DEBUG_EVAL = false
2015-03-18 09:32:35 +03:00
// String used to prefix information logged in EVAL. Increasing lengths of the
// string are used the more EVAL is recursed.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private let INDENT_TEMPLATE = "|----|----|----|----|----|----|----|----|" +
2015-03-18 09:32:35 +03:00
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|" +
"----|----|----|----|----|----|----|----|----|----|----|"
// Holds the prefix of INDENT_TEMPLATE used for actual logging.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private var indent = String()
2015-03-18 09:32:35 +03:00
// Symbols used in this module.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private let kValArgv = make_symbol("*ARGV*")
private let kValDef = make_symbol("def!")
private let kValDo = make_symbol("do")
private let kValEval = make_symbol("eval")
private let kValFn = make_symbol("fn*")
private let kValIf = make_symbol("if")
private let kValLet = make_symbol("let*")
private let kValTry = make_symbol("try*")
private let kSymbolArgv = as_symbol(kValArgv)
private let kSymbolDef = as_symbol(kValDef)
private let kSymbolDo = as_symbol(kValDo)
private let kSymbolEval = as_symbol(kValEval)
private let kSymbolFn = as_symbol(kValFn)
private let kSymbolIf = as_symbol(kValIf)
private let kSymbolLet = as_symbol(kValLet)
func substring(s: String, _ begin: Int, _ end: Int) -> String {
return s[s.startIndex.advancedBy(begin) ..< s.startIndex.advancedBy(end)]
2015-03-18 09:32:35 +03:00
}
// Parse the string into an AST.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func READ(str: String) throws -> MalVal {
return try read_str(str)
2015-03-18 09:32:35 +03:00
}
// Perform a simple evaluation of the `ast` object. If it's a symbol,
// dereference it and return its value. If it's a collection, call EVAL on all
// elements (or just the values, in the case of the hashmap). Otherwise, return
// the object unchanged.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func eval_ast(ast: MalVal, _ env: Environment) throws -> MalVal {
if let symbol = as_symbolQ(ast) {
guard let val = env.get(symbol) else {
try throw_error("'\(symbol)' not found") // Specific text needed to match MAL unit tests
}
return val
}
if let list = as_listQ(ast) {
var result = [MalVal]()
result.reserveCapacity(Int(list.count))
for item in list {
let eval = try EVAL(item, env)
result.append(eval)
}
return make_list(result)
}
if let vec = as_vectorQ(ast) {
var result = [MalVal]()
result.reserveCapacity(Int(vec.count))
for item in vec {
let eval = try EVAL(item, env)
result.append(eval)
}
return make_vector(result)
}
if let hash = as_hashmapQ(ast) {
var result = [MalVal]()
result.reserveCapacity(Int(hash.count) * 2)
for (k, v) in hash {
let new_v = try EVAL(v, env)
result.append(k)
result.append(new_v)
}
return make_hashmap(result)
2015-03-18 09:32:35 +03:00
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return ast
2015-03-18 09:32:35 +03:00
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private enum TCOVal {
case NoResult
case Return(MalVal)
case Continue(MalVal, Environment)
init() { self = .NoResult }
init(_ result: MalVal) { self = .Return(result) }
init(_ ast: MalVal, _ env: Environment) { self = .Continue(ast, env) }
}
// EVALuate "def!".
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func eval_def(list: MalSequence, _ env: Environment) throws -> TCOVal {
guard list.count == 3 else {
try throw_error("expected 2 arguments to def!, got \(list.count - 1)")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let arg1 = try! list.nth(1)
let arg2 = try! list.nth(2)
guard let sym = as_symbolQ(arg1) else {
try throw_error("expected symbol for first argument to def!")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let value = try EVAL(arg2, env)
return TCOVal(env.set(sym, value))
}
// EVALuate "let*".
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func eval_let(list: MalSequence, _ env: Environment) throws -> TCOVal {
guard list.count == 3 else {
try throw_error("expected 2 arguments to let*, got \(list.count - 1)")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let arg1 = try! list.nth(1)
let arg2 = try! list.nth(2)
guard let bindings = as_sequenceQ(arg1) else {
try throw_error("expected list for first argument to let*")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
guard bindings.count % 2 == 0 else {
try throw_error("expected even number of elements in bindings to let*, got \(bindings.count)")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let new_env = Environment(outer: env)
for var index: MalIntType = 0; index < bindings.count; index += 2 {
let binding_name = try! bindings.nth(index)
let binding_value = try! bindings.nth(index + 1)
guard let binding_symbol = as_symbolQ(binding_name) else {
try throw_error("expected symbol for first element in binding pair")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let evaluated_value = try EVAL(binding_value, new_env)
new_env.set(binding_symbol, evaluated_value)
}
if TCO {
return TCOVal(arg2, new_env)
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return TCOVal(try EVAL(arg2, new_env))
}
// EVALuate "do".
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func eval_do(list: MalSequence, _ env: Environment) throws -> TCOVal {
if TCO {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let _ = try eval_ast(list.range_from(1, to: list.count-1), env)
return TCOVal(list.last(), env)
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let evaluated_ast = try eval_ast(list.rest(), env)
let evaluated_seq = as_sequence(evaluated_ast)
return TCOVal(evaluated_seq.last())
}
// EVALuate "if".
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func eval_if(list: MalSequence, _ env: Environment) throws -> TCOVal {
guard list.count >= 3 else {
try throw_error("expected at least 2 arguments to if, got \(list.count - 1)")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let cond_result = try EVAL(try! list.nth(1), env)
var new_ast: MalVal
if is_truthy(cond_result) {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
new_ast = try! list.nth(2)
} else if list.count == 4 {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
new_ast = try! list.nth(3)
} else {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return TCOVal(make_nil())
}
if TCO {
return TCOVal(new_ast, env)
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return TCOVal(try EVAL(new_ast, env))
}
// EVALuate "fn*".
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func eval_fn(list: MalSequence, _ env: Environment) throws -> TCOVal {
guard list.count == 3 else {
try throw_error("expected 2 arguments to fn*, got \(list.count - 1)")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
guard let seq = as_sequenceQ(try! list.nth(1)) else {
try throw_error("expected list or vector for first argument to fn*")
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return TCOVal(make_closure((eval: EVAL, args: seq, body: try! list.nth(2), env: env)))
}
2015-03-18 09:32:35 +03:00
// Walk the AST and completely evaluate it, handling macro expansions, special
// forms and function calls.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func EVAL(var ast: MalVal, var _ env: Environment) throws -> MalVal {
EVAL_level++
defer { EVAL_level-- }
guard EVAL_level <= EVAL_leval_max else {
try throw_error("Recursing too many levels (> \(EVAL_leval_max))")
2015-03-18 09:32:35 +03:00
}
if DEBUG_EVAL {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
indent = substring(INDENT_TEMPLATE, 0, EVAL_level)
}
2015-03-18 09:32:35 +03:00
while true {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
if DEBUG_EVAL { print("\(indent)> \(ast)") }
2015-03-18 09:32:35 +03:00
if !is_list(ast) {
// Not a list -- just evaluate and return.
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let answer = try eval_ast(ast, env)
if DEBUG_EVAL { print("\(indent)>>> \(answer)") }
return answer
}
2015-03-18 09:32:35 +03:00
// Special handling if it's a list.
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let list = as_list(ast)
if DEBUG_EVAL { print("\(indent)>. \(list)") }
2015-03-18 09:32:35 +03:00
if list.isEmpty {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return ast
}
2015-03-18 09:32:35 +03:00
// Check for special forms, where we want to check the operation
// before evaluating all of the parameters.
let arg0 = list.first()
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
if let fn_symbol = as_symbolQ(arg0) {
let res: TCOVal
switch fn_symbol {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
case kSymbolDef: res = try eval_def(list, env)
case kSymbolLet: res = try eval_let(list, env)
case kSymbolDo: res = try eval_do(list, env)
case kSymbolIf: res = try eval_if(list, env)
case kSymbolFn: res = try eval_fn(list, env)
default: res = TCOVal()
}
switch res {
case let .Return(result): return result
case let .Continue(new_ast, new_env): ast = new_ast; env = new_env; continue
case .NoResult: break
2015-03-18 09:32:35 +03:00
}
}
2015-03-18 09:32:35 +03:00
// Standard list to be applied. Evaluate all the elements first.
2015-03-18 09:32:35 +03:00
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let eval = try eval_ast(ast, env)
2015-03-18 09:32:35 +03:00
// The result had better be a list and better be non-empty.
2015-03-18 09:32:35 +03:00
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let eval_list = as_list(eval)
if eval_list.isEmpty {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return eval
}
2015-03-18 09:32:35 +03:00
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
if DEBUG_EVAL { print("\(indent)>> \(eval)") }
// Get the first element of the list and execute it.
let first = eval_list.first()
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let rest = as_sequence(eval_list.rest())
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
if let fn = as_builtinQ(first) {
let answer = try fn.apply(rest)
if DEBUG_EVAL { print("\(indent)>>> \(answer)") }
return answer
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
} else if let fn = as_closureQ(first) {
let new_env = Environment(outer: fn.env)
let _ = try new_env.set_bindings(fn.args, with_exprs: rest)
if TCO {
env = new_env
ast = fn.body
continue
2015-03-18 09:32:35 +03:00
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let answer = try EVAL(fn.body, new_env)
if DEBUG_EVAL { print("\(indent)>>> \(answer)") }
return answer
2015-03-18 09:32:35 +03:00
}
// The first element wasn't a function to be executed. Return an
// error saying so.
2015-03-18 09:32:35 +03:00
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
try throw_error("first list item does not evaluate to a function: \(first)")
2015-03-18 09:32:35 +03:00
}
}
// Convert the value into a human-readable string for printing.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func PRINT(exp: MalVal) -> String {
2015-03-18 09:32:35 +03:00
return pr_str(exp, true)
}
// Perform the READ and EVAL steps. Useful for when you don't care about the
// printable result.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func RE(text: String, _ env: Environment) -> MalVal? {
if !text.isEmpty {
do {
let ast = try READ(text)
do {
return try EVAL(ast, env)
} catch let error as MalException {
print("Error evaluating input: \(error)")
} catch {
print("Error evaluating input: \(error)")
}
} catch let error as MalException {
print("Error parsing input: \(error)")
} catch {
print("Error parsing input: \(error)")
}
2015-03-18 09:32:35 +03:00
}
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
return nil
2015-03-18 09:32:35 +03:00
}
// Perform the full READ/EVAL/PRINT, returning a printable string.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func REP(text: String, _ env: Environment) -> String? {
2015-03-18 09:32:35 +03:00
let exp = RE(text, env)
if exp == nil { return nil }
return PRINT(exp!)
}
// Perform the full REPL.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func REPL(env: Environment) {
2015-03-18 09:32:35 +03:00
while true {
if let text = _readline("user> ") {
if let output = REP(text, env) {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
print("\(output)")
2015-03-18 09:32:35 +03:00
}
} else {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
print("")
2015-03-18 09:32:35 +03:00
break
}
}
}
// Process any command line arguments. Any trailing arguments are incorporated
// into the environment. Any argument immediately after the process name is
// taken as a script to execute. If one exists, it is executed in lieu of
// running the REPL.
//
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
private func process_command_line(args: [String], _ env: Environment) -> Bool {
var argv = make_list()
2015-03-18 09:32:35 +03:00
if args.count > 2 {
let args1 = args[2..<args.count]
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let args2 = args1.map { make_string($0) }
2015-03-18 09:32:35 +03:00
let args3 = [MalVal](args2)
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
argv = make_list(args3)
2015-03-18 09:32:35 +03:00
}
env.set(kSymbolArgv, argv)
if args.count > 1 {
RE("(load-file \"\(args[1])\")", env)
return false
}
return true
}
func main() {
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
let env = Environment(outer: nil)
2015-03-18 09:32:35 +03:00
load_history_file()
load_builtins(env)
RE("(def! not (fn* (a) (if a false true)))", env)
RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", env)
Update for Xcode 7.0 Optimizations: * In Environment, skip checking unused local slots if we’ve switched over to the general map. * Mark as much as possible with “final” and “private”, and build with -whole-module-optimization, per https://developer.apple.com/swift/blog/?id=27. * Refactor to include alternate types.swift with an implementation based on “enum”. Add Makefile variable allowing us to switch between the two so that we can compare the results. (At the time of this writing, using the class-based implementation is better in terms of both size and speed.) Swift 1.2: * Use the form of “if-let-as” that allows us to unwrap multiple optionals at once. * Use Swift’s as? rather than our own predicates after determining that the former did not incur a performance hit. Swift 2.0: * Remove some Array conversions where Foundation/Cocoa is now declared to return arrays of the desired type. * println -> print. * print() -> print("") * Remove some NSError* parameters; changed to do/try/catch. * Use Swift exception handling rather than tunneling that information in MalError. * Use `guard` statements where it makes sense. Especially `guard let a = b as? c` statements in order to reduce subsequent forced unwrapping. * Changed count(str) -> str.characters.count. * Changed Swift.reduce(coll, ...) -> coll.reduce(...). * Changed reverse(coll) -> coll.reverse(). * Changed use of .allZeros -> default OptionSet c'tor. * Changed Printable -> CustomStringConvertible. * Changed Sequence.extend -> Sequence.appendContentsOf * Changed String.join -> Sequence.joinWithSeparator * Changed advance(index, delta) -> index.advancedBy(delta) * Addressed change in function parameter name requirements. * Added and used substring(s, begin, end). * Changed “for ch in str” to “for ch in str.characters” * Changed some switch/case statements to briefer if/case statements. * Slices are no longer necessarily 0-based. * Sprinkle in some @noescapes. * Search for the most recent swiftc compiler to use if Xcode and Xcode-beta are both installed. Other: * Delete debugger symbols in `make clean`. * Rebuild if Makefile is changed.
2015-09-22 04:26:47 +03:00
env.set(kSymbolEval, make_builtin({
try! unwrap_args($0) {
(ast: MalVal) -> MalVal in
try EVAL(ast, env)
2015-03-18 09:32:35 +03:00
}
}))
if process_command_line(Process.arguments, env) {
REPL(env)
}
save_history_file()
}