2021-02-14 02:09:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2021-02-20 20:52:27 +00:00
|
|
|
// ErrNoWordFound flags if the requested word is not in the dictionary
|
|
|
|
var ErrNoWordFound = fmt.Errorf("no word found")
|
|
|
|
|
2021-02-14 02:09:56 +00:00
|
|
|
// Dictionary is a simple map of names to words
|
|
|
|
type Dictionary map[string]Word
|
|
|
|
|
|
|
|
// A Word defines a subroutine
|
|
|
|
type Word struct {
|
2021-02-20 20:52:27 +00:00
|
|
|
Name string
|
|
|
|
Impl func() error
|
|
|
|
Source []string
|
|
|
|
Immediate bool
|
|
|
|
BranchCheck bool
|
2021-02-14 02:09:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 20:52:27 +00:00
|
|
|
// AddWord inserts a new word into the dictonary, overwriting any existing word by that name
|
|
|
|
func (d Dictionary) AddWord(name string, w Word) {
|
|
|
|
d[name] = w
|
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,
|
2021-02-20 20:52:27 +00:00
|
|
|
}, ErrNoWordFound
|
2021-02-14 02:09:56 +00:00
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|