perl: Remove step 0.5.
[jackhill/mal.git] / swift / step1_read_print.swift
CommitLineData
2539e6af
KR
1//******************************************************************************
2// MAL - step 1 - read/print
3//******************************************************************************
4// This file is automatically generated from templates/step.swift. Rather than
5// editing it directly, it's probably better to edit templates/step.swift and
6// regenerate this file. Otherwise, your change might be lost if/when someone
7// else performs that process.
8//******************************************************************************
9
10import Foundation
11
12// Parse the string into an AST.
13//
425305df
KR
14private func READ(str: String) throws -> MalVal {
15 return try read_str(str)
2539e6af
KR
16}
17
18// Walk the AST and completely evaluate it, handling macro expansions, special
19// forms and function calls.
20//
425305df 21private func EVAL(ast: MalVal) -> MalVal {
2539e6af
KR
22 return ast
23}
24
25// Convert the value into a human-readable string for printing.
26//
425305df 27private func PRINT(exp: MalVal) -> String {
2539e6af
KR
28 return pr_str(exp, true)
29}
30
31// Perform the READ and EVAL steps. Useful for when you don't care about the
32// printable result.
33//
425305df
KR
34private func RE(text: String) -> MalVal? {
35 if !text.isEmpty {
36 do {
37 let ast = try READ(text)
38 return EVAL(ast)
39 } catch let error as MalException {
40 print("Error parsing input: \(error)")
41 } catch {
42 print("Error parsing input: \(error)")
43 }
2539e6af 44 }
425305df 45 return nil
2539e6af
KR
46}
47
48// Perform the full READ/EVAL/PRINT, returning a printable string.
49//
425305df 50private func REP(text: String) -> String? {
2539e6af
KR
51 let exp = RE(text)
52 if exp == nil { return nil }
53 return PRINT(exp!)
54}
55
56// Perform the full REPL.
57//
425305df 58private func REPL() {
2539e6af
KR
59 while true {
60 if let text = _readline("user> ") {
61 if let output = REP(text) {
425305df 62 print("\(output)")
2539e6af
KR
63 }
64 } else {
425305df 65 print("")
2539e6af
KR
66 break
67 }
68 }
69}
70
71func main() {
72 load_history_file()
73 REPL()
74 save_history_file()
75}