//****************************************************************************** // MAL - step 1 - read/print //****************************************************************************** // 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 // Parse the string into an AST. // private func READ(str: String) throws -> MalVal { return try read_str(str) } // Walk the AST and completely evaluate it, handling macro expansions, special // forms and function calls. // private func EVAL(ast: MalVal) -> MalVal { return ast } // Convert the value into a human-readable string for printing. // private func PRINT(exp: MalVal) -> String { return pr_str(exp, true) } // Perform the READ and EVAL steps. Useful for when you don't care about the // printable result. // private func RE(text: String) -> MalVal? { if !text.isEmpty { do { let ast = try READ(text) return EVAL(ast) } catch let error as MalException { print("Error parsing input: \(error)") } catch { print("Error parsing input: \(error)") } } return nil } // Perform the full READ/EVAL/PRINT, returning a printable string. // private func REP(text: String) -> String? { let exp = RE(text) if exp == nil { return nil } return PRINT(exp!) } // Perform the full REPL. // private func REPL() { while true { if let text = _readline("user> ") { if let output = REP(text) { print("\(output)") } } else { print("") break } } } func main() { load_history_file() REPL() save_history_file() }