-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlambda.go
More file actions
82 lines (71 loc) · 1.43 KB
/
lambda.go
File metadata and controls
82 lines (71 loc) · 1.43 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
l "github.com/reddragon/lambda/lang"
"github.com/tiborvass/uniline"
"os"
)
func process(env *l.LangEnv, line string) {
evalResult := l.Eval(line, env)
tokens := evalResult.RemainingTokens
if len(evalResult.ErrStr) > 0 {
fmt.Printf("Error: %s\n", evalResult.ErrStr)
} else {
if len(evalResult.ValStr) > 0 {
fmt.Printf("%s\n", evalResult.ValStr)
} else {
fmt.Printf("\n")
}
if len(tokens) > 0 {
process(env, tokens)
}
}
}
func initREPL() {
// Setup the language environment
env := l.NewEnv()
prompt := "lambda> "
scanner := uniline.DefaultScanner()
for scanner.Scan(prompt) {
line := scanner.Text()
if len(line) > 0 {
scanner.AddToHistory(line)
// printType(line)
process(env, line)
}
}
if err := scanner.Err(); err != nil {
panic(err)
} else {
fmt.Println("Goodbye!")
}
}
func processScriptFile(scriptFilePath string) {
file, err := os.Open(scriptFilePath)
if err != nil {
panic(err)
}
defer file.Close()
var concBuf bytes.Buffer
scanner := bufio.NewScanner(file)
for scanner.Scan() {
concBuf.WriteString(scanner.Text())
concBuf.WriteString(" ")
}
if scanner.Err() != nil {
panic(scanner.Err())
}
process(l.NewEnv(), concBuf.String())
}
func main() {
var scriptFile = flag.String("f", "", "path of the file to read from")
flag.Parse()
if len(*scriptFile) > 0 {
processScriptFile(*scriptFile)
} else {
initREPL()
}
}