Sujet : Re: Operator overloading?
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forthDate : 01. Aug 2024, 13:35:32
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2024Aug1.143532@mips.complang.tuwien.ac.at>
References : 1 2 3
User-Agent : xrn 10.11
minforth@gmx.net (minforth) writes:
the runtime action of an xVALUE (even when
compiled)
involves to walk a type-specific operator chain (a large CASE construct
in VFX).
Instead of a CASE, an alternative is to have a table that is indexted
by the number of the operator. This is used in Gforth. And AFAICS,
we managed to get rid of the OPERATOR variable. Instead, the operator
passes the index on the stack to "(to)" or "(to),", which does the
table lookup and then runs ("(to)") or compiles ("(to),") the
appropriate method.
Gforth uses a parsing approach, which allows passing the index on the
stack instead of through a variable and provides better error
checking. E.g., for TO the implementation is:
: int-to ( "name" x -- ) \ gforth-internal
\g Interpretation semantics of \code{to}.
record-name 0 (') (to) ;
: comp-to ( compilation "name" -- ; run-time x -- ) \ gforth-internal
\g Compilation semantics of \code{to}.
record-name 0 (') (to), ; immediate restrict
' int-to ' comp-to interpret/compile: TO ( value "name" -- ) \ core-ext
\g changes the value of @var{name} to @var{value}
Here 0 is the index into the table. RECORD-NAME just records
meta-information for Gforth's development environment. For (TO) the
help text is:
'(to)' ( val operation xt -- ) gforth-1.0 "paren-to"
xt is of a value like word name. Stores val 'to' name. operation
selects between 'to' (0), '+to' (1), 'addr' (2), 'action-of' (3) and
'is' (4).
(TO) is a method of the word specified by xt, so different words have
different implementations, but they are basically to look up the
appropriate table entry end execute it. E.g., for a word defined with
VALUE, the table entry with index 0 (i.e., TO) of the word is the xt
of "!", and (TO) performs
( val xt ) >body !
The thing about "(TO)," means that when compiling this stuff, the
whole table lookup is performed at compile time, and what is compiled
is equivalent to
( val ) [ xt >body ] literal !
- anton
-- M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.htmlcomp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html New standard: https://forth-standard.org/ EuroForth 2024: https://euro.theforth.net