36 lines
1.0 KiB
Go
36 lines
1.0 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
// ErrNoWordFound flags if the requested word is not in the dictionary
|
|
var ErrNoWordFound = fmt.Errorf("no word found")
|
|
|
|
// Dictionary is a simple map of names to words
|
|
type Dictionary map[string]Word
|
|
|
|
// A Word defines a subroutine
|
|
type Word struct {
|
|
Name string // Name of our word/variable
|
|
Impl func(string) error // built-in implementation of the word
|
|
Source []string // source, if user-defined
|
|
Variable int // if this is a variable, the memory address
|
|
Immediate bool // is this word immediate?
|
|
BranchCheck bool // is this word part of IF/ELSE/THEN?
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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,
|
|
}, ErrNoWordFound
|
|
}
|
|
return w, nil
|
|
}
|