Commit | Line | Data |
---|---|---|
2539e6af KR |
1 | //****************************************************************************** |
2 | // MAL - step 0 - repl | |
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 | ||
10 | import Foundation | |
11 | ||
12 | // Parse the string into an AST. | |
13 | // | |
425305df | 14 | private func READ(str: String) -> String { |
2539e6af KR |
15 | return str |
16 | } | |
17 | ||
18 | // Walk the AST and completely evaluate it, handling macro expansions, special | |
19 | // forms and function calls. | |
20 | // | |
425305df | 21 | private func EVAL(ast: String) -> String { |
2539e6af KR |
22 | return ast |
23 | } | |
24 | ||
25 | // Convert the value into a human-readable string for printing. | |
26 | // | |
425305df | 27 | private func PRINT(exp: String) -> String { |
2539e6af KR |
28 | return exp |
29 | } | |
30 | ||
31 | // Perform the READ and EVAL steps. Useful for when you don't care about the | |
32 | // printable result. | |
33 | // | |
425305df | 34 | private func RE(text: String) -> String { |
2539e6af KR |
35 | let ast = READ(text) |
36 | let exp = EVAL(ast) | |
37 | return exp | |
38 | } | |
39 | ||
40 | // Perform the full READ/EVAL/PRINT, returning a printable string. | |
41 | // | |
425305df | 42 | private func REP(text: String) -> String { |
2539e6af KR |
43 | let exp = RE(text) |
44 | return PRINT(exp) | |
45 | } | |
46 | ||
47 | // Perform the full REPL. | |
48 | // | |
425305df | 49 | private func REPL() { |
2539e6af KR |
50 | while true { |
51 | if let text = _readline("user> ") { | |
425305df | 52 | print("\(REP(text))") |
2539e6af | 53 | } else { |
425305df | 54 | print("") |
2539e6af KR |
55 | break |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | func main() { | |
61 | load_history_file() | |
62 | REPL() | |
63 | save_history_file() | |
64 | } |