@@ -15,6 +15,10 @@ func (o *Optimizer) Optimize() error {
1515 if err != nil {
1616 return errors .Join (fmt .Errorf ("could not tag recursion during optimization" ), err )
1717 }
18+ err = o .removeDeferred ()
19+ if err != nil {
20+ return errors .Join (fmt .Errorf ("could not remove deferred word" ), err )
21+ }
1822 // change calls at end of words to tail calls
1923 err = o .putTailCalls ()
2024 if err != nil {
@@ -75,6 +79,36 @@ func (o *Optimizer) putTailCalls() error {
7579 return nil
7680}
7781
82+ func (o * Optimizer ) removeDeferred () error {
83+ for _ , w := range o .u .forthWords {
84+ f := w .Entry .Flag
85+ // if this word was made with DEFER and it cannot be altered
86+ if f .isDeferred && ! f .inToken {
87+ literal , ok := w .Cells [0 ].(CellLiteral )
88+ if ! ok {
89+ return fmt .Errorf ("could not read literal %s in %s" , w .Cells [0 ], w )
90+ }
91+ address , ok := literal .cell .(CellAddress )
92+ if ! ok {
93+ return fmt .Errorf ("could not read address in %s" , w )
94+ }
95+ data , ok := address .Entry .Word .(* WordForth )
96+ if ! ok {
97+ return fmt .Errorf ("error reading a deferred word, please file a bug report." )
98+ }
99+ if len (data .Cells ) < 1 {
100+ return fmt .Errorf ("deferred word not allocated, please file a bug report." )
101+ }
102+ embedded := data .Cells [0 ]
103+ exit := w .Cells [len (w .Cells )- 1 ]
104+ w .Cells = w .Cells [0 :2 ]
105+ w .Cells [0 ] = embedded
106+ w .Cells [1 ] = exit
107+ }
108+ }
109+ return nil
110+ }
111+
78112func (o * Optimizer ) clearVisited () {
79113 for _ , w := range o .u .forthWords {
80114 w .Entry .ClearVisited ()
0 commit comments