fix silly word collision with -1 built-in

This commit is contained in:
David 2021-02-21 15:04:51 -05:00
parent 6c5df444da
commit 953764004d
2 changed files with 4 additions and 4 deletions

View File

@ -72,13 +72,13 @@ func (c *Context) Exec(w Word) error {
// we have an implementation for that word. Run it.
err := w.Impl()
if err != nil {
return err
return fmt.Errorf("error during built-in: %w", err)
}
} else if len(w.Source) != 0 {
// user-defined word; let's descend...
err := c.Eval(strings.Join(w.Source, " ") + " ")
if err != nil {
return err
return fmt.Errorf("error during nested evaluation of word %+v: %w", w, err)
}
} else {
it, err := strconv.Atoi(w.Name)

View File

@ -41,8 +41,8 @@ func main() {
dict.AddWord("/", Word{Name: "/", Impl: b.Div(&stack)})
dict.AddWord("MOD", Word{Name: "MOD", Impl: b.Mod(&stack)})
dict.AddWord("/MOD", Word{Name: "/MOD", Source: []string{"2DUP", "MOD", "ROT", "ROT", "/"}})
dict.AddWord("+1", Word{Name: "+1", Source: []string{"1", "+"}})
dict.AddWord("-1", Word{Name: "-1", Source: []string{"1", "-"}})
dict.AddWord("1+", Word{Name: "1+", Source: []string{"1", "+"}})
dict.AddWord("1-", Word{Name: "1-", Source: []string{"1", "-"}})
dict.AddWord("ABS", Word{Name: "ABS", Source: []string{"DUP", "0<", "IF", "NEGATE", "THEN"}})
dict.AddWord("NEGATE", Word{Name: "NEGATE", Source: []string{"-1", "*"}})
dict.AddWord("MAX", Word{Name: "MAX", Source: []string{"2DUP", "<", "IF", "SWAP", "THEN", "DROP"}})