/
vodka.go
142 lines (115 loc) · 2.72 KB
/
vodka.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"hawx.me/code/vodka/doc"
"hawx.me/code/vodka/interpreter"
"hawx.me/code/vodka/stack"
"hawx.me/code/vodka/table"
"hawx.me/code/vodka/types"
"hawx.me/code/vodka/types/vnil"
// "github.com/nsf/termbox-go"
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
type Prompt struct {
cursor string
interp func(string) (string, string)
r *bufio.Reader
}
func NewPrompt(cursor string, interp func(string) (string, string)) *Prompt {
return &Prompt{cursor, interp, bufio.NewReader(os.Stdin)}
}
func (p *Prompt) Loop() {
for {
line := p.promptLine()
if line == "quit" {
break
}
line = p.completeLine(line, "[", "]")
line = p.completePair(line, "'")
line = p.completePair(line, "\"")
line = p.completeLine(line, "(", ")")
stk, e := p.interp(line)
fmt.Printf("%s => %s\n", stk, e)
}
}
func (p *Prompt) promptLine() string {
fmt.Print(p.cursor)
l, _, _ := p.r.ReadLine()
return p.toString(l)
}
func (p *Prompt) continueLine(depth int) string {
s := ""
for i := 0; i < depth; i++ {
s += " "
}
fmt.Print(".. " + s)
l, _, _ := p.r.ReadLine()
return p.toString(l)
}
func (p *Prompt) toString(bytes []uint8) string {
str := ""
for _, c := range bytes {
str += string(byte(c))
}
return str
}
func (p *Prompt) completeLine(line, o, c string) string {
opens := strings.Count(line, o)
closes := strings.Count(line, c)
for opens != closes {
line += "\n" + p.continueLine(opens-closes)
opens = strings.Count(line, o)
closes = strings.Count(line, c)
}
return line
}
func (p *Prompt) completePair(line, o string) string {
for strings.Count(line, o)%2 != 0 {
line += "\n" + p.continueLine(0)
}
return line
}
var HELP_FLAGS []string = []string{"help", "-h", "--help", "-?"}
func isHelpFlag(s string) bool {
for _, flag := range HELP_FLAGS {
if flag == s {
return true
}
}
return false
}
func main() {
stk := stack.New()
tbl := interpreter.BootedTable(table.BOOT)
if len(os.Args) > 1 {
if os.Args[1] == "doc" {
doc.Doc([]string{os.Args[2]}, "doc.html")
} else if isHelpFlag(os.Args[1]) {
fmt.Println(
"Usage: vodka [files...]\n",
"\n",
" Given no files to run, vodka will launch into a REPL.\n",
" Given a list of files, vodka will run each file in turn.\n",
)
} else {
contents := ""
for _, file := range os.Args[1:] {
content, _ := ioutil.ReadFile(file)
contents += string(content)
}
interpreter.Eval(contents, stk, tbl)
}
} else {
fmt.Println("Vodka REPL, CTRL+C or type 'quit' to quit")
interp := func(line string) (string, string) {
var e types.VType = vnil.New()
stk, tbl, e = interpreter.Eval(line, stk, tbl)
return stk.TruncatedString(), e.String()
}
prompt := NewPrompt(">> ", interp)
prompt.Loop()
}
}