Selaa lähdekoodia

generalize simple language runners

i.e. if we only run a command and pass the file as the last argument,
then we can use one method for running them all.
Lucas Stadler 11 vuotta sitten
vanhempi
commit
ffc989b7fd
1 muutettua tiedostoa jossa 16 lisäystä ja 18 poistoa
  1. 16 18
      go/linguaevalia/linguaevalia.go

+ 16 - 18
go/linguaevalia/linguaevalia.go

@ -19,11 +19,23 @@ type Language interface {
19 19
	Extension() string
20 20
}
21 21
22
type LanguageGo struct{}
23
type LanguagePython struct{}
22
type LanguageGeneral struct {
23
	Name    string
24
	Ext     string
25
	Command string
26
	Args    []string
27
}
28
29
func (l LanguageGeneral) RunFile(f *os.File) ([]byte, error) {
30
	args := append(l.Args, f.Name())
31
	cmd := exec.Command(l.Command, args...)
32
	return cmd.CombinedOutput()
33
}
24 34
25
var Go = LanguageGo{}
26
var Python = LanguagePython{}
35
func (l LanguageGeneral) Extension() string { return l.Ext }
36
37
var Go = LanguageGeneral{"Go", "go", "go", []string{"run"}}
38
var Python = LanguageGeneral{"Python", "py", "python", []string{}}
27 39
28 40
var languageMappings = map[string]Language{
29 41
	"go":     Go,
@ -44,20 +56,6 @@ func writeCode(code string, extension string) (*os.File, error) {
44 56
	return f, nil
45 57
}
46 58
47
func (l LanguageGo) RunFile(f *os.File) ([]byte, error) {
48
	cmd := exec.Command("go", "run", f.Name())
49
	return cmd.CombinedOutput()
50
}
51
52
func (l LanguageGo) Extension() string { return "go" }
53
54
func (l LanguagePython) RunFile(f *os.File) ([]byte, error) {
55
	cmd := exec.Command("python", f.Name())
56
	return cmd.CombinedOutput()
57
}
58
59
func (l LanguagePython) Extension() string { return "py" }
60
61 59
func Eval(lang Language, code string) ([]byte, error) {
62 60
	// write code to temp file
63 61
	f, err := writeCode(code, lang.Extension())