aboutsummaryrefslogtreecommitdiff
path: root/go/src/step1_read_print/step1_read_print.go
blob: e008ed5d907116490cf5da89535a4655789d33b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
    "fmt"
    "strings"
)

import (
    "readline"
    . "types"
    "reader"
    "printer"
)

// read
func READ(str string) (MalType, error) {
    return reader.Read_str(str)
}

// eval
func EVAL(ast MalType, env string) (MalType, error) {
    return ast, nil
}

// print
func PRINT(exp MalType) (string, error) {
    return printer.Pr_str(exp, true), nil
}

// repl
func rep(str string) (MalType, error) {
    var exp MalType
    var res string
    var e error
    if exp, e = READ(str); e != nil { return nil, e }
    if exp, e = EVAL(exp, ""); e != nil { return nil, e }
    if res, e = PRINT(exp); e != nil { return nil, e }
    return res, nil
}

func main() {
    // repl loop
    for {
        text, err := readline.Readline("user> ")
        text = strings.TrimRight(text, "\n");
        if (err != nil) {
            return
        }
        var out MalType
        var e error
        if out, e = rep(text); e != nil {
            if e.Error() == "<empty line>" { continue }
            fmt.Printf("Error: %v\n", e)
            continue
        }
        fmt.Printf("%v\n", out)
    }
}