FORTH-ish interpreter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

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