Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Nov 8, 2024
1 parent 2ca32c9 commit 7f45954
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 83 deletions.
30 changes: 5 additions & 25 deletions impls/swift3/Sources/env.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,11 @@ class Env {
}
}

func find(_ key: MalVal) throws -> Env? {
switch key {
case MalVal.MalSymbol(let str):
if data[str] != nil {
return self
} else if outer != nil {
return try outer!.find(key)
} else {
return nil
}
default:
throw MalError.General(msg: "invalid Env.find call")
}
}

func get(_ key: MalVal) throws -> MalVal {
switch key {
case MalVal.MalSymbol(let str):
let env = try self.find(key)
if env == nil {
throw MalError.General(msg: "'\(str)' not found")
}
return env!.data[str]!
default:
throw MalError.General(msg: "invalid Env.find call")
func get(_ str: String) -> Env? {
if let value = data[str] {
return value
} else {
return outer?.get(str)
}
}

Expand Down
5 changes: 3 additions & 2 deletions impls/swift3/Sources/step2_eval/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ func EVAL(_ ast: MalVal, _ env: Dictionary<String, MalVal>) throws -> MalVal {
/* print("EVAL: " + PRINT(ast)) */
switch ast {
case MalVal.MalSymbol(let sym):
if env[sym] == nil {
if let value = env[sym] {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
return env[sym]!
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step3_env/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ func READ(_ str: String) throws -> MalVal {

// eval
func EVAL(_ ast: MalVal, _ env: Env) throws -> MalVal {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step4_if_fn_do/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ func READ(_ str: String) throws -> MalVal {

// eval
func EVAL(_ ast: MalVal, _ env: Env) throws -> MalVal {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step5_tco/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ func READ(_ str: String) throws -> MalVal {
func EVAL(_ orig_ast: MalVal, _ orig_env: Env) throws -> MalVal {
var ast = orig_ast, env = orig_env
while true {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step6_file/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ func READ(_ str: String) throws -> MalVal {
func EVAL(_ orig_ast: MalVal, _ orig_env: Env) throws -> MalVal {
var ast = orig_ast, env = orig_env
while true {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step7_quote/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ func quasiquote(_ ast: MalVal) -> MalVal {
func EVAL(_ orig_ast: MalVal, _ orig_env: Env) throws -> MalVal {
var ast = orig_ast, env = orig_env
while true {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step8_macros/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ func quasiquote(_ ast: MalVal) -> MalVal {
func EVAL(_ orig_ast: MalVal, _ orig_env: Env) throws -> MalVal {
var ast = orig_ast, env = orig_env
while true {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/step9_try/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ func quasiquote(_ ast: MalVal) -> MalVal {
func EVAL(_ orig_ast: MalVal, _ orig_env: Env) throws -> MalVal {
var ast = orig_ast, env = orig_env
while true {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
17 changes: 11 additions & 6 deletions impls/swift3/Sources/stepA_mal/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ func quasiquote(_ ast: MalVal) -> MalVal {
func EVAL(_ orig_ast: MalVal, _ orig_env: Env) throws -> MalVal {
var ast = orig_ast, env = orig_env
while true {
switch try! env.find(MalVal.MalSymbol("DEBUG-EVAL")) {
case MalVal.MalFalse, MalVal.MalNil:
default:
print("EVAL: " + PRINT(ast))
if let dbgeval = env.get("DEBUG-EVAL") {
switch dbgeval {
case MalVal.MalFalse, MalVal.MalNil: break
default: print("EVAL: " + PRINT(ast))
}
}
switch ast {
case MalVal.MalSymbol:
return try env.get(ast)
case MalVal.MalSymbol(let sym):
if let value = env.get(sym) {
return value
} else {
throw MalError.General(msg: "'\(sym)' not found")
}
case MalVal.MalVector(let lst, _):
return vector(try lst.map { try EVAL($0, env) })
case MalVal.MalHashMap(let dict, _):
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step2_eval/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func EVAL(_ ast: MalData, env: [String: MalData]) throws -> MalData {
throw MalError.SymbolNotFound(list[0] as? Symbol ?? Symbol("Symbol"))
}
let raw_args = list.dropFirst()
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
return try function.fn(args)
case .Vector:
let vector = ast as! ContiguousArray<MalData>
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step3_env/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func EVAL(_ ast: MalData, env: Env) throws -> MalData {
throw MalError.SymbolNotFound(list[0] as? Symbol ?? Symbol("Symbol"))
}
let raw_args = list.dropFirst()
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
return try function.fn(args)
case .Vector:
let vector = ast as! ContiguousArray<MalData>
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step4_if_fn_do/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func EVAL(_ ast: MalData, env: Env) throws -> MalData {
throw MalError.SymbolNotFound(list[0] as? Symbol ?? Symbol("Symbol"))
}
let raw_args = list.dropFirst()
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
return try function.fn(args)
case .Vector:
let vector = ast as! ContiguousArray<MalData>
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step5_tco/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func EVAL(_ anAst: MalData, env anEnv: Env) throws -> MalData {
throw MalError.SymbolNotFound(list[0] as? Symbol ?? Symbol("Symbol"))
}
let raw_args = list.dropFirst()
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
if let fnAst = function.ast { // a full fn
ast = fnAst
env = Env(binds: function.params!, exprs: args, outer: function.env!)
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step6_file/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func EVAL(_ anAst: MalData, env anEnv: Env) throws -> MalData {
throw MalError.SymbolNotFound(list[0] as? Symbol ?? Symbol("Symbol"))
}
let raw_args = list.dropFirst()
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
if let fnAst = function.ast { // a full fn
ast = fnAst
env = Env(binds: function.params!, exprs: args, outer: function.env!)
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step7_quote/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func EVAL(_ anAst: MalData, env anEnv: Env) throws -> MalData {
throw MalError.SymbolNotFound(list[0] as? Symbol ?? Symbol("Symbol"))
}
let raw_args = list.dropFirst()
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
if let fnAst = function.ast { // a full fn
ast = fnAst
env = Env(binds: function.params!, exprs: args, outer: function.env!)
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/step8_macros/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func EVAL(_ anAst: MalData, env anEnv: Env) throws -> MalData {
ast = function.fn(raw_args)
continue
}
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
if let fnAst = function.ast { // a full fn
ast = fnAst
env = Env(binds: function.params!, exprs: args, outer: function.env!)
Expand Down
2 changes: 1 addition & 1 deletion impls/swift4/Sources/stepA_mal/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func EVAL(_ anAst: MalData, env anEnv: Env) throws -> MalData {
ast = function.fn(raw_args)
continue
}
let args = raw_args.map { try EVAL($0, env: env) }
let args = try raw_args.map { try EVAL($0, env: env) }
if let fnAst = function.ast { // a full fn
ast = fnAst
env = Env(binds: function.params!, exprs: args, outer: function.env!)
Expand Down

0 comments on commit 7f45954

Please sign in to comment.