/
glisp.go
102 lines (83 loc) · 2.14 KB
/
glisp.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
package main
import (
glispext "github.com/zhemao/glisp/extensions"
"github.com/zhemao/glisp/interpreter"
"os"
)
// These are the equivalent of the plugin library. These are the
// variables and functions that are assumed present in every plugin,
// so plugin authors dont have to keep reinventing the wheel
// These are loaded after regexp extension is loaded
var glispPluginFunctions string = `
(def command false)
(def pattern false)
(def prefix false)
(def regexp false)
(defn update-regexp []
(def regexp
(regex-compile (apply "^" (apply prefix pattern)))))
(defn update-prefix [p]
(def prefix p))
`
type GlispPlugin struct {
prefix string
command string
pattern string
filename string
env *glisp.Glisp
}
func newGlispEnv() *glisp.Glisp {
env := glisp.NewGlisp()
glispext.ImportRegex(env)
err := env.LoadString(glispPluginFunctions)
if err != nil {
log.Fatalf("Error loading glisp functions: %v", err)
}
return env
}
func getCommand(e *glisp.Glisp) string {
sexp, success := e.FindObject("command")
if success != true {
log.Error("Failed to find object \"command\"")
return ""
} else {
log.Debug(sexp.SexpString())
}
return sexp.SexpString()
}
func (c *Connection) registerGlispPluginHandler(p *GlispPlugin) (err error) {
command, err := p.env.EvalString("command")
if err != nil {
log.Debug("Unable to obtain command from plugin")
return err
} else {
p.command = command.SexpString()
}
return nil
}
func loadGlispPlugins(pluginFiles []string) ([]*GlispPlugin, error) {
var plugins []*GlispPlugin
for _, v := range pluginFiles {
log.Debug("Attempting to initialize %s", v)
file, err := os.Open(v)
if err != nil {
log.Error("Failed to open %s: %v", v, err)
break
}
defer file.Close()
p := new(GlispPlugin)
p.env = newGlispEnv()
err = p.env.LoadFile(file)
if err != nil {
log.Error("Failed to load %s: %v", v, err)
} else {
p.prefix = "@"
plugins = append(plugins, p)
log.Debug("plugin command is %s", getCommand(p.env))
ev, _ := p.env.EvalString("command")
log.Debug("eval string %s", ev.SexpString())
log.Info("Loaded plugin: %s", v)
}
}
return plugins, nil
}