-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompile.go
236 lines (210 loc) · 5.08 KB
/
compile.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package compilers
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/eris-ltd/eris-compilers/Godeps/_workspace/src/github.com/eris-ltd/common/go/common"
)
var DefaultUrl = "https://compilers.eris.industries:9090/compile"
// Language configuration struct
// New language capabilities can be added to the server simply by
// providing a LangConfig
// Each element in IncludeReplaces is a pair of strings, between which is placed the filename
// CompileCmd is a list of what would be white-space separated tokens on the
// command line, with a `_` to denote the place of the filename
type LangConfig struct {
URL string `json:"url"`
Net bool `json:"net"`
Extensions []string `json:"extensions"`
IncludeRegexes []string `json:"regexes"`
IncludeReplaces [][]string `json:"replaces"`
CompileCmd []string `json:"cmd"`
AbiCmd []string `json:"abi"`
}
// Append the language extension to the filename
func (l LangConfig) Ext(h string) string {
return h + "." + l.Extensions[0]
}
// Fill in the filename and return the command line args
func (l LangConfig) Cmd(file string, includes []string, libraries string) (args []string) {
for _, s := range l.CompileCmd {
if s == "_" {
args = append(args, file)
} else if s == "imports" {
args = append(args, includes...)
} else {
args = append(args, s)
}
}
if libraries != "" {
args = append(args, "--libraries ")
args = append(args, libraries)
}
logger.Debugf("Command Compiled =>\t\t%v\n", args)
return
}
func (l LangConfig) Abi(file string, includes []string) (args []string) {
if len(l.AbiCmd) < 2 {
return
}
for _, s := range l.AbiCmd {
if s == "_" {
args = append(args, file)
} else if s == "imports" {
args = append(args, includes...)
} else {
args = append(args, s)
}
}
return
}
// Global variable mapping languages to their configs
var Languages = map[string]LangConfig{
"lll": {
URL: DefaultUrl,
Net: true,
Extensions: []string{"lll", "def"},
IncludeRegexes: []string{
`\(include "(.+?)"\)`,
},
IncludeReplaces: [][]string{
{`(include "`, `.lll")`},
},
CompileCmd: []string{
"lllc",
"_",
},
},
"se": {
URL: DefaultUrl,
Net: true,
Extensions: []string{"se"},
IncludeRegexes: []string{
// because I'm not that good with regex and this
// demonstrates how to have multiple expressions to match :)
`create\("(.+?)"\)`,
`create\('(.+?)'\)`,
},
IncludeReplaces: [][]string{
{`create("`, `.se")`},
{`create('`, `.se')`},
},
CompileCmd: []string{
"sc",
"compile",
"_",
},
AbiCmd: []string{
"sc",
"mk_full_signature",
"_",
},
},
"sol": {
URL: DefaultUrl,
Net: true,
Extensions: []string{"sol"},
IncludeRegexes: []string{
`import "(.+?)";`,
},
IncludeReplaces: [][]string{
{`import "`, `.sol";`},
},
CompileCmd: []string{
"/usr/bin/solc",
"--combined-json", "bin", "abi",
"_",
},
},
}
func init() {
common.InitErisDir()
common.InitDataDir(ClientCache)
common.InitDataDir(ServerCache)
f := path.Join(common.LanguagesPath, "config.json")
err := checkConfig(f)
if err != nil {
logger.Errorln(err)
logger.Errorln("resorting to default language settings")
return
}
}
func checkConfig(f string) error {
if _, err := os.Stat(f); err != nil {
err := common.WriteJson(&Languages, f)
if err != nil {
return fmt.Errorf("Could not write default config to %s: %s", f, err.Error())
}
}
b, err := ioutil.ReadFile(f)
if err != nil {
return err
}
c := new(map[string]LangConfig)
err = json.Unmarshal(b, c)
if err != nil {
return err
}
Languages = *c
return nil
}
// Set the languages url
func SetLanguageURL(lang, url string) error {
l, ok := Languages[lang]
if !ok {
return UnknownLang(lang)
}
l.URL = url
Languages[lang] = l
return nil
}
// Set whether the language should use the remote server or compile locally
func SetLanguageNet(lang string, net bool) error {
l, ok := Languages[lang]
if !ok {
return UnknownLang(lang)
}
l.Net = net
Languages[lang] = l
return nil
}
// Main client struct to wrap a compiler interface and its configuration data
type CompileClient struct {
config LangConfig
lang string
}
// Return the language name
func (c *CompileClient) Lang() string {
return c.lang //c.Lang()
}
// Return the language's main extension
func (c *CompileClient) Ext(h string) string {
return c.config.Ext(h)
}
// Return the regex string to match include statements
func (c *CompileClient) IncludeRegexes() []string {
return c.config.IncludeRegexes
}
// Return the string to replace matched regex expressions
func (c *CompileClient) IncludeReplace(h string, i int) string {
s := c.config.IncludeReplaces[i]
return s[0] + h + s[1]
}
// Unknown language error
func UnknownLang(lang string) error {
return fmt.Errorf("Unknown language %s", lang)
}
// Create a new compile client
func NewCompileClient(lang string) (*CompileClient, error) {
l, ok := Languages[lang]
if !ok {
return nil, UnknownLang(lang)
}
cc := &CompileClient{
config: l,
lang: lang,
}
return cc, nil
}