prosper/words.go

36 lines
776 B
Go
Raw Normal View History

2021-02-14 02:09:56 +00:00
package main
import "fmt"
// Dictionary is a simple map of names to words
type Dictionary map[string]Word
// A Word defines a subroutine
type Word struct {
2021-02-15 01:26:30 +00:00
Name string
Impl func() error
Source []string
Immediate bool
2021-02-14 02:09:56 +00:00
}
// AddWord inserts a new word into the dictonary, optionally overwriting the existing word
2021-02-15 01:26:30 +00:00
func (d Dictionary) AddWord(name string, impl func() error, source []string, immediate bool) {
2021-02-14 02:09:56 +00:00
d[name] = Word{
2021-02-15 01:26:30 +00:00
Name: name,
Impl: impl,
Source: source,
Immediate: immediate,
2021-02-14 02:09:56 +00:00
}
}
// GetWord returns a word from the dictionary or an error if it's undefined
func (d Dictionary) GetWord(name string) (Word, error) {
w, ok := d[name]
if !ok {
return Word{
Name: name,
}, fmt.Errorf("no word found")
}
return w, nil
}