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-25 02:27:52 +00:00
|
|
|
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?
|
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
|
|
|
|
}
|