36 lines
776 B
Go
36 lines
776 B
Go
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 {
|
|
Name string
|
|
Impl func() error
|
|
Source []string
|
|
Immediate bool
|
|
}
|
|
|
|
// AddWord inserts a new word into the dictonary, optionally overwriting the existing word
|
|
func (d Dictionary) AddWord(name string, impl func() error, source []string, immediate bool) {
|
|
d[name] = Word{
|
|
Name: name,
|
|
Impl: impl,
|
|
Source: source,
|
|
Immediate: immediate,
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|