2015-03-18 09:32:35 +03:00
|
|
|
//******************************************************************************
|
|
|
|
// MAL - step 0 - repl
|
|
|
|
//******************************************************************************
|
|
|
|
// 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.
|
|
|
|
//
|
2015-09-22 04:26:47 +03:00
|
|
|
private func READ(str: String) -> String {
|
2015-03-18 09:32:35 +03:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the AST and completely evaluate it, handling macro expansions, special
|
|
|
|
// forms and function calls.
|
|
|
|
//
|
2015-09-22 04:26:47 +03:00
|
|
|
private func EVAL(ast: String) -> String {
|
2015-03-18 09:32:35 +03:00
|
|
|
return ast
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the value into a human-readable string for printing.
|
|
|
|
//
|
2015-09-22 04:26:47 +03:00
|
|
|
private func PRINT(exp: String) -> String {
|
2015-03-18 09:32:35 +03:00
|
|
|
return exp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the READ and EVAL steps. Useful for when you don't care about the
|
|
|
|
// printable result.
|
|
|
|
//
|
2015-09-22 04:26:47 +03:00
|
|
|
private func RE(text: String) -> String {
|
2015-03-18 09:32:35 +03:00
|
|
|
let ast = READ(text)
|
|
|
|
let exp = EVAL(ast)
|
|
|
|
return exp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the full READ/EVAL/PRINT, returning a printable string.
|
|
|
|
//
|
2015-09-22 04:26:47 +03:00
|
|
|
private func REP(text: String) -> String {
|
2015-03-18 09:32:35 +03:00
|
|
|
let exp = RE(text)
|
|
|
|
return PRINT(exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the full REPL.
|
|
|
|
//
|
2015-09-22 04:26:47 +03:00
|
|
|
private func REPL() {
|
2015-03-18 09:32:35 +03:00
|
|
|
while true {
|
|
|
|
if let text = _readline("user> ") {
|
2015-09-22 04:26:47 +03:00
|
|
|
print("\(REP(text))")
|
2015-03-18 09:32:35 +03:00
|
|
|
} else {
|
2015-09-22 04:26:47 +03:00
|
|
|
print("")
|
2015-03-18 09:32:35 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
load_history_file()
|
|
|
|
REPL()
|
|
|
|
save_history_file()
|
|
|
|
}
|