User Tools

Site Tools


en:pfw:base-ffbase

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:pfw:base-ffbase [2023-09-04 18:11] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1en:pfw:base-ffbase [2024-10-20 23:31] (current) – Ausdruck etwas klarer hier und da. mka
Line 1: Line 1:
 +{{pfw:banner.png}}
 +====== BASE change temporarily -- FFBASE ======
 +
 +===== The idea =====
 +
 +Quite often a programmer needs to switch the numerical base for just a word or a number. In the worst case this involves saving the old BASE, setting BASE to the wanted value and then restoring the old BASE. A lot of code for just one word/definition.
 +
 +The routine FFBASE enables the creation of precursor words which, just for the next definition or number, temporarily set the base to another.
 +
 +So, for instance:
 +
 +<code forth>
 +    16 FFBASE HX
 +</code>
 +
 +Creates a word, called ''%%HX%%'' that will set the numeric base to 16, but ONLY for the word following it. It deals correctly with definitions being compiled, definitions being executed and values.
 +
 +The name ''%%FFBASE%%'', by the way, came from the Dutch word 'even'. It means 'for a short while'. This in colloquial language is pronounced 'effen', which is also the plural of 'F'. So ''%%FFBASE%%'' in Dutch is pronounced 'EffenBase' and it gives a different numeric base for a very short while.
 +
 +It should be mentioned that in this phrase ''%%: .hex  hx . ;%%'' the ''%%BASE%%'' is set to hex while ''%%.%%'' (dot) is compiled, but not at runtime! So this ''%%HX%%'' has no effect.
 +
 +===== Pseudo code =====
 +
 +<code>
 +Function: (BASE) ( XT temporary_base -- )
 +  execute an XT using a temporary base - used by FFBASE
 +  
 +Function: FFBASE
 +  CREATE: ( base ccc -- ) create a precursor word 'ccc' with 'base'
 +  DOES>: ( ccc -- )
 +        {get base from definition} 
 +        {find ccc in dictionary}
 +        {get state}
 +        IF compiling and ccc=not_immediate
 +            {compile XT and temp_base as literals and postpone (BASE)
 +        IF interpreting or ccc=immediate
 +            {call (BASE) with XT and temp_base on stack}
 +        IF not found in dictionary
 +            {convert to number using temp_base and put on stack}
 +</code>
 +
 +===== Generic Forth =====
 +
 +Definitions assumed to be available in your Forth: ''%%WORD%%'', ''%%EVALUATE%%'', ''%%COUNT%%''
 +
 +=== Generic Forth implementation ===
 +
 +See separate file: [[https://github.com/project-forth-works/project-forth-works/blob/main/Algorithms/FFBASE/FFBASE_comp.frt|FFBASE sample implementation]]
 +
 +===== Implementations =====
 +
 +The generic Forth version should run on the majority of Forth implementations. It is successfully tested on iForth and wabiForth. However in the present form it does not run on MeCrisp for reasons unknown to me.
 +
 +==== Contributions ====
 +
 +<html><h2 style="background-color:yellow">Alternative Implementations</h2></html>
 +